Exemple #1
0
def reject(request, organization_id):
    """
    rejects the pending organizatons

    :param organization_id: The id of the pending organization
    :return: redirect to list of organizations
    """
    unlisted_org = get_organization_by_id(organization_id)
    unlisted_org.approved_status = 2
    unlisted_org.save()
    try:
        vol_email = Volunteer.objects.get(organization=unlisted_org).email
        try:
            send_mail(
                "Organization Rejected",
                "The organization you filled while sign-up has been rejected",
                "*****@*****.**", [vol_email],
                fail_silently=False)
        except Exception:
            raise Exception("There was an error in sending emails.")
    except Exception:
        admin_email = Administrator.objects.get(
            organization=unlisted_org).email
        try:
            send_mail(
                "Organization Rejected",
                "The organization you filled while sign-up has been rejected",
                "*****@*****.**", [admin_email],
                fail_silently=False)
        except Exception:
            raise Exception("There was an error in sending emails.")
    return HttpResponseRedirect('/organization/list')
Exemple #2
0
def reject(request, organization_id):
    """
    rejects the pending organizatons

    :param organization_id: The id of the pending organization
    :return: redirect to list of organizations
    """
    unlisted_org = get_organization_by_id(organization_id)
    unlisted_org.approved_status = 2
    unlisted_org.save()
    try:
        vol_email = Volunteer.objects.get(organization=unlisted_org).email
        try:
            send_mail("Organization Rejected", "The organization you filled while sign-up has been rejected",
                      "*****@*****.**", [vol_email], fail_silently=False)
        except:
            raise Exception("There was an error in sending emails.")
    except:
        admin_email = Administrator.objects.get(organization=unlisted_org).email
        try:
            send_mail("Organization Rejected", "The organization you filled while sign-up has been rejected",
                      "*****@*****.**", [admin_email], fail_silently=False)
        except:
            raise Exception("There was an error in sending emails.")
    return HttpResponseRedirect('/organization/list')
Exemple #3
0
    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})
Exemple #4
0
    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, )))
Exemple #5
0
def approve(request, organization_id):
    """
    approves the pending organizatons

    :param organization_id: The id of the pending organization
    :return: redirect to list of organizations
    """
    unlisted_org = get_organization_by_id(organization_id)
    unlisted_org.approved_status = 1
    unlisted_org.save()
    return HttpResponseRedirect('/organization/list')
Exemple #6
0
def approve(request, organization_id):
    """
    approves the pending organizatons

    :param organization_id: The id of the pending organization
    :return: redirect to list of organizations
    """
    unlisted_org = get_organization_by_id(organization_id)
    unlisted_org.approved_status = 1
    unlisted_org.save()
    return HttpResponseRedirect('/organization/list')
    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:
            admin_to_edit.organization = None

        # update the volunteer
        admin_to_edit.save()
        return HttpResponseRedirect(
            reverse('administrator:profile', args=[admin_id,]))
Exemple #8
0
    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,]))
Exemple #9
0
    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, )))
Exemple #10
0
    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, ])
        )
Exemple #11
0
    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, )))
Exemple #12
0
    def test_get_organization_by_id(self):

        # test typical cases
        self.assertIsNotNone(get_organization_by_id(self.o1.id))
        self.assertIsNotNone(get_organization_by_id(self.o2.id))
        self.assertIsNotNone(get_organization_by_id(self.o3.id))

        self.assertEqual(get_organization_by_id(self.o1.id), self.o1)
        self.assertEqual(get_organization_by_id(self.o2.id), self.o2)
        self.assertEqual(get_organization_by_id(self.o3.id), self.o3)

        self.assertIsNone(get_organization_by_id(1000))
        self.assertIsNone(get_organization_by_id(2000))
        self.assertIsNone(get_organization_by_id(3000))

        self.assertNotEqual(get_organization_by_id(1000), self.o1)
        self.assertNotEqual(get_organization_by_id(2000), self.o1)
        self.assertNotEqual(get_organization_by_id(3000), self.o1)

        self.assertNotEqual(get_organization_by_id(1000), self.o2)
        self.assertNotEqual(get_organization_by_id(2000), self.o2)
        self.assertNotEqual(get_organization_by_id(3000), self.o2)

        self.assertNotEqual(get_organization_by_id(1000), self.o3)
        self.assertNotEqual(get_organization_by_id(2000), self.o3)
        self.assertNotEqual(get_organization_by_id(3000), self.o3)
Exemple #13
0
    def test_get_organization_by_id(self):

        # test typical cases
        self.assertIsNotNone(get_organization_by_id(self.o1.id))
        self.assertIsNotNone(get_organization_by_id(self.o2.id))
        self.assertIsNotNone(get_organization_by_id(self.o3.id))

        self.assertEqual(get_organization_by_id(self.o1.id), self.o1)
        self.assertEqual(get_organization_by_id(self.o2.id), self.o2)
        self.assertEqual(get_organization_by_id(self.o3.id), self.o3)

        self.assertIsNone(get_organization_by_id(1000))
        self.assertIsNone(get_organization_by_id(2000))
        self.assertIsNone(get_organization_by_id(3000))

        self.assertNotEqual(get_organization_by_id(1000), self.o1)
        self.assertNotEqual(get_organization_by_id(2000), self.o1)
        self.assertNotEqual(get_organization_by_id(3000), self.o1)

        self.assertNotEqual(get_organization_by_id(1000), self.o2)
        self.assertNotEqual(get_organization_by_id(2000), self.o2)
        self.assertNotEqual(get_organization_by_id(3000), self.o2)

        self.assertNotEqual(get_organization_by_id(1000), self.o3)
        self.assertNotEqual(get_organization_by_id(2000), self.o3)
        self.assertNotEqual(get_organization_by_id(3000), self.o3)
Exemple #14
0
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")
Exemple #15
0
    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})
Exemple #16
0
    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})
Exemple #17
0
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")
Exemple #18
0
    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})
Exemple #19
0
    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})
Exemple #20
0
    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,
                    })