diff --git a/litl/models.py b/litl/models.py index 1aed24b..6aa060e 100644 --- a/litl/models.py +++ b/litl/models.py @@ -11,7 +11,7 @@ class Slug(models.Model): slug = models.SlugField( max_length=20, null=False, - blank=False, + blank=True, help_text="Slug of the new URL. Up to 20 caracters including letters, number, underscores and hyphens.",) destination = models.CharField( max_length=300, @@ -24,3 +24,7 @@ class Slug(models.Model): date = models.DateTimeField( auto_now=True,) + def __str__(self): + return '{} -> {:.10}'.format(self.slug,self.destination) + + diff --git a/litl/views.py b/litl/views.py index 129cac4..fa80242 100644 --- a/litl/views.py +++ b/litl/views.py @@ -10,6 +10,9 @@ from .settings import TEMPLATES from .models import Slug from .forms import SlugAddForm +import random, string +ensemble = string.ascii_letters + string.digits + def index(request): """Display the home page of the site. TODO: Search and display the urls shortened by the user in the past. @@ -24,7 +27,14 @@ def AddSlug(request): form = SlugAddForm(request.POST) if form.is_valid(): - form.save() + slug = form.save(commit=False) + if slug.slug == '': + slug.slug = ''.join(random.choice(ensemble) for _ in range(5)) + + slug.uid = ''.join(random.choice(ensemble) for _ in range(20)) + + slug.save() + return HttpResponseRedirect('') else: form = SlugAddForm()