def register(v_id, s_id): result = "IS_VALID" ERROR_CODE_ALREADY_SIGNED_UP = "ERROR_CODE_ALREADY_SIGNED_UP" ERROR_CODE_NO_SLOTS_REMAINING = "ERROR_CODE_NO_SLOTS_REMAINING" # a volunteer must not be allowed to register # for a shift that they are already registered for signed_up = is_signed_up(v_id, s_id) if not signed_up: volunteer_obj = get_volunteer_by_id(v_id) shift_obj = get_shift_by_id(s_id) if volunteer_obj and shift_obj: num_slots_remaining = get_shift_slots_remaining(s_id) if num_slots_remaining > 0: registration_obj = VolunteerShift(volunteer=volunteer_obj, shift=shift_obj) registration_obj.save() else: result = ERROR_CODE_NO_SLOTS_REMAINING else: raise ObjectDoesNotExist else: result = ERROR_CODE_ALREADY_SIGNED_UP return result
def register(v_id, s_id): is_valid = True #a volunteer must not be allowed to register for a shift that they are already registered for signed_up = is_signed_up(v_id, s_id) if not signed_up: volunteer_obj = get_volunteer_by_id(v_id) shift_obj = get_shift_by_id(s_id) if volunteer_obj and shift_obj: if has_slots_remaining(shift_obj): registration_obj = VolunteerShift(volunteer=volunteer_obj, shift=shift_obj) registration_obj.save() decrement_slots_remaining(shift_obj) else: is_valid = False else: is_valid = False else: is_valid = False return is_valid
def post(self, request, *args, **kwargs): volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) event_list = get_signed_up_events_for_volunteer(volunteer_id) job_list = get_signed_up_jobs_for_volunteer(volunteer_id) event_name = self.request.POST['event_name'] job_name = self.request.POST['job_name'] start_date = self.request.POST['start_date'] end_date = self.request.POST['end_date'] volunteer_shift_list = get_volunteer_shifts(volunteer_id, event_name, job_name, start_date, end_date) if volunteer_shift_list: report_list = generate_report(volunteer_shift_list) total_hours = calculate_total_report_hours(report_list) report = Report.objects.create(total_hrs=total_hours, volunteer=volunteer) report.volunteer_shifts.add(*volunteer_shift_list) report.save() return render(request, 'volunteer/report.html', { 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'job_list': job_list, 'event_list': event_list, 'selected_event': event_name, 'selected_job': job_name }) else: return render(request, 'volunteer/report.html', { 'job_list': job_list, 'event_list': event_list, 'notification': True, })
def register(v_id, s_id): result = "IS_VALID" ERROR_CODE_ALREADY_SIGNED_UP = "ERROR_CODE_ALREADY_SIGNED_UP" ERROR_CODE_NO_SLOTS_REMAINING = "ERROR_CODE_NO_SLOTS_REMAINING" #a volunteer must not be allowed to register for a shift that they are already registered for signed_up = is_signed_up(v_id, s_id) if not signed_up: volunteer_obj = get_volunteer_by_id(v_id) shift_obj = get_shift_by_id(s_id) if volunteer_obj and shift_obj: num_slots_remaining = get_shift_slots_remaining(s_id) if num_slots_remaining > 0: registration_obj = VolunteerShift(volunteer=volunteer_obj, shift=shift_obj) registration_obj.save() else: result = ERROR_CODE_NO_SLOTS_REMAINING else: raise ObjectDoesNotExist else: result = ERROR_CODE_ALREADY_SIGNED_UP return result
def get_queryset(self): """ returns confirmed reports of a volunteer :return: confirmed reports of the selected volunteer """ volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) reports = Report.objects.filter(confirm_status=1, volunteer=volunteer).order_by('date_submitted') return reports
def form_valid(self, form): volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) organization_list = get_organizations_ordered_by_name() if 'resume_file' in self.request.FILES: my_file = form.cleaned_data['resume_file'] if validate_file(my_file): # delete an old uploaded resume if it exists has_file = has_resume_file(volunteer_id) if has_file: try: delete_volunteer_resume(volunteer_id) except: raise Http404 else: return render( self.request, 'volunteer/edit.html', { 'form': form, 'organization_list': organization_list, 'volunteer': volunteer, 'resume_invalid': True, }) volunteer_to_edit = form.save(commit=False) try: country_name = self.request.POST.get('country') country = Country.objects.get(name=country_name) except ObjectDoesNotExist: country = None volunteer_to_edit.country = country try: state_name = self.request.POST.get('state') state = Region.objects.get(name=state_name) except ObjectDoesNotExist: state = None volunteer_to_edit.state = state try: city_name = self.request.POST.get('city') city = City.objects.get(name=city_name) except ObjectDoesNotExist: city = None volunteer_to_edit.city = city organization_id = self.request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer_to_edit.organization = organization else: unlisted_org = self.request.POST.get('unlisted_organization') org = create_organization(unlisted_org) volunteer_to_edit.organization = org # update the volunteer volunteer_to_edit.save() return HttpResponseRedirect( reverse('volunteer:profile', args=(volunteer_id, )))
def get_queryset(self): """ returns confirmed reports of a volunteer :return: confirmed reports of the selected volunteer """ volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) reports = Report.objects.filter( confirm_status=1, volunteer=volunteer).order_by('date_submitted') return reports
def get_context_data(self, **kwargs): """ displays volunteer information :return: volunteer object and its organization """ context = super(VolunteerHistoryView, self).get_context_data(**kwargs) volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) context['volunteer'] = volunteer organization = volunteer.organization context['organization'] = organization return context
def wrapped_view(request, volunteer_id): vol = getattr(request.user, 'volunteer', hasattr(request.user, 'administrator')) if vol is None: return render(request, 'vms/no_volunteer_access.html', status=403) elif vol is False: volunteer = get_volunteer_by_id(volunteer_id) if not volunteer: return render( request, 'vms/no_volunteer_access.html', status=403) if not int(volunteer.id) == vol.id: return render( request, 'vms/no_volunteer_access.html', status=403) return func(request, volunteer_id=volunteer_id)
def wrapped_view(request, volunteer_id): vol = getattr(request.user, 'volunteer', hasattr(request.user, 'administrator')) if vol is None: return render(request, 'vms/no_volunteer_access.html', status=403) elif vol is False: volunteer = get_volunteer_by_id(volunteer_id) if not volunteer: return render(request, 'vms/no_volunteer_access.html', status=403) if not int(volunteer.id) == vol.id: return render(request, 'vms/no_volunteer_access.html', status=403) return func(request, volunteer_id=volunteer_id)
def get_context_data(self, **kwargs): context = super(VolunteerUpdateView, self).get_context_data(**kwargs) volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) organization_list = get_organizations_ordered_by_name() context['organization_list'] = organization_list country_list = Country.objects.all() if volunteer.country: country = volunteer.country state_list = Region.objects.filter(country=country) context['state_list'] = state_list if volunteer.state: state = volunteer.state city_list = City.objects.filter(region=state) context['city_list'] = city_list context['organization_list'] = organization_list context['country_list'] = country_list return context
def register(v_id, s_id): #a volunteer must not be allowed to register for a shift that they are already registered for signed_up = is_signed_up(v_id, s_id) if not signed_up: volunteer_obj = get_volunteer_by_id(v_id) shift_obj = get_shift_by_id(s_id) if volunteer_obj and shift_obj: if has_slots_remaining(shift_obj): registration_obj = VolunteerShift(volunteer=volunteer_obj, shift=shift_obj) registration_obj.save() decrement_slots_remaining(shift_obj) else: raise Exception('No slots remaining') else: raise ObjectDoesNotExist else: raise Exception('Is already signed up')
def form_valid(self, form): volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) organization_list = get_organizations_ordered_by_name() if 'resume_file' in self.request.FILES: my_file = form.cleaned_data['resume_file'] if validate_file(my_file): # delete an old uploaded resume if it exists has_file = has_resume_file(volunteer_id) if has_file: try: delete_volunteer_resume(volunteer_id) except: raise Http404 else: return render( self.request, 'volunteer/edit.html', { 'form': form, 'organization_list': organization_list, 'volunteer': volunteer, 'resume_invalid': True, }) volunteer_to_edit = form.save(commit=False) organization_id = self.request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer_to_edit.organization = organization else: unlisted_org = self.request.POST.get('unlisted_organization') org = create_organization(unlisted_org) volunteer_to_edit.organization = org # update the volunteer volunteer_to_edit.save() return HttpResponseRedirect( reverse('volunteer:profile', args=(volunteer_id, )))
def form_valid(self, form): volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) organization_list = get_organizations_ordered_by_name() if 'resume_file' in self.request.FILES: my_file = form.cleaned_data['resume_file'] if validate_file(my_file): # delete an old uploaded resume if it exists has_file = has_resume_file(volunteer_id) if has_file: try: delete_volunteer_resume(volunteer_id) except: raise Http404 else: return render( self.request, 'volunteer/edit.html', { 'form': form, 'organization_list': organization_list, 'volunteer': volunteer, 'resume_invalid': True, }) volunteer_to_edit = form.save(commit=False) organization_id = self.request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer_to_edit.organization = organization else: volunteer_to_edit.organization = None # update the volunteer volunteer_to_edit.save() return HttpResponseRedirect( reverse('volunteer:profile', args=(volunteer_id, )))
def post(self, request, *args, **kwargs): volunteer_id = self.kwargs['volunteer_id'] volunteer = get_volunteer_by_id(volunteer_id) event_list = get_signed_up_events_for_volunteer(volunteer_id) job_list = get_signed_up_jobs_for_volunteer(volunteer_id) event_name = self.request.POST['event_name'] job_name = self.request.POST['job_name'] start_date = self.request.POST['start_date'] end_date = self.request.POST['end_date'] volunteer_shift_list = get_volunteer_shifts(volunteer_id, event_name, job_name, start_date, end_date) if volunteer_shift_list: report_list = generate_report(volunteer_shift_list) total_hours = calculate_total_report_hours(report_list) report = Report.objects.create(total_hrs=total_hours, volunteer=volunteer) report.volunteer_shifts.add(*volunteer_shift_list) report.save() return render( request, 'volunteer/report.html', { 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'job_list': job_list, 'event_list': event_list, 'selected_event': event_name, 'selected_job': job_name }) else: return render( request, 'volunteer/report.html', { 'job_list': job_list, 'event_list': event_list, 'notification': True, })
def test_get_volunteer_by_id(self): # test typical cases self.assertIsNotNone(get_volunteer_by_id(self.v1.id)) self.assertIsNotNone(get_volunteer_by_id(self.v2.id)) self.assertIsNotNone(get_volunteer_by_id(self.v3.id)) self.assertEqual(get_volunteer_by_id(self.v1.id), self.v1) self.assertEqual(get_volunteer_by_id(self.v2.id), self.v2) self.assertEqual(get_volunteer_by_id(self.v3.id), self.v3) # test non-existant cases self.assertIsNone(get_volunteer_by_id(1000)) self.assertIsNone(get_volunteer_by_id(2000)) self.assertIsNone(get_volunteer_by_id(3000)) self.assertIsNone(get_volunteer_by_id(4000)) self.assertNotEqual(get_volunteer_by_id(1000), self.v1) self.assertNotEqual(get_volunteer_by_id(2000), self.v1) self.assertNotEqual(get_volunteer_by_id(3000), self.v2) self.assertNotEqual(get_volunteer_by_id(4000), self.v2)
def test_get_volunteer_by_id(self): u1 = User.objects.create_user('John') u2 = User.objects.create_user('James') u3 = User.objects.create_user('George') v1 = Volunteer( first_name="John", last_name="Doe", address="7 Alpine Street", city="Maplegrove", state="Wyoming", country="USA", phone_number="23454545", email="*****@*****.**", user=u1 ) v2 = Volunteer( first_name="James", last_name="Doe", address="7 Oak Street", city="Elmgrove", state="California", country="USA", phone_number="23454545", email="*****@*****.**", user=u2 ) v3 = Volunteer( id=999, first_name="George", last_name="Doe", address="7 Elm Street", city="Oakgrove", state="California", country="USA", phone_number="23454545", email="*****@*****.**", user=u3 ) v1.save() v2.save() v3.save() # test typical cases self.assertIsNotNone(get_volunteer_by_id(v1.id)) self.assertIsNotNone(get_volunteer_by_id(v2.id)) self.assertIsNotNone(get_volunteer_by_id(v3.id)) self.assertEqual(get_volunteer_by_id(v1.id), v1) self.assertEqual(get_volunteer_by_id(v2.id), v2) self.assertEqual(get_volunteer_by_id(v3.id), v3) # test non-existant cases self.assertIsNone(get_volunteer_by_id(100)) self.assertIsNone(get_volunteer_by_id(200)) self.assertIsNone(get_volunteer_by_id(300)) self.assertIsNone(get_volunteer_by_id(400)) self.assertNotEqual(get_volunteer_by_id(100), v1) self.assertNotEqual(get_volunteer_by_id(200), v1) self.assertNotEqual(get_volunteer_by_id(300), v2) self.assertNotEqual(get_volunteer_by_id(400), v2)
def test_get_volunteer_by_id(self): # test typical cases self.assertIsNotNone(get_volunteer_by_id(self.v1.id)) self.assertIsNotNone(get_volunteer_by_id(self.v2.id)) self.assertIsNotNone(get_volunteer_by_id(self.v3.id)) self.assertEqual(get_volunteer_by_id(self.v1.id), self.v1) self.assertEqual(get_volunteer_by_id(self.v2.id), self.v2) self.assertEqual(get_volunteer_by_id(self.v3.id), self.v3) # test non-existant cases self.assertIsNone(get_volunteer_by_id(100)) self.assertIsNone(get_volunteer_by_id(200)) self.assertIsNone(get_volunteer_by_id(300)) self.assertIsNone(get_volunteer_by_id(400)) self.assertNotEqual(get_volunteer_by_id(100), self.v1) self.assertNotEqual(get_volunteer_by_id(200), self.v1) self.assertNotEqual(get_volunteer_by_id(300), self.v2) self.assertNotEqual(get_volunteer_by_id(400), self.v2)