diff options
author | Jessica Tallon <tsyesika@tsyesika.se> | 2015-06-26 15:29:44 +0200 |
---|---|---|
committer | Jessica Tallon <tsyesika@tsyesika.se> | 2015-07-31 15:14:41 +0200 |
commit | aa9ba3ed8010362466d14037e2c9950ce3bc2cb2 (patch) | |
tree | 16a2b01658fc12719745126bded30e87ef69e722 /mediagoblin/db/models.py | |
parent | b4b04522bf765eb5c085402be11c55c80104e94e (diff) | |
download | mediagoblin-aa9ba3ed8010362466d14037e2c9950ce3bc2cb2.tar.lz mediagoblin-aa9ba3ed8010362466d14037e2c9950ce3bc2cb2.tar.xz mediagoblin-aa9ba3ed8010362466d14037e2c9950ce3bc2cb2.zip |
Add LocalUser and RemoteUser and migration
Diffstat (limited to 'mediagoblin/db/models.py')
-rw-r--r-- | mediagoblin/db/models.py | 155 |
1 files changed, 97 insertions, 58 deletions
diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index 054e1677..ef37aef8 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -222,12 +222,92 @@ class Location(Base): class User(Base, UserMixin): """ - TODO: We should consider moving some rarely used fields - into some sort of "shadow" table. + Base user that is common amongst LocalUser and RemoteUser. + + This holds all the fields which are common between both the Local and Remote + user models. + + NB: ForeignKeys should reference this User model and NOT the LocalUser or + RemoteUser models. """ __tablename__ = "core__users" id = Column(Integer, primary_key=True) + url = Column(Unicode) + bio = Column(UnicodeText) + name = Column(Unicode) + + created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow) + updated = Column(DateTime, nullable=False, default=datetime.datetime.utcnow) + + location = Column(Integer, ForeignKey("core__locations.id")) + + # Lazy getters + get_location = relationship("Location", lazy="joined") + + def has_privilege(self, privilege, allow_admin=True): + """ + This method checks to make sure a user has all the correct privileges + to access a piece of content. + + :param privilege A unicode object which represent the different + privileges which may give the user access to + content. + + :param allow_admin If this is set to True the then if the user is + an admin, then this will always return True + even if the user hasn't been given the + privilege. (defaults to True) + """ + priv = Privilege.query.filter_by(privilege_name=privilege).one() + if priv in self.all_privileges: + return True + elif allow_admin and self.has_privilege(u'admin', allow_admin=False): + return True + + return False + + def is_banned(self): + """ + Checks if this user is banned. + + :returns True if self is banned + :returns False if self is not + """ + return UserBan.query.get(self.id) is not None + + def serialize(self, request): + published = UTC.localize(self.created) + updated = UTC.localize(self.updated) + user = { + "published": published.isoformat(), + "updated": updated.isoformat(), + "objectType": self.object_type, + "pump_io": { + "shared": False, + "followed": False, + }, + } + + if self.bio: + user.update({"summary": self.bio}) + if self.url: + user.update({"url": self.url}) + if self.location: + user.update({"location": self.get_location.serialize(request)}) + + def unserialize(self, data): + if "summary" in data: + self.bio = data["summary"] + + if "location" in data: + Location.create(data, self) + +class LocalUser(User): + """ This represents a user registered on this instance """ + __tablename__ = "core__local_users" + + id = Column(Integer, ForeignKey("core__users.id"), primary_key=True) username = Column(Unicode, nullable=False, unique=True) # Note: no db uniqueness constraint on email because it's not # reliable (many email systems case insensitive despite against @@ -235,18 +315,14 @@ class User(Base, UserMixin): # point. email = Column(Unicode, nullable=False) pw_hash = Column(Unicode) - created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow) + # Intented to be nullable=False, but migrations would not work for it # set to nullable=True implicitly. wants_comment_notification = Column(Boolean, default=True) wants_notifications = Column(Boolean, default=True) license_preference = Column(Unicode) - url = Column(Unicode) - bio = Column(UnicodeText) # ?? uploaded = Column(Integer, default=0) upload_limit = Column(Integer) - location = Column(Integer, ForeignKey("core__locations.id")) - get_location = relationship("Location", lazy="joined") ## TODO # plugin data would be in a separate model @@ -278,50 +354,11 @@ class User(Base, UserMixin): super(User, self).delete(**kwargs) _log.info('Deleted user "{0}" account'.format(self.username)) - def has_privilege(self, privilege, allow_admin=True): - """ - This method checks to make sure a user has all the correct privileges - to access a piece of content. - - :param privilege A unicode object which represent the different - privileges which may give the user access to - content. - - :param allow_admin If this is set to True the then if the user is - an admin, then this will always return True - even if the user hasn't been given the - privilege. (defaults to True) - """ - priv = Privilege.query.filter_by(privilege_name=privilege).one() - if priv in self.all_privileges: - return True - elif allow_admin and self.has_privilege(u'admin', allow_admin=False): - return True - - return False - - def is_banned(self): - """ - Checks if this user is banned. - - :returns True if self is banned - :returns False if self is not - """ - return UserBan.query.get(self.id) is not None - - def serialize(self, request): - published = UTC.localize(self.created) user = { "id": "acct:{0}@{1}".format(self.username, request.host), - "published": published.isoformat(), "preferredUsername": self.username, "displayName": "{0}@{1}".format(self.username, request.host), - "objectType": self.object_type, - "pump_io": { - "shared": False, - "followed": False, - }, "links": { "self": { "href": request.urlgen( @@ -347,21 +384,23 @@ class User(Base, UserMixin): }, } - if self.bio: - user.update({"summary": self.bio}) - if self.url: - user.update({"url": self.url}) - if self.location: - user.update({"location": self.get_location.serialize(request)}) - + user.update(super(LocalUser, self).serialize(request)) return user - def unserialize(self, data): - if "summary" in data: - self.bio = data["summary"] +class RemoteUser(User): + """ User that is on another (remote) instance """ + __tablename__ = "core__remote_users" + + id = Column(Integer, ForeignKey("core__users.id"), primary_key=True) + webfinger = Column(Unicode, unique=True) + + def __repr__(self): + return "<{0} #{1} {2}>".format( + self.__class__.__name__, + self.id, + self.webfinger + ) - if "location" in data: - Location.create(data, self) class Client(Base): """ |