Example #1
0
def public_sell(request):
    error = ''
    success = False
    liu_id_form = LiuIDForm(request.POST or None)
    ticket_type_form = PublicTicketTypeForm(request.POST or None)

    if liu_id_form.is_valid() and ticket_type_form.is_valid():
        try:
            term = liu_id_form.cleaned_data['liu_id']

            try:
                person = Person.objects.search(term=term)

                if person.invoice_set.exists():
                    raise InvoiceExists()

            except Person.DoesNotExist:
                client = KOBRAClient(settings.KOBRA_USER, settings.KOBRA_KEY)
                student = client.get_student(term)
                visitor = Visitor.objects.create(**get_kwargs(student))
                person = visitor.person_ptr

            invoice = Invoice(person=person, is_verified=False)
            invoice.generate_data()
            invoice.save()

            ticket_type_ids = ticket_type_form.cleaned_data.get('ticket_type')

            for ticket_type_id in ticket_type_ids:
                ticket_type = TicketType.objects.select_for_update().get(pk=ticket_type_id)

                if ticket_type.ticket_set.count() >= ticket_type.max_amount:
                    raise TicketSoldOut()

                Ticket.objects.create(invoice=invoice, ticket_type=ticket_type, person=person)
            invoice.send_verify_email()

            success = True
            liu_id_form = LiuIDForm()
            ticket_type_form = PublicTicketTypeForm()

        except TicketSoldOut:
            error = _('This ticket type is sold out')

        except InvoiceExists:
            error = _('An invoice already exist for this person')

        except StudentNotFound:
            error = _('Student was not found')

    return render(request, 'tickets/public_sell.html',
                  {'ticket_type_form': ticket_type_form,
                   'liu_id_form': liu_id_form,
                   'error': error, 'success': success})
Example #2
0
def search(request):
    def render_worker_form(initial={}):
        form = AddWorkerForm(initial=initial)
        return render(request, 'functionary/add_functionary.html', {'form': form})

    form = SearchForm(request.POST or None)
    error = None

    if form.is_valid():
        k = KOBRAClient(settings.KOBRA_USER, settings.KOBRA_KEY)
        term = form.cleaned_data.get('term')

        if not term:
            return render_worker_form()

        try:
            w = Worker.objects.search(term)
            return redirect('add_registrations', worker_id=w.pk)

        except Worker.DoesNotExist:
            pass

        try:
            student = k.get_student(term)

            if student.get('blocked'):
                error = _('Blocked user or LiU-card')

            else:
                return render_worker_form({
                    'first_name': student.get('first_name').title(),
                    'last_name': student.get('last_name').title(),
                    'email': student.get('email'),
                    'lintek': student.get('union') == 'LinTek',
                    'pid': student.get('personal_number'),
                })

        except StudentNotFound:
            error = _('Student was not found')

        except ValueError:
            error = _('Could not get the result')

    return render(request, 'functionary/search.html',
                  {'form': form, 'error': error})
Example #3
0
def turbo_submit(request):
    response = {}
    worker_job_count = None
    worker_no_contract = False
    error = None
    ticket_type_form = TicketTypeForm(request.POST or None, display_all=request.user.is_staff)
    turbo_form = TurboTicketForm(request.POST or None)
    payment_type_form = PaymentTypeForm(request.POST or None)

    csrf_token_value = get_token(request)

    if turbo_form.is_valid() and ticket_type_form.is_valid():
        client = KOBRAClient(settings.KOBRA_USER, settings.KOBRA_KEY)
        term = turbo_form.cleaned_data.get('term')
        ticket_type_ids = ticket_type_form.cleaned_data.get('ticket_type')

        try:
            for ticket_type_id in ticket_type_ids:
                ticket_type = TicketType.objects.select_for_update().get(pk=ticket_type_id)

                if ticket_type.ticket_set.count() >= ticket_type.max_amount:
                    raise TicketSoldOut()

            try:
                person = Person.objects.search(term)

                if Invoice.objects.filter(person=person).count() > 1:
                    raise InvoiceExists()

                try:
                    person.worker
                    person_form = WorkerForm(instance=person)
                    response['worker_id'] = person.id
                    worker_job_count = person.worker.workerregistration_set.count()
                    worker_no_contract = not person.worker.contract_approved

                except Worker.DoesNotExist:
                    person_form = VisitorForm(instance=person)
                    response['visitor_id'] = person.id

            except Person.DoesNotExist:
                # Otherwise, fetch the person from KOBRA
                student = client.get_student(term)
                person_form = VisitorForm(instance=Visitor(**get_kwargs(student)))

            response['is_valid'] = True
            response['html'] = render_to_string('tickets/partials/turbo_confirm.html',
                                                {'ticket_type_form': ticket_type_form,
                                                 'person_form': person_form,
                                                 'payment_type_form': payment_type_form,
                                                 'worker_job_count': worker_job_count,
                                                 'worker_no_contract': worker_no_contract,
                                                 'csrf_token_value': csrf_token_value},
                                                RequestContext(request))
            return HttpResponse(json.dumps(response),
                                content_type='application/json')

        except TicketSoldOut:
            error = _('This ticket type is sold out')

        except StudentNotFound:
            error = _('Student was not found')

        except InvoiceExists:
            error = _('More than one invoice already exist for this person - automatically adding more tickets is not supported')

        except ValueError:
            error = _('Could not get the result')

    response['is_valid'] = False
    response['html'] = render_to_string('tickets/partials/turbo.html',
                                        {'ticket_type_form': ticket_type_form,
                                         'payment_type_form': payment_type_form,
                                         'turbo_form': turbo_form,
                                         'error': error}, RequestContext(request))

    return HttpResponse(json.dumps(response),
                        content_type='application/json')
Example #4
0
from django.conf import settings

from sof.functionary.models import Worker
from sof.utils.forms import format_kobra_pid
from sof.utils.kobra_client import KOBRAClient, StudentNotFound

client = KOBRAClient(settings.KOBRA_USER, settings.KOBRA_KEY)

for worker in Worker.objects.order_by('first_name', 'last_name'):
    if not worker.liu_id:
        print worker.get_full_name()

        try:
            student = client.get_student_by_pid(format_kobra_pid(worker.pid))

            worker.lintek_member = (student.get('union') == 'LinTek')
            worker.liu_id = student.get('liu_id')
            worker.rfid_number = student.get('rfid_number')
            worker.barcode_number = student.get('barcode_number')
            worker.save()

        except StudentNotFound:
            print '  !!! Not found'