Example #1
0
def reservation_form(request, **kwargs):  # kwargs przekazywanie z urls
    time_now = timezone.now()
    showtime_id = kwargs['showtime_id']
    showtime = Showtime.objects.get(showtime_id=showtime_id)
    # pass initial data to form https://www.geeksforgeeks.org/initial-form-data-django-forms/
    initial = {'showtime_id': showtime_id}

    r_form = forms.ReservationModelForm(initial=initial)
    client_form = forms.ClientModelForm()

    # lista zajetych siedzien
    taken_seats = Ticket.objects.filter(showtime_id=showtime_id).values_list(
        'seat_id', flat=True)

    s_form = forms.SeatForm(taken_seats=taken_seats)

    # siedzenia wolne
    seats = Seat.objects
    seats_row_a = seats.filter(row_number='A')
    seats_row_b = seats.filter(row_number='B')
    seats_row_c = seats.filter(row_number='C')
    seats_row_d = seats.filter(row_number='D')
    seats_row_e = seats.filter(row_number='E')
    seats_row_f = seats.filter(row_number='F')
    seats_row_g = seats.filter(row_number='G')
    seats_row_h = seats.filter(row_number='H')
    seats_row_i = seats.filter(row_number='I')
    seats_row_j = seats.filter(row_number='J')

    if request.POST:
        taken = []
        for i in request.POST.lists():
            if i[0].startswith('seats_row_'):
                taken.extend(i[1])

        r_form = forms.ReservationModelForm(request.POST)
        r_form.fields['showtime_id'].widget = r_form.fields[
            'showtime_id'].hidden_widget()
        client_form = forms.ClientModelForm(request.POST)

        if client_form.is_valid() and r_form.is_valid():
            request.session['taken'] = taken
            request.session['data'] = request.POST

            if len(taken) > 10:
                messages.add_message(
                    request, messages.ERROR,
                    'Możesz zarezerwować maksymalnie 10 miejsc!')
            else:
                return redirect('reservation-tickets-client')

    return render(request,
                  'client/rezerwacja_na_seans.html',
                  context={
                      'showtime_id': showtime_id,
                      'showtime': showtime,
                      'client_form': client_form,
                      'reservation_form': r_form,
                      'seats_row_a': seats_row_a,
                      'seats_row_b': seats_row_b,
                      'seats_row_c': seats_row_c,
                      'seats_row_d': seats_row_d,
                      'seats_row_e': seats_row_e,
                      'seats_row_f': seats_row_f,
                      'seats_row_g': seats_row_g,
                      'seats_row_h': seats_row_h,
                      'seats_row_i': seats_row_i,
                      'seats_row_j': seats_row_j,
                      'taken_seats': taken_seats,
                      's_form': s_form,
                      'time_now': time_now
                  })
Example #2
0
def ticket_types_client(request, **kwargs):
    if 'data' in request.session:
        taken = request.session.get('taken')
        data = request.session.get('data')
        showtime_id = data['showtime_id']

        paid = data['paid'] if data.get('paid') else ''
        confirmed = data['confirmed'] if data.get('confirmed') else ''

        reservation_initial = {
            'showtime_id': data['showtime_id'],
            'confirmed': confirmed,
            'paid': paid
        }

        client_initial = {
            'first_name': data['first_name'],
            'last_name': data['last_name'],
            'email': data['email'],
            'phone_number': data['phone_number']
        }

        showtime = Showtime.objects.get(showtime_id=showtime_id)

        r_form = forms.ReservationModelForm(initial=reservation_initial)
        r_form.fields['showtime_id'].widget = r_form.fields[
            'showtime_id'].hidden_widget()

        client_form = forms.ClientModelForm(initial=client_initial)
        client_form.fields['first_name'].widget = client_form.fields[
            'first_name'].hidden_widget()
        client_form.fields['last_name'].widget = client_form.fields[
            'last_name'].hidden_widget()
        client_form.fields['email'].widget = client_form.fields[
            'email'].hidden_widget()
        client_form.fields['phone_number'].widget = client_form.fields[
            'phone_number'].hidden_widget()

        ticket_formset = modelformset_factory(
            Ticket,
            fields=('seat_id', 'tickettype_id'),
            labels={
                'seat_id': '',
                'tickettype_id': 'Typ Biletu'
            },
            extra=len(taken),
            widgets={'seat_id': django.forms.Select(attrs={'hidden': ''})},
            max_num=10)

        ticket_form = ticket_formset(queryset=Ticket.objects.none(),
                                     initial=[{
                                         'seat_id': z
                                     } for z in taken])

        # pokazuje tylko typy biletów, które nie są usunięte
        for form in ticket_form:
            form.fields['tickettype_id'].queryset = TicketType.objects.filter(
                deleted=False)

        if request.POST:
            r_form = forms.ReservationModelForm(request.POST)
            client_form = forms.ClientModelForm(request.POST)
            ticket_form = ticket_formset(request.POST)

            # pokazuje tylko typy biletów, które nie są usunięte
            for form in ticket_form:
                form.fields[
                    'tickettype_id'].queryset = TicketType.objects.filter(
                        deleted=False)

            r_form.fields['showtime_id'].widget = r_form.fields[
                'showtime_id'].hidden_widget()
            client_form.fields['first_name'].widget = client_form.fields[
                'first_name'].hidden_widget()
            client_form.fields['last_name'].widget = client_form.fields[
                'last_name'].hidden_widget()
            client_form.fields['email'].widget = client_form.fields[
                'email'].hidden_widget()
            client_form.fields['phone_number'].widget = client_form.fields[
                'phone_number'].hidden_widget()

            if ticket_form.is_valid() and (client_form.is_valid()
                                           and r_form.is_valid()):
                request.session['formset_data'] = ticket_form.data

                return redirect('summary-client')

        return render(request,
                      'client/wybierz_typy_biletow.html',
                      context={
                          'taken': taken,
                          'ticket_form': ticket_form,
                          'reservation_form': r_form,
                          'client_form': client_form,
                          'client_initial': client_initial,
                          'reservation_initial': reservation_initial,
                          'showtime': showtime
                      })
    else:
        taken = ''
        ticket_form = ''
        r_form = ''
        client_form = ''
        reservation_initial = ''
        client_initial = ''
        showtime = ''
        return render(request,
                      'client/wybierz_typy_biletow.html',
                      context={
                          'taken': taken,
                          'ticket_form': ticket_form,
                          'reservation_form': r_form,
                          'client_form': client_form,
                          'client_initial': client_initial,
                          'reservation_initial': reservation_initial,
                          'showtime': showtime
                      })
