def migrate_events(): event_index = Page.objects.get(id=6).specific user = get_user_model().objects.get(id=1) old_events = OldEvent.objects.using('old_data').all() # Migrate events for old_event in old_events: old_event_type = old_event.type try: # We don't actually care about this - its a test to migrate the event across event_type = EventType.objects.get(name=old_event_type.name, target=old_event_type.target) except EventType.DoesNotExist: event_type = EventType(name=old_event_type.name, target=old_event_type.target) event_type.save() title = '{type} on {date}'.format(type=old_event_type.name, date=date(old_event.start, 'D jS F Y')) slug = slugify('{title} - {rand}'.format( title=title, rand=int(round(time.time() * 1000)))) if old_event.shortDescription: description = old_event.shortDescription else: if old_event_type.info: description = old_event_type.info else: description = old_event_type.name new_event = EventPage(title=title.strip(), slug=slug, description=description.strip(), start=old_event.start, finish=old_event.finish, cancelled=old_event.cancelled, category=event_type, location=old_event.location.name) new_event.body.stream_data = [ ('paragraph', RichText('<p>{body}</p>'.format( body=linebreaks(old_event.longDescription)))) ] print('Restoring event {type} from {date}'.format( type=old_event.type.name, date=old_event.start)) event_index.add_child(instance=new_event) revision = new_event.save_revision(user=user, submitted_for_moderation=False) revision.publish() new_event.save() # Deal with signups old_signups = Signup.objects.using('old_data').filter( event_id=old_event.id) for old_signup in old_signups: print('Restoring signup for {type} from {date}'.format( type=old_event.type.name, date=old_event.start)) new_signup = EventSignup( comment=truncatechars(old_signup.comment, 1024), member=get_user_model().objects.get(id=old_signup.user_id), event_id=new_event.id, signup_created=old_signup.time) new_signup.save()
def post(self, request): payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] try: # Verify the Stripe signature event = stripe.Webhook.construct_event(payload, sig_header, settings.STRIPE_WEBHOOK_KEY) except ValueError: return HttpResponse(status=400) except stripe.error.SignatureVerificationError: return HttpResponse(status=400) if event['type'] == 'checkout.session.completed': session = event['data']['object'] ticket_info = json.loads(session['client_reference_id']) ticket_created_at = datetime.strptime(ticket_info['created_at'], '%Y-%m-%dT%H:%M:%S%z') # If the ticket was created after the current time, then something screwy is going on if ticket_created_at > timezone.now(): return HttpResponse(status=400) event = Event.objects.get(id__exact=ticket_info['event']) ticket = Ticket.objects.get(id__exact=ticket_info['ticket']) user = ticket.user # Create the event signup for the user signup = EventSignup(user=user, event=event, ticket=ticket) # Check the charge payment = stripe.PaymentIntent.retrieve(session['payment_intent']) charge = payment['charges']['data'][0] # Update the ticket ticket.status = Ticket.IN_PROGRESS ticket.last_updated_at = timezone.now() ticket.charge_id = charge['id'] if charge['paid']: ticket.status = Ticket.COMPLETE ticket.save() signup.save() elif event['type'] == 'charge.succeeded': charge = event['data']['object'] try: ticket = Ticket.objects.get(charge_id__exact=charge['id']) except Ticket.DoesNotExist: # The ticket doesn't exist for some reason, refund the charge and 400 return HttpResponse(400) if charge['paid']: ticket.status = Ticket.COMPLETE ticket.last_updated_at = timezone.now() ticket.save() elif event['type'] == 'charge.refunded': charge = event['data']['object'] try: ticket = Ticket.objects.get(charge_id__exact=charge['id']) ticket.status = Ticket.REFUNDED ticket.last_updated_at = timezone.now() ticket.comment = 'Refunded at {time}'.format( time=timezone.now().strftime('%Y-%m-%dT%H:%M:%S%z')) ticket.save() except Ticket.DoesNotExist: # Nothing needs to be done since the ticket doesn't exist and the refund has been processed pass return HttpResponse(status=200)
def post(self, request): if not request.POST.get('event_id'): return HttpResponseBadRequest() event = get_object_or_404(Event, id=request.POST.get('event_id')) has_signed_up = EventSignup.objects.for_event(event, request.user).exists() if has_signed_up: messages.error(request, 'You\'re already signed up to that event.', extra_tags='is-danger') return redirect('event_home', slug=event.slug) profile = WarwickGGUser.objects.get(user=request.user) # If the event is hosted by UWCS if 'UWCS' in event.hosted_by: uwcs_member = check_membership(settings.UWCS_API_KEY, profile, request, 'UWCS') else: uwcs_member = False # If the event is hosted by Esports if 'WE' in event.hosted_by: esports_member = check_membership(settings.ESPORTS_API_KEY, profile, request, 'Esports') else: esports_member = False signup_cost = event.cost_member if esports_member or uwcs_member else event.cost_non_member # If the user should be charged but there's no stripe token then error if signup_cost > 0 and not request.POST.get('stripe_token'): return HttpResponseBadRequest() # If there was an issue processing the signup form then error - there shouldn't be any reason since # all of the fields are optional but ??? signup = EventSignup(user=request.user, event=event) signup_form = SignupForm(request.POST, instance=signup) if not signup_form.is_valid(): messages.error( request, 'There was an error processing your signup, double check everything is in order and try again.', extra_tags='is-danger') return redirect('event_signup', slug=event.slug) if signup_cost > 0: token = request.POST.get('stripe_token') try: charge = Charge.create(amount=int(signup_cost * 100), currency='gbp', source=token, receipt_email=request.user.email) except StripeError: # If there was an error with the request messages.error( request, 'There was an error processing the payment. Please check your balance and try again later.', extra_tags='is-danger') return redirect('event_signup', slug=event.slug) if charge.paid: signup = signup_form.save(commit=False) signup.transaction_token = charge.stripe_id signup.save() messages.success( request, 'Signup successful!{seating}'.format( seating= ' You can now reserve a seat on the seating plan.' if event.has_seating else ''), extra_tags='is-success') return redirect('event_home', slug=event.slug) else: messages.error( request, 'There was an error processing your payment. Make sure you have sufficient balance and try again.', extra_tags='is-danger') return redirect('event_signup', slug=event.slug) else: signup_form.save() messages.success( request, 'Signup successful!{seating}'.format( seating=' You can now reserve a seat on the seating plan.' if event.has_seating else ''), extra_tags='is-success') return redirect(event.get_absolute_url() + '#signup')
def post(self, request): if not request.POST.get('event_id'): return HttpResponseBadRequest() event = get_object_or_404(Event, id=request.POST.get('event_id')) has_signed_up = list( filter(lambda x: x.is_complete(), EventSignup.objects.for_event(event, request.user).all())) if has_signed_up: messages.error(request, 'You\'re already signed up to that event.', extra_tags='is-danger') return redirect('event_home', slug=event.slug) # Check if there is still space left if event.signup_count == event.signup_limit: messages.error(request, 'There\'s no more space for that event, sorry.', extra_tags='is-danger') return redirect('event_home', slug=event.slug) profile = WarwickGGUser.objects.get(user=request.user) # If the event is hosted by UWCS if 'UWCS' in event.hosted_by: uwcs_member = check_membership(settings.UWCS_API_KEY, profile, request, 'UWCS') else: uwcs_member = False # If the event is hosted by Esports if 'WE' in event.hosted_by: esports_member = check_membership(settings.ESPORTS_API_KEY, profile, request, 'Esports') else: esports_member = False signup_cost = event.cost_member if esports_member or uwcs_member else event.cost_non_member # If there was an issue processing the signup form then error - there shouldn't be any reason since # all of the fields are optional but ??? signup = EventSignup(user=request.user, event=event) signup_form = EventSignupForm(request.POST, instance=signup) if not signup_form.is_valid(): messages.error( request, 'There was an error processing your signup, double check everything is in order and try again.', extra_tags='is-danger') return redirect('event_signup', slug=event.slug) if signup_cost > 0: # This shouldn't happen since the Stripe checkout should have taken over return HttpResponseBadRequest() else: signup_form.save() messages.success( request, 'Signup successful!{seating}'.format( seating=' You can now reserve a seat on the seating plan.' if event.has_seating else ''), extra_tags='is-success') return redirect(event.get_absolute_url() + '#signup')