-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (119 loc) · 4.12 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
import os
from datetime import datetime as dt
from flask import Flask, flash, redirect, render_template, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_required, login_user, current_user, logout_user
from werkzeug.security import check_password_hash
from dotenv import load_dotenv
from form import ContactForm, PortfolioForm, AdminLogin
from mailer import Mailer
load_dotenv()
app = Flask(__name__)
login_manager = LoginManager()
app.secret_key = os.getenv('APP_SECRET')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager.init_app(app)
class Work(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False, unique=True)
description = db.Column(db.String, nullable=False)
url = db.Column(db.String, nullable=False)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String, nullable=False, unique=True)
password = db.Column(db.String, nullable=False)
@login_manager.user_loader
def load_user(uid):
return db.session.query(User).get(uid)
@app.context_processor
def inject_now():
return {
'now': dt.utcnow()
}
@app.route('/')
def home():
return render_template('index.html')
@app.route('/skills')
def skills():
return render_template('skills.html')
@app.route('/portfolio')
def portfolio():
portfolio = db.session.query(Work).all()
return render_template('portfolio.html', works=portfolio)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form = ContactForm()
mailer = Mailer()
if form.validate_on_submit():
mailer.send_mail(
s_subj='From Zoárd\'s Site',
s_name=form.name.data,
s_email=form.email.data,
s_msg=form.message.data
)
return render_template('contact.html', form=form, submitted=True)
return render_template('contact.html', form=form, submitted=False)
@app.route('/privacy-policy')
def privacy_policy():
return render_template('privacy-policy.html')
@app.route('/imprint')
def imprint():
return render_template('imprint.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
form = AdminLogin()
if current_user.is_authenticated:
return redirect(url_for('portfolio'))
if form.validate_on_submit():
user = db.session.query(User).filter_by(email=form.email.data).first()
if user and check_password_hash(pwhash=user.password, password=form.password.data):
login_user(user)
return redirect(url_for('portfolio'))
flash('Invalid credentials, please try again.')
return render_template('login.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('home'))
@app.route('/add', methods=['GET', 'POST'])
@login_required
def add():
form = PortfolioForm()
if form.validate_on_submit():
new_work = Work(
title = form.title.data,
description = form.description.data,
url = form.url.data
)
db.session.add(new_work)
db.session.commit()
return redirect(url_for('portfolio'))
return render_template('portfolio_item.html', form=form)
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit(id:int):
work = db.session.query(Work).get(id)
form = PortfolioForm(obj=work)
if form.validate_on_submit():
work.title = form.title.data
work.description = form.description.data
work.url = form.url.data
db.session.commit()
return redirect(url_for('portfolio'))
return render_template('portfolio_item.html', form=form)
@app.route('/remove/<int:id>')
@login_required
def remove(id:int):
work = db.session.query(Work).get(id)
db.session.delete(work)
db.session.commit()
return redirect(url_for('portfolio'))
if __name__ == '__main__':
db.create_all()
app.run(debug=True)