Created
February 5, 2012 08:29
-
-
Save fish2000/1744090 to your computer and use it in GitHub Desktop.
this is totally wrong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TaggedQuerySet(JSONQuerySet): | |
| count_re = re.compile(r'(select) (distinct) (.+) (count)(.*)(group by)(.+)(asc)$', flags=re.S|re.I|re.M) | |
| @delegate | |
| def tagged(self, name=None): | |
| """ Return a QuerySet of the model instances tagged with the named tag. """ | |
| if name: | |
| try: | |
| t = tags.Tag.objects.filter(name=name).get() | |
| except ObjectDoesNotExist: | |
| return self.none() | |
| else: | |
| return self.filter( | |
| id__in=t.items.get_by_model(self.model, t).values('id') | |
| ) | |
| return self.none() | |
| @delegate | |
| def tags(self, related_to=None): | |
| """ Returns a QuerySet of tagging.models.Tag objects related to members of the QuerySet, | |
| optionally limited to those also related to the tag or tags named in related_to """ | |
| tags_raw = tags.Tag.objects.usage_for_queryset(self.all(), counts=True) | |
| if related_to is None: | |
| return tags.Tag.objects.extra(select=dict( | |
| count=('%s AND "tagging_taggeditem"."object_id" = "twemoir_tmtweet"."id"' % self.all().count_re.sub(r'\1 \2 \4 \5', tags_raw.raw_query).strip()) | |
| )).filter( | |
| id__in=(t.id for t in tags_raw) | |
| ) | |
| else: | |
| return tags.Tag.objects.filter( | |
| Q(id__in=(t.id for t in tags.Tag.objects.related_for_model(related_to, self.model))) & | |
| Q(id__in=(t.id for t in tags.Tag.objects.usage_for_queryset(self.all()))) | |
| ) | |
| @delegate | |
| def tagnames(self, related_to=None): | |
| """ Returns a list of the string names of all tags used by members of the QuerySet, | |
| optionally limited to those related to the tag or tags named in related_to """ | |
| #return (t.name for t in tags.Tag.objects.usage_for_queryset(self.all())) | |
| return (t.name for t in self.tags(related_to=related_to)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment