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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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from django.http import HttpResponse
from .models import Profile, Department, Event, Ticket, Notifications, Gallery
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
import json
import re
import requests
import random
from django.views.decorators.csrf import csrf_exempt


def index(request):
    objects = Profile.objects.all().first()
    name = objects.user.get_full_name()
    profilePic = objects.profilePic
    location = objects.location
    phone = objects.phone
    otp = objects.otp
    isVolunteer = objects.isVolunteer
    isOrganiser = objects.isOrganiser
    isAccountSetup = objects.isAccountSetup
    data = {
        "name": name,
        "profilePic": profilePic,
        "location": location,
        "phone": phone,
        "otp": otp,
        "isVolunteer": isVolunteer,
        "isOrganiser": isOrganiser,
        "isAccountSetup": isAccountSetup
    }
    return HttpResponse(json.dumps(data), content_type="application/json")


def check(email):
    regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
    if(re.fullmatch(regex, email)):
        return True
    else:
        return False


@csrf_exempt
def loginApp(request):
    if request.method == "POST":
        try:
            body = json.loads(request.body)
            email = body['email']
            password = body['password']
            try:
                username = User.objects.filter(email=email).first().username
                user = authenticate(username=username, password=password)
                if user is not None:
                    profile = Profile.objects.filter(user=user).first()
                    isVerified = profile.isVerified
                    isAccountSetup = profile.isAccountSetup
                    if isVerified == True:
                        login(request, user)
                        return HttpResponse(json.dumps({"msg": "Logged in successfully.", "accountSetup": isAccountSetup}), content_type="application/json")
                    else:
                        email = user.email
                        return HttpResponse(json.dumps({"msg": "OTP sent to "+email}), content_type="application/json")
            except:
                return HttpResponse(json.dumps({"error": "Email or password must be Incorrect."}), content_type="application/json")
        except Exception as error:
            return HttpResponse(json.dumps({"error": error}), content_type="application/json")

        else:
            return HttpResponse(json.dumps({"error": "Email or password must be Incorrect."}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def registerApp(request):
    if request.method == "POST":
        try:

            body = json.loads(request.body)
            email = body['email']
            pass1 = body['password1']
            pass2 = body['password2']

            if not check(email):
                return HttpResponse(json.dumps({"error": "Invalid Email."}), content_type="application/json")
            elif pass1 != pass2:
                return HttpResponse(json.dumps({"error": "Password Does Not Match."}), content_type="application/json")
            else:
                users = User.objects.filter(email=email).all().count()
                if users == 0:
                    newUser = User.objects.create_user(
                        username=email, email=email, password=pass1)

                    newUser.save()
                    otp = str(random.randint(1000, 9999))
                    profileForNewUser = Profile()
                    profileForNewUser.user = User.objects.filter(
                        email=email).first()
                    profileForNewUser.otp = otp
                    profileForNewUser.events = {"data": []}
                    profileForNewUser.notification = {"data": []}
                    profileForNewUser.isAccountSetup = False
                    profileForNewUser.isVolunteer = False
                    profileForNewUser.isOrganiser = False
                    profileForNewUser.isVerified = False
                    profileForNewUser.save()
                    # https://script.google.com/macros/s/AKfycbzW-cQR5jK5dWpfdH7yJ0Rb_gR9dC7YMn0VFnQU9ZCzhCx7wXZgbnTwDcFDsLo6Vn_V/[email protected]&subject=auto&body=auto&otp=1234
                    r = requests.get('https://script.google.com/macros/s/AKfycbzW-cQR5jK5dWpfdH7yJ0Rb_gR9dC7YMn0VFnQU9ZCzhCx7wXZgbnTwDcFDsLo6Vn_V/exec?email=' +
                                    email + '&subject=Welcome to Xenesis 2023&body=Hi there&otp='+otp)
                    return HttpResponse(json.dumps({"msg": "OTP sent to "+email}), content_type="application/json")
        except Exception as error:
                    return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    else:
            return HttpResponse(json.dumps({"error": "There already exist a account with this email."}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def locationSetterApp(request):
    if request.method == "POST" and request.session['accountSetup'] == True:
        try: 
            body = json.loads(request.body)
            email = body['email']
            location = body['location']
            user = User.objects.filter(email=email).first()
            profile = Profile.objects.filter(user=user).first()
            profile.location = location
            profile.isVerified = True
            profile.isAccountSetup = True
            profile.save()
            request.session['accountSetup'] = False
            return HttpResponse(json.dumps({"msg": "New Location has been saved.", "location": location}), content_type="application/json")
        except Exception as error:
                    return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def accountSetupApp(request):
    if request.method == "POST":
        try:
            body = json.loads(request.body)
            email = body['email']
            name = body['name']
            college = body['college']
            phone = body['phone']
            profilePic = body['profilePic']
            user = User.objects.filter(email=email).first()
            user.first_name = name
            user.save()
            profile = Profile.objects.filter(user=user).first()
            profile.college = college
            profile.phone = phone
            profile.profilePic = profilePic
            profile.save()
            request.session['accountSetup'] = True
            return HttpResponse(json.dumps({"msg": "Account has been setup."}), content_type="application/json")
        except Exception as error:
            return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def resendotpApp(request):
    if request.method == "POST":
        try: 
            body = json.loads(request.body)
            email = body['email']
            if "passwordRecovery" in body.keys():
                otp = str(random.randint(1000, 9999))
                profile = Profile.objects.filter(
                    user=User.objects.filter(email=email).first()).first()
                profile.otp = otp
                profile.save()
                r = requests.get('https://script.google.com/macros/s/AKfycbzW-cQR5jK5dWpfdH7yJ0Rb_gR9dC7YMn0VFnQU9ZCzhCx7wXZgbnTwDcFDsLo6Vn_V/exec?email=' +email + '&subject=Welcome to Xenesis 2023&body=Hi there&otp='+otp)
                request.session['isPasswordRecovery'] = True
                return HttpResponse(json.dumps({"msg": "OTP sent to "+email + " for Password Verification."}), content_type="application/json")
            else:
                otp = str(random.randint(1000, 9999))
                profile = Profile.objects.filter(user=User.objects.filter(email=email).first()).first()
                profile.otp = otp
                profile.save()
                r = requests.get('https://script.google.com/macros/s/AKfycbzW-cQR5jK5dWpfdH7yJ0Rb_gR9dC7YMn0VFnQU9ZCzhCx7wXZgbnTwDcFDsLo6Vn_V/exec?email=' +email + '&subject=Welcome to Xenesis 2023&body=Hi there&otp='+otp)
                return HttpResponse(json.dumps({"msg": "OTP sent to "+email}), content_type="application/json")
        except Exception as error:
                return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def forgotpasswordApp(request):
    if request.method == "POST" and request.session['isPasswordRecovery'] == True:
        try:
            body = json.loads(request.body)
            email = body['email']
            password1 = body['password1']
            password2 = body['password2']
            if password1 == password2:
                user = User.objects.filter(email=email).first()
                user.set_password(password1)
                print(email, password1, password2)
                request.session['isPasswordRecovery'] = False
                return HttpResponse(json.dumps({"msg": "OTP sent to "+email}), content_type="application/json")
            else:
                return HttpResponse(json.dumps({"error": "Passwords doesn't match."}), content_type="application/json")
        except Exception as error:
                    return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def otpvalidationApp(request):
    if request.method == "POST":
        try:      
            body = json.loads(request.body)
            userOtp = body['otp']
            email = body['email']
            user = Profile.objects.filter(
                user=User.objects.filter(email=email).first()).first()
            if userOtp == user.otp:
                profile = Profile.objects.filter(
                    user=User.objects.filter(email=email).first()).first()
                profile.isVerified = True
                profile.save()
                return HttpResponse(json.dumps({"msg": "The user has been verified."}), content_type="application/json")
            else:
                return HttpResponse(json.dumps({"error": "OTP does not match."}), content_type="application/json")
        except Exception as error:
                    return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def accountsetupApp(request):
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")


@csrf_exempt
def homeDataFetcherApp(request):
    data = {"department": [],"event":[],"gallery":[]}

    departmentArr = Department.objects.all()
    for department in departmentArr:
        data["department"].append(department.name)

    images = Gallery.objects.all()
    for image in images:
        data["gallery"].append(image.path)
    
    events = Event.objects.filter(department=Department.objects.filter(name="Computer Engineering").first()).order_by('name').all()
    eventArr = []
    impEvent = []
    for event in events:
        if event.name != "X - Motion Game Mania":
            eventArr.append([event.name, event.price, event.description, event.tagline,event.posterImage, (event.name).replace(" ", "-").replace("---", ":")])
        else:
            impEvent = [[event.name, event.price, event.description, event.tagline, event.posterImage, (event.name).replace(" ", "-").replace("---", ":")]]
    impEvent.extend(eventArr[:10])
    data["event"] = impEvent

    return HttpResponse(json.dumps(data), content_type="application/json")

@csrf_exempt
def galleryListApp(request):
    images = Gallery.objects.all()
    imageArr = []
    for image in images:
        imageArr.append(image.path)
    return HttpResponse(json.dumps({"data":imageArr}), content_type="application/json")

@csrf_exempt
def eventListApp(request):
    events = Event.objects.all()
    eventArr = []
    impEvent = []
    for event in events:
        if event.name != "X - Motion Game Mania":
            eventArr.append([event.name, event.price, event.description, event.tagline,event.posterImage, (event.name).replace(" ", "-").replace("---", ":")])
        else:
            impEvent = [[event.name, event.price, event.description, event.tagline, event.posterImage, (event.name).replace(" ", "-").replace("---", ":")]]
    impEvent.extend(eventArr[:20])
    departments = Department.objects.all()
    departmentArr = []
    for department in departments:
        departmentArr.append([department.name,department.abbriviation])
    return HttpResponse(json.dumps({"event":impEvent,"department":departmentArr}), content_type="application/json")

@csrf_exempt
def eventsSearchApp(request):
    if request.method == "POST":
        try:
            body = json.loads(request.body)
            query = body['query']
            events = Event.objects.filter(name__contains=query).order_by('name').all()
            eventArr = []
            impEvent = []
            departmentTempArr = []
            for event in events:
                if event.name != "X - Motion Game Mania":
                    eventArr.append([event.name, event.price, event.description, event.tagline,event.posterImage, (event.name).replace(" ", "-").replace("---", ":")])
                    departmentTempArr.append([event.department.name,event.department.abbriviation])
                else:
                    impEvent = [[event.name, event.price, event.description, event.tagline, event.posterImage, (event.name).replace(" ", "-").replace("---", ":")]]
            impEvent.extend(eventArr[:10])

            tempArr = [['Computer Engineering', 'CE']]
            tempArr.extend(departmentTempArr)

            departmentArr = []
            for i in range(len(impEvent)):
                if tempArr[i] not in departmentArr:
                    departmentArr.append(tempArr[i])
            
            data = {
                "event": impEvent,
                "department" : departmentArr
            }
            return HttpResponse(json.dumps(data), content_type="application/json")
        except Exception as error:
            return HttpResponse(json.dumps({"error": error}), content_type="application/json")    
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")

@csrf_exempt
def eventDetailFetcherApp(request):
    if request.method == "POST":
        try:
            body = json.loads(request.body)
            event = body['event']
            event = Event.objects.filter(name=event).first()
            eventArr = []
            eventArr.append(event.name)
            eventArr.append(event.price)
            eventArr.append(event.description)
            eventArr.append(event.posterImage)
            eventArr.append((event.rules).split("•")[1:] if event.rules != None and event.rules != "" else [])
            eventArr.append(event.round1Title)
            eventArr.append((event.round1).split("•")[1:] if event.round1 != None else [])
            eventArr.append(event.round2Title)
            eventArr.append((event.round2).split("•")[1:] if event.round2 != None else [])
            eventArr.append(event.round3Title)
            eventArr.append((event.round3).split("•")[1:] if event.round3 != None else [])
            eventArr.append(event.round4Title)
            eventArr.append((event.round4).split("•")[1:] if event.round4 != None else [])
            eventArr.append(event.round5Title)
            eventArr.append((event.round5).split("•")[1:] if event.round5 != None else [])
            eventArr.append(event.coordinator1.user.first_name)
            eventArr.append(event.coordinator2.user.first_name)
            return HttpResponse(json.dumps({"data":eventArr}), content_type="application/json")
        except Exception as error:
            return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error": "You were not suppose be here."}), content_type="application/json")

@csrf_exempt
def QRScanner(request):
    if request.method == "POST":
        try:
            body = json.loads(request.body)
            if "uuid" in body.keys():
                uuid = body['uuid']
                ticket = Ticket.objects.filter(qrCodeData=uuid).first()
                if ticket is not None:
                    if ticket.userCount >0:
                        count = 1
                        if ticket.owner1 != None:
                            count=count+1
                        if ticket.owner2 != None:
                            count=count+1
                        if ticket.owner3 != None:
                            count=count+1
                        if ticket.owner4 != None:
                            count=count+1
                        temp = {}
                        temp["id"] = ticket.id
                        temp["profilePic"] = ticket.owner.profilePic
                        temp["username"] = ticket.owner.user.first_name
                        temp["email"] = ticket.owner.user.email
                        if ticket.event.isTeamEvent != True:
                            temp["price"] = ticket.event.price
                        else: 
                            temp["price"] = ticket.event.teamPrice
                        temp["eventName"] = ticket.event.name
                        temp["isPaid"] = ticket.isPaid
                        temp["qrCodeData"] = ticket.qrCodeData
                        temp["isTeamPriceFull"] = ticket.event.isTeamPriceFull
                        try:
                            temp["userCount"] = count
                            if count != 1:
                                temp["total"] = count*int(ticket.event.price)
                        except:
                            temp["userCount"] = 1
                            temp["total"] = 0
                        return HttpResponse(json.dumps({"data":temp}),content_type="application/json")
                    else:
                        return HttpResponse(json.dumps({"error":"Ticket Has already been used."}),content_type="application/json")
                else:
                    return HttpResponse(json.dumps({"error":"Ticket Does Not Exist."}),content_type="application/json")
            elif "ticketId" in body.keys():
                ticketId = body['ticketId']
                ticket = Ticket.objects.filter(id=ticketId).first()
                if ticket.userCount >0:
                    ticket.userCount = ticket.userCount - 1
                    ticket.save()
                    return HttpResponse(json.dumps({"msg":"Ticket Has Been Confirmed."}),content_type="application/json")
                else:
                    return HttpResponse(json.dumps({"error":"Ticket Has already been used."}),content_type="application/json")
            else:
                return HttpResponse(json.dumps({"error":"Server Didnot Recieve QR code. Please Rescan the code."}),content_type="application/json")
        except Exception as error:
            return HttpResponse(json.dumps({"error": error}), content_type="application/json")
    return HttpResponse(json.dumps({"error":"You were not supposed to be here"}),content_type="application/json")