-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
170 lines (132 loc) · 5.65 KB
/
routes.py
File metadata and controls
170 lines (132 loc) · 5.65 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
from flask import render_template, url_for, request, flash, redirect, abort
import forms
from app import app, database, bcrypt
from models import Usuario, Post
from flask_login import login_user, logout_user, current_user, login_required
import secrets
import os
from PIL import Image
def salvar_imagem(imagem):
cod_unique = secrets.token_hex(6)
arquivo_nome, arquivo_extensao = os.path.splitext(imagem.filename)
novo_arquivo = arquivo_nome + cod_unique + arquivo_extensao
caminho_arquivo = os.path.join(app.root_path, 'static/img_profiles', novo_arquivo)
tamanho_img = (200, 200)
imagem_reduzida = Image.open(imagem)
imagem_reduzida.thumbnail(tamanho_img)
imagem_reduzida.save(caminho_arquivo)
return novo_arquivo
def atualizar_skills(form):
lista_skills = []
for campo in form:
if 'check_skill' in campo.name:
if campo.data:
lista_skills.append(campo.label.text)
return ';'.join(lista_skills)
@app.route('/')
def home():
posts = Post.query.order_by(Post.id.desc())
return render_template('home.html', posts=posts)
@app.route('/contato')
def contato():
return render_template('contato.html')
@app.route('/users')
@login_required
def usuarios():
lista_usuarios = Usuario.query.all()
return render_template('users.html', lista_usuarios=lista_usuarios)
@app.route('/login', methods=['GET', 'POST'])
def login():
form_login = forms.FormLogin()
form_criar_conta = forms.FormCriarConta()
if form_login.validate_on_submit() and 'btn_submit_login' in request.form:
user = Usuario.query.filter_by(email=form_login.email.data).first()
if user and bcrypt.check_password_hash(user.senha, form_login.senha.data):
login_user(user, remember=form_login.check_lembrar_dados.data)
flash(f'Seja bem vindo {user.username}', 'alert-success')
param_next = request.args.get('next')
if param_next:
return redirect(param_next)
else:
return redirect(url_for('home'))
else:
flash(f'E-mail ou senha incorreta', 'alert-danger')
if form_criar_conta.validate_on_submit() and 'btn_submit_criar_conta' in request.form:
cripto_hash = bcrypt.generate_password_hash(form_criar_conta.senha.data)
user, mail, pwd = form_criar_conta.username.data, form_criar_conta.email.data, cripto_hash
usuario = Usuario(username=user, email=mail, senha=pwd)
database.session.add(usuario)
database.session.commit()
flash(f'Conta criada com sucesso para {form_criar_conta.email.data}', 'alert-success')
return redirect(url_for('home'))
return render_template('login.html', form_login=form_login, form_criar_conta=form_criar_conta)
@app.route('/sair')
@login_required
def sair():
logout_user()
flash('Logout realizado com sucesso!', 'alert-success')
return redirect(url_for('home'))
@app.route('/perfil')
@login_required
def perfil():
profile_image = url_for('static', filename=f'img_profiles/{current_user.user_photo}')
return render_template('perfil.html', profile_image=profile_image)
@app.route('/perfil/editar', methods=['GET', 'POST'])
@login_required
def perfil_editar():
form = forms.FormEditarPerfil()
if form.validate_on_submit():
current_user.username = form.username.data
current_user.email = form.email.data
if form.foto_perfil.data:
nova_imagem = salvar_imagem(form.foto_perfil.data)
current_user.user_photo = nova_imagem
current_user.skills = atualizar_skills(form)
database.session.commit()
flash('Perfil atualizado com sucesso!', 'alert-success')
return redirect(url_for('perfil'))
if request.method == 'GET':
form.username.data = current_user.username
form.email.data = current_user.email
profile_image = url_for('static', filename=f'img_profiles/{current_user.user_photo}')
return render_template('perfil_editar.html', profile_image=profile_image, form=form)
@app.route('/post/new', methods=['GET', 'POST'])
@login_required
def post_new():
form_new_post = forms.FormCriarPost()
if form_new_post.validate_on_submit():
post = Post(post_title=form_new_post.post_title.data, post_text=form_new_post.post_text.data,
author=current_user)
database.session.add(post)
database.session.commit()
flash('Postagem criada com sucesso!', 'alert-success')
return redirect(url_for('home'))
return render_template('post-new.html', form_new_post=form_new_post)
@app.route('/post/<post_id>', methods=['GET', 'POST'])
@login_required
def post_show(post_id):
post = Post.query.get(post_id)
if current_user == post.author:
form_new_post = forms.FormCriarPost()
if request.method == 'GET':
form_new_post.post_title.data = post.post_title
form_new_post.post_text.data = post.post_text
if form_new_post.validate_on_submit():
post.post_title = form_new_post.post_title.data
post.post_text = form_new_post.post_text.data
database.session.commit()
flash('Edição realizada com sucesso!', 'alert-success')
else:
form = None
return render_template('post.html', post=post, form_new_post=form_new_post)
@app.route('/post/<post_id>/excluir', methods=['GET', 'POST'])
@login_required
def excluir_post(post_id):
post = Post.query.get(post_id)
if current_user == post.author:
database.session.delete(post)
database.session.commit()
flash('Post Excluído com Sucesso', 'alert-danger')
return redirect(url_for('home'))
else:
abort(403)