This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
userrel.py
215 lines (174 loc) · 7.3 KB
/
userrel.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
# Inc. All Rights Reserved.
###############################################################################
import functools
import types
from r2.lib.db.thing import CreationError
from r2.lib.memoize import memoize
class UserRelManager(object):
"""Manages access to a relation between a type of thing and users."""
def __init__(self, name, relation, permission_class):
self.name = name
self.relation = relation
self.permission_class = permission_class
def get(self, thing, user):
if user:
q = self.relation._fast_query([thing], [user], self.name)
rel = q.get((thing, user, self.name))
if rel:
rel._permission_class = self.permission_class
return rel
def add(self, thing, user, permissions=None, **attrs):
if self.get(thing, user):
return None
r = self.relation(thing, user, self.name, **attrs)
if permissions is not None:
r.set_permissions(permissions)
try:
r._commit()
except CreationError:
return None
r._permission_class = self.permission_class
return r
def remove(self, thing, user):
r = self.get(thing, user)
if r:
r._delete()
return True
return False
def mutate(self, thing, user, **attrs):
r = self.get(thing, user)
if r:
for k, v in attrs.iteritems():
setattr(r, k, v)
r._commit()
r._permission_class = self.permission_class
return r
else:
return self.add(thing, user, **attrs)
def ids(self, thing):
q = self.relation._simple_query(
["_thing2_id"],
self.relation.c._thing1_id == thing._id,
self.relation.c._name == self.name,
sort='_date',
)
return [r._thing2_id for r in q]
def reverse_ids(self, user):
q = self.relation._simple_query(
["_thing1_id"],
self.relation.c._thing2_id == user._id,
self.relation.c._name == self.name,
)
return [r._thing1_id for r in q]
def by_thing(self, thing):
q = self.relation._query(
self.relation.c._thing1_id == thing._id,
self.relation.c._name == self.name,
sort='_date',
data=True,
)
for r in q:
r._permission_class = self.permission_class
yield r
class MemoizedUserRelManager(UserRelManager):
"""Memoized manager for a relation to users."""
def __init__(self, name, relation, permission_class,
disable_ids_fn=False, disable_reverse_ids_fn=False):
super(MemoizedUserRelManager, self).__init__(
name, relation, permission_class)
self.disable_ids_fn = disable_ids_fn
self.disable_reverse_ids_fn = disable_reverse_ids_fn
self.ids_fn_name = self.name + '_ids'
self.reverse_ids_fn_name = 'reverse_' + self.name + '_ids'
sup = super(MemoizedUserRelManager, self)
self.ids = memoize(self.ids_fn_name)(sup.ids)
self.reverse_ids = memoize(self.reverse_ids_fn_name)(sup.reverse_ids)
self.add = self._update_caches_on_success(sup.add)
self.remove = self._update_caches_on_success(sup.remove)
def _update_caches(self, thing, user):
if not self.disable_ids_fn:
self.ids(thing, _update=True)
if not self.disable_reverse_ids_fn:
self.reverse_ids(user, _update=True)
def _update_caches_on_success(self, method):
@functools.wraps(method)
def wrapper(thing, user, *args, **kwargs):
try:
result = method(thing, user, *args, **kwargs)
except:
raise
else:
self._update_caches(thing, user)
return result
return wrapper
def UserRel(name, relation, disable_ids_fn=False, disable_reverse_ids_fn=False,
permission_class=None):
"""Mixin for Thing subclasses for managing a relation to users.
Provides the following suite of methods for a relation named "<relation>":
- is_<relation>(self, user) - whether user is related to self
- add_<relation>(self, user) - relates user to self
- remove_<relation>(self, user) - dissolves relation of user to self
This suite also usually includes (unless explicitly disabled):
- <relation>_ids(self) - list of user IDs related to self
- (static) reverse_<relation>_ids(user) - list of thing IDs user is
related to
"""
mgr = MemoizedUserRelManager(
name, relation, permission_class,
disable_ids_fn, disable_reverse_ids_fn)
class UR:
@classmethod
def _bind(cls, fn):
return types.UnboundMethodType(fn, None, cls)
setattr(UR, 'is_' + name, UR._bind(mgr.get))
setattr(UR, 'get_' + name, UR._bind(mgr.get))
setattr(UR, 'add_' + name, UR._bind(mgr.add))
setattr(UR, 'remove_' + name, UR._bind(mgr.remove))
setattr(UR, 'each_' + name, UR._bind(mgr.by_thing))
setattr(UR, name + '_permission_class', permission_class)
if not disable_ids_fn:
setattr(UR, mgr.ids_fn_name, UR._bind(mgr.ids))
if not disable_reverse_ids_fn:
setattr(UR, mgr.reverse_ids_fn_name, staticmethod(mgr.reverse_ids))
return UR
def MigratingUserRel(name, relation, disable_ids_fn=False,
disable_reverse_ids_fn=False, permission_class=None):
"""
Replacement for UserRel to be used during migrations away from the system.
The resulting "UserRel" classes generated are to be used as standalones and
not included in Subreddit.__bases__.
"""
mgr = MemoizedUserRelManager(
name, relation, permission_class,
disable_ids_fn, disable_reverse_ids_fn)
class URM: pass
setattr(URM, 'is_' + name, mgr.get)
setattr(URM, 'get_' + name, mgr.get)
setattr(URM, 'add_' + name, staticmethod(mgr.add))
setattr(URM, 'remove_' + name, staticmethod(mgr.remove))
setattr(URM, 'each_' + name, mgr.by_thing)
setattr(URM, name + '_permission_class', permission_class)
if not disable_ids_fn:
setattr(URM, mgr.ids_fn_name, mgr.ids)
if not disable_reverse_ids_fn:
setattr(URM, mgr.reverse_ids_fn_name, staticmethod(mgr.reverse_ids))
return URM