Skip to content

Commit

Permalink
[update]
Browse files Browse the repository at this point in the history
  • Loading branch information
boke0 committed Mar 4, 2021
1 parent 89c5114 commit 61eef0e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mitama/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from mitama._extra import _classproperty

from .core_db import db
from .nodes import User, Group, UserGroup, UserInvite, AuthorizationError, Role, InnerRole
from .nodes import User, Group, Node, UserGroup, UserInvite, AuthorizationError, Role, InnerRole
from .permissions import permission, inner_permission

Permission = permission(db, [
Expand Down
43 changes: 40 additions & 3 deletions mitama/models/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class UserGroup(db.Model):
user = relationship("User")
group = relationship("Group")

class Node(object):

class AbstractNode(object):
_icon = Column(LargeBinary)
_name = Column("name", String(255))
_screen_name = Column("screen_name", String(255))
Expand Down Expand Up @@ -134,7 +135,7 @@ def retrieve(cls, _id=None, screen_name=None, **kwargs):
def __eq__(self, other):
return self._id == other._id

class User(Node, db.Model):
class User(AbstractNode, db.Model):
"""ユーザーのモデルクラスです
:param _id: 固有のID
Expand Down Expand Up @@ -266,7 +267,7 @@ def is_ancestor(self, node):
return False


class Group(Node, db.Model):
class Group(AbstractNode, db.Model):
"""グループのモデルクラスです
:param _id: 固有のID
Expand Down Expand Up @@ -536,4 +537,40 @@ def exists(self, group, user):
relation = UserGroup.retrieve(group=group, user=user)
return relation in self.relations

class Node(db.Model):
__tablename__ = "mitama_node"
user_id = Column(String(64), ForeignKey("mitama_user._id", ondelete="CASCADE"), unique=True)
group_id = Column(String(64), ForeignKey("mitama_group._id", ondelete="CASCADE"), unique=True)
user = relationship(User)
group = relationship(Group)

@classmethod
def retrieve(cls, obj=None, id=None, **kwargs):
if obj is not None:
if isinstance(obj, str):
if obj.split("-")[0] == "user":
obj = User.retrieve(obj)
else:
obj = Group.retrieve(obj)
try:
if isinstance(obj, User):
return cls.query.filter(cls.user == obj).one()
else:
return cls.query.filter(cls.group == obj).one()
except Exception:
if isinstance(obj, User):
node = cls()
node.user = obj
node.create()
return node
else:
node = cls()
node.group = obj
node.create()
return node
else:
return super().retrieve(id=id, **kwargs)

@property
def object(self):
return self.user if self.user is not None else self.group

0 comments on commit 61eef0e

Please sign in to comment.