Example #3
0
def summary_client(request, **kwargs):
    if 'formset_data' in request.session:

        formset_data = request.session['formset_data']

        # wyliczanie koncowej ceny do wyswietlenia userowi
        total_price = 0
        for k, v in formset_data.items():
            if k.endswith('tickettype_id'):
                total_price += TicketType.objects.get(ticket_id=v).price

        taken = request.session.get('taken')
        data = request.session.get('data')
        showtime_id = data['showtime_id']

        paid = data['paid'] if data.get('paid') else ''
        confirmed = data['confirmed'] if data.get('confirmed') else ''

        reservation_initial = {
            'showtime_id': data['showtime_id'],
            'confirmed': confirmed,
            'paid': paid
        }

        client_initial = {
            'first_name': data['first_name'],
            'last_name': data['last_name'],
            'email': data['email'],
            'phone_number': data['phone_number']
        }

        showtime = Showtime.objects.get(showtime_id=showtime_id)

        r_form = forms.ReservationModelForm(initial=reservation_initial)
        r_form.fields['showtime_id'].widget = r_form.fields[
            'showtime_id'].hidden_widget()

        client_form = forms.ClientModelForm(initial=client_initial)
        client_form.fields['first_name'].widget = client_form.fields[
            'first_name'].hidden_widget()
        client_form.fields['last_name'].widget = client_form.fields[
            'last_name'].hidden_widget()
        client_form.fields['email'].widget = client_form.fields[
            'email'].hidden_widget()
        client_form.fields['phone_number'].widget = client_form.fields[
            'phone_number'].hidden_widget()

        ticket_formset = modelformset_factory(
            Ticket,
            fields=('seat_id', 'tickettype_id'),
            labels={
                'seat_id': '',
                'tickettype_id': ''
            },
            extra=len(taken),
            widgets={
                'seat_id': django.forms.Select(attrs={'hidden': ''}),
                'tickettype_id': django.forms.Select(attrs={'hidden': ''})
            },
            max_num=10)

        ticket_form = ticket_formset(queryset=Ticket.objects.none(),
                                     data=formset_data)

        db_ticket_types = TicketType.objects.all()

        if request.POST:
            r_form = forms.ReservationModelForm(request.POST)
            client_form = forms.ClientModelForm(request.POST)
            ticket_form = ticket_formset(request.POST)

            r_form.fields['showtime_id'].widget = r_form.fields[
                'showtime_id'].hidden_widget()

            client_form.fields['first_name'].widget = client_form.fields[
                'first_name'].hidden_widget()
            client_form.fields['last_name'].widget = client_form.fields[
                'last_name'].hidden_widget()
            client_form.fields['email'].widget = client_form.fields[
                'email'].hidden_widget()
            client_form.fields['phone_number'].widget = client_form.fields[
                'phone_number'].hidden_widget()

            if ticket_form.is_valid() and (client_form.is_valid()
                                           and r_form.is_valid()):
                showtime = Showtime.objects.get(
                    showtime_id=showtime_id)  # obiekt seansu
                client = client_form.save()
                reservation = r_form.save(commit=False)
                reservation.client_id = client

                # https://docs.djangoproject.com/en/3.0/topics/db/examples/many_to_many/#many-to-many-relationships
                instances = ticket_form.save(commit=False)

                # get total price
                # zapisuje cene biletu, aby w przy zmiane cennika ceny juz zakupionych biletow pozostaly takie same
                total_price = 0
                for instance in instances:
                    price = TicketType.objects.get(
                        ticket_id=instance.tickettype_id_id).price
                    total_price += price
                    instance.price = price

                # wyslij maila do klienta
                reservation.confirmation_email = True

                reservation.cost = total_price
                reservation.save()

                for instance in instances:
                    instance.client_id = client
                    instance.showtime_id = showtime
                    instance.save()
                    reservation.ticket_id.add(instance)

                r_form.save_m2m()

                if reservation.confirmation_email:
                    # https://simpleisbetterthancomplex.com/tutorial/2016/06/13/how-to-send-email.html
                    # https://stackoverflow.com/questions/29466796/sending-a-html-email-in-django
                    html_mail = loader.render_to_string(
                        template_name='client/rezerwacja/email.html',
                        context={
                            'first_name': client.first_name,
                            'last_name': client.last_name,
                            'reservation': reservation,
                            'domain': request.META['HTTP_HOST']
                        })

                    mail = send_mail(subject='Potwierdzenie rezerwacji',
                                     message='',
                                     from_email=EMAIL_HOST_USER,
                                     recipient_list=[
                                         client.email,
                                     ],
                                     fail_silently=True,
                                     html_message=html_mail)
                    if mail:
                        messages.add_message(
                            request, messages.SUCCESS,
                            'Rezerwacja została pomyślnie utworzona, na twój adres'
                            'mailowy została wysłana wiadomość z potwierdzeniem.'
                            'Jeśli nie potwierdzisz rezerwacji w ciągu 30 minut, '
                            'to zostanie ona usunięta z systemu')
                    else:
                        confirm_url = request.META['HTTP_HOST'] + reverse(
                            'reservation-accept-client',
                            kwargs={
                                'id':
                                str(reservation.reservation_confirmation_code)
                            })
                        reject_url = request.META['HTTP_HOST'] + reverse(
                            'reservation-deny-client',
                            kwargs={
                                'id':
                                str(reservation.reservation_confirmation_code)
                            })

                        messages.add_message(
                            request, messages.ERROR,
                            'Wystąpił problem z wysłaniem wiadomości. W celu potwierdzenia rezerwacji prosimy przejść pod adres\n'
                            + confirm_url +
                            '\nW celu odzucenia rezerwacji prosimy przejść pod adres\n'
                            + reject_url)
                request.session.flush()
                return redirect(
                    reverse('movie-details-client',
                            kwargs={'pk': str(showtime.movie_id.movie_id)}))

        return render(request,
                      'client/podsumowanie.html',
                      context={
                          'taken': taken,
                          'ticket_form': ticket_form,
                          'reservation_form': r_form,
                          'client_form': client_form,
                          'client_initial': client_initial,
                          'reservation_initial': reservation_initial,
                          'showtime': showtime,
                          'total': total_price,
                          'db_ticket_types': db_ticket_types
                      })
    else:
        taken = ''
        ticket_form = ''
        r_form = ''
        client_form = ''
        reservation_initial = ''
        client_initial = ''
        showtime = ''
        db_ticket_types = ''
        return render(request,
                      'client/podsumowanie.html',
                      context={
                          'taken': taken,
                          'ticket_form': ticket_form,
                          'reservation_form': r_form,
                          'client_form': client_form,
                          'client_initial': client_initial,
                          'reservation_initial': reservation_initial,
                          'showtime': showtime,
                          'db_ticket_types': db_ticket_types
                      })
