Esempio n. 1
0
    def post(request):
        """Method responsible for saving new offer.

        :param request: WSGIRequest instance
        """
        form = CreateOfferForm(request.POST)
        if form.is_valid():
            offer = form.save()
            offer.create_new()
            offer.save()
            save_history(request, offer, action=ADDITION)
            send_mail(request, 'offer_creation',
                      ['*****@*****.**'], {'offer': offer})
            messages.success(request, "Dziękujemy za dodanie oferty.")
            return redirect(
                'offers_view',
                slug=slugify(offer.title),
                id_=offer.id,
            )
        messages.error(
            request,
            "Formularz zawiera niepoprawnie wypełnione pola <br />{0}".format(
                '<br />'.join(form.errors)),
        )
        return render(
            request, 'offers/offer_form.html', {
                'form': form,
                'offer': Offer(),
                'organizations': request.user.userprofile.organizations.all(),
            })
Esempio n. 2
0
def contact_form(request):
    """View responsible for contact forms.

    :param request: WSGIRequest instance
    """
    if request.method == 'POST':
        form = AdministratorContactForm(request.POST)
        if form.is_valid():
            # get administrators by IDS
            administrator_id = request.POST.get('administrator')
            admin = User.objects.get(id=administrator_id)
            send_mail(
                request,
                'contact_to_admin',
                [
                    admin.email,
                    request.POST.get('email'),
                ],
                {k: v
                 for k, v in request.POST.items()},
            )
            messages.success(request, 'Email został wysłany.')
        else:
            errors = '<br />'.join(form.errors)
            messages.error(request,
                           'Proszę poprawić błędy w formularzu: ' + errors)
            return render(request, 'contact.html', {
                'contact_form': form,
            })

    form = AdministratorContactForm()
    return render(request, 'contact.html', {
        'contact_form': form,
    })
Esempio n. 3
0
def password_reset(request):
    """REST API reset password view"""
    serializer = UsernameSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    username = serializer.validated_data.get('username')
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        pass
    else:
        context = {
            'email': username,
            'domain': get_current_site(request).domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'user': user,
            'token': default_token_generator.make_token(user),
            'protocol': 'https' if request.is_secure() else 'http',
        }
        send_mail(
            request,
            'password_reset',
            [username],
            context=context,
            send_copy_to_admin=False)
    return Response(dict(), status=status.HTTP_201_CREATED)
Esempio n. 4
0
def organization_view(request, slug, id_):
    u"""View responsible for viewing organization."""
    org = get_object_or_404(Organization, id=id_)
    offers = Offer.objects.filter(organization_id=id_)
    allow_contact = True
    allow_edit = False
    allow_offer_create = False
    if (
            request.user.is_authenticated() and
            request.user.userprofile in org.userprofiles.all()
    ):
        allow_contact = False
        allow_edit = True
        allow_offer_create = True

    if request.method == 'POST':
        form = VolounteerToOrganizationContactForm(request.POST)
        if form.is_valid():
            # send email to first organization user (I assume it's main user)
            profile = Organization.objects.get(id=id_).userprofiles.all()[0]
            send_mail(
                request,
                'volunteer_to_organisation',
                [
                    profile.user.email,
                    request.POST.get('email'),
                ],
                {k: v for k, v in request.POST.items()},
            )
            messages.success(request, u'Email został wysłany.')
        else:
            messages.error(
                request,
                u"Formularz zawiera nieprawidłowe dane: {}".format(form.errors)
            )
            return render(
                request,
                "organizations/organization_view.html",
                {
                    'organization': org,
                    'contact_form': form,
                    'offers': offers,
                    'allow_contact': allow_contact,
                    'allow_edit': allow_edit,
                    'allow_offer_create': allow_offer_create,
                },
            )
    return render(
        request,
        "organizations/organization_view.html",
        {
            'organization': org,
            'contact_form': VolounteerToOrganizationContactForm(),
            'offers': offers,
            'allow_contact': allow_contact,
            'allow_edit': allow_edit,
            'allow_offer_create': allow_offer_create,
        }
    )
