-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (94 loc) · 4.11 KB
/
main.py
File metadata and controls
112 lines (94 loc) · 4.11 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
import smtplib
import sqlite3
from flask import Flask, session, render_template, request, redirect, get_flashed_messages, flash
from fpdf import FPDF, XPos, YPos
from os.path import basename
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from functions import results_compare_items, results_reasoning, login_required, results_reasoning_pdf, \
results_compare_items_pdf
from registration_login import auth_bp
from solving_tests import solving_tests_bp
from api_gia import api_gia_bp
from login_mail import user, password
app = Flask(__name__)
app.secret_key = 'secret-key-1103'
app.register_blueprint(auth_bp)
app.register_blueprint(solving_tests_bp)
app.register_blueprint(api_gia_bp)
@app.route('/')
def welcome():
if session:
user = session['username']
else:
user = None
return render_template('welcome.html', user=user)
@app.route('/results', methods=['GET', 'POST'], endpoint='results')
@login_required
def results():
if request.method == 'GET':
messages = get_flashed_messages()
answers_reasoning = results_reasoning()
answers_compare_items = results_compare_items()
return render_template('results.html', answers_reasoning=answers_reasoning,
answers_compare_items=answers_compare_items, messages=messages)
if request.method == 'POST':
answers_reasoning = results_reasoning_pdf()
answers_compare_items = results_compare_items_pdf()
pdf = FPDF()
pdf.add_page()
pdf.add_font('DejaVu', fname='DejaVuSansCondensed.ttf')
pdf.set_font('DejaVu', size=10)
line_height = pdf.font_size * 2.5
col_width = pdf.epw / 5
pdf.cell(0, 0, 'TEST ROZUMOWANIE', align='C', new_x=XPos.RIGHT, new_y=YPos.TOP)
pdf.ln(line_height)
heading = ['Nr testu', 'Twierdzenie', 'Pytanie', 'Poprawna odp.', 'Twoja odp.']
for item in heading:
pdf.multi_cell(col_width, line_height, item,
new_x=XPos.RIGHT, new_y=YPos.TOP, max_line_height=pdf.font_size)
pdf.ln(line_height)
for item in answers_reasoning:
for el in item:
el = str(el)
pdf.multi_cell(col_width, line_height, el, border=1,
new_x=XPos.RIGHT, new_y=YPos.TOP, max_line_height=pdf.font_size)
pdf.ln(line_height)
pdf.ln(line_height)
pdf.ln(line_height)
pdf.cell(0, 0, 'TEST PORÓWNYWANIE ELEMENTÓW', align='C', new_x=XPos.RIGHT, new_y=YPos.TOP)
pdf.ln(line_height)
heading = ['Nr testu', 'Litery 1', 'Litery 2', 'Poprawna odp.', 'Twoja odp.']
for item in heading:
pdf.multi_cell(col_width, line_height, item,
new_x=XPos.RIGHT, new_y=YPos.TOP, max_line_height=pdf.font_size)
pdf.ln(line_height)
for item in answers_compare_items:
for el in item:
el = str(el)
pdf.multi_cell(col_width, line_height, el, border=1,
new_x=XPos.RIGHT, new_y=YPos.TOP, max_line_height=pdf.font_size)
pdf.ln(line_height)
pdf.output('pdf_results.pdf')
msg = MIMEMultipart()
msg['Subject'] = 'Wyniki testów'
msg['From'] = 'Wyniki testów'
msg['To'] = session['email_address']
content = '''Witaj
W załączniku przesyłamy wyniki testów.
Miłego dnia!'''
body = MIMEText(content, 'plain')
msg.attach(body)
compare_items_file = 'pdf_results.pdf'
with open(compare_items_file, mode='rb') as f:
part = MIMEApplication(f.read(), Name=basename(compare_items_file))
part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(compare_items_file))
msg.attach(part)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(user, password)
smtp.send_message(msg)
flash('Mail został wysłany')
return redirect('/results')
if __name__ == '__main__':
app.run(debug=True)