Example #4
0
def reservation_update(request, **kwargs):
    reservation_id = kwargs['reservation_id']
    reservation = models.Reservation.objects.get(reservation_id=reservation_id)

    # bilety przekazywane jako initial do formset factory
    tickets_initial = reservation.ticket_id.values('seat_id', 'tickettype_id')

    showtime_id = reservation.showtime_id.showtime_id

    reservation_initial = {'showtime_id': reservation.showtime_id.showtime_id,
                           'confirmed': reservation.confirmed,
                           'paid': reservation.paid}
    r_form = forms.ReservationModelForm(initial=reservation_initial)

    client_initial = {'first_name': reservation.client_id.first_name,
                      'last_name': reservation.client_id.last_name,
                      'email': reservation.client_id.email,
                      'phone_number': reservation.client_id.phone_number}
    client_form = forms.ClientModelForm(initial=client_initial)

    ticket_formset = modelformset_factory(models.Ticket,
                                          fields=('seat_id', 'tickettype_id'),
                                          labels={'seat_id': 'Miejsce',
                                                  'tickettype_id': 'Typ Biletu', },
                                          extra=len(reservation.ticket_id.values()),
                                          max_num=10)

    ticket_form = ticket_formset(queryset=models.Ticket.objects.none(), initial=tickets_initial)

    taken_seats = models.Ticket.objects.filter(showtime_id=showtime_id).values('seat_id')
    free_seats = models.Seat.objects.exclude(seat_id__in=taken_seats)

    # pokazuje tylko typy biletów, które nie są usunięte, oraz wolne miejsca
    for form in ticket_form:
        form.fields['tickettype_id'].queryset = models.TicketType.objects.filter(deleted=False)
        form.fields['seat_id'].queryset = free_seats

    if request.POST:
        if 'ticket_num' in request.POST:
            ticket_formset = modelformset_factory(models.Ticket,
                                                  fields=('seat_id', 'tickettype_id'),
                                                  labels={'seat_id': 'Miejsce',
                                                          'tickettype_id': 'Typ Biletu', },
                                                  extra=int(request.POST['ticket_select']),
                                                  max_num=60)

            ticket_form = ticket_formset(queryset=models.Ticket.objects.none(), initial=tickets_initial)

            # pokazuje tylko typy biletów, które nie są usunięte, oraz wolne miejsca
            for form in ticket_form:
                form.fields['tickettype_id'].queryset = models.TicketType.objects.filter(deleted=False)
                form.fields['seat_id'].queryset = free_seats
        else:
            # current objects
            reservation_object = get_object_or_404(models.Reservation, reservation_id=reservation_id)
            client_object = get_object_or_404(models.Client, client_id=reservation.client_id.client_id)

            r_form = forms.ReservationModelForm(request.POST, instance=reservation_object)
            r_form.fields['showtime_id'].widget = r_form.fields['showtime_id'].hidden_widget()

            ticket_form = ticket_formset(request.POST)
            client_form = forms.ClientModelForm(request.POST, instance=client_object)

            # pokazuje tylko typy biletów, które nie są usunięte, oraz wolne miejsca
            for form in ticket_form:
                form.fields['tickettype_id'].queryset = models.TicketType.objects.filter(deleted=False)
                form.fields['seat_id'].queryset = free_seats

            if ticket_form.is_valid() and (client_form.is_valid() and r_form.is_valid()):
                showtime = models.Showtime.objects.get(showtime_id=showtime_id)  # obiekt seansu
                client = client_form.save()
                reservation = r_form.save(commit=False)
                reservation.client_id = client

                # https://docs.djangoproject.com/en/3.0/topics/db/examples/many_to_many/#many-to-many-relationships
                instances = ticket_form.save(commit=False)

                # get total price
                # zapisuje cene biletu, aby w przy zmiane cennika ceny juz zakupionych biletow pozostaly takie same
                total_price = 0
                for instance in instances:
                    price = models.TicketType.objects.get(ticket_id=instance.tickettype_id_id).price
                    total_price += price
                    instance.price = price

                reservation.cost = total_price
                reservation.save()

                tickets = models.Ticket.objects.filter(reservation__reservation_id=reservation_id)

                # usuwa wszytkie bilety powiązane z rezerwacją, oraz rezerwację
                for t in tickets.iterator():
                    t.delete()

                taken_seats = models.Ticket.objects.filter(showtime_id=showtime_id).values_list('seat_id', flat=True)
                for instance in instances:
                    if instance.seat_id.seat_id not in taken_seats:
                        instance.client_id = client
                        instance.showtime_id = showtime
                        instance.save()
                        reservation.ticket_id.add(instance)
                    else:
                        try:
                            raise ValueError('seats taken')
                        except Exception:
                            messages.add_message(request, messages.ERROR, 'Wybrane miejsca zostały zarezerwowane przez '
                                                                          'innego klienta. Spróbuj ponownie. Miejsce ' +
                                                 str(instance.seat_id.seat_number) +
                                                 ' Rząd: ' + str(instance.seat_id.row_number))
                            return redirect(
                                reverse('movie-details-client', kwargs={'pk': str(showtime.movie_id.movie_id)}))
                r_form.save_m2m()

                # https://simpleisbetterthancomplex.com/tutorial/2016/06/13/how-to-send-email.html
                # https://stackoverflow.com/questions/29466796/sending-a-html-email-in-django
                if reservation.confirmation_email and not reservation.confirmed:
                    html_mail = loader.render_to_string(template_name='client/rezerwacja/email.html',
                                                        context={'first_name': client.first_name,
                                                                 'last_name': client.last_name,
                                                                 'reservation': reservation,
                                                                 'domain': request.META['HTTP_HOST']})

                    mail = send_mail(subject='Potwierdzenie rezerwacji na seans ' + showtime.movie_id.title,
                                     message='',
                                     from_email=EMAIL_HOST_USER,
                                     recipient_list=[client.email, ],
                                     fail_silently=True,
                                     html_message=html_mail)
                    if mail:
                        messages.add_message(request, messages.SUCCESS,
                                             'Rezerwacja została pomyślnie zaktualizowana, na adres'
                                             'mailowy klienta została wysłana wiadomość z potwierdzeniem.'
                                             'Jeśli klient nie potwierdzi rezerwacji w ciągu 30 minut, '
                                             'to zostanie ona usunięta z systemu')
                    else:
                        messages.add_message(request,
                                             messages.ERROR,
                                             'Wystąpił problem z wysłaniem wiadomości. Skontaktuj się z klientem w celu '
                                             'potwierdzenia lub odrzucenia rezerwacji.')
                else:
                    messages.add_message(request, messages.SUCCESS,
                                         'Rezerwacja została pomyślnie zaktualizowana.\n '
                                         'Nie została zaznaczona opcja wysyłki wiadomości email do klienta.')

                return redirect(reverse('showtime-details-worker', kwargs={'pk': str(showtime.showtime_id)}))

    return render(request, 'worker/rezerwacje/edytuj-rezerwacje.html', context={'reservation': reservation,
                                                                                'r_form': r_form,
                                                                                'client_form': client_form,
                                                                                'ticket_form': ticket_form,
                                                                                'ticket_number': [x for x in
                                                                                                  range(1, 61)], })
