microblog/app/models.py

111 lines
3.2 KiB
Python
Raw Normal View History

2012-12-16 08:31:21 +00:00
from hashlib import md5
import re
2012-12-16 08:29:49 +00:00
from app import db
2012-12-16 08:35:16 +00:00
from app import app
2013-04-23 05:19:12 +00:00
from config import WHOOSH_ENABLED
2012-12-16 08:29:49 +00:00
import sys
if sys.version_info >= (3, 0):
enable_search = False
else:
enable_search = WHOOSH_ENABLED
if enable_search:
import flask.ext.whooshalchemy as whooshalchemy
2012-12-16 08:29:49 +00:00
followers = db.Table(
'followers',
2012-12-16 08:34:24 +00:00
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
)
2012-12-16 08:29:49 +00:00
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
posts = db.relationship('Post', backref='author', lazy='dynamic')
2012-12-16 08:31:21 +00:00
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime)
followed = db.relationship('User',
secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'),
lazy='dynamic')
2012-12-16 08:32:38 +00:00
2013-02-01 05:48:20 +00:00
@staticmethod
def make_valid_nickname(nickname):
return re.sub('[^a-zA-Z0-9_\.]', '', nickname)
2012-12-16 08:32:38 +00:00
@staticmethod
def make_unique_nickname(nickname):
if User.query.filter_by(nickname=nickname).first() is None:
2012-12-16 08:32:38 +00:00
return nickname
version = 2
while True:
new_nickname = nickname + str(version)
if User.query.filter_by(nickname=new_nickname).first() is None:
2012-12-16 08:32:38 +00:00
break
version += 1
return new_nickname
2012-12-16 08:30:41 +00:00
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
try:
return unicode(self.id) # python 2
except NameError:
return str(self.id) # python 3
2012-12-16 08:30:41 +00:00
2012-12-16 08:31:21 +00:00
def avatar(self, size):
return 'http://www.gravatar.com/avatar/%s?d=mm&s=%d' % \
(md5(self.email.encode('utf-8')).hexdigest(), size)
2012-12-16 08:34:24 +00:00
def follow(self, user):
if not self.is_following(user):
self.followed.append(user)
return self
2012-12-16 08:34:24 +00:00
def unfollow(self, user):
if self.is_following(user):
self.followed.remove(user)
return self
2012-12-16 08:34:24 +00:00
def is_following(self, user):
return self.followed.filter(
followers.c.followed_id == user.id).count() > 0
2012-12-16 08:34:24 +00:00
def followed_posts(self):
return Post.query.join(
followers, (followers.c.followed_id == Post.user_id)).filter(
followers.c.follower_id == self.id).order_by(
Post.timestamp.desc())
def __repr__(self): # pragma: no cover
return '<User %r>' % (self.nickname)
2013-02-20 06:59:54 +00:00
2012-12-16 08:29:49 +00:00
class Post(db.Model):
2012-12-16 08:35:16 +00:00
__searchable__ = ['body']
id = db.Column(db.Integer, primary_key=True)
2012-12-16 08:29:49 +00:00
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
2013-02-20 06:59:54 +00:00
language = db.Column(db.String(5))
def __repr__(self): # pragma: no cover
2012-12-16 08:35:16 +00:00
return '<Post %r>' % (self.body)
if enable_search:
2013-04-23 05:19:12 +00:00
whooshalchemy.whoosh_index(app, Post)