-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
144 lines (106 loc) · 3.91 KB
/
views.py
File metadata and controls
144 lines (106 loc) · 3.91 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
from typing import Optional
from flask import Blueprint, render_template, redirect, url_for, flash, abort, request
from flask_login import login_required, current_user
from forms.utils import get_all_errors_from_form
from forms import RegistrationForm, AuthorizationForm, ProfileSettingsForm
from services.account_services import *
from models import User
from errors.service_errors import AccountServiceError
login_manager.login_view = "views.authorization"
view_blueprint = Blueprint('views', __name__)
@view_blueprint.route('/')
def main_page():
return render_template("main-page.html", user=current_user)
@view_blueprint.route('/policy')
def policy():
return render_template("policy-page.html")
@view_blueprint.route("/registration", methods=('GET', 'POST'))
def registration():
form = RegistrationForm()
if form.validate_on_submit():
try:
register_user(
official_username=form.official_username.data,
email=form.email.data,
password=form.password.data
)
if current_user.is_anonymous:
authorize_user(form.official_username.data, form.password.data)
return redirect(url_for("views.main_page"))
except AccountServiceError as error:
flash(str(error), category="additional errors")
return render_template(
"form-pages/registration-page.html",
form=form,
error_by_field_id=get_all_errors_from_form(form)
)
@view_blueprint.route("/login", methods=('GET', 'POST'))
def authorization():
form = AuthorizationForm()
if form.validate_on_submit():
try:
authorize_user(form.login.data, form.password.data)
return redirect(url_for("views.profile"))
except AccountServiceError as error:
flash(str(error), category="additional errors")
return render_template(
"form-pages/authorization-page.html",
form=form,
error_by_field_id=get_all_errors_from_form(form)
)
@view_blueprint.route("/logout")
@login_required
def logout():
flash(
f"You are logged out of your account \"{current_user.official_username}\"",
category="alert_messages"
)
logout_user()
return redirect(url_for("views.main_page"))
@view_blueprint.route("/users/<string:official_username>")
def user_catalog(official_username):
found_user = User.query.filter_by(official_username=official_username).first_or_404()
return render_template(
"profile-page.html",
current_account_belongs_to_current_user=current_user.get_id() == found_user.get_id(),
user=found_user
)
@view_blueprint.route("/profile")
@login_required
def profile():
return render_template(
"profile-page.html",
current_account_belongs_to_current_user=True,
user=current_user
)
@view_blueprint.route("/settings/profile", methods=("GET", "POST"))
@login_required
def change_profile():
form = ProfileSettingsForm()
if form.validate_on_submit():
update_profile_data(
public_username=form.public_username.data,
user_description=form.user_description.data,
blob_user_icon=form.user_icon.data.read()
)
return redirect(url_for("views.profile"))
return render_template(
"form-pages/change-profile-page.html",
form=form,
error_by_field_id=get_all_errors_from_form(form),
user=current_user
)
@view_blueprint.app_errorhandler(404)
def not_found_handler(error: Optional[Exception]):
return render_template("error-pages/not-found-page.html"), 404
@view_blueprint.before_request
def before_request():
if (
request.method == "GET" and
not current_user.is_anonymous and
not current_user.is_email_confirmed
):
flash(
"Your email has not been verified. Confirm it in account settings",
category="alert_messages"
)