Example #5
0
def summary(request, **kwargs):
    if 'formset_data' in request.session:
        formset_data = request.session['formset_data']

        # wyliczanie koncowej ceny do wyswietlenia userowi
        total_price = 0
        for k, v in formset_data.items():
            if k.endswith('tickettype_id'):
                total_price += models.TicketType.objects.get(ticket_id=v).price

        taken = request.session.get('taken')
        data = request.session.get('data')
        showtime_id = data['showtime_id']

        # paid = data['paid'] if data.get('paid') else ''
        # confirmed = data['confirmed'] if data.get('confirmed') else ''
        # confirmation_email = data['confirmation_email'] if data.get('confirmation_email') else ''

        reservation_initial = {'showtime_id': data['showtime_id'],
                               'confirmed': False,
                               'paid': False,
                               'confirmation_email': False}

        client_initial = {'first_name': data['first_name'],
                          'last_name': data['last_name'],
                          'email': data['email'],
                          'phone_number': data['phone_number']}

        showtime = models.Showtime.objects.get(showtime_id=showtime_id)

        r_form = forms.ReservationModelForm(initial=reservation_initial)
        r_form.fields['showtime_id'].widget = r_form.fields['showtime_id'].hidden_widget()

        client_form = forms.ClientModelForm(initial=client_initial)
        client_form.fields['first_name'].widget = client_form.fields['first_name'].hidden_widget()
        client_form.fields['last_name'].widget = client_form.fields['last_name'].hidden_widget()
        client_form.fields['email'].widget = client_form.fields['email'].hidden_widget()
        client_form.fields['phone_number'].widget = client_form.fields['phone_number'].hidden_widget()

        # r_form.fields['confirmed'].widget = r_form.fields['confirmed'].hidden_widget()
        # r_form.fields['paid'].widget = r_form.fields['paid'].hidden_widget()
        # r_form.fields['confirmation_email'].widget = r_form.fields['confirmation_email'].hidden_widget()

        ticket_formset = modelformset_factory(models.Ticket,
                                              fields=('seat_id', 'tickettype_id'),
                                              labels={'seat_id': '',
                                                      'tickettype_id': ''},
                                              extra=len(taken),
                                              widgets={'seat_id': django.forms.Select(attrs={'hidden': '', }),
                                                       'tickettype_id': django.forms.Select(attrs={'hidden': '', })},
                                              max_num=60)  # pracownik moze zarezerwowac cala sale

        ticket_form = ticket_formset(queryset=models.Ticket.objects.none(), data=formset_data)

        db_ticket_types = models.TicketType.objects.all()

        seats = []
        for form in ticket_form:
            seats.append(models.Seat.objects.get(seat_id=form['seat_id'].value()))

        if request.POST:
            r_form = forms.ReservationModelForm(request.POST)
            client_form = forms.ClientModelForm(request.POST)
            ticket_form = ticket_formset(request.POST)

            if ticket_form.is_valid() and (client_form.is_valid() and r_form.is_valid()):
                showtime = models.Showtime.objects.get(showtime_id=showtime_id)  # obiekt seansu
                client = client_form.save()
                reservation = r_form.save(commit=False)
                reservation.client_id = client

                # https://docs.djangoproject.com/en/3.0/topics/db/examples/many_to_many/#many-to-many-relationships
                instances = ticket_form.save(commit=False)

                # get total price
                # zapisuje cene biletu, aby w przy zmiane cennika ceny juz zakupionych biletow pozostaly takie same
                total_price = 0
                for instance in instances:
                    price = models.TicketType.objects.get(ticket_id=instance.tickettype_id_id).price
                    total_price += price
                    instance.price = price

                reservation.cost = total_price
                reservation.save()
                taken_seats = models.Ticket.objects.filter(showtime_id=showtime_id).values_list('seat_id', flat=True)
                for instance in instances:
                    if instance.seat_id.seat_id not in taken_seats:
                        instance.client_id = client
                        instance.showtime_id = showtime
                        instance.save()
                        reservation.ticket_id.add(instance)
                    else:
                        try:
                            raise ValueError('seats taken')
                        except Exception:
                            messages.add_message(request, messages.ERROR, 'Wybrane miejsca zostały zarezerwowane przez '
                                                                          'innego klienta. Spróbuj ponownie. Miejsce ' +
                                                 str(instance.seat_id.seat_number) +
                                                 ' Rząd: ' + str(instance.seat_id.row_number))
                            return redirect(
                                reverse('movie-details-client', kwargs={'pk': str(showtime.movie_id.movie_id)}))
                        finally:
                            request.session.pop('taken')
                            request.session.pop('data')
                            request.session.pop('formset_data')

                r_form.save_m2m()
                # https://simpleisbetterthancomplex.com/tutorial/2016/06/13/how-to-send-email.html
                # https://stackoverflow.com/questions/29466796/sending-a-html-email-in-django
                if reservation.confirmation_email and not reservation.confirmed:
                    html_mail = loader.render_to_string(template_name='client/rezerwacja/email.html',
                                                        context={'first_name': client.first_name,
                                                                 'last_name': client.last_name,
                                                                 'reservation': reservation,
                                                                 'domain': request.META['HTTP_HOST']})

                    mail = send_mail(subject='Potwierdzenie rezerwacji na seans ' + showtime.movie_id.title,
                                     message='',
                                     from_email=EMAIL_HOST_USER,
                                     recipient_list=[client.email, ],
                                     fail_silently=True,
                                     html_message=html_mail)
                    if mail:
                        messages.add_message(request, messages.SUCCESS,
                                             'Rezerwacja została pomyślnie utworzona, na adres '
                                             'mailowy klienta została wysłana wiadomość z potwierdzeniem. '
                                             'Jeśli klient nie potwierdzi rezerwacji w ciągu 30 minut, '
                                             'to zostanie ona usunięta z systemu')
                    else:

                        messages.add_message(request,
                                             messages.ERROR,
                                             'Wystąpił problem z wysłaniem wiadomości. Skontaktuj się z klientem w celu '
                                             'potwierdzenia lub odrzucenia rezerwacji.')
                else:
                    messages.add_message(request, messages.SUCCESS,
                                         'Rezerwacja została pomyślnie utworzona.\n '
                                         'Nie została zaznaczona opcja wysyłki wiadomości email do klienta.')
                request.session.pop('taken')
                request.session.pop('data')
                request.session.pop('formset_data')
                return redirect(reverse('showtime-details-worker', kwargs={'pk': showtime_id}))

        return render(request, 'worker/rezerwacje/podsumowanie.html', context={'taken': taken,
                                                                               'ticket_form': ticket_form,
                                                                               'reservation_form': r_form,
                                                                               'client_form': client_form,
                                                                               'client_initial': client_initial,
                                                                               'reservation_initial': reservation_initial,
                                                                               'showtime': showtime,
                                                                               'total': total_price,
                                                                               'db_ticket_types': db_ticket_types,
                                                                               'seats': seats})
    else:
        taken = ''
        ticket_form = ''
        r_form = ''
        client_form = ''
        reservation_initial = ''
        client_initial = ''
        showtime = ''
        total_price = ''
        seats = ''
        return render(request, 'worker/rezerwacje/podsumowanie.html', context={'taken': taken,
                                                                               'ticket_form': ticket_form,
                                                                               'reservation_form': r_form,
                                                                               'client_form': client_form,
                                                                               'client_initial': client_initial,
                                                                               'reservation_initial': reservation_initial,
                                                                               'showtime': showtime,
                                                                               'total': total_price,
                                                                               'seats': seats})
