def post(self, request, *args, **kwargs): meal_id = request.POST.get('meal_id', None) qr_id = request.POST.get('qr_code', None) if not qr_id or not meal_id: messages.error(self.request, 'The QR code or meal is not available.') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) current_meal = Meal.objects.filter(id=meal_id).first() if not current_meal.opened and not self.request.user.is_organizer: messages.error( self.request, 'This meal is not open yet or it has ended. Reach out to an organizer to activate it again' ) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) hacker_checkin = CheckIn.objects.filter(qr_identifier=qr_id).first() if not hacker_checkin: messages.error(self.request, 'Invalid QR code!') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) hacker_application = getattr(hacker_checkin, 'application', None) if not hacker_application: messages.error(self.request, 'No user found for this QR code!') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) times_hacker_ate = Eaten.objects.filter( meal=current_meal, user=hacker_application.user).count() if times_hacker_ate >= current_meal.times: error_message = 'Warning! Hacker already ate %d out of %d available times!' % \ (times_hacker_ate, current_meal.times) messages.error(self.request, error_message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) checkin = Eaten(meal=current_meal, user=hacker_application.user) checkin.save() if hacker_application.diet == models_app.D_NONE: diet = 'No dietary restriction.' elif hacker_application.diet == models_app.D_OTHER: diet = hacker_application.other_diet else: diet = hacker_application.diet messages.success( self.request, f'Hacker has been successfully logged for this meal! DIET INFO: {diet}' ) return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def post(self, request, *args, **kwargs): mealid = request.POST.get('meal_id', None) qrid = request.POST.get('qr_id', None) if not qrid or not mealid: return JsonResponse( {'error': 'Missing meal and/or QR. Trying to trick us?'}) current_meal = Meal.objects.filter(id=mealid).first() if not current_meal.opened and not self.request.user.is_organizer: return JsonResponse({ 'error': 'Meal has been closed. Reach out to an organizer to activate it again' }) hacker_checkin = CheckIn.objects.filter(qr_identifier=qrid).first() if not hacker_checkin: return JsonResponse({'error': 'Invalid QR code!'}) hacker_application = hacker_checkin.application() if not hacker_application: return JsonResponse( {'error': 'No application found for current code'}) times_hacker_ate = Eaten.objects.filter( meal=current_meal, user=hacker_application.user).count() if times_hacker_ate >= current_meal.times: error_message = 'Warning! Hacker already ate %d out of %d available times!' % \ (times_hacker_ate, current_meal.times) return JsonResponse({'error': error_message}) checkin = Eaten(meal=current_meal, user=hacker_application.user) checkin.save() if hacker_application.diet == models_app.D_NONE: diet = 'No dietary restriction.' elif hacker_application.diet == models_app.D_OTHER: diet = hacker_application.other_diet else: diet = hacker_application.diet return JsonResponse({'success': True, 'diet': diet})
def meal_scan(request): id = request.POST.get('id', None) qr_code = request.POST.get('badgeQR', None) response, hacker_user = get_user_from_qr(qr_code) if response is not None: return response meal = Meal.objects.filter(id=id).first() if not meal.opened and not request.user.is_organizer: return JsonResponse( { 'status': 403, 'message': 'This meal is not open yet or it has ended. Reach out to an organizer to activate it again' }, status=403) times_hacker_ate = Eaten.objects.filter(meal=meal, user=hacker_user).count() if times_hacker_ate == meal.times: return JsonResponse( { 'status': 409, 'message': f'Warning! Hacker already ate the max number of available times ({times_hacker_ate})!' }, status=409) eaten = Eaten(meal=meal, user=hacker_user) eaten.save() user_application = Application.objects.filter( user_id=hacker_user.id).first() return JsonResponse({ 'status': 200, 'message': 'Hacker successfully logged for this meal!', 'data': { 'diet': user_application.diet, 'other_diet': user_application.other_diet } })
def post(self, request, format=None): var_token = request.GET.get('token') if var_token != settings.MEALS_TOKEN: return HttpResponse(status=500) var_object = request.GET.get('object') if var_object not in ['user', 'meal']: return HttpResponse(json.dumps({'code': 1, 'message': 'Invalid object'}), content_type='application/json') var_meal = request.GET.get('meal') obj_meal = Meal.objects.filter(id=var_meal).first() if obj_meal is None: return HttpResponse(json.dumps({'code': 1, 'message': 'Invalid meal'}), content_type='application/json') if var_object == 'user': var_repetitions = obj_meal.times var_user = request.GET.get('user') obj_checkin = CheckIn.objects.filter(qr_identifier=var_user).first() if obj_checkin is None: return HttpResponse(json.dumps({'code': 1, 'message': 'Invalid user'}), content_type='application/json') obj_user = obj_checkin.application_user obj_application = Application.objects.filter(user=obj_user).first() if obj_user.diet: var_diet = obj_user.diet elif obj_application: var_diet = obj_application.diet else: var_diet = "UNKNOWN" var_eatens = Eaten.objects.filter(meal=obj_meal, user=obj_user).count() if var_eatens >= var_repetitions: return HttpResponse(json.dumps({'code': 2, 'message': 'Hacker alreay ate'}), content_type='application/json') obj_eaten = Eaten() obj_eaten.meal = obj_meal obj_eaten.user = obj_user obj_eaten.save() return HttpResponse(json.dumps({'code': 0, 'content': {'diet': var_diet}}), content_type='application/json') var_repetitions = request.GET.get('times') obj_meal.times = var_repetitions obj_meal.save() return HttpResponse(json.dumps({'code': 0, 'message': 'Times updated'}), content_type='application/json')