myprecious/views.py (view raw)
1from myprecious import app, login_manager
2from flask import request, redirect, render_template
3from flask_login import login_user, logout_user, current_user
4import myprecious.constants as c
5from myprecious.utils import handle_response, parse_remember
6from myprecious.auth import handle_register, handle_login, get_logged_user
7from myprecious.files import handle_upload
8from myprecious.encoding import obj_decode
9if app.debug:
10 from myprecious.games_api_test import search_game
11else:
12 from myprecious.games_api import search_game
13
14def render(template, **context):
15 return render_template(template, user=current_user, **context)
16
17@login_manager.user_loader
18def load_user(user_id):
19 return get_logged_user(user_id)
20
21@app.route('/')
22def route_index():
23 return render("index.html")
24
25@app.route('/login', methods=['GET', 'POST'])
26def route_login():
27 if current_user.is_authenticated:
28 return redirect('/')
29 if request.method == "GET":
30 return render("login.html")
31
32 form = request.form
33 remember = parse_remember(form)
34
35 error, user = handle_login(request.form)
36 if error is None:
37 login_user(user, remember=remember)
38 return redirect("/")
39 last_user = user.username if user else None
40 return render("login.html", last_user=last_user, error=error)
41
42
43@app.route('/register', methods=['GET', 'POST'])
44def route_register():
45 if current_user.is_authenticated:
46 return redirect('/')
47 if request.method == "GET":
48 return render("register.html")
49 error = handle_register(request.form)
50 if error is None:
51 return render("done.html", text="Your registration request has been taken into account and will likely be processed in a few days.")
52 return render("register.html", error=error)
53
54@app.route('/logout')
55def route_logout():
56 logout_user()
57 return redirect("/")
58
59@app.route('/search', methods=['GET', 'POST'])
60def route_search():
61 if not current_user.is_authenticated:
62 return redirect('/login')
63 if request.method == 'GET':
64 return render("search.html")
65 query = request.form["query"]
66 search_response = search_game(query)
67 games = handle_response(search_response)
68 return render("search.html", games=games, query=query)
69
70@app.route('/upload', methods=['GET', 'POST'])
71def route_upload():
72 if not current_user.is_authenticated:
73 return redirect('/login')
74 if request.method == 'GET':
75 info = request.args.get("info")
76 if info is None:
77 return render("upload.html", game=c.NO_GAME)
78 return render("upload.html", game=obj_decode(info))
79
80 error = handle_upload(request, current_user.id)
81 if error is None:
82 return render("done.html", text="Your save file was uploaded correctly.")
83 return render("input.html", error=error)
84
85
86@app.route('/admin', methods=['GET', 'POST'])
87def route_admin():
88 if not current_user.is_authenticated:
89 return redirect('/')
90
91 if current_user.id != 1:
92 return redirect('/')
93
94 if request.method == "GET":
95 return render("admin.html")
96
97 return render("admin.html")
98
99@app.route('/about')
100def route_about():
101 return render("about.html")