-
Notifications
You must be signed in to change notification settings - Fork 569
/
managers.py
184 lines (139 loc) · 5.14 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from django.db import models
from django.db.models import Count
from django.db.models import Q
from django.db.models.query import EmptyQuerySet
from django.db.models.query import QuerySet
from mptt.managers import TreeManager
class ArticleQuerySet(QuerySet):
def can_read(self, user):
"""Filter objects so only the ones with a user's reading access
are included"""
if user.has_perm("wiki.moderator"):
return self
if user.is_anonymous:
q = self.filter(other_read=True)
else:
q = self.filter(
Q(other_read=True)
| Q(owner=user)
| (Q(group__user=user) & Q(group_read=True))
).annotate(Count("id"))
return q
def can_write(self, user):
"""Filter objects so only the ones with a user's writing access
are included"""
if user.has_perm("wiki.moderator"):
return self
if user.is_anonymous:
q = self.filter(other_write=True)
else:
q = self.filter(
Q(other_write=True)
| Q(owner=user)
| (Q(group__user=user) & Q(group_write=True))
)
return q
def active(self):
return self.filter(current_revision__deleted=False)
class ArticleEmptyQuerySet(EmptyQuerySet):
def can_read(self, user):
return self
def can_write(self, user):
return self
def active(self):
return self
class ArticleFkQuerySetMixin:
def can_read(self, user):
"""Filter objects so only the ones with a user's reading access
are included"""
if user.has_perm("wiki.moderate"):
return self
if user.is_anonymous:
q = self.filter(article__other_read=True)
else:
# https://github.com/django-wiki/django-wiki/issues/67
q = self.filter(
Q(article__other_read=True)
| Q(article__owner=user)
| (Q(article__group__user=user) & Q(article__group_read=True))
).annotate(Count("id"))
return q
def can_write(self, user):
"""Filter objects so only the ones with a user's writing access
are included"""
if user.has_perm("wiki.moderate"):
return self
if user.is_anonymous:
q = self.filter(article__other_write=True)
else:
# https://github.com/django-wiki/django-wiki/issues/67
q = self.filter(
Q(article__other_write=True)
| Q(article__owner=user)
| (Q(article__group__user=user) & Q(article__group_write=True))
).annotate(Count("id"))
return q
def active(self):
return self.filter(article__current_revision__deleted=False)
class ArticleFkEmptyQuerySetMixin:
def can_read(self, user):
return self
def can_write(self, user):
return self
def active(self):
return self
class ArticleFkQuerySet(ArticleFkQuerySetMixin, QuerySet):
pass
class ArticleFkEmptyQuerySet(ArticleFkEmptyQuerySetMixin, EmptyQuerySet):
pass
class ArticleManager(models.Manager):
def get_empty_query_set(self):
return self.get_queryset().none()
def get_queryset(self):
return ArticleQuerySet(self.model, using=self._db)
def active(self):
return self.get_queryset().active()
def can_read(self, user):
return self.get_queryset().can_read(user)
def can_write(self, user):
return self.get_queryset().can_write(user)
class ArticleFkManager(models.Manager):
def get_empty_query_set(self):
return self.get_queryset().none()
def get_queryset(self):
return ArticleFkQuerySet(self.model, using=self._db)
def active(self):
return self.get_queryset().active()
def can_read(self, user):
return self.get_queryset().can_read(user)
def can_write(self, user):
return self.get_queryset().can_write(user)
class URLPathEmptyQuerySet(EmptyQuerySet, ArticleFkEmptyQuerySetMixin):
def select_related_common(self):
return self
def default_order(self):
return self
class URLPathQuerySet(QuerySet, ArticleFkQuerySetMixin):
def select_related_common(self):
return self.select_related(
"parent", "article__current_revision", "article__owner"
)
def default_order(self):
"""Returns elements by there article order"""
return self.order_by("article__current_revision__title")
class URLPathManager(TreeManager):
def get_empty_query_set(self):
return self.get_queryset().none()
def get_queryset(self):
"""Return a QuerySet with the same ordering as the TreeManager."""
return URLPathQuerySet(self.model, using=self._db).order_by(
self.tree_id_attr, self.left_attr
)
def select_related_common(self):
return self.get_queryset().common_select_related()
def active(self):
return self.get_queryset().active()
def can_read(self, user):
return self.get_queryset().can_read(user)
def can_write(self, user):
return self.get_queryset().can_write(user)