-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
130 lines (97 loc) · 3.94 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import datetime, re
from app import db, login_manager, bcrypt
def slugify(s):
"""Helper function which uses a regular expression to generate a string that can be used as URL."""
return re.sub('[^\w]+', '-', s).lower()
entry_tags = db.Table('entry_tags',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('entry_id', db.Integer, db.ForeignKey('entry.id'))
)
@login_manager.user_loader
def _user_loader(user_id):
return User.query.get(int(user_id))
class Entry(db.Model):
"""Model of blog entry. It can be saved as a draft or published. After deleting it stays in the database.
It does have a title, body, time of creation and publication.
It can be tagged."""
STATUS_PUBLIC = 0
STATUS_DRAFT = 1
STATUS_DELETED = 2
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
slug = db.Column(db.String(100), unique=True)
body = db.Column(db.Text)
status = db.Column(db.SmallInteger, default=STATUS_PUBLIC)
created_timestamp = db.Column(db.DateTime, default=datetime.datetime.now)
modified_timestamp = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
tags = db.relationship('Tag', secondary=entry_tags, lazy='dynamic', backref=db.backref('entries', lazy='dynamic'))
def __init__(self, *args, **kwargs):
super(Entry, self).__init__(*args, **kwargs) # Call parent constructor.
self.generate_slug()
def generate_slug(self):
self.slug = ''
if self.title:
self.slug = slugify(self.title)
def __repr__(self):
return '<Entry: {}>'.format(self.title)
@property
def tag_list(self):
return ', '.join(tag.name for tag in self.tags)
@property
def tease(self):
return self.body[:100]
class Tag(db.Model):
"""Model of a tag with which the entries can be connected."""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64))
slug = db.Column(db.String(64), unique=True)
def __init__(self, *args, **kwargs):
super(Tag, self).__init__(*args, **kwargs)
self.slug = slugify(self.name)
def __repr__(self):
return '<Tag {}>'.format(self.name)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(64), unique=True)
password_hash = db.Column(db.String(255))
name = db.Column(db.String(64))
slug = db.Column(db.String(64), unique=True)
active = db.Column(db.Boolean, default=True)
admin = db.Column(db.Boolean, default=False)
created_timestamp = db.Column(db.DateTime, default=datetime.datetime.now)
entries = db.relationship('Entry', backref='author', lazy='dynamic')
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs)
self.generate_slug()
def generate_slug(self):
if self.name:
self.slug = slugify(self.name)
# Flask-Login interface.
def get_id(self):
return str(self.id)
def is_authenticated(self):
return True
def is_active(self):
return self.active
def is_anonymous(self):
return False
def is_admin(self):
return self.admin
@staticmethod
def make_password(plaintext):
return bcrypt.generate_password_hash(plaintext).decode('utf-8')
def check_password(self, raw_password):
return bcrypt.check_password_hash(self.password_hash, raw_password)
@classmethod
def create(cls, email, password, **kwargs):
return cls(
email=email,
password_hash=cls.make_password(password),
**kwargs)
@staticmethod
def authenticate(email, password):
user = User.query.filter(User.email == email).first()
if user and user.check_password(password):
return user
return False