Esempio n. 5
0
def organization_view(request, slug, id_):  # pylint: disable=unused-argument
    """View responsible for viewing organization."""
    org = get_object_or_404(Organization, id=id_)
    offers = Offer.objects.filter(organization_id=id_)
    allow_contact = True
    allow_edit = False
    allow_offer_create = False
    if (
            request.user.is_authenticated() and
            request.user.userprofile in org.userprofiles.all()
    ):
        allow_contact = False
        allow_edit = True
        allow_offer_create = True

    if request.method == 'POST':
        form = VolounteerToOrganizationContactForm(request.POST)
        if form.is_valid():
            send_mail(
                request,
                'volunteer_to_organisation',
                [
                    userprofile.user.email
                    for userprofile in org.userprofiles.all()
                ],
                {k: v for k, v in request.POST.items()},
            )
            messages.success(request, "Email został wysłany.")
        else:
            messages.error(
                request,
                "Formularz zawiera nieprawidłowe dane: {errors}".format(
                    errors=form.errors
                )
            )
            return render(
                request,
                "organizations/organization_view.html",
                {
                    'organization': org,
                    'contact_form': form,
                    'offers': offers,
                    'allow_contact': allow_contact,
                    'allow_edit': allow_edit,
                    'allow_offer_create': allow_offer_create,
                },
            )
    return render(
        request,
        "organizations/organization_view.html",
        {
            'organization': org,
            'contact_form': VolounteerToOrganizationContactForm(),
            'offers': offers,
            'allow_contact': allow_contact,
            'allow_edit': allow_edit,
            'allow_offer_create': allow_offer_create,
        }
    )
Esempio n. 6
0
def organization_view(request, slug, id_):
    u"""View responsible for viewing organization."""
    org = get_object_or_404(Organization, id=id_)
    offers = Offer.objects.filter(organization_id=id_)
    allow_contact = True
    allow_edit = False
    allow_offer_create = False
    if (request.user.is_authenticated()
            and request.user.userprofile in org.userprofiles.all()):
        allow_contact = False
        allow_edit = True
        allow_offer_create = True

    if request.method == 'POST':
        form = VolounteerToOrganizationContactForm(request.POST)
        if form.is_valid():
            # send email to first organization user (I assume it's main user)
            profile = Organization.objects.get(id=id_).userprofiles.all()[0]
            send_mail(
                request,
                'volunteer_to_organisation',
                [
                    profile.user.email,
                    request.POST.get('email'),
                ],
                {k: v
                 for k, v in request.POST.items()},
            )
            messages.success(request, u'Email został wysłany.')
        else:
            messages.error(
                request, u"Formularz zawiera nieprawidłowe dane: {}".format(
                    form.errors))
            return render(
                request,
                "organizations/organization_view.html",
                {
                    'organization': org,
                    'contact_form': form,
                    'offers': offers,
                    'allow_contact': allow_contact,
                    'allow_edit': allow_edit,
                    'allow_offer_create': allow_offer_create,
                },
            )
    return render(
        request, "organizations/organization_view.html", {
            'organization': org,
            'contact_form': VolounteerToOrganizationContactForm(),
            'offers': offers,
            'allow_contact': allow_contact,
            'allow_edit': allow_edit,
            'allow_offer_create': allow_offer_create,
        })
Esempio n. 7
0
    def post(cls, request):
        u"""Method handles creation of new user.

        :param request: WSGIRequest instance
        """
        # validation of register form:
        user_form = UserForm(request.POST)
        if not user_form.is_valid():
            messages.error(
                request,
                u'Wprowadzono nieprawidłowy email, hasło lub nie wyrażono '
                u'zgody na przetwarzanie danych osobowych.')
            return cls.get(request, user_form)

        username = request.POST.get('email')
        password = request.POST.get('password')

        ctx = {}

        # attempt of new user creation:
        try:
            user = User.objects.create_user(
                username=username,
                email=username,
                password=password,
            )
            user.is_active = False
            user.save()
            profile = UserProfile(user=user)
            ctx['uuid'] = profile.uuid
            profile.save()
        except IntegrityError:
            # if attempt failed, because user already exists we need show
            # error message:
            messages.info(request, u'Użytkownik o podanym emailu już istnieje')
            return cls.get(request, user_form)

        # sending email to user:
        send_mail(request, 'registration', [user.email], context=ctx)

        # automatically login new user:
        user = auth.authenticate(username=username, password=password)
        auth.login(request, user)

        # show info about successful creation of new user and redirect to
        # homepage:
        messages.success(request, u'Rejestracja przebiegła pomyślnie')
        messages.info(
            request, u'Na podany przy rejestracji email został wysłany link '
            u'aktywacyjny. Aby w pełni wykorzystać konto należy je aktywować '
            u'poprzez kliknięcie linku lub wklejenie go w przeglądarce.')
        return redirect('homepage')
