diff options
Diffstat (limited to 'brevisurl')
| -rw-r--r-- | brevisurl/backends/local.py | 6 | ||||
| -rw-r--r-- | brevisurl/models.py | 9 |
2 files changed, 7 insertions, 8 deletions
diff --git a/brevisurl/backends/local.py b/brevisurl/backends/local.py index 1b72e64..69944e8 100644 --- a/brevisurl/backends/local.py +++ b/brevisurl/backends/local.py @@ -12,6 +12,8 @@ from brevisurl.models import ShortUrl log = logging.getLogger(__name__) +class TokensExhausted(Exception): + pass class BrevisUrlBackend(BaseBrevisUrlBackend): @@ -53,9 +55,11 @@ class BrevisUrlBackend(BaseBrevisUrlBackend): def __generate_token(self, size=5): chars = list(string.ascii_letters + string.digits) + if ShortUrl.objects.count() >= len(chars) ** size: + raise TokensExhausted('Consider incrementing the token length or chars list') random.shuffle(chars) while True: token = ''.join([random.choice(chars) for x in range(size)]) if not ShortUrl.objects.filter(backend=self.class_path, shortened_url__endswith=token).count(): break - return token
\ No newline at end of file + return token diff --git a/brevisurl/models.py b/brevisurl/models.py index a9264ab..0a583ad 100644 --- a/brevisurl/models.py +++ b/brevisurl/models.py @@ -13,7 +13,7 @@ log = logging.getLogger(__name__) class ShortUrl(models.Model): """Model that represents shortened url.""" - original_url = models.URLField(null=False, blank=False) + original_url = models.CharField(max_length=200, null=False, blank=False) original_url_hash = models.CharField(max_length=64, null=False, blank=False) shortened_url = models.URLField(max_length=200, null=False, blank=False, unique=True) backend = models.CharField(max_length=200, null=False, blank=False) @@ -33,11 +33,6 @@ class ShortUrl(models.Model): def clean(self): url_validator = URLValidator() try: - url_validator(self.original_url) - except ValidationError: - log.exception('ShortUrl.original_url is not valid URL') - raise - try: url_validator(self.shortened_url) except ValidationError: log.exception('ShortUrl.shortened_url is not valid URL') @@ -55,4 +50,4 @@ class ShortUrl(models.Model): verbose_name = 'Short url' verbose_name_plural = 'Short urls' ordering = ['-created'] - get_latest_by = 'created'
\ No newline at end of file + get_latest_by = 'created' |
