Esempio n. 1
0
def submit(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        form = VolunteerForm(data)
        if form.is_valid():
            volunteer = Volunteer()
            volunteer.name = data['name'].strip()
            volunteer.surname = data['lastname'].strip()
            volunteer.email = data['email'].strip()
            volunteer.expedient = data['expedient'].strip()
            volunteer.phone = data['phone'].strip()
            volunteer.shirt_size = data['shirt']
            volunteer.android_phone = data['android']

            if 'commentary' in data:
                volunteer.commentary = data['commentary'].strip()

            # School and degree
            volunteer.school = School.objects.get(code=data['college'])
            volunteer.degree = Degree.objects.get(code=data['degree'])

            volunteer.save()

            # Insert schedules
            for schedule in data['schedule']:
                volunteer_schedule = VolunteerSchedule()
                volunteer_schedule.schedule = Schedule.objects.get(
                    pk=schedule[4:])
                volunteer_schedule.volunteer = volunteer

                # Calculate schedule day
                date = Edition.objects.get(year=EDITION_YEAR).start_date
                volunteer_schedule.day = datetime.date(year=EDITION_YEAR,
                                                       month=date.month,
                                                       day=int(schedule[1:3]))

                volunteer_schedule.save()

            return HttpResponse()
        else:
            error = {'id': 1, 'message': 'Error en la validación'}
            return HttpResponseBadRequest(json.dumps(error))
    else:
        return HttpResponseNotAllowed(permitted_methods=['POST'])
Esempio n. 2
0
def submit(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        error = VolunteerForm(data).get_error()
        if error != '':
            return HttpResponseBadRequest(json.dumps({'id': 1, 'message': error}))
        attendant = Attendant.objects.filter(identity=data['dni_nie'].strip().upper(), edition__year=EDITION_YEAR)
        if attendant.count() == 0:
            error = {'id': 2, 'message': 'Error, no existe ninguna entrada para tu DNI, consigue una antes de '
                                         'apuntarte para voluntario.'}
            return HttpResponseBadRequest(json.dumps(error))

        if Volunteer.objects.filter(identity__in=attendant).count() != 0:
            error = {'id': 3, 'message': 'Error, ya estas registrado como voluntario.'}
            return HttpResponseBadRequest(json.dumps(error))

        volunteer = Volunteer()
        volunteer.identity = attendant[0]
        volunteer.shirt_size = data['shirt']
        volunteer.android_phone = data['android']

        if 'commentary' in data:
            volunteer.commentary = data['commentary'].strip()

        volunteer.save()

        # Insert schedules
        for schedule in data['schedule_options']:
            volunteer_schedule = VolunteerSchedule()
            volunteer_schedule.schedule = schedule['schedule_type']
            volunteer_schedule.volunteer = volunteer
            # Calculate schedule day
            schedule_day = schedule["date"].split('-')
            volunteer_schedule.day = datetime.date(int(schedule_day[0]), int(schedule_day[1]), int(schedule_day[2]))

            volunteer_schedule.save()

        return HttpResponse()

    else:
        return HttpResponseNotAllowed(permitted_methods=['POST'])