110067722: Create the model and dao for services.

This commit creates the data model for services and user_to_service.
The dao is also created to insert, get, activate, and unrestrict the service.
This commit is contained in:
Rebecca Law
2015-12-14 15:59:29 +00:00
parent 6c40e72e78
commit 4b01335703
4 changed files with 156 additions and 0 deletions

View File

@@ -78,6 +78,41 @@ class User(db.Model):
return True
user_to_service = db.Table(
'user_to_service',
db.Model.metadata,
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('service_id', db.Integer, db.ForeignKey('services.id'))
)
class Service(db.Model):
__tablename__ = 'services'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
token_id = db.Column(db.BigInteger, index=True, unique=True)
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
active = db.Column(db.Boolean, index=False, unique=False, nullable=False)
limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False)
users = db.relationship('User', secondary=user_to_service, backref=db.backref('user_to_service', lazy='dynamic'))
restricted = db.Column(db.Boolean, index=False, unique=False, nullable=False)
def serialize(self):
serialized = {
'id': self.id,
'name': self.name,
'createdAt': self.created_at.strftime(DATETIME_FORMAT),
'active': self.active,
'restricted': self.restricted,
'limit': self.limit,
'user': self.users.serialize()
}
return filter_null_value_fields(serialized)
def filter_null_value_fields(obj):
return dict(
filter(lambda x: x[1] is not None, obj.items())