sexy draggable marquee
[wolnelektury.git] / apps / newtagging / managers.py
1 # -*- coding: utf-8 -*-
2 """
3 Custom managers for Django models registered with the tagging
4 application.
5 """
6 from django.contrib.contenttypes.models import ContentType
7 from django.db import models
8
9
10 class ModelTagManager(models.Manager):
11     """
12     A manager for retrieving tags for a particular model.
13     """
14     def __init__(self, tag_model):
15         super(ModelTagManager, self).__init__()
16         self.tag_model = tag_model
17
18     def get_query_set(self):
19         content_type = ContentType.objects.get_for_model(self.model)
20         return self.tag_model.objects.filter(
21             items__content_type__pk=content_type.pk).distinct()
22
23     def related(self, tags, *args, **kwargs):
24         return self.tag_model.objects.related_for_model(tags, self.model, *args, **kwargs)
25
26     def usage(self, *args, **kwargs):
27         return self.tag_model.objects.usage_for_model(self.model, *args, **kwargs)
28
29
30 class ModelTaggedItemManager(models.Manager):
31     """
32     A manager for retrieving model instances based on their tags.
33     """
34     def __init__(self, tag_model):
35         super(ModelTaggedItemManager, self).__init__()
36         self.intermediary_table_model = tag_model.objects.intermediary_table_model
37
38     def related_to(self, obj, queryset=None, num=None):
39         if queryset is None:
40             return self.intermediary_table_model.objects.get_related(obj, self.model, num=num)
41         else:
42             return self.intermediary_table_model.objects.get_related(obj, queryset, num=num)
43
44     def with_all(self, tags, queryset=None):
45         if queryset is None:
46             return self.intermediary_table_model.objects.get_by_model(self.model, tags)
47         else:
48             return self.intermediary_table_model.objects.get_by_model(queryset, tags)
49
50     def with_any(self, tags, queryset=None):
51         if queryset is None:
52             return self.intermediary_table_model.objects.get_union_by_model(self.model, tags)
53         else:
54             return self.intermediary_table_model.objects.get_union_by_model(queryset, tags)
55
56
57 class TagDescriptor(object):
58     """
59     A descriptor which provides access to a ``ModelTagManager`` for
60     model classes and simple retrieval, updating and deletion of tags
61     for model instances.
62     """
63     def __init__(self, tag_model):
64         self.tag_model = tag_model
65
66     def __get__(self, instance, owner):
67         if not instance:
68             tag_manager = ModelTagManager(self.tag_model)
69             tag_manager.model = owner
70             return tag_manager
71         else:
72             return self.tag_model.objects.get_for_object(instance)
73
74     def __set__(self, instance, value):
75         self.tag_model.objects.update_tags(instance, value)
76
77     def __del__(self, instance):
78         self.tag_model.objects.update_tags(instance, [])
79