def render_ticket(context, ticket): from p3 import forms user = context['request'].user if ticket.fare.ticket_type == 'conference': try: inst = ticket.p3_conference except: inst = None form = forms.FormTicket( instance=inst, initial={ 'ticket_name': ticket.name, }, prefix='t%d' % (ticket.id, ), single_day=ticket.fare.code[2] == 'D', ) if inst and inst.assigned_to: blocked = inst.assigned_to != user.email else: blocked = False elif ticket.fare.code in ('SIM01', ): try: inst = ticket.p3_conference_sim except: inst = None form = forms.FormTicketSIM( instance=inst, initial={ 'ticket_name': ticket.name, }, prefix='t%d' % (ticket.id, ), ) blocked = False elif ticket.fare.code.startswith('H'): # le instanze di TicketRoom devono esistere, ci pensa un listener a # crearle inst = ticket.p3_conference_room form = forms.FormTicketRoom( instance=inst, initial={ 'ticket_name': ticket.name, }, prefix='t%d' % (ticket.id, ), ) blocked = False else: form = forms.FormTicketPartner(instance=ticket, prefix='t%d' % (ticket.id, )) blocked = False ctx = Context(context) ctx.update({ 'ticket': ticket, 'form': form, 'user': user, 'blocked': blocked, }) return ctx
def ticket(request, tid): t = get_object_or_404(cmodels.Ticket, pk=tid) try: p3c = t.p3_conference except models.TicketConference.DoesNotExist: p3c = None assigned_to = None else: assigned_to = p3c.assigned_to if t.user != request.user: if assigned_to is None or assigned_to.lower( ) != request.user.email.lower(): raise http.Http404() if request.method == 'POST': if t.frozen: return http.HttpResponseForbidden() if 'refund' in request.POST: r = amodels.Refund.objects.create_from_orderitem( t.orderitem, reason=request.POST['refund'][:200]) t = cmodels.Ticket.objects.get(id=t.id) elif t.fare.ticket_type == 'conference': #print ('ticket name 0: %r' % t.name) # # MAL: TBD This code needs a serious refactoring. There # are too many weird cases addressed here, most of which # can be handled more generically by # autils.get_user_account_from_email() and # p3utils.assign_ticket_to_user(). # data = request.POST.copy() # We want to maximize the number of assigned tickets, and to do # this we discurage users from filling in tickets for others. # If the ticket is unassigned I'm forcing the name to be the same # of the profile. # # If the user is the one that bought the ticket and it's not assigning # it then for this POST I'll use the name of current user. if t.user == request.user and not data.get('assigned_to'): data['t%d-ticket_name' % t.id] = '%s %s' % (t.user.first_name, t.user.last_name) form = p3forms.FormTicket( instance=p3c, data=data, prefix='t%d' % (t.id, ), single_day=t.fare.code[2] == 'D', ) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) data = form.cleaned_data # first of all I'm fixing conference tickets... t.name = data['ticket_name'].strip() t.save() # ...later I take care of extras of tickets p3 x = form.save(commit=False) x.ticket = t if t.user != request.user: # only the owner can reassign a ticket x.assigned_to = assigned_to x.save() #print ('ticket name 1: %r' % t.name) if t.user == request.user: old = assigned_to or '' new = x.assigned_to or '' if old != new: if x.assigned_to: changed = _assign_ticket(t, x.assigned_to) if changed != x.assigned_to: log.info('ticket assigned to "%s" instead of "%s"', changed, x.assigned_to) x.assigned_to = changed x.save() else: log.info('ticket assigned to "%s"', x.assigned_to) else: log.info( 'ticket reclaimed (previously assigned to "%s")', assigned_to) _reset_ticket(t) # Assign to the buyer p3utils.assign_ticket_to_user(t, t.user) #print ('ticket name 2: %r' % t.name) if t.user != request.user and not request.user.first_name and not request.user.last_name and data[ 'ticket_name']: # the user has neither first or last name inthe progle (and also # it's not the person who bought the ticket). I can use the name # used for the ticket to fill the profile. try: f, l = data['ticket_name'].strip().split(' ', 1) except ValueError: f = data['ticket_name'].strip() l = '' request.user.first_name = f request.user.last_name = l request.user.save() #print ('ticket name 3: %r' % t.name) elif t.fare.code in ('SIM01', ): try: sim_ticket = t.p3_conference_sim except models.TicketSIM.DoesNotExist: sim_ticket = None form = p3forms.FormTicketSIM(instance=sim_ticket, data=request.POST, files=request.FILES, prefix='t%d' % (t.id, )) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) else: data = form.cleaned_data t.name = data['ticket_name'] t.save() x = form.save(commit=False) x.ticket = t x.save() elif t.fare.code.startswith('H'): room_ticket = t.p3_conference_room form = p3forms.FormTicketRoom(instance=room_ticket, data=request.POST, files=request.FILES, prefix='t%d' % (t.id, )) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) else: data = form.cleaned_data t.name = data['ticket_name'] t.save() x = form.save(commit=False) x.ticket = t x.save() else: form = p3forms.FormTicketPartner(instance=t, data=request.POST, prefix='t%d' % (t.id, )) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) form.save() # returning the rendering of the new ticket, so the caller can easily show it tpl = Template('{% load p3 %}{% render_ticket t %}') return http.HttpResponse(tpl.render(RequestContext(request, {'t': t})))
def ticket(request, tid): t = get_object_or_404(cmodels.Ticket, pk=tid) try: p3c = t.p3_conference except models.TicketConference.DoesNotExist: p3c = None assigned_to = None else: assigned_to = p3c.assigned_to if t.user != request.user: if assigned_to is None or assigned_to != request.user.email: raise http.Http404() if request.method == 'POST': if t.frozen: return http.HttpResponseForbidden() if 'refund' in request.POST: r = amodels.Refund.objects.create_from_orderitem( t.orderitem, reason=request.POST['refund'][:200]) t = cmodels.Ticket.objects.get(id=t.id) elif t.fare.ticket_type == 'conference': data = request.POST.copy() # vogliamo massimizzare il numero dei biglietti assegnati, e per # farlo scoraggiamo le persone nel compilare i biglietti di altri. # Se il biglietto non è assegnato lo forzo ad avere lo stesso nome # del profilo. # # Se l'utente è quello che ha comprato il biglietto e non lo sta # assegnando allora per questa POST utilizzo il nome dell'utente # corrente if t.user == request.user and not data.get('assigned_to'): data['t%d-ticket_name' % t.id] = '%s %s' % (t.user.first_name, t.user.last_name) form = p3forms.FormTicket( instance=p3c, data=data, prefix='t%d' % (t.id, ), single_day=t.fare.code[2] == 'D', ) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) data = form.cleaned_data # prima di tutto sistemo il ticket di conference... t.name = data['ticket_name'].strip() t.save() # ...poi penso alle funzionalità aggiuntive dei biglietti p3 x = form.save(commit=False) x.ticket = t if t.user != request.user: # solo il proprietario del biglietto può riassegnarlo x.assigned_to = assigned_to x.save() if t.user == request.user: old = assigned_to or '' new = x.assigned_to or '' if old != new: if x.assigned_to: changed = _assign_ticket(t, x.assigned_to) if changed != x.assigned_to: log.info('ticket assigned to "%s" instead of "%s"', changed, x.assigned_to) x.assigned_to = changed x.save() else: log.info('ticket assigned to "%s"', x.assigned_to) else: log.info( 'ticket reclaimed (previously assigned to "%s")', assigned_to) _reset_ticket(t) if t.user != request.user and not request.user.first_name and not request.user.last_name and data[ 'ticket_name']: # l'utente non ha, nel suo profilo, né il nome né il cognome (e # tra l'altro non è la persona che ha comprato il biglietto) # posso usare il nome che ha inserito per il biglietto nei dati # del profilo try: f, l = data['ticket_name'].strip().split(' ', 1) except ValueError: f = data['ticket_name'].strip() l = '' request.user.first_name = f request.user.last_name = l request.user.save() elif t.fare.code in ('SIM01', ): try: sim_ticket = t.p3_conference_sim except models.TicketSIM.DoesNotExist: sim_ticket = None form = p3forms.FormTicketSIM(instance=sim_ticket, data=request.POST, files=request.FILES, prefix='t%d' % (t.id, )) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) else: data = form.cleaned_data t.name = data['ticket_name'] t.save() x = form.save(commit=False) x.ticket = t x.save() elif t.fare.code.startswith('H'): room_ticket = t.p3_conference_room form = p3forms.FormTicketRoom(instance=room_ticket, data=request.POST, files=request.FILES, prefix='t%d' % (t.id, )) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) else: data = form.cleaned_data t.name = data['ticket_name'] t.save() x = form.save(commit=False) x.ticket = t x.save() else: form = p3forms.FormTicketPartner(instance=t, data=request.POST, prefix='t%d' % (t.id, )) if not form.is_valid(): return http.HttpResponseBadRequest(str(form.errors)) form.save() # restituisco il rendering del nuovo biglietto, così il chiamante può # mostrarlo facilmente tpl = Template('{% load p3 %}{% render_ticket t %}') return http.HttpResponse(tpl.render(RequestContext(request, {'t': t})))