forked from sammyfung/badgeprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
201 lines (181 loc) · 7.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
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
from django.shortcuts import render, Http404
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.urls import reverse
from badgeprint.models import Event, Printer, PrinterUser, Participant, Log
from badgeprint.labelprint import print_text
import re
def list_all_event(request):
# List all events
if request.user.is_authenticated():
return render(request, 'events.html')
else:
return HttpResponseRedirect(reverse('badgeprint_logon'))
def json_all_event(request):
# return all events in json
if request.user.is_authenticated():
item_list = Event.objects.filter(id__gte=0, active=True).order_by('-id')
total = item_list.count()
json_items = {'total': total, 'data': []}
for i in item_list:
json_item = {}
json_item['id'] = i.id
if i.platform=='eventbrite':
json_item['code'] = "%s" % (i.platform, i.code)
else:
json_item['code'] = "%s" % i.platform
json_item['name'] = "%s" % i.name
json_item['logo'] = "%s" % i.logo
json_items['data'].append(json_item)
return JsonResponse(json_items)
else:
raise Http404("Authentication is required.")
def list_event_participant(request, event_id):
# List all participants from requested event.
if request.user.is_authenticated():
return render(request, 'participants.html', {'id': event_id})
else:
raise Http404("Authentication is required.")
def json_event_participant(request, event_id):
# return all events in json
if request.user.is_authenticated():
item_list = Participant.objects.filter(event=event_id).order_by('first_name')
total = item_list.count()
attended = item_list.filter(status='Attended').count()
json_items = {'total': total, 'attended': attended, 'data': []}
for i in item_list:
json_item = {}
json_item['id'] = i.id
json_item['event'] = "%s" % i.event
json_item['code'] = "%s" % i.code
json_item['ticket_type'] = "%s" % i.ticket_type
json_item['first_name'] = "%s" % i.first_name
json_item['last_name'] = "%s" % i.last_name
json_item['company'] = "%s" % i.company
json_item['phone'] = "%s" % i.phone
json_item['email'] = "%s" % i.email
json_item['status'] = "%s" % i.status
json_items['data'].append(json_item)
return JsonResponse(json_items)
else:
raise Http404("Authentication is required.")
def json_event_stats(request, event_id):
# return all events in json
if request.user.is_authenticated():
item_list = Participant.objects.filter(event=event_id).order_by('first_name')
total = item_list.count()
attended = item_list.filter(status='Attended').count()
json_items = {'total': total, 'attended': attended}
return JsonResponse(json_items)
else:
raise Http404("Authentication is required.")
def event_checkinreset(request, event_id):
if request.user.is_authenticated():
participant_list = Participant.objects.filter(event=event_id)
for participant in participant_list:
participant.status = "Attending"
participant.save()
return HttpResponseRedirect('/')
else:
raise Http404("Authentication is required.")
def print_participant_label(request, participant_id):
if request.user.is_authenticated():
# Retrieve participant information
participant = Participant.objects.get(id=participant_id)
# Additional: marking participant
if participant.status != 'Attended':
participant.status = 'Attended'
participant.save()
log = Log(event=participant.event, user=request.user, participant=participant, \
type="action", action="checkin")
log.save()
# Log
log = Log(event=participant.event, user=request.user, participant=participant, \
type="action", action="print")
log.save()
# Print to label printer
try:
printer = PrinterUser.objects.filter(user=request.user, ticket_type=participant.ticket_type)[0].printer
except IndexError:
printer = PrinterUser.objects.filter(user=request.user)[0].printer
data = {
'event_name': participant.event.name,
'first_name': participant.first_name,
'last_name': participant.last_name,
'company': participant.company,
'label_size': printer.label,
'printer_uri': printer.uri,
'printer_model': 'QL-720NW',
'orientation': 'rotated',
'logo': participant.event.logo,
'label_tpl': participant.event.label_tpl,
'ticket_type': participant.ticket_type,
'debug': printer.debug,
}
print_text(**data)
# return to list_event_participant page
return HttpResponseRedirect(reverse('list_event_participant', kwargs={'event_id':participant.event.id}))
else:
raise Http404("Authentication is required.")
def print_participant_label_api(request, participant_id):
# Retrieve participant information
if len(participant_id) == 22:
participant = Participant.objects.get(code=participant_id)
else:
participant = Participant.objects.get(id=participant_id)
# Additional: marking participant
if participant.status != 'Attended':
participant.status = 'Attended'
participant.save()
log = Log(event=participant.event, user=User(id=1), participant=participant, \
type="action", action="checkin")
log.save()
# Log
log = Log(event=participant.event, user=User(id=1), participant=participant, \
type="action", action="print")
log.save()
# Print to label printer
try:
printer = PrinterUser.objects.filter(user=User(id=1), ticket_type=participant.ticket_type)[0].printer
except IndexError:
printer = PrinterUser.objects.filter(user=User(id=1))[0].printer
data = {
'event_name': participant.event.name,
'first_name': participant.first_name,
'last_name': participant.last_name,
'company': participant.company,
'label_size': printer.label,
'printer_uri': printer.uri,
'printer_model': 'QL-720NW',
'orientation': 'rotated',
'logo': participant.event.logo,
'label_tpl': participant.event.label_tpl,
'ticket_type': participant.ticket_type,
'debug': printer.debug,
}
print_text(**data)
# return to list_event_participant page
return HttpResponse()
def badgeprint_logon(request):
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('list_all_event'))
else:
email = request.POST.get('inputEmail', '')
password = request.POST.get('inputPassword', '')
if email != '' and password != '':
try:
username = User.objects.get(email=email).username
except ObjectDoesNotExist:
return render(request, 'logon.html')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse('list_all_event'))
else:
return render(request, 'logon.html')
def badgeprint_logoff(request):
if request.user.is_authenticated():
logout(request)
return HttpResponseRedirect(reverse('badgeprint_logon'))