-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
executable file
·122 lines (97 loc) · 3.52 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
from sqlalchemy import Column, Integer, String
from database import Base
from sqlalchemy import *
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
import json
import random
USERNAME_LEN = 50
SENTENCE_LEN = 300
ANNOTATE_LEN = 1000
class User(UserMixin, Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String(USERNAME_LEN), unique=True)
email = Column(String(120), unique=True)
password_hash = Column(String(128))
favorites = Column(String(1000))
profile_pic = Column(String(2500))
def __init__(self, name=None, email=None):
self.name = name
self.email = email
L = []
for i in range(2500):
L.append(str(random.randint(0, 3)))
self.profile_pic = "".join(L)
self.favorites = "[]"
def __str__(self):
return '<User %r>' % (self.name)
def __repr__(self):
return {'id':self.id,
'name':self.name,
'email':self.email}
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)
def getFavorites(self):
if self.favorites == None:
return []
return json.loads(self.favorites)
def add_favorite(self, article_id):
L = json.loads(self.favorites)
L.append(article_id)
self.favorites = json.dumps(L)
def remove_favorite(self, article_id):
try:
L = json.loads(self.favorites)
print(L)
L.remove(article_id)
self.favorites = json.dumps(L)
print(json.loads(self. favorites))
except ValueError:
return
class Annotation(Base):
__tablename__ = "annotations_table"
id = Column(Integer, primary_key=True)
user = Column(String(USERNAME_LEN))
rating = Column(BigInteger)
ratingCount = Column(BigInteger)
prevSentence = Column(String(SENTENCE_LEN))
sentence = Column(String(SENTENCE_LEN))
nextSentence = Column(String(SENTENCE_LEN))
annotation = Column(String(ANNOTATE_LEN))
usersRated = Column(String(1000))
def __init__(self, sentence, annotation, user, prevSentence = None, nextSentence = None):
self.sentence = sentence
self.annotation = annotation
self.prevSentence = prevSentence
self.nextSentence = nextSentence
self.user = user
self.rating = 0
self.ratingCount = 0
self.usersRated = "[]"
def __str__(self):
return f'<Annotation {self.id} {self.annotation} in {self.sentence} by {self.user} :-: {self.getRating()}>'
def __repr__(self):
return {'id':self.id, 'sentence':self.sentence, 'annotation':self.annotation, 'user':self.user.__repr__(), 'rating':self.getRating()}
def getRating(self):
if not self.ratingCount:
return "Unrated"
return self.rating / self.ratingCount
def addRating(self, rating):
self.rating += rating
self.ratingCount += 1
def addUser(self, user):
L = json.loads(self.usersRated)
L += [user]
self.usersRated = json.dumps(L)
def checkuser(self, user):
L = json.loads(self.usersRated)
return user in L
class Article(Base):
__tablename__ = "articles"
id = Column(Integer, primary_key=True)
content = Column(String(5000))
def __init__(self, text):
self.content = text