Example #6
0
def ticket_types_worker(request, **kwargs):
    if 'data' in request.session:
        taken = request.session.get('taken')
        data = request.session.get('data')
        showtime_id = data['showtime_id']

        paid = data['paid'] if data.get('paid') else ''
        confirmed = data['confirmed'] if data.get('confirmed') else ''
        confirmation_email = data['confirmation_email'] if data.get('confirmation_email') else ''

        reservation_initial = {'showtime_id': data['showtime_id'],
                               'confirmed': confirmed,
                               'paid': paid,
                               'confirmation_email': confirmation_email}

        client_initial = {'first_name': data['first_name'],
                          'last_name': data['last_name'],
                          'email': data['email'],
                          'phone_number': data['phone_number']}

        showtime = models.Showtime.objects.get(showtime_id=showtime_id)

        r_form = forms.ReservationModelForm(initial=reservation_initial)
        r_form.fields['showtime_id'].widget = r_form.fields['showtime_id'].hidden_widget()

        client_form = forms.ClientModelForm(initial=client_initial)
        client_form.fields['first_name'].widget = client_form.fields['first_name'].hidden_widget()
        client_form.fields['last_name'].widget = client_form.fields['last_name'].hidden_widget()
        client_form.fields['email'].widget = client_form.fields['email'].hidden_widget()
        client_form.fields['phone_number'].widget = client_form.fields['phone_number'].hidden_widget()

        r_form.fields['confirmed'].widget = r_form.fields['confirmed'].hidden_widget()
        r_form.fields['paid'].widget = r_form.fields['paid'].hidden_widget()
        r_form.fields['confirmation_email'].widget = r_form.fields['confirmation_email'].hidden_widget()

        ticket_formset = modelformset_factory(models.Ticket,
                                              fields=('seat_id', 'tickettype_id'),
                                              labels={'seat_id': '',
                                                      'tickettype_id': 'Typ Biletu'},
                                              extra=len(taken),
                                              widgets={'seat_id': django.forms.Select(attrs={'hidden': ''})},
                                              max_num=60)  # pracownik moze zarezerwowac cala sale

        ticket_form = ticket_formset(queryset=models.Ticket.objects.none(), initial=[{'seat_id': z} for z in taken])

        seats = []

        # pokazuje tylko typy biletów, które nie są usunięte
        # https://simpleisbetterthancomplex.com/questions/2017/03/22/how-to-dynamically-filter-modelchoices-queryset-in-a-modelform.html
        for form in ticket_form:
            form.fields['tickettype_id'].queryset = models.TicketType.objects.filter(deleted=False)
            seats.append(models.Seat.objects.get(seat_id=form.initial['seat_id']))

        if request.POST:
            r_form = forms.ReservationModelForm(request.POST)
            client_form = forms.ClientModelForm(request.POST)
            ticket_form = ticket_formset(request.POST)

            # pokazuje tylko typy biletów, które nie są usunięte
            for form in ticket_form:
                form.fields['tickettype_id'].queryset = models.TicketType.objects.filter(deleted=False)

            if ticket_form.is_valid() and (client_form.is_valid() and r_form.is_valid()):
                request.session['formset_data'] = ticket_form.data

                return redirect('summary-worker')

            client_form.fields['first_name'].widget = client_form.fields['first_name'].hidden_widget()
            client_form.fields['last_name'].widget = client_form.fields['last_name'].hidden_widget()
            client_form.fields['email'].widget = client_form.fields['email'].hidden_widget()
            client_form.fields['phone_number'].widget = client_form.fields['phone_number'].hidden_widget()

            r_form.fields['showtime_id'].widget = r_form.fields['showtime_id'].hidden_widget()
            r_form.fields['confirmed'].widget = r_form.fields['confirmed'].hidden_widget()
            r_form.fields['paid'].widget = r_form.fields['paid'].hidden_widget()
            r_form.fields['confirmation_email'].widget = r_form.fields['confirmation_email'].hidden_widget()

        return render(request, 'worker/rezerwacje/wybierz_typy_biletow.html', context={'taken': taken,
                                                                                       'ticket_form': ticket_form,
                                                                                       'reservation_form': r_form,
                                                                                       'client_form': client_form,
                                                                                       'client_initial': client_initial,
                                                                                       'reservation_initial': reservation_initial,
                                                                                       'showtime': showtime,
                                                                                       'seats': seats})
    else:
        taken = ''
        ticket_form = ''
        r_form = ''
        client_form = ''
        reservation_initial = ''
        client_initial = ''
        showtime = ''
        seats = ''
        return render(request, 'worker/rezerwacje/wybierz_typy_biletow.html', context={'taken': taken,
                                                                                       'ticket_form': ticket_form,
                                                                                       'reservation_form': r_form,
                                                                                       'client_form': client_form,
                                                                                       'client_initial': client_initial,
                                                                                       'reservation_initial': reservation_initial,
                                                                                       'showtime': showtime,
                                                                                       'seats': seats})