Esempio n. 8
0
    def post(cls, request):
        """Method handles creation of new user.

        :param request: WSGIRequest instance
        """
        # validation of register form:
        user_form = UserForm(request.POST)
        if not user_form.is_valid():
            messages.error(
                request,
                "Wprowadzono nieprawidłowy email, hasło lub nie wyrażono " "zgody na przetwarzanie danych osobowych.",
            )
            return cls.get(request, user_form)

        username = request.POST.get("email")
        password = request.POST.get("password")

        ctx = {}

        # attempt of new user creation:
        try:
            user = User.objects.create_user(username=username, email=username, password=password)
            user.is_active = False
            user.save()
            profile = UserProfile(user=user)
            ctx["uuid"] = profile.uuid
            profile.save()
        except IntegrityError:
            # if attempt failed, because user already exists we need show
            # error message:
            messages.info(request, "Użytkownik o podanym emailu już istnieje")
            return cls.get(request, user_form)

        # sending email to user:
        send_mail(request, "registration", [user.email], context=ctx)

        # automatically login new user:
        user = auth.authenticate(username=username, password=password)
        auth.login(request, user)

        # show info about successful creation of new user and redirect to
        # homepage:
        messages.success(request, "Rejestracja przebiegła pomyślnie")
        messages.info(
            request,
            "Na podany przy rejestracji email został wysłany link "
            "aktywacyjny. Aby w pełni wykorzystać konto należy je aktywować "
            "poprzez kliknięcie linku lub wklejenie go w przeglądarce.",
        )
        return redirect("homepage")
Esempio n. 9
0
 def contact(request, pk):
     """Endpoint to send contact message to organization"""
     org = get_object_or_404(Organization, id=pk)
     serializer = OrganizationContactSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     send_mail(
         request,
         'volunteer_to_organisation',
         [
             user_profile.user.email
             for user_profile in org.userprofiles.all()
         ],
         serializer.validated_data,
     )
     return Response(data=True, status=status.HTTP_201_CREATED)
Esempio n. 10
0
    def post(self, request):
        """Sends contact email."""
        serializer = ContactSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        admin = User.objects.get(email=data['administrator_email'])
        # For now send the same message to both user and administrator
        send_mail(
            request,
            'contact_to_admin',
            [
                admin.email,
                data['applicant_email'],
            ],
            data,
            send_copy_to_admin=False,
        )
        return Response({}, status.HTTP_201_CREATED)
Esempio n. 11
0
def contact_form(request):
    u"""View responsible for contact forms.

    :param request: WSGIRequest instance
    """
    if request.method == 'POST':
        form = AdministratorContactForm(request.POST)
        if form.is_valid():
            # get administrators by IDS
            administrator_id = request.POST.get('administrator')
            admin = User.objects.get(id=administrator_id)
            send_mail(
                request,
                'volunteer_to_admin',
                [
                    admin.email,
                    request.POST.get('email'),
                ],
                {k: v for k, v in request.POST.items()},
            )
            messages.success(request, u'Email został wysłany.')
        else:
            errors = u'<br />'.join(form.errors)
            messages.error(
                request,
                u'Proszę poprawić błędy w formularzu: ' + errors
            )
            return render(
                request,
                "contact.html",
                {
                    'contact_form': form,
                }
            )

    form = AdministratorContactForm()
    return render(
        request,
        "contact.html",
        {
            'contact_form': form,
        }
    )
