def register_multiple(req): if not (req.user.is_staff and req.user.has_perm('main.on_spot_registration')): raise PermissionDenied try: data = json.loads(req.body.decode('utf-8')) except ValueError: return JsonResponse({ 'status': 'error', 'errorMessage': 'Invalid JSON' }, status=400) success_obj = [] events = [] for event in data['events']: event = Event.objects.get(id=event) team_id = generate_team_id(str(random.random()) + str(random.random()), event) events.append((event, team_id)) for i, member in enumerate(data['members']): email = member.get('email', '') if not email: email = str(i) + '|' + member['name'][:10] + '|' + member['college'][:5] + '@onspotteam.com' email = email.replace(' ', '_') user = MUser.objects.create_user(email=email) profile = user.profile profile.name = member['name'] profile.college = member['college'] profile.mobile = member.get('mobile', '') profile.pack = data['pack'] profile.auth_provider = 'on-spot-team' profile.on_spot = True profile.checked_in = True profile.on_spot_registerer = req.user.profile.name profile.save() success_obj.append({ 'name': profile.name, 'id': user.get_id() }) for event, team_id in events: Registration.objects.create( event=event, profile=profile, team_id=team_id, on_spot=True, mode='on-spot-team', ) return JsonResponse({ 'status': 'success', 'data': success_obj })
def test(req): # Closed raise PermissionDenied if req.GET.get('secret') != settings.SECRET_KEY[:5]: return JsonResponse({'status': 'unauthorized'}, status=403) try: data = json.loads(req.body.decode('utf-8'))['data'] except ValueError: return JsonResponse({ 'status': 'error', 'errorMessage': 'Invalid JSON' }, status=400) print(len(data)) for obj in data: user = MUser.objects.create_user(email=obj['email']) profile = user.profile profile.name = obj['name'] profile.college = obj['college'] profile.mobile = obj['mobile'] profile.pack = obj['pack'] profile.auth_provider = 'publicity' profile.remarks = obj['remarks'] for id in obj['events']: try: event = Event.objects.get(id=id) except Event.DoesNotExist: return JsonResponse({'error': 'Invalid event ' + str(id)}) team_id = '' is_owner = False if event.is_multiple(): team_id = generate_team_id(user.email, event) if event.is_group(): is_owner = True Registration.objects.create( event=event, profile=profile, team_id=team_id, is_owner=is_owner, mode='publicity' ) profile.save() return JsonResponse({'status': 'success'})
def process_invoice(req, invoice): """ This is not a view, rather a helper function which processes the invoices """ invoice.status = req.POST.get('status', '') invoice.success = True invoice.pending = False invoice.post_data = str(req.POST) invoice.save() if invoice.invoice_type in ('single', 'multiple', 'upgrade'): new_pack = invoice.invoice_type if new_pack == 'upgrade': new_pack = 'multiple' invoice.profile.pack = new_pack invoice.profile.save() messages.success(req, invoice.profile.get_pack_display() + ' has successfully been activated!') return redirect('/profile/#pack') elif invoice.invoice_type == 'team': team_id = generate_team_id(req.user.email, invoice.event) r = Registration() r.event = invoice.event r.profile = req.user.profile r.team_id = team_id r.is_owner = True r.save() messages.success(req, 'Scucessfully registered for ' + r.event.title) return redirect(r.event.get_absolute_url() + '#view-team') elif invoice.invoice_type == 'test': messages.success(req, 'Payment success!') return redirect('/profile/#pack') # invalid invoice return None
def process_invoice(req, invoice): """ This is not a view, rather a helper function which processes the invoices """ invoice.status = req.POST.get('status', '') invoice.success = True invoice.pending = False invoice.post_data = str(req.POST) invoice.save() if invoice.invoice_type in ('single', 'multiple', 'upgrade'): new_pack = invoice.invoice_type payment = 100 if new_pack == 'multiple': payment = 200 elif new_pack == 'upgrade': payment = 100 new_pack = 'multiple' invoice.profile.pack = new_pack invoice.profile.total_payment += payment invoice.profile.save() messages.success( req, invoice.profile.get_pack_display() + ' has successfully been activated!') return redirect('/profile/#pack') elif invoice.invoice_type == 'team': team_id = generate_team_id(req.user.email, invoice.event) invoice.profile.total_payment += 500 invoice.profile.save() r = Registration() r.event = invoice.event r.profile = req.user.profile r.team_id = team_id r.is_owner = True r.save() messages.success(req, 'Scucessfully registered for ' + r.event.title) return redirect(r.event.get_absolute_url() + '#view-team') elif invoice.invoice_type == 'workshop': invoice.profile.registered_workshops.add(invoice.workshop) invoice.profile.total_payment += invoice.workshop.price invoice.profile.save() messages.success( req, 'Successfully registered for ' + invoice.workshop.title) return redirect('/workshops/') elif invoice.invoice_type == 'test': messages.success(req, 'Payment success!') return redirect('/profile/#pack') elif invoice.invoice_type == 'hospitality': invoice.profile.total_payment += invoice.amount invoice.profile.hospitality_days = invoice.days invoice.profile.save() # Success template_email('*****@*****.**', settings.ACCOMODATION_INCHARGE, 'New Accomodation Request', 'accomodation_request', { 'profile': invoice.profile, }) messages.success( req, 'Successfully applied for ' + str(invoice.days) + ' day(s)') return redirect('/profile/#hospitality') # invalid invoice return None
def process_invoice(req, invoice): """ This is not a view, rather a helper function which processes the invoices """ invoice.status = req.POST.get('status', '') invoice.success = True invoice.pending = False invoice.post_data = str(req.POST) invoice.save() if invoice.invoice_type in ('single', 'multiple', 'upgrade'): new_pack = invoice.invoice_type payment = 100 if new_pack == 'multiple': payment = 200 elif new_pack == 'upgrade': payment = 100 new_pack = 'multiple' invoice.profile.pack = new_pack invoice.profile.total_payment += payment invoice.profile.save() messages.success(req, invoice.profile.get_pack_display() + ' has successfully been activated!') return redirect('/profile/#pack') elif invoice.invoice_type == 'team': team_id = generate_team_id(req.user.email, invoice.event) invoice.profile.total_payment += 500 invoice.profile.save() r = Registration() r.event = invoice.event r.profile = req.user.profile r.team_id = team_id r.is_owner = True r.save() messages.success(req, 'Scucessfully registered for ' + r.event.title) return redirect(r.event.get_absolute_url() + '#view-team') elif invoice.invoice_type == 'workshop': invoice.profile.registered_workshops.add(invoice.workshop) invoice.profile.total_payment += invoice.workshop.price invoice.profile.save() messages.success(req, 'Successfully registered for ' + invoice.workshop.title) return redirect('/workshops/') elif invoice.invoice_type == 'test': messages.success(req, 'Payment success!') return redirect('/profile/#pack') elif invoice.invoice_type == 'hospitality': invoice.profile.total_payment += invoice.amount invoice.profile.hospitality_days = invoice.days invoice.profile.save() # Success template_email( '*****@*****.**', settings.ACCOMODATION_INCHARGE, 'New Accomodation Request', 'accomodation_request', { 'profile': invoice.profile, } ) messages.success(req, 'Successfully applied for ' + str(invoice.days) + ' day(s)') return redirect('/profile/#hospitality') # invalid invoice return None
def register_create(req): if not (req.user.is_staff and req.user.has_perm('main.on_spot_registration')): raise PermissionDenied # IN params # name, email, college, mobile, referred, pack, events [{id, teamid?}] try: data = json.loads(req.body.decode('utf-8')) except ValueError: return JsonResponse({ 'status': 'error', 'errorMessage': 'Invalid JSON' }, status=400) f = RegistrationForm(data) if not f.is_valid(): return JsonResponse({ 'status': 'error', 'errors': f.errors }, status=400) try: MUser.objects.get(email=f.cleaned_data['email']) # duplicate user return JsonResponse({ 'status': 'error', 'errors': {'email': 'This user is already registered'} }, status=400) except MUser.DoesNotExist: user = MUser.objects.create_user(email=f.cleaned_data['email']) profile = user.profile profile.name=f.cleaned_data['name'] profile.college=f.cleaned_data['college'] profile.mobile=f.cleaned_data['mobile'] profile.referral=f.cleaned_data.get('referred', '') profile.pack=f.cleaned_data['pack'] profile.auth_provider='on-spot' profile.save() events = data.get('events', []) for eventObj in events: if eventObj.get('id', '') == '': return JsonResponse({ 'status': 'error', 'errors': {'form': 'Event ID not given'} }, status=400) try: event = Event.objects.get(id=eventObj['id']) except Event.DoesNotExist: return JsonResponse({ 'status': 'error', 'errors': {'form': 'Invalid Event ID'} }, status=400) team_id = eventObj.get('teamid', '') is_owner = False if event.is_multiple(): if team_id == '': team_id = generate_team_id(user.email, event) if event.is_group(): is_owner = True else: try: Registration.objects.get(team_id=team_id) except Registration.DoesNotExist: return JsonResponse({ 'status': 'error', 'errors': {'tid-' + str(event.id): 'Invalid team id: ' + team_id} }, status=400) Registration.objects.create( event=event, profile=profile, team_id=team_id, is_owner=is_owner ) return JsonResponse({ 'status': 'success', 'url': '/profile/' })
def register_create(req): if not (req.user.is_staff and req.user.has_perm('main.on_spot_registration')): raise PermissionDenied # IN params # name, email, college, mobile, referred, pack, events[{id, teamid?}], workshops[id] try: data = json.loads(req.body.decode('utf-8')) except ValueError: return JsonResponse({ 'status': 'error', 'errorMessage': 'Invalid JSON' }, status=400) f = RegistrationForm(data) if not f.is_valid(): return JsonResponse({ 'status': 'error', 'errors': f.errors }, status=400) workshops_objs = data.get('workshops', []) events_objs = data.get('events', []) events = [] workshops = [] # validate events passed in for event_obj in events_objs: if event_obj.get('id', '') == '': return JsonResponse({ 'status': 'error', 'errors': {'form': ['Event ID not given']} }, status=400) try: event = Event.objects.get(id=event_obj['id']) events.append(event) except Event.DoesNotExist: return JsonResponse({ 'status': 'error', 'errors': {'form': ['Invalid Event ID']} }, status=400) # validate teamids team_id = event_obj.get('teamid', '') if team_id != '': regs = Registration.objects.filter(team_id=team_id, event=event).count() if regs == 0: return JsonResponse({ 'status': 'error', 'errors': {'tid-' + str(event.id): ['Invalid team id: ' + team_id]} }, status=400) elif regs == event.team_max: return JsonResponse({ 'status': 'error', 'errors': {'tid-' + str(event.id): ['Team (' + team_id + ') is full']} }, status=400) # validate workshops passed in for workshop_obj in workshops_objs: if not workshop_obj.get('id', ''): return JsonResponse({ 'status': 'error', 'errrors': {'form': ['Invalid Workshop ID']} }, status=400) try: workshop = Workshop.objects.get(id=workshop_obj['id']) workshops.append(workshop) except Workshop.DoesNotExist: return JsonResponse({ 'status': 'error', 'errors': {'form': ['Invalid Workshop ID']} }, status=400) # validate pack/event combination num_quota_events = 0 for event in events: if event.team_type in ('individual', 'team'): num_quota_events += 1 if f.cleaned_data['pack'] == 'single' and num_quota_events > 1: return JsonResponse({ 'status': 'error', 'errors': {'pack': ['Cant register to more than one individual/team event on single pack']} }, status=400) elif f.cleaned_data['pack'] == 'none' and num_quota_events > 0: return JsonResponse({ 'status': 'error', 'errors': {'pack': ['Cant register to individual/team events on No Pack']} }, status=400) try: MUser.objects.get(email=f.cleaned_data['email']) # duplicate user return JsonResponse({ 'status': 'error', 'errors': {'email': ['This user is already registered']} }, status=400) except MUser.DoesNotExist: user = MUser.objects.create_user(email=f.cleaned_data['email']) # calculate payment due if f.cleaned_data['pack'] == 'single': payment = 100 elif f.cleaned_data['pack'] == 'multiple': payment = 200 else: payment = 0 # setup profile profile = user.profile profile.name=f.cleaned_data['name'] profile.college=f.cleaned_data['college'] profile.mobile=f.cleaned_data['mobile'] profile.referral=f.cleaned_data.get('referred', '') profile.pack=f.cleaned_data['pack'] profile.auth_provider='on-spot' profile.on_spot = True profile.checked_in = True profile.on_spot_registerer = req.user.profile.name # profile saved at the end success_obj = { 'name': profile.name, 'multipleEvents': [] } # do event registrations for i, event in enumerate(events): team_id = events_objs[i].get('teamid', '') is_owner = False if event.is_multiple(): # if team_id given it would already be validated above if team_id == '': team_id = generate_team_id(user.email, event) if event.is_group(): is_owner = True # creating group team, cost 500 payment += 500 success_obj['multipleEvents'].append({ 'id': event.id, 'title': event.title, 'teamid': team_id }) Registration.objects.create( event=event, profile=profile, team_id=team_id, is_owner=is_owner, on_spot=True, mode='on-spot' ) # do workshop registrations for workshopObj in workshops: payment += workshop.price profile.registered_workshops.add(workshop) profile.total_payment = payment profile.save() # template_email( # '*****@*****.**', # (user.email,), # 'Welcome to Magnovite 2015', # 'on_spot_receipt', # {'profile': profile} # ) success_obj['data'] = { 'id': user.get_id(), 'name': profile.name } success_obj['status'] = 'success' success_obj['reciptURL'] = '/receipt/' + profile.receipt_id + '/' return JsonResponse(success_obj)