--- a/.gitmodules +++ /dev/null @@ -1,4 +1 @@ -[submodule "swtstore/classes/models/um"] - path = swtstore/classes/models/um - url = git@git.pantoto.org:user-management/user-management.git --- /dev/null +++ b/swtstore/classes/models/group.py @@ -1 +1,40 @@ +# -*- coding utf-8 -*- + + +# -*- coding: utf-8 -*- +# User Management::Group + +from datetime import datetime + +from .config import db + +class Group(db.Model): + """ + docstring + """ + __tablename__ = 'groups' + + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(80), unique=True) + created = db.Column(db.DateTime, default=datetime.utcnow) + created_by = db.Column(db.Integer, db.ForeignKey('users.id')) + + creator = db.relationship('User', backref='groups') + + def __init__(self, name, creator): + self.name = name + self.creator = creator + + # persist object to database + def persist(self): + db.session.add(self) + db.session.commit() + + # delete from database + def remove(self): + db.session.delete(self) + db.session.commit() + + def __repr__(self): + return '' % self.name --- /dev/null +++ b/swtstore/classes/models/membership.py @@ -1 +1,37 @@ +# -*- coding utf-8 -*- +# User Management::Membership + +from datetime import datetime + +from .config import db + +class Membership(db.Model): + """ docstring """ + + __tablename__ = 'memberships' + + id = db.Column(db.Integer, primary_key=True) + uid = db.Column(db.Integer, db.ForeignKey('users.id')) + gid = db.Column(db.Integer, db.ForeignKey('groups.id')) + created = db.Column(db.DateTime, default=datetime.utcnow) + + user = db.relationship('User', backref='memberships') + group = db.relationship('Group', backref='memberships') + + def __init__(self, user, group): + self.user = user + self.group = group + + # persist in the database + def persist(self): + db.session.add(self) + db.session.commit() + + # delete from a database + def remove(self): + db.session.delete(self) + db.session.commit() + + def __repr__(self): + return'' % self.uid --- /dev/null +++ b/swtstore/classes/models/user.py @@ -1 +1,71 @@ +# -*- coding utf-8 -*- +# User Management::User + +from datetime import datetime +from flask import session + +#from .config import db +from flask import current_app +# TODO: remove dependency from config file +db = current_app.db + + +class User(db.Model): + """ docstring """ + + __tablename__ = 'users' + + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(80)) + email = db.Column(db.String(120), unique=True) + created = db.Column(db.DateTime, default=datetime.utcnow) + last_active = db.Column(db.DateTime, default=datetime.utcnow) + + def __init__(self, username, email): + self.username = username + self.email = email + + def update(self, **kwargs): + if kwargs.get('username'): + self.username = kwargs.get('username') + if kwargs.get('last_active'): + current_app.logger.debug('Updated last_active timestamp %s for %s', + self, kwargs.get('last_active')) + self.last_active = kwargs.get('last_active') + + self.persist() + + # persist current object in the database + def persist(self): + db.session.add(self) + db.session.commit() + + # delete from database + def remove(self): + db.session.delete(self) + db.session.commit() + + @staticmethod + def getCurrentUser(): + if 'email' in session: + user = User.query.filter_by(email=session['email']).first() + user.update(last_active=datetime.utcnow()) + return user + return None + + @staticmethod + def getByName(username): + return User.query.filter_by(username=username).first() + + def to_dict(self): + return { + 'id': self.id, + 'username': self.username, + 'email': self.email, + 'created': self.created.isoformat(), + 'last_active': self.last_active.isoformat() + } + + def __repr__(self): + return '' % (self.username, self.email)