Beispiel #1
0
    def test_saving_and_retrieving_volunteers(self):
        first_volunteer = Volunteer()
        first_volunteer.email = '*****@*****.**'
        first_volunteer.save()

        second_volunteer = Volunteer()
        second_volunteer.email = '*****@*****.**'
        second_volunteer.save()

        saved_volunteers = Volunteer.objects.all()
        self.assertEqual(saved_volunteers.count(), 2)

        first_saved_volunteer = saved_volunteers[0]
        second_saved_volunteer = saved_volunteers[1]
        self.assertEqual(first_saved_volunteer.email,
                         '*****@*****.**')
        self.assertEqual(second_saved_volunteer.email,
                         '*****@*****.**')
Beispiel #2
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'])