Esempio n. 12
0
    def post(request):
        u"""Method responsible for saving new offer.

        :param request: WSGIRequest instance
        """
        form = CreateOfferForm(request.POST)
        if form.is_valid():
            offer = form.save()
            offer.create_new()
            offer.save()
            save_history(request, offer, action=ADDITION)
            send_mail(
                request,
                'offer_creation',
                ['*****@*****.**'],
                {'offer': offer}
            )
            messages.success(request, u"Dziękujemy za dodanie oferty.")
            return redirect(
                'offers_view',
                slug=slugify(offer.title),
                id_=offer.id,
            )
        messages.error(
            request,
            u"Formularz zawiera niepoprawnie wypełnione pola <br />{0}".format(
                '<br />'.join(form.errors)),
        )
        return render(
            request,
            'offers/offer_form.html',
            {
                'form': form,
                'offer': Offer(),
                'organizations': request.user.userprofile.organizations.all(),
            }
        )
Esempio n. 13
0
def register_view(request):
    """REST API register view."""
    if request.user.is_authenticated():
        return Response(status=status.HTTP_400_BAD_REQUEST)

    email = request.data.get('email')
    password = request.data.get('password')
    try:
        user = User.objects.create_user(
            username=email,
            email=email,
            password=password,
            is_active=False,
        )
        user.save()
    except IntegrityError:
        return Response(status=status.HTTP_201_CREATED)

    profile = UserProfile(user=user)
    ctx = {'token': profile.uuid}
    profile.save()
    send_mail(request, 'registration', [user.email], context=ctx)

    return Response(status=status.HTTP_201_CREATED)
Esempio n. 14
0
    def post(request, slug, id_):  # pylint: disable=unused-argument
        """View responsible for saving join for particular offer."""
        form = OfferApplyForm(request.POST)
        offer = Offer.objects.get(id=id_)
        if form.is_valid():
            if request.user.is_authenticated():
                user = request.user
            else:
                user = User.objects.filter(
                    email=request.POST.get('email')).exists()

                if user:
                    messages.info(request,
                                  "Zaloguj się, aby zapisać się do oferty.")
                    return redirect('{login}?next={path}'.format(
                        login=reverse('login'), path=request.path))

                messages.info(request,
                              "Zarejestruj się, aby zapisać się do oferty.")
                return redirect('register')

            has_applied = Offer.objects.filter(
                volunteers=user,
                volunteers__offer=id_,
            ).count()
            if has_applied:
                messages.error(
                    request, "Już wyraziłeś chęć uczestnictwa w tej ofercie.")
                return redirect('offers_list')

            offer.volunteers.add(user)
            offer.save()
            send_mail(
                request, 'offer_application', [
                    userprofile.user.email
                    for userprofile in offer.organization.userprofiles.all()
                ],
                dict(
                    email=request.POST.get('email'),
                    phone_no=request.POST.get('phone_no'),
                    fullname=request.POST.get('fullname'),
                    comments=request.POST.get('comments'),
                    offer=offer,
                ))
            messages.success(request,
                             "Zgłoszenie chęci uczestnictwa zostało wysłane.")
            return redirect(
                'offers_view',
                slug=slugify(offer.title),
                id_=offer.id,
            )
        else:
            errors = "<br />".join(form.errors)
            messages.error(request,
                           "Formularz zawiera nieprawidłowe dane" + errors)
            volunteer_user = UserProfile()
            if request.user.is_authenticated():
                volunteer_user = request.user.userprofile
            return render(request, 'offers/offer_apply.html', {
                'offer': offer,
                'form': form,
                'volunteer_user': volunteer_user,
            })
