def save(self, *args, **kwargs): cache.delete('link_%s' % self.pk) self.do_unique_slug() if not self.id: from resrc.utils.hash2 import hash2 self.hash2 = hash2(self.url) cache.set('link_%s' % self.pk, self, 60*5) super(Link, self).save(*args, **kwargs)
def search(request, tags, operand, excludes): lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by('name').values_list('pk', flat=True) langs = '_'.join(map(str, lang_filter)) from resrc.utils.hash2 import hash2 h = hash2("%s%s%s%s" % (tags, operand, excludes, langs)) result = cache.get(h) if result is None: from django.db.models import Q import operator tags = tags.split(',') excludes = excludes.split(',') # filter after operands if tags[0] != u'': if operand == 'or': # clever "or" trick op = operator.or_ tag_qs = reduce(op, (Q(tags__name=tag) for tag in tags)) links = Link.objects.filter(tag_qs) else: # stupid "and" trick links = Link.objects.filter(tags__name=tags[0]) for tag in tags: links = links.filter(tags__name=tag) else: links = Link.objects.all() for exclude in excludes: links = links.exclude(tags__name=exclude) links = links.filter(language__in=lang_filter) links = links.distinct() link_result = [] links_pk = [] for link in links: link_result.append({ 'pk': link.pk, 'title': link.title, 'url': link.get_absolute_url() }) links_pk.append(link.pk) from resrc.list.models import List lists = List.objects.filter(links__in=links_pk) lists = lists.exclude(is_public=False) lists = lists.filter(language__in=lang_filter) lists = lists.distinct() list_result = [] for alist in lists: list_result.append({ 'pk': alist.pk, 'title': alist.title, 'url': alist.get_absolute_url() }) result = [] result.append(link_result) result.append(list_result) cache.set(h, result, 60*3) result = simplejson.dumps(result) return HttpResponse(result, mimetype="application/javascript")
def search(request, tags, operand, excludes): lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by('name').values_list('pk', flat=True) langs = '_'.join(map(str, lang_filter)) from resrc.utils.hash2 import hash2 h = hash2("%s%s%s%s" % (tags, operand, excludes, langs)) result = cache.get(h) if result is None: from django.db.models import Q import operator tags = tags.split(',') excludes = excludes.split(',') # filter after operands if tags[0] != u'': if operand == 'or': # clever "or" trick op = operator.or_ tag_qs = reduce(op, (Q(tags__name=tag) for tag in tags)) links = Link.objects.filter(tag_qs) else: # stupid "and" trick links = Link.objects.filter(tags__name=tags[0]) for tag in tags: links = links.filter(tags__name=tag) else: links = Link.objects.all() for exclude in excludes: links = links.exclude(tags__name=exclude) links = links.exclude(list__is_public=False) links = links.filter(language__in=lang_filter) link_result = [] links_pk = [] for link in links: link_result.append({ 'pk': link.pk, 'title': link.title, 'url': link.get_absolute_url() }) links_pk.append(link.pk) from resrc.list.models import List lists = List.objects.filter(links__in=links_pk) lists = lists.filter(language__in=lang_filter) lists = lists.distinct() list_result = [] for alist in lists: list_result.append({ 'pk': alist.pk, 'title': alist.title, 'url': alist.get_absolute_url() }) result = [] result.append(link_result) result.append(list_result) cache.set(h, result, 60*60*24*3) result = simplejson.dumps(result) return HttpResponse(result, mimetype="application/javascript")
def test_hash2(self): '''Test hash2''' self.assertEqual(hash2('http://example.com'), 's1o0gijd8y')