-
Notifications
You must be signed in to change notification settings - Fork 30
/
views.py
341 lines (280 loc) · 13.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import datetime,csv
from django.shortcuts import render
from .models import record
from .models import student as studentrec
from .forms import DateForm, MonthForm
from datetime import datetime
from django.shortcuts import redirect
from datetime import date, timedelta, datetime
from django.http import HttpResponse
from .functions import async_bg_jobs_thread
from django.db.models import Count, Q
from django.db.models.functions import ExtractWeekDay
from django.db.models import F
def library(request):
# Get the current date and yesterday's date
today = date.today()
yesterday = datetime.now().date() - timedelta(days=2)
# Update entries with status 'IN' and date before or equal to yesterday
#record.objects.filter(status='IN', date__lte=yesterday, exittime__isnull=True).update(exittime='23:30:00', status='OUT')
now = datetime.now()
#threshold_time = datetime.now() - timedelta(hours=16)
#students_to_update = record.objects.filter(entrytime__lt=threshold_time, exittime__isnull=True)
#students_to_update.update(exittime=F('entrytime'), status='OUT')
current_time = now.strftime("%H:%M:%S")
#update entries with status 'IN' and time before 16 hours from now to current time
#record.objects.filter(status='IN', entrytime__lte=(now - timedelta(minutes=1)).strftime("%H:%M:%S")).update(exittime=current_time, status='OUT')
#record.objects.filter(status='IN', entrytime__gte=(now - timedelta(hours=16)).strftime("%H:%M:%S")).update(exittime=current_time, status='OUT')
# Count the number of students with 'IN' status for today
count = record.objects.filter(status='IN').count()
# Count total visits today
total_visits_today = record.objects.filter(date=today).count()
# Separate counts for different shifts
morning = record.objects.filter(entrytime__gte='00:00:00', entrytime__lte='08:00:00', date=today).count()
general = record.objects.filter(entrytime__gte='08:00:00', entrytime__lte='16:30:00', date=today).count()
night = record.objects.filter(entrytime__gte='16:30:00', entrytime__lte='23:59:59', date=today).count()
# Count the number of students with 'IN' status for today
count = record.objects.filter(status='IN').count()
# Count total visits today
total_visits_today = record.objects.filter(date=today).count()
# Separate counts for different shifts
morning = record.objects.filter(entrytime__gte='00:00:00', entrytime__lte='08:00:00', date=today).count()
general = record.objects.filter(entrytime__gte='08:00:00', entrytime__lte='16:30:00', date=today).count()
night = record.objects.filter(entrytime__gte='16:30:00', entrytime__lte='23:59:59', date=today).count()
return render(request, 'library.html', {
'count': count,
'total_visits_today': total_visits_today,
'morning_count': morning,
'general_count': general,
'night_count': night
})
# view to display records of today
def records_today(request):
today = date.today()
students = record.objects.filter(date=today).order_by('-entrytime')
return render(request, 'records_today.html', {'students': students})
#show all records
def allrecords(request):
students = record.objects.all().order_by('-date','-entrytime')
form = DateForm(request.POST)
return render(request, 'allrecords.html', {'students': students, 'form': form})
# view to display records of a particular date
def records(request):
if request.method == 'POST':
date = request.POST.get('date')
students = record.objects.filter(date=date).order_by('-date','-entrytime')
return render(request, 'records.html', {'students': students, 'form': DateForm(),'date': date})
return render(request, 'records_form.html', {'form': DateForm()})
# view to display records of a particular month
def recordsbymonth(request):
if request.method == 'POST':
month = request.POST.get('month')
monthname = datetime.strptime(month, '%Y-%m-%d').strftime('%B')
year = int(month[0:4])
month = int(month[5:7])
students = record.objects.filter(date__month=month,date__year=year).order_by('-date','-entrytime')
return render(request, 'records_month.html', {'students': students, 'form': MonthForm,'month': month, 'monthname': monthname, 'year': year})
return render(request, 'records_month_form.html', {'form': MonthForm()})
# view to display records by status
def recordsbystatus(request):
students = record.objects.filter(status='IN').order_by('-date','-entrytime')
return render(request, 'current.html', {'students': students})
# view to export records to csv
def export(request):
response = HttpResponse(content_type='text/csv')
writer = csv.writer(response)
writer.writerow(['Roll No', 'Entry Time', 'Exit Time', 'Date'])
for row in record.objects.all().values_list('rollno', 'entrytime', 'exittime', 'date').order_by('-date','-entrytime'):
row = list(row) # Convert the values_list tuple to a list
if row[1]: # Check if entry time is not None
row[1] = row[1].strftime('%H:%M:%S') # Format entry time without milliseconds
writer.writerow(row)
response['Content-Disposition'] = 'attachment; filename="allrecords.csv"'
return response
# view to export records of a particular date to csv
def exportbydate(request):
if request.method == 'POST':
date = request.POST.get('date')
response = HttpResponse(content_type='text/csv')
writer = csv.writer(response)
writer.writerow(['Roll No', 'Entry Time', 'Exit Time', 'Date'])
for row in record.objects.filter(date=date).values_list('rollno', 'entrytime', 'exittime','date').order_by('-date','-entrytime'):
writer.writerow(row)
response['Content-Disposition'] = 'attachment; filename="{0}.csv"'.format(date)
return response
else:
return render(request, 'records_formcsv.html', {'form': DateForm()})
# view to export records of a particular month to csv
def exportbymonth(request):
if request.method == 'POST':
date = request.POST.get('month')
year=date[0:4]
month = int(date[5:7])
monthname = datetime.strptime(date, '%Y-%m-%d').strftime('%B')
yearname=str(year)
response = HttpResponse(content_type='text/csv')
writer = csv.writer(response)
writer.writerow(['Roll No', 'Entry Time', 'Exit Time', 'Date'])
for row in record.objects.filter(date__month=month,date__year=year).values_list('rollno', 'entrytime', 'exittime', 'date').order_by('-date','-entrytime'):
writer.writerow(row)
response['Content-Disposition'] = 'attachment; filename="{0}.csv"'.format(monthname+' '+yearname)
return response
else:
return render(request, 'records_month_formcsv.html', {'form': MonthForm()})
def shifts(request):
import datetime as dt
morning_start = dt.time(0, 0, 0)
morning_end = dt.time(8, 0, 0)
general_start = dt.time(8, 0, 0)
general_end = dt.time(16, 30, 0)
night_start = dt.time(16, 30, 0)
night_end = dt.time(23, 59, 59)
if request.method == 'GET':
#count number of morning shifts with atleast one entry
# all_entries = record.objects.all().order_by('-entrytime')
# morning_shift_count=0
# general_shift_count=0
# night_shift_count=0
# prev_date_m = None
# prev_date_g = None
# prev_date_n = None
# for entry in all_entries:
# if entry.entrytime >= morning_start and entry.entrytime <= morning_end:
# if entry.date != prev_date_m:
# morning_shift_count += 1
# prev_date_m = entry.date
# if entry.entrytime >= general_start and entry.entrytime <= general_end:
# if entry.date != prev_date_g:
# general_shift_count += 1
# prev_date_g = entry.date
# if entry.entrytime >= night_start and entry.entrytime <= night_end:
# if entry.date != prev_date_n:
# night_shift_count += 1
# prev_date_n = entry.date
# if(morning_shift_count==0):
# morning_shift_count=1
# if(general_shift_count==0):
# general_shift_count=1
# if(night_shift_count==0):
# night_shift_count=1
morning = record.objects.filter(exittime__gte='00:00:00', exittime__lte='08:00:00').count()
general = record.objects.filter(exittime__gte='08:00:00', exittime__lte='16:30:00').count()
night = record.objects.filter(exittime__gte='16:30:00', exittime__lte='23:59:59').count()
# mx=morning/morning_shift_count
# gx=general/general_shift_count
# nx=night/night_shift_count
return render(request, 'shifts.html', {'morning_count': morning, 'general_count': general, 'night_count': night,
#'morning': mx, 'general': gx, 'night': nx,
'form': DateForm()})
else:
date = request.POST.get('date')
# all_entries = record.objects.filter(date=date).order_by('-entrytime')
# morning_shift_count=0
# general_shift_count=0
# night_shift_count=0
# prev_date_m = None
# prev_date_g = None
# prev_date_n = None
# for entry in all_entries:
# if entry.date != prev_date_m:
# if entry.entrytime >= datetime.time(0, 0, 0) and entry.entrytime <= datetime.time(8, 0, 0):
# morning_shift_count += 1
# prev_date_m = entry.date
# if entry.date != prev_date_g:
# if entry.entrytime >= datetime.time(8, 0, 0) and entry.entrytime <= datetime.time(16, 30, 0):
# general_shift_count += 1
# prev_date_g = entry.date
# if entry.date != prev_date_n:
# if entry.entrytime >= datetime.time(16, 30, 0) and entry.entrytime <= datetime.time(23, 59, 59):
# night_shift_count += 1
# prev_date_n = entry.date
# if(morning_shift_count==0):
# morning_shift_count=1
# if(general_shift_count==0):
# general_shift_count=1
# if(night_shift_count==0):
# night_shift_count=1
morning = record.objects.filter(exittime__gte='00:00:00', exittime__lte='08:00:00', date=date).count()
general = record.objects.filter(exittime__gte='08:00:00', exittime__lte='16:30:00', date=date).count()
night = record.objects.filter(exittime__gte='16:30:00', exittime__lte='23:59:59', date=date).count()
# mx=morning/morning_shift_count
# gx=general/general_shift_count
# nx=night/night_shift_count
return render(request, 'shifts.html', {'morning_count': morning, 'general_count': general, 'night_count': night,
# 'morning': mx, 'general': gx, 'night': nx,
'date': date,'form': DateForm()})
def statistics(request):
monday=0;
tuesday=0;
wednesday=0;
thursday=0;
friday=0;
saturday=0;
sunday=0;
monday_entries=0;
tuesday_entries=0;
wednesday_entries=0;
thursday_entries=0;
friday_entries=0;
saturday_entries=0;
sunday_entries=0;
# Query the database for all entry records
all_entries = record.objects.all().order_by('-date')
# Iterate through the entries and update the dictionaries
prev_date = None
for entry in all_entries:
if entry.date != prev_date:
if entry.date.strftime("%A")=="Monday":
monday+=1
elif entry.date.strftime("%A")=="Tuesday":
tuesday+=1
elif entry.date.strftime("%A")=="Wednesday":
wednesday+=1
elif entry.date.strftime("%A")=="Thursday":
thursday+=1
elif entry.date.strftime("%A")=="Friday":
friday+=1
elif entry.date.strftime("%A")=="Saturday":
saturday+=1
elif entry.date.strftime("%A")=="Sunday":
sunday+=1
prev_date = entry.date
if entry.status=="OUT":
if entry.date.strftime("%A")=="Monday":
monday_entries+=1
elif entry.date.strftime("%A")=="Tuesday":
tuesday_entries+=1
elif entry.date.strftime("%A")=="Wednesday":
wednesday_entries+=1
elif entry.date.strftime("%A")=="Thursday":
thursday_entries+=1
elif entry.date.strftime("%A")=="Friday":
friday_entries+=1
elif entry.date.strftime("%A")=="Saturday":
saturday_entries+=1
elif entry.date.strftime("%A")=="Sunday":
sunday_entries+=1
if(monday==0):
monday=1
if(tuesday==0):
tuesday=1
if(wednesday==0):
wednesday=1
if(thursday==0):
thursday=1
if(friday==0):
friday=1
if(saturday==0):
saturday=1
if(sunday==0):
sunday=1
#return render(request, "statistics.html", {'monday':monday,'tuesday':2,'wednesday':3,'thursday':4,'friday':5,'saturday':6,'sunday':7})
#return render(request, "statistics.html", {'monday':monday,'tuesday':tuesday,'wednesday':wednesday,'thursday':thursday,'friday':friday,'saturday':saturday,'sunday':sunday,'monday_entries':monday_entries,'tuesday_entries':tuesday_entries,'wednesday_entries':wednesday_entries,'thursday_entries':thursday_entries,'friday_entries':friday_entries,'saturday_entries':saturday_entries,'sunday_entries':sunday_entries})
return render(request, "statistics.html", {'monday':monday_entries/monday,
'tuesday':tuesday_entries/tuesday,
'wednesday':wednesday_entries/wednesday,
'thursday':thursday_entries/thursday,
'friday':friday_entries/friday,
'saturday':saturday_entries/saturday,
'sunday':sunday_entries/sunday,
})