示例#1
0
def register_participants(request, event=None, id=None):
	from participants.models import Participant
	from bookings.models import BookingLine
	from events.models import Event
	from vouchers.models import Voucher
	from browsersessions.models import BrowserSession
	from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
	from django.core.urlresolvers import reverse
	from django.http import HttpResponseRedirect




	try:
		event = Event.objects.get_bookable(request.session['event_id'])
	except Exception as e:
		raise SuspiciousOperation('invalid event', e)

	stages = RegistrationStages()
	stages.set_active(RegistrationStages.STAGE_PARTICIPANT)

	dbsession = BrowserSession.objects.get_session(request.session.session_key)

	entered_bookinglines = BookingLine.objects.filter(
			browser_session__name=request.session.session_key).exists()
	if entered_bookinglines:
		has_bookinglines = True
	else:
		has_bookinglines = False

	form = None
	instance_participant = None
	voucher = None

	instance_bookingline = None

	form_kwargs_participant = {}
	form_kwargs_bookingline = {}

	form_kwargs_bookingline['dbsession'] = dbsession;

	if id:
		try:
			instance_bookingline = BookingLine.objects.get(pk=id)
		except:
			# this happens if a BL is is deleted while it is in the edit
			id = None

	if instance_bookingline:
		form_kwargs_bookingline['instance'] = instance_bookingline

		instance_participant = instance_bookingline.participant
		form_kwargs_participant['instance'] = instance_participant

		if instance_bookingline.voucher:
			form_kwargs_bookingline['voucher_code'] = str(instance_bookingline.voucher)

	if request.method == 'POST':

		form_participant = ParticipantForm(request.POST,prefix='pp',**form_kwargs_participant)
		form_bookingline = BookingLineForm(request.POST,prefix='bl',**form_kwargs_bookingline)

		if form_bookingline.is_valid():
			"""does not mean that the voucher is valid: let us check that first
			"""
			voucher = None
			voucher_code = form_bookingline.cleaned_data.get('voucher_code')
			if voucher_code:
				try:
					voucher = Voucher.objects.get_voucher_by_code(event, voucher_code)
					if voucher.used_by and voucher.used_by <> instance:
						# TODO: "does not exist" if not in the same session to obscure codes?
						form_bookingline.add_error('voucher_code', 'This voucher is already in use')
				except Voucher.DoesNotExist:
					form_bookingline.add_error('voucher_code', 'This voucher code does not exist')



			""" now the form can be checked again and voucher can be used """


		if form_bookingline.is_valid() and form_participant.is_valid():

			first_name = form_participant.cleaned_data.get('first_name')
			last_name = form_participant.cleaned_data.get('last_name')
			email = form_participant.cleaned_data.get('email')
			mail_option = form_participant.cleaned_data.get('mail_option')

			ticket_type = form_bookingline.cleaned_data.get('ticket_type')

			if not instance_bookingline:
				instance_bookingline = BookingLine()
				instance_participant = Participant()
			else:
				instance_participant = instance_bookingline.participant

			instance_participant.first_name = first_name
			instance_participant.last_name = last_name
			instance_participant.email = email
			instance_participant.mail_option = mail_option
			instance_participant.save()

			instance_bookingline.participant = instance_participant
			instance_bookingline.ticket_type = ticket_type
			instance_bookingline.browser_session = dbsession
			instance_bookingline.voucher = voucher
			instance_bookingline.save()


			# bounce back to an empty form after saving
			bounce_target = reverse('register_participants')
			return HttpResponseRedirect(bounce_target)

		context =  {
			'form_pp': form_participant,
			'form_bl': form_bookingline,
			'event': event,
			'has_bookinglines': has_bookinglines,
			'stages': stages.as_list(),
			'has_vat': True,
		}
		return render(request, 'ewtisite/register_participants.html', context)

	form_participant = ParticipantForm(prefix='pp',**form_kwargs_participant)
	form_bookingline = BookingLineForm(prefix='bl',**form_kwargs_bookingline)

	context =  {
		'form_pp': form_participant,
		'form_bl': form_bookingline,
		'has_bookinglines': has_bookinglines,
		'participant': instance_participant,
		'stages': stages.as_list(),
		'has_vat': True,
	}

	return render(request, 'ewtisite/register_participants.html', context)