-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
66 lines (56 loc) · 2.25 KB
/
server.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
from flask import Flask, render_template, url_for
import csv
from datetime import datetime
from flask import request, redirect
import os
app = Flask(__name__)
name_storage = {"current_name": ""}
info_storage = {}
session = {}
LANGUAGE_PATHS = {
'en': 'English',
'fr': 'French'
}
def get_template_path(language, template_name):
return os.path.join(LANGUAGE_PATHS[language],template_name)
@app.route("/")
def default_home():
return redirect(url_for('home', lang='en'))
@app.route("/<string:lang>")
def home(lang):
if lang in LANGUAGE_PATHS:
session["current_language"] = lang
return render_template(get_template_path(lang, 'index.html'))
return redirect(url_for('home', lang='en'))
@app.route("/<string:lang>/<string:page_name>")
def html_page(lang,page_name):
if lang in LANGUAGE_PATHS:
session['current_language'] = lang
print(render_template(get_template_path(lang,page_name)))
return render_template(get_template_path(lang,page_name))
return redirect(url_for('home', lang='en'))
def write_to_csv(data):
with open('database.csv', mode='a', newline='') as database:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
language = session.get('current_language','en')
csv_writer = csv.writer(database,delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow([timestamp,data['fullname'],data['email'],data['subject'], data['message'], ])
@app.route('/<lang>/submit_form', methods=['POST', 'GET'])
def submit(lang):
if request.method == 'POST':
try:
data = request.form.to_dict()
write_to_csv(data)
name_storage["current_name"] = request.form.get("fullname")
return redirect(url_for('thankyou', lang=lang))
except:
error_messages = {
'en': "Failed to save to database.",
'fr': "Échec de la sauvegarde dans la base de données."
}
return error_messages.get(lang, error_messages['en'])
return redirect(url_for('home', lang=lang))
@app.route('/<lang>/thankyou.html')
def thankyou(lang):
template_name = 'thankyou.html'
return render_template(get_template_path(lang, template_name), info=name_storage["current_name"])