Commit 0b96a0752f247e7f3ba336886f07d5c973b05203

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.
  
1[submodule "swtstore/classes/models/um"]
2 path = swtstore/classes/models/um
3 url = git@git.pantoto.org:user-management/user-management.git
  
1# -*- coding utf-8 -*-
2
3
4# -*- coding: utf-8 -*-
5# User Management::Group
6
7from datetime import datetime
8
9from .config import db
10
11class Group(db.Model):
12 """
13 docstring
14 """
15 __tablename__ = 'groups'
16
17 id = db.Column(db.Integer, primary_key=True)
18 name = db.Column(db.String(80), unique=True)
19 created = db.Column(db.DateTime, default=datetime.utcnow)
20 created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
21
22 creator = db.relationship('User', backref='groups')
23
24 def __init__(self, name, creator):
25 self.name = name
26 self.creator = creator
27
28 # persist object to database
29 def persist(self):
30 db.session.add(self)
31 db.session.commit()
32
33 # delete from database
34 def remove(self):
35 db.session.delete(self)
36 db.session.commit()
37
38 def __repr__(self):
39 return '<Group %r>' % self.name
  
1# -*- coding utf-8 -*-
2# User Management::Membership
3
4from datetime import datetime
5
6from .config import db
7
8class Membership(db.Model):
9 """ docstring """
10
11 __tablename__ = 'memberships'
12
13 id = db.Column(db.Integer, primary_key=True)
14 uid = db.Column(db.Integer, db.ForeignKey('users.id'))
15 gid = db.Column(db.Integer, db.ForeignKey('groups.id'))
16 created = db.Column(db.DateTime, default=datetime.utcnow)
17
18 user = db.relationship('User', backref='memberships')
19 group = db.relationship('Group', backref='memberships')
20
21 def __init__(self, user, group):
22 self.user = user
23 self.group = group
24
25 # persist in the database
26 def persist(self):
27 db.session.add(self)
28 db.session.commit()
29
30 # delete from a database
31 def remove(self):
32 db.session.delete(self)
33 db.session.commit()
34
35 def __repr__(self):
36 return'<Membership %d>' % self.uid
  
1# -*- coding utf-8 -*-
2# User Management::User
3
4from datetime import datetime
5from flask import session
6
7#from .config import db
8from flask import current_app
9# TODO: remove dependency from config file
10db = current_app.db
11
12
13class User(db.Model):
14 """ docstring """
15
16 __tablename__ = 'users'
17
18 id = db.Column(db.Integer, primary_key=True)
19 username = db.Column(db.String(80))
20 email = db.Column(db.String(120), unique=True)
21 created = db.Column(db.DateTime, default=datetime.utcnow)
22 last_active = db.Column(db.DateTime, default=datetime.utcnow)
23
24 def __init__(self, username, email):
25 self.username = username
26 self.email = email
27
28 def update(self, **kwargs):
29 if kwargs.get('username'):
30 self.username = kwargs.get('username')
31 if kwargs.get('last_active'):
32 current_app.logger.debug('Updated last_active timestamp %s for %s',
33 self, kwargs.get('last_active'))
34 self.last_active = kwargs.get('last_active')
35
36 self.persist()
37
38 # persist current object in the database
39 def persist(self):
40 db.session.add(self)
41 db.session.commit()
42
43 # delete from database
44 def remove(self):
45 db.session.delete(self)
46 db.session.commit()
47
48 @staticmethod
49 def getCurrentUser():
50 if 'email' in session:
51 user = User.query.filter_by(email=session['email']).first()
52 user.update(last_active=datetime.utcnow())
53 return user
54 return None
55
56 @staticmethod
57 def getByName(username):
58 return User.query.filter_by(username=username).first()
59
60 def to_dict(self):
61 return {
62 'id': self.id,
63 'username': self.username,
64 'email': self.email,
65 'created': self.created.isoformat(),
66 'last_active': self.last_active.isoformat()
67 }
68
69 def __repr__(self):
70 return '<User:: %r %r>' % (self.username, self.email)