disable search
[wolnelektury.git] / src / 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_queryset(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):
39         if queryset is None:
40             return self.intermediary_table_model.objects.get_related(
41                 obj, self.model)
42         else:
43             return self.intermediary_table_model.objects.get_related(
44                 obj, queryset)
45
46     def with_all(self, tags, queryset=None):
47         if queryset is None:
48             return self.intermediary_table_model.objects.get_by_model(self.model, tags)
49         else:
50             return self.intermediary_table_model.objects.get_by_model(queryset, tags)
51
52     def with_any(self, tags, queryset=None):
53         if queryset is None:
54             return self.intermediary_table_model.objects.get_union_by_model(self.model, tags)
55         else:
56             return self.intermediary_table_model.objects.get_union_by_model(queryset, tags)
57
58
59 class TagDescriptor(object):
60     """
61     A descriptor which provides access to a ``ModelTagManager`` for
62     model classes and simple retrieval, updating and deletion of tags
63     for model instances.
64     """
65     def __init__(self, tag_model):
66         self.tag_model = tag_model
67
68     def __get__(self, instance, owner):
69         if not instance:
70             tag_manager = ModelTagManager(self.tag_model)
71             tag_manager.model = owner
72             return tag_manager
73         else:
74             return self.tag_model.objects.get_for_object(instance)
75
76     def __set__(self, instance, value):
77         self.tag_model.objects.update_tags(instance, value)
78
79     def __del__(self, instance):
80         self.tag_model.objects.update_tags(instance, [])