-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
270 lines (198 loc) · 10.4 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
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import get_object_or_404, redirect, render
from django.contrib.auth.decorators import login_required
from .forms import LoginForm, ParticipantForm, EntryForm
from django.template.loader import render_to_string
from .models import Participant,QrCodeId, Entry
from PIL import Image, ImageOps, ImageFont, ImageDraw
import qrcode
from django.http import HttpResponse, JsonResponse
import os
import shutil
# Create your views here.
@login_required(login_url='/login')
def index_view(request):
if not request.user.is_authenticated:
return redirect('login')
participants = Participant.objects.all()
total = participants.count
attended=participants.filter(attended=True).count
notAttended=participants.filter(attended=False).count
inside = participants.filter(inside=True).count
return render(request, 'tracking_app/index.html', {
'totalNr': total,
'inside': inside,
'attended': attended,
'notAttended':notAttended,
})
return render(request, 'tracking_app/index.html')
def login_view(request):
form = LoginForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
user = authenticate(username=form.cleaned_data["username"],
password=form.cleaned_data["password"])
if user is not None:
login(request, user)
return redirect('index')
else:
form.add_error(None, "Invalid username or password")
return render(request, 'tracking_app/login.html', {
'form': form
})
def logout_view(request):
logout(request)
return redirect('login')
@login_required(redirect_field_name='login',login_url='login')
def participant_info_view(request,id):
try:
participant = Participant.objects.get(pk=id)
path='/QR_Codes/%s_%s_%s.png' % (participant.first_name, participant.last_name, participant.participant_id)
if not os.path.isfile(path):
participant_id=participant.participant_id
page_width = 2480
page_height = 3508
qr_border = 5
qr_width = 500 - qr_border
qr_height = 500 - qr_border
page = Image.new('RGB', (page_width, page_height), (255, 255, 255))
fontBold=ImageFont.truetype('Tracking/management/commands/Swansea-q3pd.ttf',size=125)
fontRegular=ImageFont.truetype('Tracking/management/commands/Swansea-q3pd.ttf',size=100)
qr_img=qrcode.make(participant_id)
qr_img = qr_img.resize((qr_width, qr_height))
qr_img = ImageOps.expand(qr_img, border=qr_border, fill='black')
template = Image.open('Tracking/management/commands/template.png')
template = template.resize((page_width, page_height))
page.paste(template,(0,0))
name=participant.first_name+" "+participant.last_name
page.paste(qr_img,(370,1000))
draw = ImageDraw.Draw(page)
draw.text(((page_width/2 - fontBold.getlength(name))/2,550),name,'black',fontBold)
sponsors=["AstraZeneca","Novartis"]
if participant.institution in sponsors:
draw.text(((page_width/2 - fontRegular.getlength(participant.institution))/2,750),participant.institution,'black',fontRegular)
else:
draw.text(((page_width/2 - fontRegular.getlength(participant.title))/2,750),participant.title,'black',fontRegular)
page.save('Tracking/static/QR_Codes/%s_%s_%s.png' % (participant.first_name, participant.last_name,participant.participant_id))
except Participant.DoesNotExist:
return HttpResponse("404 Participant with id '%s' does not exist "%id)
context={
'participant':participant,
'badge_url': '/QR_Codes/%s_%s_%s.png' % (participant.first_name, participant.last_name, participant.participant_id)
}
return render(request, 'tracking_app/participant_info.html' , context)
@login_required(redirect_field_name='login', login_url='/login')
def new_participant_view(request):
form = ParticipantForm(request.POST or None)
context = {'form': form}
if request.method == "POST":
if form.is_valid():
attendee = form.save()
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
institution = form.cleaned_data['institution']
email=form.cleaned_data['email']
title=form.cleaned_data['title']
phone=form.cleaned_data['phone']
city=form.cleaned_data['city']
country=form.cleaned_data['country']
scfhs_number=form.cleaned_data['scfhs_number']
#Id Generation
qrCode=QrCodeId.objects.create()
qrCode.is_used=True
qrCode.save()
participant_id=qrCode.id
page_width = 2480
page_height = 3508
qr_border = 5
qr_width = 500 - qr_border
qr_height = 500 - qr_border
page = Image.new('RGB', (page_width, page_height), (255, 255, 255))
fontBold=ImageFont.truetype('Tracking/management/commands/Swansea-q3pd.ttf',size=125)
fontRegular=ImageFont.truetype('Tracking/management/commands/Swansea-q3pd.ttf',size=100)
qr_img=qrcode.make(participant_id)
qr_img = qr_img.resize((qr_width, qr_height))
qr_img = ImageOps.expand(qr_img, border=qr_border, fill='black')
template = Image.open('Tracking/management/commands/badge.png')
template = template.resize((page_width, page_height))
page.paste(template,(0,0))
name=first_name+" "+last_name
page.paste(qr_img,(370,900))
draw = ImageDraw.Draw(page)
draw.text(((page_width/2 - fontBold.getlength(name))/2,550),name,'black',fontBold)
sponsors=["AstraZeneca","Novartis"]
if institution in sponsors:
draw.text(((page_width/2 - fontRegular.getlength(institution))/2,750),institution,'black',fontRegular)
else:
draw.text(((page_width/2 - fontRegular.getlength(title))/2,750),title,'black',fontRegular)
page.save('Tracking/static/QR_Codes/%s_%s_%s.png' % (first_name, last_name,participant_id))
attendee.participant_id=participant_id
attendee.save()
context['first_name'] = "%d %s" % (participant_id, first_name)
form = ParticipantForm()
return render(request, 'tracking_app/new_participant.html', context)
@login_required(redirect_field_name='login', login_url='/login')
def participants_view(request):
ctx={}
url_parameter = request.GET.get("q")
if url_parameter:
url_parameter=url_parameter.strip()
participants = Participant.objects.filter(first_name__icontains=url_parameter) | Participant.objects.filter(last_name__icontains=url_parameter) | Participant.objects.filter(phone__icontains=url_parameter)
else:
participants = Participant.objects.all()
ctx["participants"]=participants
does_req_accept_json = request.accepts("application/json")
is_ajax_request = request.headers.get("x-requested-with") == "XMLHttpRequest" and does_req_accept_json
if is_ajax_request:
html = render_to_string(
template_name="tracking_app/participants-results-partial.html",
context={"participants": participants}
)
data_dict = {"html_from_view": html}
return JsonResponse(data=data_dict, safe=False)
# Archive Generation
# archive_participants=Participant.objects.all()
# for participant in archive_participants:
# path='/QR_Codes/%s_%s_%s.png' % (participant.first_name, participant.last_name, participant.participant_id)
# if not os.path.isfile(path):
# participant_id=participant.participant_id
# page_width = 2480
# page_height = 3508
# page = Image.new('RGB', (page_width, page_height), (255, 255, 255))
# fontBold=ImageFont.truetype('Tracking/management/commands/Swansea-q3pd.ttf',size=125)
# fontRegular=ImageFont.truetype('Tracking/management/commands/Swansea-q3pd.ttf',size=100)
# qr_img=qrcode.make(participant_id)
# template = Image.open('Tracking/management/commands/template.png')
# template = template.resize((page_width, page_height))
# page.paste(qr_img,(370,1000))
# name=participant.first_name+" "+participant.last_name
# draw = ImageDraw.Draw(page)
# draw.text(((page_width/2 - fontBold.getlength(name))/2,550),name,'black',fontBold)
# page.save('Tracking/static/QR_Codes/%s_%s_%s.png' % (participant.first_name, participant.last_name,participant.participant_id))
# archived = shutil.make_archive('Tracking/static/Badges', 'zip', 'Tracking/static/QR_Codes')
# ctx["archive_url"]="/Badges.zip"
return render(request, 'tracking_app/participants.html', context=ctx)
def participant_entry_view(request):
form = EntryForm(request.POST or None)
context = {'form': form}
if form.is_valid():
try:
participant = Participant.objects.get(participant_id=form.cleaned_data['qrcode_uuid'])
except Participant.DoesNotExist:
form.add_error(None, "Invalid code")
return render(request, 'tracking_app/entry.html', context)
participant.inside= not participant.inside
participant.attended=True
participant.save()
Entry.objects.create(participant=participant, exit=not participant.inside)
context['succes'] = '%d %s %s has %s' % (
participant.id,
participant.last_name,
participant.first_name,
'entered' if participant.inside else 'exited')
if participant.inside:
context['succes'] = '<div style="color:green; font-size:2em;">' + context['succes'] + '</div>'
else:
context['succes'] = '<div style="color:red; font-size:2em;">' + context['succes'] + '</div>'
return render(request, 'tracking_app/entry.html', context)