Commits

Josh VanderLinden  committed 5336c6c

Fixed the tag autocompletion. Added more caching. Bumped version.

  • Participants
  • Parent commits c1c7b71

Comments (0)

Files changed (4)

File articles/__init__.py

-__version__ = '1.7a'
+__version__ = '1.8a'
 
 """
 The Pygments reStructuredText directive

File articles/forms.py

     def clean_tags(self):
         """Turns the string of tags into a list"""
 
-        tags = [Tag.objects.get_or_create(name__iexact=t.strip())[0] for t in self.cleaned_data['tags'].split()]
+        tag = lambda n: Tag.objects.get_or_create(name=Tag.clean(n))[0]
+        tags = [tag(t) for t in self.cleaned_data['tags'].split()]
         self.cleaned_data['tags'] = tags
         return self.cleaned_data['tags']
 

File articles/models.py

 # regex used to find links in an article
 LINK_RE = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I|re.M)
 TITLE_RE = re.compile('<title>(.*?)</title>', re.I|re.M)
-TAG_RE = re.compile('[^a-z0-9\-_\+\@\$\%\^\*\(\)\{\}\[\]\:\'\<\>,=\.\|\\\]?', re.I)
+TAG_RE = re.compile('[^a-z0-9\-_\+\:\.]?', re.I)
 
 def get_name(user):
     """
     def __unicode__(self):
         return self.name
 
+    @staticmethod
+    def clean(name):
+        """Replace spaces with dashes, in case someone adds such a tag manually"""
+
+        name = name.replace(' ', '-')
+        name = TAG_RE.sub('', name)
+        return name.lower().strip()
+
     def save(self, *args, **kwargs):
         """Cleans up any characters I don't want in a URL"""
 
-        # replace spaces with dashes, in case someone adds such a tag manually
-        self.name = self.name.replace(' ', '-')
-        self.name = TAG_RE.sub('', self.name)
+        self.name = Tag.clean(self.name)
         super(Tag, self).save(*args, **kwargs)
 
     @models.permalink

File articles/views.py

 from django.conf import settings
 from django.contrib.auth.models import User
+from django.core.cache import cache
 from django.core.paginator import Paginator, EmptyPage
 from django.core.urlresolvers import reverse
 from django.http import HttpResponsePermanentRedirect, Http404, HttpResponseRedirect, HttpResponse
     """Offers a list of existing tags that match the specified query"""
 
     if 'q' in request.GET:
-        tags = Tag.objects.filter(name__istartswith=request.GET['q'])[:10]
-        return HttpResponse(u'\n'.join(tag.name for tag in tags))
+        q = request.GET['q']
+        key = 'ajax_tag_auto_%s' % q
+        response = cache.get(key)
+
+        if response is None:
+            tags = list(Tag.objects.filter(name__istartswith=q)[:10])
+            response = HttpResponse(u'\n'.join(tag.name for tag in tags))
+            cache.set(key, response, 300)
+
+        return response
 
     return HttpResponse()