示例#1
0
文件: auth.py 项目: riv2q/volontulo
    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')
示例#2
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")
    def setUpTestData(cls):
        """Set up data for all tests."""
        for i in range(1, 6):
            Organization.objects.create(
                name='Organization {0} name'.format(i),
                address='Organization {0} address'.format(i),
                description='Organization {0} description'.format(i),
            )

        organizations = Organization.objects.all()
        for idx, org in enumerate(organizations):
            for i in range(1, 6):
                user = User.objects.create_user(
                    'volunteer{0}{1}@example.com'.format(idx + 1, i),
                    'volunteer{0}{1}@example.com'.format(idx + 1, i),
                    'password',
                )
                userprofile = UserProfile(user=user)
                userprofile.save()
                userprofile.organizations.add(org)
                userprofile.save()

        for idx, org in enumerate(organizations):
            for i in range(0, idx + 1):
                Offer.objects.create(
                    organization=org,
                    benefits='Offer {0}-{1} benefits'.format(idx + 1, i),
                    location='Offer {0}-{1} location'.format(idx + 1, i),
                    title='Offer {0}-{1} title'.format(idx + 1, i),
                    time_period='',
                    description='',
                    requirements='',
                    time_commitment='',
                    offer_status='published',
                    recruitment_status='closed',
                    action_status='finished',
                    started_at='2010-10-10 10:10:10',
                    finished_at='2012-12-12 12:12:12'
                )
示例#4
0
    def setUpTestData(cls):
        """Set up data for all tests."""
        for i in range(1, 6):
            Organization.objects.create(
                name='Organization {0} name'.format(i),
                address='Organization {0} address'.format(i),
                description='Organization {0} description'.format(i),
            )

        organizations = Organization.objects.all()
        for idx, org in enumerate(organizations):
            for i in range(1, 6):
                user = User.objects.create_user(
                    'volunteer{0}{1}@example.com'.format(idx + 1, i),
                    'volunteer{0}{1}@example.com'.format(idx + 1, i),
                    'password',
                )
                userprofile = UserProfile(user=user)
                userprofile.save()
                userprofile.organizations.add(org)
                userprofile.save()

        for idx, org in enumerate(organizations):
            for i in range(0, idx + 1):
                Offer.objects.create(
                    organization=org,
                    benefits='Offer {0}-{1} benefits'.format(idx + 1, i),
                    location='Offer {0}-{1} location'.format(idx + 1, i),
                    title='Offer {0}-{1} title'.format(idx + 1, i),
                    time_period='',
                    description='',
                    requirements='',
                    time_commitment='',
                    offer_status='published',
                    recruitment_status='closed',
                    action_status='finished',
                    started_at='2010-10-10 10:10:10',
                    finished_at='2012-12-12 12:12:12'
                )
示例#5
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)
示例#6
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,
            })