Commits

Lucian Brănescu-Mihăilă  committed cc19a21

Switch from ids to slugs.

  • Participants
  • Parent commits 0ee360d

Comments (0)

Files changed (6)

+from django.template.defaultfilters import slugify
+
 from django.db import models
 
+
 class Blog(models.Model):
+    SHORT_CONTENT_LEN = 35
+
+    slug = models.SlugField(primary_key=True)
     title = models.CharField(max_length=35)
-    content = models.CharField(max_length=200)
-    slug = models.CharField(max_length=20)
+    content = models.CharField(max_length=500)
     
     def __unicode__(self):
         return self.title
 
+    @property
+    def short_content(self):
+        if len(self.content) > self.SHORT_CONTENT_LEN:
+            return self.content[:self.SHORT_CONTENT_LEN] + u' ...'
+        else:
+            return self.content
+
+    def get_unique_slug(self, title):
+        potential = slugify(title)
+        suffix = 0
+
+        while True:
+            if suffix:
+                potential = "{}-{}".format(potential, suffix)
+
+            if not Blog.objects.filter(slug=potential).count():
+                return potential
+            
+            suffix += 1
+
     def save(self, *args, **kwargs):
-        if self.slug is None:
-            self.slug = self.title
+        if not self.slug:
+           self.slug = self.get_unique_slug(self.title)
+
 
         super(Blog, self).save(*args, **kwargs)
 
 TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)
 
 ROOT_URLCONF = 'urls'
+APPEND_SLASH = True

File templates/delete.html

 </form>
 
 <h2> {{ object.title }} </h2>
-{{ object.content }}
+{{ object.short_content }}
 
 {% endblock %}

File templates/index.html

 {% block content %}
 {% for post in posts %}
     <article>
-        <a href="{% url show post.pk %}">
+        <a href="{% url show post.slug %}">
             <h2>{{ post.title }}</h2>
         </a>
-        <p>{{ post.content }}</p>
+        <p>{{ post.short_content }}</p>
     </article>
 </ul>
 {% endfor %}

File templates/show.html

 {% block title %} {{ title }} {% endblock %}
 
 {% block nav %}
-<li><a href={% url edit post.id %}>Edit</a></li>
-<li><a href={% url delete post.id %}>Delete</a></li>
+<li><a href={% url edit post.slug %}>Edit</a></li>
+<li><a href={% url delete post.slug %}>Delete</a></li>
 {% endblock %}
 
 {% block content %} 
                          context_object_name='posts'),
         name='index'),
 
-    url(r'^create$',
+    url(r'^create/$',
         CreateView.as_view(template_name='create.html',
                            model=Blog,
                            form_class=BlogForm,
-                           success_url="/%(id)s"),
+                           success_url="/%(slug)s"),
         name='create'),
 
-    url(r'^(?P<pk>\d+)$',
+    url(r'^(?P<slug>[\w-]+)/$',
         DetailView.as_view(template_name="show.html",
                            model=Blog,
                            context_object_name='post'),
         name='show'),
 
-    url(r'^edit/(?P<pk>\d+)$',
+    url(r'^edit/(?P<slug>[\w-]+)/$',
         UpdateView.as_view(template_name='create.html',
                            model=Blog,
                            form_class=BlogForm),
         name='edit'),
 
-    url(r'^delete/(?P<pk>\d+)$',
+    url(r'^delete/(?P<slug>[\w-]+)/$',
         DeleteView.as_view(template_name='delete.html',
                            model=Blog,
                            success_url="/"),