-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
193 lines (149 loc) · 6.21 KB
/
Copy pathmain.py
File metadata and controls
193 lines (149 loc) · 6.21 KB
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
from flask import Flask, redirect,render_template, request,session
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
import os
from werkzeug.utils import secure_filename
import math
import json
from datetime import datetime
with open('config.json','r') as f:
params = json.load(f)["params"]
local_server = True
app = Flask(__name__)
app.secret_key = 'super-secret-key'
app.config['UPLOAD_FOLDER'] = params['upload_location']
app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = '465',
MAIL_USE_SSL = True,
MAIL_USERNAME = params['gmail-user'],
MAIL_PASSWORD= params['gmail-pass']
)
mail = Mail(app)
if(local_server):
app.config['SQLALCHEMY_DATABASE_URI'] = params['local_url']
else:
app.config['SQLALCHEMY_DATABASE_URI'] = params['prod_url']
db = SQLAlchemy(app)
class Contact(db.Model):
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
phonenumber = db.Column(db.String(20), nullable=False)
message = db.Column(db.String(12), nullable=False)
date = db.Column(db.String(120), nullable=True)
email = db.Column(db.String(12), nullable=False)
class Posts(db.Model):
sno = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
content = db.Column(db.String(50000), nullable=False)
tagline = db.Column(db.String(20), nullable=False)
date = db.Column(db.String(12), nullable=True)
slug = db.Column(db.String(12), nullable=False)
img_file = db.Column(db.String(12), nullable=False)
@app.route("/index ")
def home():
posts = Posts.query.filter_by().all()
last = math.ceil(len(posts)/int(params['no_of_posts']))
page = request.args.get('page')
if (not str(page).isnumeric()):
page = 1
page = int(page)
posts = posts[(page-1)*int(params['no_of_posts']):(page-1)*int(params['no_of_posts'])+ int(params['no_of_posts'])]
if page==1:
prev = "#"
next = "/?page="+ str(page+1)
elif page==last:
prev = "/?page="+ str(page-1)
next = "#"
else:
prev = "/?page="+ str(page-1)
next = "/?page="+ str(page+1)
return render_template('index.html', params=params, posts=posts, prev=prev, next=next)
#posts = Posts.query.filter_by().all()
#[0:params['no_post']]
@app.route("/dash",methods=['GET','POST'])
def dashboard():
if "user" in session and session['user'] == params['admin_username']:
posts = Posts.query.all()
return render_template('dashboard.html',params=params,posts=posts)
if request.method == 'POST':
username = request.form.get("uname")
userpass = request.form.get("password")
if (username == params['admin_username'] and userpass == params['admin_password']):
session['user']=username
posts = Posts.query.all()
return render_template('dashboard.html',params = params,posts=posts)
return render_template('login.html',params=params)
@app.route("/about")
def about():
return render_template('about.html',params = params)
@app.route("/upload",methods=['GET','POST'])
def upload():
if "user" in session and session['user'] == params['admin_username']:
if request.method=='POST':
f = request.files['file1']
f.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename) ))
return render_template('upload.html',params=params)
@app.route('/logout')
def logout():
session.pop('user')
return redirect('/dash')
@app.route("/delete/<string:sno>",methods = ['GET','POST'])
def delete(sno):
if 'user' in session and session['user'] == params['admin_username']:
post = Posts.query.filter_by(sno=sno).first()
db.session.delete(post)
db.session.commit()
return redirect("/dash")
@app.route("/post/<string:post_slug>",methods=['GET'])
def post_route(post_slug):
post = Posts.query.filter_by(slug=post_slug).first()
return render_template('post.html',params = params,post = post)
@app.route("/edit/<string:sno>",methods = ['GET','POST'])
def edit(sno):
#print(request.form.get('title'))
if "user" in session and session['user'] == params['admin_username']:
if (request.method == 'POST'):
title = request.form.get('title')
content = request.form.get('content')
tagline = request.form.get('tagline')
date = datetime.now()
slug = request.form.get('slug')
img_file = request.form.get('img_file')
if sno =='0':#add to the database if serial number is 0
post = Posts(title = title,content = content,tagline=tagline,date=date,slug = slug,img_file=img_file)
db.session.add(post)
db.session.commit()
else:#this condition run if other value except 0
post = Posts.query.filter_by(sno=sno).first()
post.title = title
post.slug = slug
post.content = content
post.tagline = tagline
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,sno=sno,post=post)
@app.route("/post")
def post():
post = Posts.query.filter_by().all()#[0:params['no_post']]
return render_template('ps.html',params=params,post=post)
@app.route("/contact",methods = ['GET','POST'])
def contact():
if(request.method=='POST'):
'''add to the database'''
name = request.form.get('name')
phone = request.form.get('phonenumber')
message = request.form.get('message')
email = request.form.get('email')
entry = Contact(name=name,phonenumber = phone,message = message,date = datetime.now(),email= email)
db.session.add(entry)
db.session.commit()
mail.send_message('New message from ' + name,
sender=email,
recipients = [params['gmail-user']],
body = message + "\n" + phone
)
return render_template('contact.html',params = params)
app.run(debug=True)