From 0b96a0752f247e7f3ba336886f07d5c973b05203 Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Tue, 3 Jun 2014 12:34:10 +0530 Subject: [PATCH] Remove submodule user-management Removed the user management code as a sub-module and made it part of swtstore. The relevant files are under classes/models directory. Two reasons for doing this: - user management code has started to become swtstore specific. - setting up of swtstore becomes easier. --- .gitmodules | 3 -- swtstore/classes/models/group.py | 40 +++++++++++++++++++ swtstore/classes/models/membership.py | 37 +++++++++++++++++ swtstore/classes/models/um | 1 - swtstore/classes/models/user.py | 70 +++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 4 deletions(-) delete mode 100644 .gitmodules create mode 100644 swtstore/classes/models/group.py create mode 100644 swtstore/classes/models/membership.py delete mode 160000 swtstore/classes/models/um create mode 100644 swtstore/classes/models/user.py diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index ca5bc55..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "swtstore/classes/models/um"] - path = swtstore/classes/models/um - url = git@git.pantoto.org:user-management/user-management.git diff --git a/swtstore/classes/models/group.py b/swtstore/classes/models/group.py new file mode 100644 index 0000000..f78a899 --- /dev/null +++ b/swtstore/classes/models/group.py @@ -0,0 +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 + diff --git a/swtstore/classes/models/membership.py b/swtstore/classes/models/membership.py new file mode 100644 index 0000000..e62019a --- /dev/null +++ b/swtstore/classes/models/membership.py @@ -0,0 +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 + diff --git a/swtstore/classes/models/um b/swtstore/classes/models/um deleted file mode 160000 index 688a5be..0000000 --- a/swtstore/classes/models/um +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 688a5beac835a26efe7c06efe6b9127f83adeb03 diff --git a/swtstore/classes/models/user.py b/swtstore/classes/models/user.py new file mode 100644 index 0000000..891db7a --- /dev/null +++ b/swtstore/classes/models/user.py @@ -0,0 +1,70 @@ +# -*- 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) -- 1.7.10.4