-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
44 lines (40 loc) · 1.21 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
from flask import Flask, render_template, request, redirect
from datetime import datetime
import csv
app = Flask(__name__)
#Homepage
@app.route('/')
def hello_world():
return render_template('index.html')
#other pages
@app.route('/<string:page_name>')
def html_page(page_name):
return render_template(page_name)
#Contact form
@app.route('/submit_form', methods=['POST', 'GET'])
def submit_form():
if request.method == 'POST':
try:
data = request.form.to_dict()
write_to_csv(data)
return redirect('/thankyou.html')
except:
return 'Algo salió mal.'
else:
return 'something went wrong. Try again!'
def write_to_file(data):
with open('database.txt', 'a') as database:
registro = ''
for value in data.values():
registro += value + ';'
database.write(f'\n{registro}{datetime.now()}')
#database.write('{}\n'.format(registro + str(datetime.now())))
database.close()
def write_to_csv(data):
with open('database.csv', 'a', newline='') as database2:
email = data['email']
subject = data['subject']
message = data['message']
date = datetime.now()
csv_writer = csv.writer(database2, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow([email, subject, message, date])