realms-wiki/realms/models.py

124 lines
3.1 KiB
Python
Raw Normal View History

2013-10-04 05:57:19 +03:00
import bcrypt
2013-12-03 01:50:19 +02:00
from sqlalchemy import Column, Integer, String, Time
2013-10-05 00:42:45 +03:00
from flask import session, flash
from flask.ext.login import login_user, logout_user
2013-11-08 20:20:40 +02:00
from realms.lib.services import db
2014-01-14 01:07:13 +02:00
from realms.lib.util import gravatar_url, to_dict
2014-01-14 01:07:13 +02:00
class ModelMixin(object):
def __getitem__(self, k):
return self.__getattribute__(k)
@classmethod
def create(cls, **kwargs):
obj = cls(**kwargs)
db.session.add(obj)
db.session.commit()
return obj
2013-10-02 04:50:48 +03:00
2013-10-03 17:58:07 +03:00
class CurrentUser():
id = None
def __init__(self, id):
self.id = id
if id:
2014-01-14 01:07:13 +02:00
session['user'] = to_dict(User.query.filter_by(id=id).first())
2013-10-03 17:58:07 +03:00
def get_id(self):
return self.id
def is_active(self):
2013-10-05 00:42:45 +03:00
return True if self.id else False
2013-10-03 17:58:07 +03:00
def is_anonymous(self):
return False if self.id else True
def is_authenticated(self):
return True if self.id else False
2013-10-05 08:04:38 +03:00
@staticmethod
def get(key):
try:
return session['user'][key]
except KeyError:
return None
2013-10-03 17:58:07 +03:00
2014-01-14 01:07:13 +02:00
class Site(ModelMixin, db.Model):
2013-12-03 01:50:19 +02:00
__tablename__ = 'sites'
id = Column(Integer, primary_key=True)
name = Column(String(100))
pages = Column(Integer)
views = Column(Integer)
2014-01-14 01:07:13 +02:00
founder = Column(Integer)
created_at = Column(Time)
updated_at = Column(Time)
2013-12-03 01:50:19 +02:00
2014-01-14 01:07:13 +02:00
@classmethod
def get_by_name(cls, name):
return Site.query.filter_by(name=name).first()
2013-12-03 01:50:19 +02:00
2014-01-14 01:07:13 +02:00
class User(db.Model, ModelMixin):
2013-12-03 01:50:19 +02:00
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(100))
email = Column(String(255))
2014-01-14 01:07:13 +02:00
avatar = Column(String(255))
2013-12-03 01:50:19 +02:00
password = Column(String(255))
2014-01-14 01:07:13 +02:00
created_at = Column(Time)
updated_at = Column(Time)
2013-10-02 04:50:48 +03:00
2014-01-14 01:07:13 +02:00
@classmethod
def get_by_email(cls, email):
return User.query.filter_by(email=email).first()
2013-10-03 17:58:07 +03:00
2014-01-14 01:07:13 +02:00
@classmethod
def get_by_username(cls, username):
return User.query.filter_by(username=username).first()
2013-10-02 04:50:48 +03:00
def login(self, login, password):
2013-10-03 17:58:07 +03:00
pass
@classmethod
def auth(cls, username, password):
u = User()
data = u.get_by_email(username)
if not data:
return False
2013-10-04 05:57:19 +03:00
if bcrypt.checkpw(password, data['password']):
cls.login(data['id'])
2013-10-03 17:58:07 +03:00
return True
else:
2013-10-05 00:42:45 +03:00
return False
@classmethod
def register(cls, username, email, password):
user = User()
email = email.lower()
if user.get_by_email(email):
flash('Email is already taken')
return False
2014-01-14 01:07:13 +02:00
2013-10-05 00:42:45 +03:00
if user.get_by_username(username):
flash('Username is already taken')
return False
# Create user and login
u = User.create(email=email,
username=username,
password=bcrypt.hashpw(password, bcrypt.gensalt(10)),
avatar=gravatar_url(email))
User.login(u.id)
2013-10-05 00:42:45 +03:00
@classmethod
2013-11-08 20:20:40 +02:00
def login(cls, id):
2013-12-04 00:28:16 +02:00
login_user(CurrentUser(id), remember=True)
2013-10-05 00:42:45 +03:00
@classmethod
def logout(cls):
logout_user()
session.pop('user', None)