-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeds.py
122 lines (92 loc) · 4.01 KB
/
meds.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
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
from flask import render_template, request, redirect, Blueprint, get_flashed_messages, flash
from db_utils import get_connection
from auth import login_required
meds_bp = Blueprint('meds_bp', __name__)
@meds_bp.route('/add', methods=['GET', 'POST'])
@login_required
def add():
if request.method == 'GET':
messages = get_flashed_messages()
return render_template('add.html', messages=messages)
if request.method == 'POST':
name = str(request.form['name']).title()
active_substance = str(request.form['active_substance']).lower()
price = float(request.form['price'])
dosage_form = request.form['dosage_form']
quantity = int(request.form['quantity'])
capacity = int(request.form['capacity'])
parameters_dosage_form = ('pills', 'mixture', 'drops' 'suspension', 'injection', 'plaster', 'aerosol')
if '' in (name, active_substance, price, quantity, capacity,):
flash('Please try again.')
return redirect('/add')
elif dosage_form not in parameters_dosage_form:
return redirect('/add')
elif price is not float:
price = float(price) # Czy to ma sens?
elif capacity < 0 or quantity < 0 or price < 0:
flash('Wrong data. Please try again.')
return redirect('/add')
else:
return redirect('/add')
conn = get_connection()
c = conn.cursor()
query = 'INSERT INTO "medicines" ("name", "active_substance", "price", "dosage_form", "capacity", "quantity") ' \
'VALUES (?, ?, ?, ?, ?, ?)'
parameters = (name, active_substance, price, dosage_form, capacity, quantity,)
c.execute(query, parameters)
conn.commit()
return redirect('/')
@meds_bp.route('/delete', methods=['GET', 'POST'])
@login_required
def delete():
if request.method == 'GET':
messages = get_flashed_messages()
return render_template('delete.html', messages=messages)
if request.method == 'POST':
id = int(request.form['id'])
conn = get_connection()
c = conn.cursor()
if id == None:
flash('Please try again.')
return redirect('/delete')
query = 'DELETE FROM "medicines" WHERE id = ?'
parameters = (id,)
c.execute(query, parameters)
conn.commit()
if c.rowcount != 1:
flash('Wrong id. Try again.')
return redirect('/delete')
else:
return redirect('/')
@meds_bp.route('/find', methods=['GET', 'POST'])
@login_required
def find():
if request.method == 'GET':
messages = get_flashed_messages()
return render_template('find.html', messages=messages)
if request.method == 'POST':
find_name = request.form['find_name'].title()
find_active_substance = request.form['find_active_substance'].lower()
conn = get_connection()
c = conn.cursor()
query = "SELECT * FROM medicines WHERE name LIKE ? AND active_substance LIKE ?"
find_name = f'%{find_name}%'
find_active_substance = f'%{find_active_substance}%'
parameters = (find_name, find_active_substance)
result = c.execute(query, parameters)
meds = result.fetchall()
context = {'meds': meds}
return render_template('find.html', **context)
@meds_bp.route('/find_in_pharmacy', methods=['GET', 'POST'])
@login_required
def find_in_pharmacy():
if request.method == 'GET':
messages = get_flashed_messages()
return render_template('find_in_pharmacy.html', messages=messages)
if request.method == 'POST':
find_name_pharmacy = request.form['find_name_pharmacy']
if find_name_pharmacy:
return redirect('https://www.aptekagemini.pl/search/st_search/?q=' + find_name_pharmacy)
else:
flash('Try again.')
return redirect('/find_in_pharmacy')