Example #7
0
def reservation_form(request, **kwargs):  # kwargs przekazywanie z urls
    showtime_id = kwargs['showtime_id']
    # pass initial data to form https://www.geeksforgeeks.org/initial-form-data-django-forms/
    initial = {'showtime_id': showtime_id}

    r_form = forms.ReservationModelForm(initial=initial)
    client_form = forms.ClientModelForm()

    # lista zajetych siedzien
    taken_seats = models.Ticket.objects.filter(showtime_id=showtime_id).values_list('seat_id', flat=True)

    s_form = forms.SeatForm(taken_seats=taken_seats)

    # ukrycie opcji potwierdzenia/oplacenia
    r_form.fields['confirmed'].widget = r_form.fields['confirmed'].hidden_widget()
    r_form.fields['paid'].widget = r_form.fields['paid'].hidden_widget()
    r_form.fields['confirmation_email'].widget = r_form.fields['confirmation_email'].hidden_widget()
    r_form.fields['showtime_id'].widget = r_form.fields['showtime_id'].hidden_widget()

    # siedzenia wolne
    seats = models.Seat.objects

    seats_row_a = seats.filter(row_number='A')
    seats_row_b = seats.filter(row_number='B')
    seats_row_c = seats.filter(row_number='C')
    seats_row_d = seats.filter(row_number='D')
    seats_row_e = seats.filter(row_number='E')
    seats_row_f = seats.filter(row_number='F')
    seats_row_g = seats.filter(row_number='G')
    seats_row_h = seats.filter(row_number='H')
    seats_row_i = seats.filter(row_number='I')
    seats_row_j = seats.filter(row_number='J')

    if request.POST:
        taken = []
        for i in request.POST.lists():
            if i[0].startswith('seats_row_'):
                taken.extend(i[1])

        r_form = forms.ReservationModelForm(request.POST)
        client_form = forms.ClientModelForm(request.POST)

        # ukrycie opcji potwierdzenia/oplacenia
        r_form.fields['confirmed'].widget = r_form.fields['confirmed'].hidden_widget()
        r_form.fields['paid'].widget = r_form.fields['paid'].hidden_widget()
        r_form.fields['confirmation_email'].widget = r_form.fields['confirmation_email'].hidden_widget()
        r_form.fields['showtime_id'].widget = r_form.fields['showtime_id'].hidden_widget()

        if client_form.is_valid() and r_form.is_valid():
            request.session['taken'] = taken
            request.session['data'] = request.POST

            # jesli nie wybrano miejsc
            if len(taken) == 0:
                messages.add_message(request, messages.ERROR, 'Nie wybrano żadnych miejsc!')
            else:
                return redirect('reservation-tickets-worker')

    return render(request, 'worker/rezerwacje/dodaj_rezerwacje.html', context={'showtime_id': showtime_id,
                                                                               'client_form': client_form,
                                                                               'reservation_form': r_form,
                                                                               'seats_row_a': seats_row_a,
                                                                               'seats_row_b': seats_row_b,
                                                                               'seats_row_c': seats_row_c,
                                                                               'seats_row_d': seats_row_d,
                                                                               'seats_row_e': seats_row_e,
                                                                               'seats_row_f': seats_row_f,
                                                                               'seats_row_g': seats_row_g,
                                                                               'seats_row_h': seats_row_h,
                                                                               'seats_row_i': seats_row_i,
                                                                               'seats_row_j': seats_row_j,
                                                                               'taken_seats': taken_seats,
                                                                               's_form': s_form})
