-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
156 lines (126 loc) · 4.14 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
from flask.ext.sqlalchemy import SQLAlchemy
import os
from flask import Flask, render_template, request, session, flash, redirect, url_for, g
from functools import wraps
from hashlib import sha256
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
from models import Post, Link, Article
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('login'))
return wrap
@app.route('/', methods=["GET"])
def landing_page():
posts = Post.query.all()
links = Link.query.all()
return render_template('visitor.html', posts=posts, links=links)
@app.route('/admin', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if sha256(request.form['username'].encode('utf-8')).hexdigest() \
!= app.config['USERNAME'] \
or sha256(request.form['password'].encode('utf-8')).hexdigest() \
!= app.config['PASSWORD']:
error = 'Invalid Credentials. Please try again.'
else:
session['logged_in'] = True
return redirect(url_for('backend'))
return render_template('login.html', error=error)
@app.route('/visitor_articles')
def posts():
articles = Article.query.all()
return render_template('article.html', articles=articles)
@app.route('/articles')
@login_required
def articles_route():
articles = Article.query.all()
return render_template('adding_articles.html', articles=articles)
@app.route('/backend')
@login_required
def backend():
posts = Post.query.all()
return render_template('adding_posts.html', posts=posts)
@app.route('/links')
@login_required
def links_route():
links = Link.query.all()
return render_template("adding_links.html", links=links)
@app.route('/add', methods=['POST'])
@login_required
def add():
title = request.form['title']
post = request.form['post']
if not title or not post:
flash("All fields are required. Please try again")
return redirect(url_for('backend'))
else:
new_post = Post(
title=title,
post=post
)
db.session.add(new_post)
db.session.commit()
flash('New entry was successfully posted!')
return redirect(url_for('backend'))
@app.route('/add_link', methods=['POST'])
@login_required
def add_link():
title = request.form['title']
link = request.form['link']
if not title or not link:
flash("All fields are required. Please try again")
return redirect(url_for('add_link'))
else:
new_link = Link(
title=title,
link=link
)
db.session.add(new_link)
db.session.commit()
flash('New entry was successfully posted!')
return redirect(url_for('links_route'))
@app.route('/add_article', methods=['POST'])
@login_required
def add_article():
title = request.form['title']
article = request.form['article']
if not title or not article:
flash("All fields are required. Please try again")
return redirect(url_for('add_article'))
else:
new_article = Article(
title=title,
article=article
)
db.session.add(new_article)
db.session.commit()
flash('New entry was successfully posted!')
return redirect(url_for('articles_route'))
@app.route('/delete/<string:title>/')
@login_required
def delete_entry(title):
entry = Post.query.filter_by(title=title).first()
if not entry:
entry = Link.query.filter_by(title=title).first()
if not entry:
entry = Article.query.filter_by(title=title).first()
db.session.delete(entry)
db.session.commit()
flash('The entry was deleted.')
return redirect(url_for('backend'))
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('login'))
if __name__ == '__main__':
app.run()