-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (159 loc) · 6.71 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from flask import Flask,render_template,request,session,redirect
from flask_sqlalchemy import SQLAlchemy
import json
import datetime
import os
from werkzeug.utils import secure_filename
'''
TODO:Add about text functionality
'''
with open("config.json","r") as c:
params = json.load(c)["params"]
local_server =params["local_server"]
app = Flask(__name__)
if local_server:
app.config['SQLALCHEMY_DATABASE_URI'] = params["local_uri"]
else:
app.config["SQLALCHEMY_DATABASE_URI"] = params["prod_uri"]
app.secret_key = "my-secret-key"
app.config['UPLOADER_FOLDER'] = params['upload_location']
db = SQLAlchemy(app)
class Contactmessage(db.Model):
sno = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(120),unique=False,nullable=False)
phoneNumber = db.Column(db.String(12),unique=False,nullable=False)
message =db.Column(db.String(120),unique=False,nullable=False)
date = db.Column(db.String(12),unique=False,nullable=False)
email = db.Column(db.String(20),nullable=False)
class Posts(db.Model):
sno = db.Column(db.Integer,primary_key=True)
title = db.Column(db.String(120),unique=False,nullable=False)
content = db.Column(db.String(12),unique=False,nullable=False)
slug =db.Column(db.String(120),unique=False,nullable=False)
date = db.Column(db.String(12),unique=False,nullable=False)
img_file =db.Column(db.String(120),unique=False,nullable=False)
upvote = db.Column(db.Integer,unique=False,nullable=False)
class Comments(db.Model):
sno = db.Column(db.Integer,primary_key=True)
user = db.Column(db.String(120),unique=False,nullable=False)
content = db.Column(db.String(120),unique=False,nullable=False)
date = db.Column(db.String(120),unique=False,nullable=False)
post = db.Column(db.String(120),unique=False,nullable=False)
class Users(db.Model):
sno = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String,unique=False,nullable=False)
email = db.Column(db.String,unique=False,nullable=False)
pasword = db.Column(db.String,unique=False,nullable=False)
@app.route("/")
def home():
posts = Posts.query.filter_by().all()[0:params["number_of_posts"]]
return render_template("index.html",params=params,posts=posts)
@app.route("/about")
def about():
return render_template("about.html",params=params)
@app.route("/contact",methods = ["GET","POSt"])
def contact():
if(request.method=="POST"):
'''add entry to db'''
name = request.form.get('name')
email = request.form.get('email')
msg = request.form.get('msg')
print(msg)
phNumber = request.form.get('phNumber')
date = str("23/03/6969")
entry = Contactmessage(name=name,message=msg,phoneNumber=phNumber,email=email,date=date)
db.session.add(entry)
db.session.commit()
return render_template("contact.html",params=params)
@app.route("/post/<string:post_slug>",methods=["GET"])
def post_route(post_slug):
post = Posts.query.filter_by(slug=post_slug).first()
comment = Comments.query.filter_by(post=post_slug)
return render_template("blog.html",params=params,post=post,comment=comment)
@app.route("/blogs")
def blog():
return render_template("blog.html",params=params)
@app.route("/dashboard",methods=["GET","POST"])
def dashboard():
if "user" in session and session["user"]==params["admin_user"]:
posts= Posts.query.all()
return render_template("dashboard.html",params=params,posts=posts)
if request.method=="POST":
username = request.form.get("username")
password = request.form.get("password")
user = Users.query.filter_by(username=username)[0]
try:
session["user"] = username
posts = Posts.query.all()
return render_template("dashboard.html",params=params,posts=posts)
except Exception as e:
print(e)
return e
else:
return render_template("login.html",params=params)
@app.route("/edit/<string:sno>",methods=["GET","POST"])
def edit(sno):
if "user" in session and session["user"]==params["admin_user"]:
if request.method == "POST":
box_title = request.form.get('title')
tline= request.form.get('tline')
slug= request.form.get('slug')
content = request.form.get('content')
img_file = request.form.get('img')
if sno=="0":
post= Posts(title=box_title,slug=slug,content=content,date="1/2/11",img_file=img_file)
db.session.add(post)
db.session.commit()
print("post added succesfully")
return redirect('/dashboard')
else:
post= Posts.query.filter_by(sno=sno).first()
post.title = box_title
post.slug = slug
post.content = content
post.date= "1/23/56"
post.img_file= img_file
db.session.commit()
return redirect('/edit/'+sno)
post= Posts.query.filter_by(sno=sno).first()
return render_template("edit.html",params=params,post = post,sno=sno)
@app.route("/uploader" ,methods = ["GET", "POST"])
def uploader():
if "user" in session and session["user"]==params["admin_user"]:
if request.method == "POST":
f = request.files['file1']
f.save(os.path.join(app.config['UPLOADER_FOLDER'],secure_filename(f.filename)))
return "Uploaded successfully"
@app.route("/logout")
def logout():
session.pop('user')
return redirect('/dashboard')
@app.route("/upvote/<string:post_slug>")
def upvote(post_slug):
post = Posts.query.filter_by(slug=post_slug).first()
post.upvote+=1
db.session.commit()
return redirect("/post/"+str(post_slug))
@app.route("/comment/<string:post_slug>",methods = ["GET","POST"])
def saveComments(post_slug):
if "user" in session and session["user"]==params["admin_user"]:
if request.method =="POST":
user = params["admin_user"]
content = request.form.get("content")
comment = Comments(user=user,content=content,date="000",post=post_slug)
db.session.add(comment)
db.session.commit()
return redirect(f"/post/{post_slug}")
return "login ker yaar"
@app.route("/register",methods=['GET','POST'])
def register():
if request.method =="POST":
email = request.form.get('email')
password = request.form.get('password')
username = request.form.get('username')
user = Users(email=email,password=password,username=username)
db.session.add(user)
db.session.commit()
redirect('/dashboard')
return render_template('login.html',params=params)
app.run(debug=True)