added user and group API and models
This commit is contained in:
@@ -46,23 +46,38 @@ user_group_table = db.Table('user_group',
|
||||
primary_key=True))
|
||||
|
||||
|
||||
# This is the association table for the many-to-many relationship between
|
||||
# groups and permissions.
|
||||
group_permission_table = db.Table('group_permission',
|
||||
db.Column('group_id', db.Integer,
|
||||
db.ForeignKey('group.id',
|
||||
onupdate="CASCADE",
|
||||
ondelete="CASCADE"),
|
||||
primary_key=True),
|
||||
db.Column('permission_id', db.Integer,
|
||||
db.ForeignKey('permission.id',
|
||||
onupdate="CASCADE",
|
||||
ondelete="CASCADE"),
|
||||
primary_key=True))
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
"""
|
||||
Example user model representation.
|
||||
"""
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
social_id = db.Column(db.String(64), nullable=True, unique=True)
|
||||
nickname = db.Column(db.String(64), index=True, unique=True)
|
||||
first_name = db.Column(db.String(64), index=True, nullable=True)
|
||||
last_name = db.Column(db.String(64), index=True, nullable=True)
|
||||
social_id = db.Column(db.Unicode(63), nullable=True, unique=True)
|
||||
nickname = db.Column(db.Unicode(63), index=True, unique=True)
|
||||
first_name = db.Column(db.Unicode(63), index=True, nullable=True)
|
||||
last_name = db.Column(db.Unicode(63), index=True, nullable=True)
|
||||
email = db.Column(db.String(120), nullable=False, index=True, unique=True)
|
||||
lang = db.Column(db.String(16), index=False, unique=False)
|
||||
timezone = db.Column(db.String(32), index=False, unique=False)
|
||||
lang = db.Column(db.Unicode(32), index=False, unique=False)
|
||||
timezone = db.Column(db.Unicode(63), index=False, unique=False)
|
||||
posts = db.relationship('Post', backref='author', lazy='dynamic')
|
||||
example_data_item = db.relationship('ExampleDataItem', backref='owner')
|
||||
example_data_item_id = db.Column(db.ForeignKey(ExampleDataItem.id))
|
||||
about_me = db.Column(db.String(140))
|
||||
role = db.Column(db.String(64))
|
||||
about_me = db.Column(db.Unicode(255))
|
||||
role = db.Column(db.Unicode(63))
|
||||
groups = db.relationship('Group', secondary=user_group_table, back_populates='users')
|
||||
password = db.Column(db.String(255), nullable=True)
|
||||
registered_on = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
|
||||
@@ -110,6 +125,16 @@ class User(UserMixin, db.Model):
|
||||
User.email == identifier,
|
||||
User.id == identifier)).first()
|
||||
|
||||
@staticmethod
|
||||
@login_manager.user_loader
|
||||
def get_by_id(identifier):
|
||||
"""
|
||||
Find user by ID.
|
||||
:param identifier:
|
||||
:return:
|
||||
"""
|
||||
return User.query.filter(User.id == identifier).first()
|
||||
|
||||
@staticmethod
|
||||
def get_all():
|
||||
"""
|
||||
@@ -396,8 +421,9 @@ class Group(db.Model):
|
||||
super(Group, self).__init__(**kwargs)
|
||||
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
name = db.Column(db.Unicode(64), unique=True, nullable=False)
|
||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||
users = db.relationship('User', secondary=user_group_table, back_populates='groups')
|
||||
permissions = db.relationship('Permission', secondary=group_permission_table, back_populates='groups')
|
||||
|
||||
@staticmethod
|
||||
def get_by_name(name):
|
||||
@@ -417,3 +443,12 @@ class Group(db.Model):
|
||||
def toJSON(self):
|
||||
return json.dumps(self.to_dict(), default=lambda o: o.__dict__,
|
||||
sort_keys=True, indent=4)
|
||||
|
||||
|
||||
class Permission(db.Model):
|
||||
"""Table containing permissions associated with groups."""
|
||||
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
||||
name = db.Column(db.Unicode(63), unique=True, nullable=False)
|
||||
description = db.Column(db.Unicode(255))
|
||||
groups = db.relationship(Group, secondary=group_permission_table,
|
||||
back_populates='permissions')
|
||||
|
||||
Reference in New Issue
Block a user