-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathowner.py
150 lines (135 loc) · 5.05 KB
/
owner.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import redirect, render_template, session, request, Blueprint, url_for, flash
import mysql.connector
from db import db, cursor
from auth import login_required
import re
owner = Blueprint('owner', __name__)
@owner.route('/owner')
@login_required
def display():
update_query = '''
UPDATE owner o
JOIN bookingslot b on o.OwnerID = b.BSlotID
SET o.Name=Null, o.contact = Null, o.address = Null
WHERE b.TimeFrom is Null and b.TimeTo is Null
'''
try:
cursor.execute(update_query)
db.commit()
except mysql.connector.Error as e:
db.rollback()
flash('Error updating data to null')
cursor.execute('SELECT * FROM owner')
db.commit()
data = cursor.fetchall()
Null_query = '''
SELECT o.OwnerID
FROM Owner o
JOIN bookingslot b on o.OwnerID = b.BSlotID
WHERE b.TimeFrom is Null and b.TimeTo is Null
'''
cursor.execute(Null_query)
NullID = cursor.fetchall()
return render_template('view/owner.html', data=data, NullID=NullID)
def ValidContact(contact):
pattern = r"^[6-9]\d*$"
return re.match(pattern, contact)
def ValidName(Name):
pattern = r"^[a-zA-Z][a-zA-Z\s'-]*$"
return re.match(pattern, Name)
@owner.route('/owner/add', methods = ['GET','POST'])
@login_required
def add_data():
# SharedID = session.get('SharedID')
if request.method == 'POST':
print("In try")
OwnerID = session.get('VehicleID')
print("OwnerID: ", OwnerID)
Name = request.form['Name']
contact = request.form['contact']
address = request.form['address']
S_No = session.get('SNo')
print('S_No in owner: ', SNo)
print(request.form)
if not ValidName(Name):
flash('Name should start with an alphabet','error')
return redirect(url_for('owner.add_data'))
if not ValidContact(contact):
print("not valid")
flash('Mobile number not valid', 'error')
return redirect(url_for('owner.add_data'))
if len(contact) > 12:
flash('Contact connot be more than 12 numbers')
return redirect(url_for('owner.add_data'))
maxSNo = 'SELECT MAX(SNo) FROM user'
cursor.execute(maxSNo)
db.commit()
result = cursor.fetchone()
maxS_No = result[0]
incrementedSNo = maxS_No + 1
update_query = '''
UPDATE owner
SET Name=%s, contact=%s, address=%s, SNo=%s
WHERE OwnerID=%s
'''
cursor.execute(update_query, (Name, contact, address, incrementedSNo, OwnerID))
db.commit()
#flash('Data added successfully')
# return redirect(url_for('payment.add_data'))
return redirect(url_for('payment.add_data', VehicleID=OwnerID))
cursor.execute('SELECT OwnerID FROM owner WHERE Name is Null and contact is Null and address is Null')
availableSlots = cursor.fetchone()
return render_template('add/owner.html', availableSlots=availableSlots)
@owner.route('/payment/add/<int:VehicleID>')
@login_required
def Payment(OwnerID):
return render_template('add/payment.html', VehicleID=OwnerID)
@owner.route('/owner/edit/<int:OwnerID>', methods=['GET','POST'])
@login_required
def edit_data(OwnerID):
if request.method == 'POST':
Name = request.form['Name']
contact = request.form['contact']
address = request.form['address']
try:
update_query = '''UPDATE owner
SET Name=%s, contact=%s, address=%s
WHERE OwnerID=%s'''
cursor.execute(update_query, (Name, contact, address, OwnerID,))
db.commit()
#flash('Data updated successfully')
return redirect(url_for('owner.display'))
except mysql.connector.Error as e:
db.rollback()
#flash('Error updating data')
fetch_query = 'SELECT OwnerID, name, contact, address FROM owner WHERE OwnerID=%s'
cursor.execute(fetch_query, (OwnerID,))
db.commit()
data = cursor.fetchone()
if data is None:
flash('No data found')
return redirect(url_for('owner.display'))
return render_template('edit/owner.html',data=data)
@owner.route('/owner/delete/<int:OwnerID>', methods = ['GET', 'POST'])
@login_required
def delete_data(OwnerID):
if request.method == 'POST':
try:
delete_query = '''DELETE FROM owner WHERE OwnerID=%s'''
cursor.execute(delete_query, (OwnerID,))
db.commit()
#flash('Data deleted successfully')
return redirect(url_for('owner.display'))
except mysql.connector.Error as e:
db.rollback()
print(e)
print('In the except')
flash('Error deleting data')
fetch_query = 'SELECT OwnerId, name, contact, address FROM owner WHERE OwnerID=%s'
cursor.execute(fetch_query, (OwnerID,))
db.commit()
data = cursor.fetchone()
if data is None:
flash('Data not found')
return redirect(url_for('owner.display'))
return render_template('delete/owner.html', data=data)