Example #8
0
def reservation_form(request, **kwargs):  # kwargs przekazywanie z urls
    showtime_id = kwargs['showtime_id']
    # pass initial data to form https://www.geeksforgeeks.org/initial-form-data-django-forms/
    initial = {'showtime_id': showtime_id}

    r_form = forms.ReservationModelForm(initial=initial)
    client_form = forms.ClientModelForm()

    # lista zajetych siedzien
    taken_seats = models.Ticket.objects.filter(
        showtime_id=showtime_id).values_list('seat_id', flat=True)

    s_form = forms.SeatForm(taken_seats=taken_seats)

    # siedzenia wolne
    seats = models.Seat.objects

    seats_row_a = seats.filter(row_number='A')
    seats_row_b = seats.filter(row_number='B')
    seats_row_c = seats.filter(row_number='C')
    seats_row_d = seats.filter(row_number='D')
    seats_row_e = seats.filter(row_number='E')
    seats_row_f = seats.filter(row_number='F')
    seats_row_g = seats.filter(row_number='G')
    seats_row_h = seats.filter(row_number='H')
    seats_row_i = seats.filter(row_number='I')
    seats_row_j = seats.filter(row_number='J')

    if request.POST:
        taken = []
        for i in request.POST.lists():
            if i[0].startswith('seats_row_'):
                taken.extend(i[1])

        r_form = forms.ReservationModelForm(request.POST)
        client_form = forms.ClientModelForm(request.POST)

        if client_form.is_valid() and r_form.is_valid():
            request.session['taken'] = taken
            request.session['data'] = request.POST

            return redirect('reservation-tickets-worker')

    return render(request,
                  'worker/rezerwacje/dodaj_rezerwacje.html',
                  context={
                      'showtime_id': showtime_id,
                      'client_form': client_form,
                      'reservation_form': r_form,
                      'seats_row_a': seats_row_a,
                      'seats_row_b': seats_row_b,
                      'seats_row_c': seats_row_c,
                      'seats_row_d': seats_row_d,
                      'seats_row_e': seats_row_e,
                      'seats_row_f': seats_row_f,
                      'seats_row_g': seats_row_g,
                      'seats_row_h': seats_row_h,
                      'seats_row_i': seats_row_i,
                      'seats_row_j': seats_row_j,
                      'taken_seats': taken_seats,
                      's_form': s_form
                  })