Esempio n. 15
0
    def post(request, slug, id_):  # pylint: disable=unused-argument
        """View responsible for saving join for particular offer."""
        form = OfferApplyForm(request.POST)
        offer = Offer.objects.get(id=id_)
        if form.is_valid():
            if request.user.is_authenticated():
                user = request.user
            else:
                user = User.objects.filter(
                    email=request.POST.get('email')
                ).exists()

                if user:
                    messages.info(
                        request,
                        'Zaloguj się, aby zapisać się do oferty.'
                    )
                    return redirect(
                        reverse('login') + '?next={}'.format(request.path)
                    )
                else:
                    messages.info(
                        request,
                        'Zarejestruj się, aby zapisać się do oferty.'
                    )
                    return redirect('register')

            has_applied = Offer.objects.filter(
                volunteers=user,
                volunteers__offer=id_,
            ).count()
            if has_applied:
                messages.error(
                    request,
                    u'Już wyraziłeś chęć uczestnictwa w tej ofercie.'
                )
                return redirect('offers_list')

            offer.volunteers.add(user)
            offer.save()

            send_mail(
                request,
                'offer_application',
                [
                    user.email,
                    request.POST.get('email'),
                ],
                dict(
                    email=request.POST.get('email'),
                    phone_no=request.POST.get('phone_no'),
                    fullname=request.POST.get('fullname'),
                    comments=request.POST.get('comments'),
                    offer=offer,
                )
            )
            messages.success(
                request,
                u'Zgłoszenie chęci uczestnictwa zostało wysłane.'
            )
            return redirect(
                'offers_view',
                slug=slugify(offer.title),
                id_=offer.id,
            )
        else:
            errors = '<br />'.join(form.errors)
            messages.error(
                request,
                u'Formularz zawiera nieprawidłowe dane' + errors
            )
            volunteer_user = UserProfile()
            if request.user.is_authenticated():
                volunteer_user = request.user.userprofile
            return render(
                request,
                'offers/offer_apply.html',
                {
                    'offer': offer,
                    'form': form,
                    'volunteer_user': volunteer_user,
                }
            )
Esempio n. 16
0
    def post(request, slug, id_):  # pylint: disable=unused-argument
        u"""View responsible for saving join for particular offer."""
        form = OfferApplyForm(request.POST)
        offer = Offer.objects.get(id=id_)
        if form.is_valid():
            if request.user.is_authenticated():
                user = request.user
            else:
                try:
                    user = User.objects.create_user(
                        username=request.POST.get('email'),
                        email=request.POST.get('email'),
                        password=User.objects.make_random_password(),
                    )
                    profile = UserProfile(user=user)
                    profile.save()
                except IntegrityError:
                    messages.info(
                        request, u'Użytkownik o podanym emailu już istnieje.'
                        u' Zaloguj się.')
                    return render(
                        request, 'offers/offer_apply.html', {
                            'offer': offer,
                            'form': form,
                            'volunteer_user': UserProfile(),
                        })

            has_applied = Offer.objects.filter(
                volunteers=user,
                volunteers__offer=id_,
            ).count()
            if has_applied:
                messages.error(
                    request, u'Już wyraziłeś chęć uczestnictwa w tej ofercie.')
                return redirect('offers_list')

            offer_content_type = ContentType.objects.get(app_label='volontulo',
                                                         model='offer')

            offer.volunteers.add(user)
            UserBadges.apply_participant_badge(
                offer_content_type,
                user.userprofile,
            )
            offer.save()

            send_mail(
                request, 'offer_application', [
                    user.email,
                    request.POST.get('email'),
                ],
                dict(
                    email=request.POST.get('email'),
                    phone_no=request.POST.get('phone_no'),
                    fullname=request.POST.get('fullname'),
                    comments=request.POST.get('comments'),
                    offer=offer,
                ))
            messages.success(
                request, u'Zgłoszenie chęci uczestnictwa zostało wysłane.')
            return redirect(
                'offers_view',
                slug=slugify(offer.title),
                id_=offer.id,
            )
        else:
            errors = '<br />'.join(form.errors)
            messages.error(request,
                           u'Formularz zawiera nieprawidłowe dane' + errors)
            volunteer_user = UserProfile()
            if request.user.is_authenticated():
                volunteer_user = request.user.userprofile
            return render(request, 'offers/offer_apply.html', {
                'offer': offer,
                'form': form,
                'volunteer_user': volunteer_user,
            })