-
Notifications
You must be signed in to change notification settings - Fork 0
/
managers.py
56 lines (46 loc) · 1.84 KB
/
managers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Custom managers for Django models registered with the tagging
application.
"""
from django.contrib.contenttypes.models import ContentType
from django.db import models
from tagging.models import Tag, TaggedItem
class ModelTagManager(models.Manager):
"""
A manager for retrieving tags for a particular model.
"""
def get_query_set(self):
ctype = ContentType.objects.get_for_model(self.model)
return Tag.objects.filter(
items__content_type__pk=ctype.pk).distinct()
def cloud(self, *args, **kwargs):
return Tag.objects.cloud_for_model(self.model, *args, **kwargs)
def related(self, tags, *args, **kwargs):
return Tag.objects.related_for_model(tags, self.model, *args, **kwargs)
def usage(self, *args, **kwargs):
return Tag.objects.usage_for_model(self.model, *args, **kwargs)
class ModelTaggedItemManager(models.Manager):
"""
A manager for retrieving model instances based on their tags.
"""
def with_all(self, tags, filter_function=None):
return TaggedItem.objects.match_all(self.model, tags, filter_function)
def with_any(self, tags, filter_function=None):
return TaggedItem.objects.match_any(self.model, tags, filter_function)
class TagDescriptor(object):
"""
A descriptor which provides access to a ``ModelTagManager`` for
model classes and simple retrieval, updating and deletion of tags
for model instances.
"""
def __get__(self, instance, owner):
if not instance:
tag_manager = ModelTagManager()
tag_manager.model = owner
return tag_manager
else:
return Tag.objects.get_for_object(instance)
def __set__(self, instance, value):
Tag.objects.update_tags(instance, value)
def __delete__(self, instance):
Tag.objects.update_tags(instance, None)