def search(request): organizations_list = [] if request.method == 'POST': form = SearchVolunteerForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] city = form.cleaned_data['city'] state = form.cleaned_data['state'] country = form.cleaned_data['country'] organization = form.cleaned_data['organization'] organizations_list = get_organizations_ordered_by_name() search_result_list = search_volunteers(first_name, last_name, city, state, country, organization) return render( request, 'volunteer/search.html', { 'organizations_list': organizations_list, 'form': form, 'has_searched': True, 'search_result_list': search_result_list }) else: organizations_list = get_organizations_ordered_by_name() form = SearchVolunteerForm() return render( request, 'volunteer/search.html', { 'organizations_list': organizations_list, 'form': form, 'has_searched': False })
def search(request): organizations_list = get_organizations_ordered_by_name() if request.method == 'POST': form = SearchVolunteerForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] city = form.cleaned_data['city'] state = form.cleaned_data['state'] country = form.cleaned_data['country'] organization = form.cleaned_data['organization'] event = form.cleaned_data['event'] job = form.cleaned_data['job'] search_result_list = search_volunteers( first_name, last_name, city, state, country, organization, event, job) return render( request, 'volunteer/search.html', { 'organizations_list': organizations_list, 'form': form, 'has_searched': True, 'search_result_list': search_result_list }) else: form = SearchVolunteerForm() return render( request, 'volunteer/search.html', { 'organizations_list': organizations_list, 'form': form, 'has_searched': False })
class ShowReportListView(LoginRequiredMixin, AdministratorLoginRequiredMixin, ListView): """ Generate the report using ListView """ template_name = "administrator/report.html" organization_list = get_organizations_ordered_by_name() event_list = get_events_ordered_by_name() job_list = get_jobs_ordered_by_title() def post(self, request, *args, **kwargs): report_list = get_administrator_report( self.request.POST['first_name'], self.request.POST['last_name'], self.request.POST['organization'], self.request.POST['event_name'], self.request.POST['job_name'], self.request.POST['start_date'], self.request.POST['end_date'], ) organization = self.request.POST['organization'] event_name = self.request.POST['event_name'] total_hours = calculate_total_report_hours(report_list) return render( request, 'administrator/report.html', { 'report_list': report_list, 'total_hours': total_hours, 'notification': True, 'organization_list': self.organization_list, 'selected_organization': organization, 'event_list': self.event_list, 'selected_event': event_name, 'job_list': self.job_list })
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") administrator_form = AdministratorForm( request.POST, prefix="admin") if user_form.is_valid() and administrator_form.is_valid(): ad_country = request.POST.get('admin-country') ad_phone = request.POST.get('admin-phone_number') if (ad_country and ad_phone): if not validate_phone(ad_country, ad_phone): self.phone_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() administrator = administrator_form.save(commit=False) administrator.user = user # if organization isn't chosen from dropdown, # the organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: administrator.organization = organization administrator.save() registered = True messages.success(request, 'You have successfully registered!') return HttpResponseRedirect(reverse('home:index')) else: print(user_form.errors, administrator_form.errors) return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def get_administrator_report( first_name, last_name, organization, event_name, job_name, start_date, end_date ): volunteer_shift_list = get_all_volunteer_shifts_with_hours() if first_name: volunteer_shift_list = volunteer_shift_list.filter( volunteer__first_name__icontains=first_name ) if last_name: volunteer_shift_list = volunteer_shift_list.filter( volunteer__last_name__icontains=last_name ) if organization: organization_obj = get_organization_by_name(organization) organization_list = get_organizations_ordered_by_name() if organization_obj in organization_list: # organization associated with a volunteer can be null # therefore exclude from the search query volunteers # with no associated organization # then filter by organization_name volunteer_shift_list = volunteer_shift_list.exclude( volunteer__organization__isnull=True).filter( volunteer__organization__name__icontains=organization ) else: # unlisted_organization associated # with a volunteer can be left blank # therefore exclude from the search query volunteers # with a blank unlisted_organization # then filter by the unlisted organization name volunteer_shift_list = volunteer_shift_list.exclude( volunteer__unlisted_organization__exact='').filter( volunteer__unlisted_organization__icontains=organization ) if event_name: volunteer_shift_list = volunteer_shift_list.filter( shift__job__event__name__icontains=event_name ) if job_name: volunteer_shift_list = volunteer_shift_list.filter( shift__job__name__icontains=job_name ) if (start_date and end_date): volunteer_shift_list = volunteer_shift_list.filter( shift__date__gte=start_date, shift__date__lte=end_date ) report_list = generate_report(volunteer_shift_list) return report_list
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 search_volunteers(first_name, last_name, city, state, country, organization): """Volunteers search None, one, or more parameters may be sent: first_name, last_name, city, state, country, organization If no search parameters are given, it returns all volunteers Examples: search_volunteers(None, None, None, None, None, None) will return all volunteers search_volunteers("Yoshi", None, None, None, None, None) will return all volunteers with the first name "Yoshi" search_volunteers(None, "Doe", None, None, None, None) will return all volunteers with the last name "Doe" """ # if no search parameters are given, it returns all volunteers search_query = Volunteer.objects.all() # build query based on parameters provided if first_name: search_query = search_query.filter(first_name__icontains=first_name) if last_name: search_query = search_query.filter(last_name__icontains=last_name) if city: search_query = search_query.filter(city__icontains=city) if state: search_query = search_query.filter(state__icontains=state) if country: search_query = search_query.filter(country__icontains=country) if organization: organization_obj = get_organization_by_name(organization) organization_list = get_organizations_ordered_by_name() if organization_obj in organization_list: # organization associated with a volunteer can be null # therefore exclude from the search # query volunteers with no associated organization # then filter by organization_name search_query = search_query.exclude( organization__isnull=True).filter( organization__name__icontains=organization) else: # unlisted_organization associated # with a volunteer can be left blank # therefore exclude from the search query volunteers # with a blank unlisted_organization # then filter by the unlisted organization name search_query = search_query.exclude( unlisted_organization__exact='').filter( unlisted_organization__icontains=organization) return search_query
def test_get_organizations_ordered_by_name(self): # test typical cases organization_list = get_organizations_ordered_by_name() self.assertIsNotNone(organization_list) self.assertIn(self.o1, organization_list) self.assertIn(self.o2, organization_list) self.assertIn(self.o3, organization_list) self.assertEqual(len(organization_list), 3) # test order self.assertEqual(organization_list[0], self.o1) self.assertEqual(organization_list[1], self.o3) self.assertEqual(organization_list[2], self.o2)
def get(self, request): organization_list = get_organizations_ordered_by_name() user_form = UserForm(prefix="usr") administrator_form = AdministratorForm(prefix="admin") return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, })
def test_get_organizations_ordered_by_name(self): # test typical cases organization_list = get_organizations_ordered_by_name() self.assertIsNotNone(organization_list) self.assertIn(self.o1, organization_list) self.assertIn(self.o2, organization_list) self.assertIn(self.o3, organization_list) self.assertEqual(len(organization_list), 3) # test order self.assertEqual(organization_list[0], self.o1) self.assertEqual(organization_list[1], self.o3) self.assertEqual(organization_list[2], self.o2)
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
class ShowFormView(AdministratorLoginRequiredMixin, FormView): """ Displays the form """ model = Administrator form_class = ReportForm template_name = "administrator/report.html" event_list = get_events_ordered_by_name() job_list = get_jobs_ordered_by_title() organization_list = get_organizations_ordered_by_name() def get(self, request, *args, **kwargs): return render( request, 'administrator/report.html', { 'event_list': self.event_list, 'organization_list': self.organization_list, 'job_list': self.job_list })
def get_administrator_report(first_name, last_name, organization, event_name, job_name, start_date, end_date): volunteer_shift_list = get_all_volunteer_shifts_with_hours() if first_name: volunteer_shift_list = volunteer_shift_list.filter( volunteer__first_name__icontains=first_name) if last_name: volunteer_shift_list = volunteer_shift_list.filter( volunteer__last_name__icontains=last_name) if organization: organization_obj = get_organization_by_name(organization) organization_list = get_organizations_ordered_by_name() if organization_obj in organization_list: # organization associated with a volunteer can be null # therefore exclude from the search query volunteers # with no associated organization # then filter by organization_name volunteer_shift_list = volunteer_shift_list.exclude( volunteer__organization__isnull=True).filter( volunteer__organization__name__icontains=organization) else: # unlisted_organization associated # with a volunteer can be left blank # therefore exclude from the search query volunteers # with a blank unlisted_organization # then filter by the unlisted organization name volunteer_shift_list = volunteer_shift_list.exclude( volunteer__unlisted_organization__exact='').filter( volunteer__unlisted_organization__icontains=organization) if event_name: volunteer_shift_list = volunteer_shift_list.filter( shift__job__event__name__icontains=event_name) if job_name: volunteer_shift_list = volunteer_shift_list.filter( shift__job__name__icontains=job_name) if (start_date and end_date): volunteer_shift_list = volunteer_shift_list.filter( shift__date__gte=start_date, shift__date__lte=end_date) report_list = generate_report(volunteer_shift_list) return report_list
class AdminUpdateView(AdministratorLoginRequiredMixin, UpdateView, FormView): @method_decorator(admin_required) def dispatch(self, *args, **kwargs): return super(AdminUpdateView, self).dispatch(*args, **kwargs) form_class = AdministratorForm template_name = 'administrator/edit.html' organization_list = get_organizations_ordered_by_name() success_url = reverse_lazy('administrator:profile') def get_context_data(self, **kwargs): context = super(AdminUpdateView, self).get_context_data(**kwargs) context['organization_list'] = self.organization_list return context def get_object(self, queryset=None): admin_id = self.kwargs['admin_id'] obj = Administrator.objects.get(pk=admin_id) return obj def form_valid(self, form): admin_id = self.kwargs['admin_id'] administrator = Administrator.objects.get(pk=admin_id) admin_to_edit = form.save(commit=False) organization_id = self.request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: admin_to_edit.organization = organization else: unlisted_org = self.request.POST.get('unlisted_organization') org = create_organization(unlisted_org) admin_to_edit.organization = org # update the volunteer admin_to_edit.save() return HttpResponseRedirect( reverse('administrator:profile', args=[ admin_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: 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, )))
class AdminUpdateView(AdministratorLoginRequiredMixin, UpdateView, FormView): @method_decorator(admin_required) def dispatch(self, *args, **kwargs): return super(AdminUpdateView, self).dispatch(*args, **kwargs) form_class = AdministratorForm template_name = 'administrator/edit.html' organization_list = get_organizations_ordered_by_name() success_url = reverse_lazy('administrator:profile') def get_context_data(self, **kwargs): context = super(AdminUpdateView, self).get_context_data(**kwargs) admin_id = self.kwargs['admin_id'] admin = Administrator.objects.get(pk=admin_id) country_list = Country.objects.all() if admin.country: country = admin.country state_list = Region.objects.filter(country=country) context['state_list'] = state_list if admin.state: state = admin.state city_list = City.objects.filter(region=state) context['city_list'] = city_list context['organization_list'] = self.organization_list context['country_list'] = country_list return context def get_object(self, queryset=None): admin_id = self.kwargs['admin_id'] obj = Administrator.objects.get(pk=admin_id) return obj def form_valid(self, form): admin_id = self.kwargs['admin_id'] administrator = Administrator.objects.get(pk=admin_id) admin_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 admin_to_edit.country = country try: state_name = self.request.POST.get('state') state = Region.objects.get(name=state_name) except ObjectDoesNotExist: state = None admin_to_edit.state = state try: city_name = self.request.POST.get('city') city = City.objects.get(name=city_name) except ObjectDoesNotExist: city = None admin_to_edit.city = city organization_id = self.request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: admin_to_edit.organization = organization else: unlisted_org = self.request.POST.get('unlisted_organization') org = create_organization(unlisted_org) admin_to_edit.organization = org # update the volunteer admin_to_edit.save() return HttpResponseRedirect( reverse('administrator:profile', args=[admin_id, ]) )
def signup_volunteer(request): registered = False organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == "POST": # each form must have its own namespace (prefix) if multiple forms # are to be put inside one <form> tag user_form = UserForm(request.POST, prefix="usr") volunteer_form = VolunteerForm(request.POST, request.FILES, prefix="vol") if user_form.is_valid() and volunteer_form.is_valid(): if "resume_file" in request.FILES: my_file = volunteer_form.cleaned_data["resume_file"] if not validate_file(my_file): return render( request, "registration/signup_volunteer.html", { "user_form": user_form, "volunteer_form": volunteer_form, "registered": registered, "organization_list": organization_list, }, ) user = user_form.save() user.set_password(user.password) user.save() volunteer = volunteer_form.save(commit=False) volunteer.user = user # if an organization isn't chosen from the dropdown, # then organization_id will be 0 organization_id = request.POST.get("organization_name") organization = get_organization_by_id(organization_id) if organization: volunteer.organization = organization volunteer.reminder_days = 1 volunteer.save() registered = True messages.success(request, "You have successfully registered!") return HttpResponseRedirect(reverse("home:index")) else: print(user_form.errors, volunteer_form.errors) return render( request, "registration/signup_volunteer.html", { "user_form": user_form, "volunteer_form": volunteer_form, "registered": registered, "organization_list": organization_list, }, ) else: user_form = UserForm(prefix="usr") volunteer_form = VolunteerForm(prefix="vol") return render( request, "registration/signup_volunteer.html", { "user_form": user_form, "volunteer_form": volunteer_form, "registered": registered, "organization_list": organization_list, }, ) else: return render(request, "organization/add_organizations.html")
def signup_administrator(request): """ This method is responsible for diplaying the register user view Register Admin or volunteer is judged on the basis of users access rights. Only if user is registered and logged in and registered as an admin user, he/she is allowed to register others as an admin user """ registered = False organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == "POST": user_form = UserForm(request.POST, prefix="usr") administrator_form = AdministratorForm(request.POST, prefix="admin") if user_form.is_valid() and administrator_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() administrator = administrator_form.save(commit=False) administrator.user = user # if organization isn't chosen from dropdown, # the organization_id will be 0 organization_id = request.POST.get("organization_name") organization = get_organization_by_id(organization_id) if organization: administrator.organization = organization administrator.save() registered = True messages.success(request, "You have successfully registered!") return HttpResponseRedirect(reverse("home:index")) else: print(user_form.errors, administrator_form.errors) return render( request, "registration/signup_administrator.html", { "user_form": user_form, "administrator_form": administrator_form, "registered": registered, "organization_list": organization_list, }, ) else: user_form = UserForm(prefix="usr") administrator_form = AdministratorForm(prefix="admin") return render( request, "registration/signup_administrator.html", { "user_form": user_form, "administrator_form": administrator_form, "registered": registered, "organization_list": organization_list, }, ) else: return render(request, "organization/add_organizations.html")
def get_context_data(self, **kwargs): context = super(VolunteerUpdateView, self).get_context_data(**kwargs) organization_list = get_organizations_ordered_by_name() context['organization_list'] = organization_list return context
def search_volunteers( first_name, last_name, city, state, country, organization ): """Volunteers search None, one, or more parameters may be sent: first_name, last_name, city, state, country, organization If no search parameters are given, it returns all volunteers Examples: search_volunteers(None, None, None, None, None, None) will return all volunteers search_volunteers("Yoshi", None, None, None, None, None) will return all volunteers with the first name "Yoshi" search_volunteers(None, "Doe", None, None, None, None) will return all volunteers with the last name "Doe" """ # if no search parameters are given, it returns all volunteers search_query = Volunteer.objects.all() # build query based on parameters provided if first_name: search_query = search_query.filter(first_name__icontains=first_name) if last_name: search_query = search_query.filter(last_name__icontains=last_name) if city: search_query = search_query.filter(city__icontains=city) if state: search_query = search_query.filter(state__icontains=state) if country: search_query = search_query.filter(country__icontains=country) if organization: organization_obj = get_organization_by_name(organization) organization_list = get_organizations_ordered_by_name() if organization_obj in organization_list: # organization associated with a volunteer can be null # therefore exclude from the search # query volunteers with no associated organization # then filter by organization_name search_query = search_query.exclude( organization__isnull=True ).filter( organization__name__icontains=organization ) else: # unlisted_organization associated # with a volunteer can be left blank # therefore exclude from the search query volunteers # with a blank unlisted_organization # then filter by the unlisted organization name search_query = search_query.exclude( unlisted_organization__exact='' ).filter( unlisted_organization__icontains=organization ) return search_query
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") volunteer_form = VolunteerForm( request.POST, request.FILES, prefix="vol") if user_form.is_valid() and volunteer_form.is_valid(): vol_country = request.POST.get('vol-country') vol_phone = request.POST.get('vol-phone_number') if (vol_country and vol_phone): if not validate_phone(vol_country, vol_phone): self.phone_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) if 'resume_file' in request.FILES: my_file = volunteer_form.cleaned_data['resume_file'] if not validate_file(my_file): return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() volunteer = volunteer_form.save(commit=False) volunteer.user = user # if an organization isn't chosen from the dropdown, # then organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer.organization = organization volunteer.reminder_days = 1 volunteer.save() registered = True messages.success(request, 'You have successfully registered!') return HttpResponseRedirect(reverse('home:index')) else: print(user_form.errors, volunteer_form.errors) return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def post(self, request): organization_list = get_organizations_ordered_by_name() country_list = Country.objects.all() if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") administrator_form = AdministratorForm( request.POST, prefix="admin") if user_form.is_valid() and administrator_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, }) try: admin_country_name = request.POST.get('country') admin_country = Country.objects.get(name=admin_country_name) except ObjectDoesNotExist: admin_country = None try: admin_state_name = request.POST.get('state') admin_state = Region.objects.get(name=admin_state_name) except ObjectDoesNotExist: admin_state = None try: admin_city_name = request.POST.get('city') admin_city = City.objects.get(pk=admin_city_name) except ObjectDoesNotExist: admin_city = None admin_phone = request.POST.get('admin-phone_number') if (admin_country and admin_phone): if not validate_phone(admin_country, admin_phone): self.phone_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, }) user = user_form.save() user.set_password(user.password) user.save() administrator = administrator_form.save(commit=False) administrator.user = user # if organization isn't chosen from dropdown, # the organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: administrator.organization = organization else: unlisted_org = request.POST.get('admin-unlisted_' 'organization') org = create_organization(unlisted_org) administrator.organization = org administrator.country = admin_country administrator.state = admin_state administrator.city = admin_city administrator.save() current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = administrator_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return render(request, 'home/email_ask_confirm.html') else: return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, })
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") administrator_form = AdministratorForm( request.POST, prefix="admin") if user_form.is_valid() and administrator_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) ad_country = request.POST.get('admin-country') ad_phone = request.POST.get('admin-phone_number') if ad_country and ad_phone: if not validate_phone(ad_country, ad_phone): self.phone_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() administrator = administrator_form.save(commit=False) administrator.user = user # if organization isn't chosen from dropdown, # the organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: administrator.organization = organization else: unlisted_org = request.POST.get('admin-unlisted_organization') org = create_organization(unlisted_org) administrator.organization = org administrator.save() registered = True messages.success(request, 'You have successfully registered!') return HttpResponseRedirect(reverse('home:index')) else: return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") volunteer_form = VolunteerForm( request.POST, request.FILES, prefix="vol") if user_form.is_valid() and volunteer_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) vol_country = request.POST.get('vol-country') vol_phone = request.POST.get('vol-phone_number') if (vol_country and vol_phone): if not validate_phone(vol_country, vol_phone): self.phone_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) if 'resume_file' in request.FILES: my_file = volunteer_form.cleaned_data['resume_file'] if not validate_file(my_file): return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() volunteer = volunteer_form.save(commit=False) volunteer.user = user # if an organization isn't chosen from the dropdown, # then organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer.organization = organization else: unlisted_org = request.POST.get('vol-unlisted_organization') org = Organization.objects.create(name=unlisted_org, approved_status=False) org.save() volunteer.organization = org volunteer.reminder_days = 1 volunteer.save() current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = volunteer_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return render(request, 'home/email_ask_confirm.html') else: return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") volunteer_form = VolunteerForm(request.POST, request.FILES, prefix="vol") if user_form.is_valid() and volunteer_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) vol_country = request.POST.get('vol-country') vol_phone = request.POST.get('vol-phone_number') if (vol_country and vol_phone): if not validate_phone(vol_country, vol_phone): self.phone_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) if 'resume_file' in request.FILES: my_file = volunteer_form.cleaned_data['resume_file'] if not validate_file(my_file): return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() volunteer = volunteer_form.save(commit=False) volunteer.user = user # if an organization isn't chosen from the dropdown, # then organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer.organization = organization else: unlisted_org = request.POST.get( 'vol-unlisted_organization') org = Organization.objects.create( name=unlisted_org, approved_status=False) org.save() volunteer.organization = org volunteer.reminder_days = 1 volunteer.save() current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = volunteer_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return render(request, 'home/email_ask_confirm.html') else: return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def get_context_data(self, **kwargs): context = super(VolunteerUpdateView,self).get_context_data(**kwargs) organization_list = get_organizations_ordered_by_name() context['organization_list'] = organization_list return context
class AdministratorSignupView(TemplateView): """ Administrator and Volunteer signup is implemented as a TemplateView that displays the signup form. This method is responsible for displaying the register user view. Register Admin or volunteer is judged on the basis of users access rights. Only if user is registered and logged in and registered as an admin user, he/she is allowed to register others as an admin user. """ registered = False organization_list = get_organizations_ordered_by_name() phone_error = False match_error = False @method_decorator(volunteer_denied) def dispatch(self, *args, **kwargs): return super(AdministratorSignupView, self).dispatch(*args, **kwargs) def get(self, request): user_form = UserForm(prefix="usr") administrator_form = AdministratorForm(prefix="admin") return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") administrator_form = AdministratorForm(request.POST, prefix="admin") if user_form.is_valid() and administrator_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) ad_country = request.POST.get('admin-country') ad_phone = request.POST.get('admin-phone_number') if ad_country and ad_phone: if not validate_phone(ad_country, ad_phone): self.phone_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() administrator = administrator_form.save(commit=False) administrator.user = user # if organization isn't chosen from dropdown, # the organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: administrator.organization = organization else: unlisted_org = request.POST.get( 'admin-unlisted_organization') org = create_organization(unlisted_org) administrator.organization = org administrator.save() registered = True messages.success(request, 'You have successfully registered!') return HttpResponseRedirect(reverse('home:index')) else: return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})