Commits

Gregory Petukhov committed 4e9e830

Add html auto-cleaning

Comments (0)

Files changed (4)

 
 * Add to urls.py::
 
-    url(r'^blog/', include('article.urls'), namespace='article'),
+    url(r'^blog/', include('article.urls', namespace='article')),
 
 * Run ./manage.py syncdb --migrate
 

article/models.py

     slug = models.SlugField(max_length=255, blank=False, default=None)
     category = models.ForeignKey('article.Category', related_name='articles')
     is_starred = models.BooleanField(blank=True, default=False)
-    teaser = RichTextField()
+    teaser = RichTextField(blank=True)
     content = RichTextField()
     image = models.ImageField(blank=True,
             upload_to='upload/article/article_image')
 
     def __unicode__(self):
         return self.name
+
+import article.signals

article/signals.py

+from lxml.html import fromstring, tostring
+
+from django.db.models.signals import pre_save
+from django.dispatch import receiver
+
+from article.models import Article
+
+def normalize_html(html):
+    tree = fromstring(html)
+    # remove leading empty P tags
+    for elem in tree.xpath('./*'):
+        if elem.tag != 'p':
+            break
+        if len(elem) == 0 and elem.text.strip() == "":
+            elem.getparent().remove(elem)
+        else:
+            break
+    return tostring(tree, encoding='utf-8')
+
+
+@receiver(pre_save, sender=Article)
+def article_pre_save(instance, **kwargs):
+    instance.teaser = normalize_html(instance.teaser)
+    instance.content = normalize_html(instance.content)
 # -*- coding: utf-8 -*-
 from django.shortcuts import redirect, get_object_or_404, render
 from django.conf import settings
-import pymongo
-from bson import ObjectId
-from grab.tools.lxml_tools import render_html, parse_html, disable_links, drop_node
 from random import sample
 
 from common.pagination import paginate
 
 from article.models import Article, Category
 
-db = pymongo.Connection()['turtips']
-
 def home_page(request):
     article_qs = Article.objects.order_by('-created')
     page = paginate(article_qs, request)
 def article_details(request, pk, article_slug):
     article = get_object_or_404(Article, pk=pk)
 
-    qs = Article.objects.all()
+    qs = Article.objects.exclude(pk=article.pk)
     also = []
     ids = qs.values_list('pk', flat=True)
     also.extend(list(qs.filter(pk__lt=article.pk).order_by('-pk')[:1]))
     also.extend(list(qs.filter(pk__lt=article.pk).order_by('-pk')[:1]))
-    also.extend(list(qs.filter(pk__in=sample(ids, 1))))
+    if ids:
+        also.extend(list(qs.filter(pk__in=sample(ids, 1))))
     also = set(also)
 
     context = {'article': article, 'also_articles': also}