Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add MigratingUserRel class to aid migrations away from UserRel.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsimpson63 committed Jun 30, 2015
1 parent b0cddf4 commit 221b3b0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions r2/r2/lib/db/userrel.py
Expand Up @@ -161,3 +161,35 @@ def _bind(cls, 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

0 comments on commit 221b3b0

Please sign in to comment.