-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
58 lines (43 loc) · 2.12 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
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from app import db, login
from flask_login import UserMixin
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic', cascade = 'all, delete, delete-orphan')
liked = db.relationship('Like', backref='user', lazy='dynamic', cascade = 'all, delete, delete-orphan')
def like_post(self, post):
if not self.has_liked_post(post):
like = Like(user_id=self.id, post_id=post.id)
db.session.add(like)
def unlike_post(self, post):
if self.has_liked_post(post):
Like.query.filter_by(user_id=self.id, post_id=post.id).delete()
def has_liked_post(self, post):
return Like.query.filter(Like.user_id == self.id, Like.post_id == post.id).count() > 0
def delete_post(self, post):
db.session.delete(post)
def __repr__(self):
return '<User {}>'.format(self.username)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
city = db.Column(db.String(140))
country = db.Column(db.String(140))
description = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
likes = db.relationship('Like', backref='post', lazy='dynamic', cascade = 'all, delete, delete-orphan')
def __repr__(self):
return '<Post {}>'.format(self.description)
class Like(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
# db.create_all()