-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
160 lines (141 loc) · 5.53 KB
/
app.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from flask import Flask, render_template, request, redirect, make_response
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
linking_table = db.Table("linking_table",
db.Column("post_id", db.Integer, db.ForeignKey("post.id")),
db.Column("tag_id", db.Integer, db.ForeignKey("tag.id"))
)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
desc = db.Column(db.String(500), nullable=False)
tags = db.relationship("Tag", secondary=linking_table, backref="Posts")
def __repr__(self) -> str:
return f"{self.id} - {self.title}"
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
value = db.Column(db.Integer, nullable=True)
def __repr__(self) -> str:
return f"{self.id} - {self.name}"
def getRaterTags(post: Post) -> list:
raterTags = []
for tag in post.tags:
if tag.value != None:
raterTags.append(tag)
return raterTags
def getMaxRating(post: Post) -> Tag:
ratings = []
raterTags = getRaterTags(post)
totalRatings = 0
for raterTag in raterTags:
totalRatings += raterTag.value
if totalRatings % 2 != 0:
median = (totalRatings + 1)/2
else:
median = int(totalRatings/2)
cumulativeF = 0
for raterTag in raterTags:
cumulativeF += raterTag.value
if cumulativeF >= median:
return raterTag
@app.route("/")
def about():
return render_template("about.html")
@app.route("/post", methods=["GET", "POST"])
def post():
if request.method== "POST":
blankPostError = False
title = request.form["title"]
desc = request.form["desc"]
tags = request.form["tags"].split()
# raterTags = request.form["raterTags"].split()
raterTags = ["easy", "medium", "hard"]
post = Post(title=title, desc=desc)
if title == "" or desc == "" or tags == "" or raterTags == "":
blankPostError = True
else:
for tag_name in tags + raterTags:
if tag_name in raterTags:
tag = Tag(name=tag_name, value=0)
else:
tag = Tag.query.filter_by(name=tag_name).first()
if tag is None:
tag = Tag(name=tag_name, value=None)
post.tags.append(tag)
db.session.add(tag)
db.session.add(post)
db.session.commit()
return render_template("post.html", blankPostError=blankPostError)
return render_template("post.html")
@app.route("/discover", methods=["GET", "POST"])
def discover():
posts = Post.query.all()
if request.method == "POST":
tags = request.form["tags"].split()
for tag_name in tags:
tag = Tag.query.filter_by(name=tag_name).first()
if tag == None:
return render_template("discover.html", posts=[])
search_results = tag.Posts
posts = list(set(search_results).intersection(posts))
if request.form["raterTag"] != "":
i = 0
while i < len(posts):
if request.form["raterTag"] != getMaxRating(posts[i]).name:
posts.pop(i)
i -= 1
i += 1
try:
ratedPostsIds = eval(request.cookies.get("ratedPostsIds"))
except:
ratedPostsIds = None
if request.form.get("ratedRadioBtn") == "unratedRadio":
if ratedPostsIds is not None:
i = 0
while i < len(posts):
if posts[i].id in ratedPostsIds:
posts.pop(i)
i -= 1
i += 1
elif request.form.get("ratedRadioBtn") == "ratedRadio":
if ratedPostsIds == None:
posts = []
else:
i = 0
while i < len(posts):
if posts[i].id not in ratedPostsIds:
posts.pop(i)
i -= 1
i += 1
return render_template("discover.html", posts=posts, getMaxRating=getMaxRating)
@app.route("/discover/<int:id>", methods=["GET", "POST"])
def postDesc(id):
post = Post.query.filter_by(id=id).first()
totalRatings = 0
raterTags = getRaterTags(post)
for tag in raterTags:
totalRatings += tag.value
totalVal = totalRatings
if totalVal == 0: totalVal += 1
if request.method == "POST":
for raterTag in raterTags:
if request.form.get("ratingOptions") == str(raterTag.id):
try:
ratedPostsIds = eval(request.cookies.get("ratedPostsIds"))
except:
ratedPostsIds = []
if post.id not in ratedPostsIds:
raterTag.value += 1
db.session.commit()
resp = make_response(redirect(f"/discover/{post.id}"))
ratedPostsIds.append(post.id)
resp.set_cookie("ratedPostsIds", str(ratedPostsIds))
return resp
return redirect(f"/discover/{post.id}")
return render_template("postDescription.html", post=post, raterTags=raterTags, totalVal=totalVal)
if __name__ == "__main__":
app.run(debug=True)