-
Notifications
You must be signed in to change notification settings - Fork 13
/
views.py
162 lines (140 loc) · 5.71 KB
/
views.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from django.shortcuts import render
from decimal import Decimal
# Create your views here.
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from .models import User, Bus, Book
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from .forms import UserLoginForm, UserRegisterForm
from django.contrib.auth.decorators import login_required
from decimal import Decimal
def home(request):
if request.user.is_authenticated:
return render(request, 'myapp/base.html')
else:
return render(request, 'users/login.html')
def about(request):
return render(request, 'myapp/about.html', {'title': 'About'})
@login_required(login_url='login')
def findbus(request):
context = {}
if request.method == 'POST':
source_r = request.POST.get('source')
dest_r = request.POST.get('destination')
date_r = request.POST.get('date')
bus_list = Bus.objects.filter(source=source_r, dest=dest_r, date=date_r)
if bus_list:
return render(request, 'myapp/list.html', locals())
else:
context["error"] = "Sorry no buses available"
return render(request, 'myapp/findbus.html', context)
else:
return render(request, 'myapp/findbus.html')
@login_required(login_url='login')
def bookings(request):
context = {}
if request.method == 'POST':
id_r = request.POST.get('bus_id')
seats_r = int(request.POST.get('no_seats'))
bus = Bus.objects.get(id=id_r)
if bus:
if bus.rem > int(seats_r):
name_r = bus.bus_name
cost = int(seats_r) * bus.price
source_r = bus.source
dest_r = bus.dest
nos_r = Decimal(bus.nos)
price_r = bus.price
date_r = bus.date
time_r = bus.time
username_r = request.user.username
email_r = request.user.email
userid_r = request.user.id
rem_r = bus.rem - seats_r
Bus.objects.filter(id=id_r).update(rem=rem_r)
book = Book.objects.create(name=username_r, email=email_r, userid=userid_r, bus_name=name_r,
source=source_r, busid=id_r,
dest=dest_r, price=price_r, nos=seats_r, date=date_r, time=time_r,
status='BOOKED')
print('------------book id-----------', book.id)
# book.save()
return render(request, 'myapp/bookings.html', locals())
else:
context["error"] = "Sorry select fewer number of seats"
return render(request, 'myapp/findbus.html', context)
else:
return render(request, 'myapp/findbus.html')
@login_required(login_url='login')
def cancellings(request):
context = {}
if request.method == 'POST':
id_r = request.POST.get('bus_id')
#seats_r = int(request.POST.get('no_seats'))
try:
book = Book.objects.get(id=id_r)
bus = Bus.objects.get(id=book.busid)
rem_r = bus.rem + book.nos
Bus.objects.filter(id=book.busid).update(rem=rem_r)
#nos_r = book.nos - seats_r
Book.objects.filter(id=id_r).update(status='CANCELLED')
Book.objects.filter(id=id_r).update(nos=0)
return redirect(seebookings)
except Book.DoesNotExist:
context["error"] = "Sorry You have not booked that bus"
return render(request, 'myapp/error.html', context)
else:
return render(request, 'myapp/findbus.html')
@login_required(login_url='login')
def seebookings(request,new={}):
context = {}
id_r = request.user.id
book_list = Book.objects.filter(userid=id_r)
if book_list:
return render(request, 'myapp/booklist.html', locals())
else:
context["error"] = "Sorry no buses booked"
return render(request, 'myapp/findbus.html', context)
def signup(request):
context = {}
if request.method == 'POST':
name_r = request.POST.get('name')
email_r = request.POST.get('email')
password_r = request.POST.get('password')
user = User.objects.create_user(name_r, email_r, password_r, )
if user:
login(request, user)
return render(request, 'myapp/thank.html')
else:
context["error"] = "Provide valid credentials"
return render(request, 'users/registers.html', context)
else:
return render(request, 'users/signup.html', context)
def signin(request):
context = {}
if request.method == 'POST':
name_r = request.POST.get('name')
password_r = request.POST.get('password')
user = authenticate(request, username=name_r, password=password_r)
if user:
login(request, user)
# username = request.session['username']
context["user"] = name_r
context["id"] = request.user.id
return render(request, 'myapp/base.html', context)
# return HttpResponseRedirect('success')
else:
context["error"] = "Provide valid credentials"
return render(request, 'users/login.html', context)
else:
context["error"] = "You are not logged in"
return render(request, 'users/login.html', context)
def signout(request):
context = {}
logout(request)
context['error'] = "You have been logged out"
return render(request, 'users/login.html', context)
def success(request):
context = {}
context['user'] = request.user
return render(request, 'myapp/success.html', context)