Beispiel #1
0
    def post(self, request):

        try:
            user, profile = user_and_profile(request)
        except ProfileHttp404:
            return HttpResponseRedirect(reverse('server_cas:signIn'))

        form = UpdateAccountForm(request.POST, instance=user)

        if not form.is_valid():
            return render_with_info_profile(request,
                                            self.template,
                                            context={'form': form})

        try:
            with transaction.atomic():

                if form.new_password:
                    user.set_password(form.new_password)
                    user.save()
                    logout(request)
                    login(request,
                          user,
                          backend='django.contrib.auth.backends.ModelBackend')

                for field in form.Meta.profile_fields:
                    setattr(profile, field, form.cleaned_data[field])
                profile.save()

                for field in form.Meta.user_fields:
                    setattr(user, field, form.cleaned_data[field])
                user.save()

                CkanHandler.update_user(user)

        except ValidationError as e:
            messages.error(request, e.message)
            return render_with_info_profile(request,
                                            self.template,
                                            context={'form': form})
        except CkanBaseError as e:
            form.add_error('__all__', e.__str__())
            messages.error(request, e.__str__())
            return render_with_info_profile(request,
                                            self.template,
                                            context={'form': form})

        messages.success(request, 'Votre compte a bien été mis à jour.')

        return render_with_info_profile(request,
                                        self.template,
                                        context={'form': form},
                                        status=200)
Beispiel #2
0
    def get(self, request):

        try:
            user, profile = user_and_profile(request)
        except ProfileHttp404:
            return HttpResponseRedirect(reverse('server_cas:signIn'))

        return render_with_info_profile(
            request, self.template, {'form': UpdateAccountForm(instance=user)})
Beispiel #3
0
def handle_pust_request(request, username=None):

    user = None
    if username:
        user = get_object_or_404(User, username=username)

    query_data = getattr(request, request.method)  # QueryDict

    # `first_name` est obligatoire
    first_name = query_data.pop('first_name', user and [user.first_name])
    if first_name:
        query_data.__setitem__('first_name', first_name[-1])

    # `last_name` est obligatoire
    last_name = query_data.pop('last_name', user and [user.last_name])
    if last_name:
        query_data.__setitem__('last_name', last_name[-1])

    # `email` est obligatoire
    email = query_data.pop('email', user and [user.email])
    if email:
        query_data.__setitem__('email', email[-1])

    # organisation
    organisation_slug = query_data.pop('organisation', None)
    if organisation_slug:
        try:
            organisation = Organisation.objects.get(slug=organisation_slug[-1])
        except Organisation.DoesNotExist as e:
            raise GenericException(details=e.__str__())
    elif user and user.profile:
        organisation = user.profile.organisation
    else:
        organisation = None
    if organisation:
        query_data.__setitem__('organisation', organisation.pk)

    password = query_data.pop('password', None)
    if password:
        query_data.__setitem__('password1', password[-1])
        query_data.__setitem__('password2', password[-1])

    if user:
        form = UpdateAccountForm(query_data, instance=user)
    else:
        form = SignUpForm(query_data, unlock_terms=True)
    if not form.is_valid():
        raise GenericException(details=form._errors)
    try:
        with transaction.atomic():
            if user:
                phone = form.cleaned_data.pop('phone', None)
                for k, v in form.cleaned_data.items():
                    if k == 'password':
                        # user.set_password(v)
                        pass
                    else:
                        setattr(user, k, v)
                user.save()
                if phone:
                    user.profile.phone = phone
                    user.profile.save
                CkanHandler.update_user(user)
            else:
                user = User.objects.create_user(**form.cleaned_user_data)
                profile_data = {**form.cleaned_profile_data, **{'user': user, 'is_active': True}}
                Profile.objects.create(**profile_data)
                CkanHandler.add_user(user, form.cleaned_user_data['password'], state='active')
    except (ValidationError, CkanBaseError) as e:
        raise GenericException(details=e.__str__())

    if organisation:
        user.profile.membership = True
        user.profile.save(update_fields=['membership'])

    # contribute
    contribute_for = None
    contribute_for_slugs = query_data.pop('contribute', [])
    if contribute_for_slugs:
        try:
            contribute_for = Organisation.objects.filter(slug__in=contribute_for_slugs)
        except Organisation.DoesNotExist as e:
            raise GenericException(details=e.__str__())

    # referent
    referent_for = None
    referent_for_slugs = query_data.pop('referent', None)
    if referent_for_slugs:
        try:
            referent_for = Organisation.objects.filter(slug__in=referent_for_slugs)
        except Organisation.DoesNotExist as e:
            raise GenericException(details=e.__str__())

    if contribute_for:
        for organisation in contribute_for:
            try:
                LiaisonsContributeurs.objects.get_or_create(
                    profile=user.profile, organisation=organisation,
                    defaults={'validated_on': timezone.now()}
                )
            except IntegrityError:
                pass
            else:
                AccountActions.objects.get_or_create(
                    action='confirm_contribution', organisation=organisation,
                    profile=user.profile, defaults={'closed': timezone.now()}
                )

    if referent_for:
        for organisation in referent_for:
            try:
                LiaisonsReferents.objects.get_or_create(
                    profile=user.profile, organisation=organisation,
                    defaults={'validated_on': timezone.now()})
            except IntegrityError:
                pass
            else:
                AccountActions.objects.get_or_create(
                    action='confirm_referent', organisation=organisation,
                    profile=user.profile, defaults={'closed': timezone.now()}
                )

    return user
Beispiel #4
0
def handle_pust_request(request, username=None):

    user = None
    if username:
        user = get_object_or_404(User, username=username)

    data = getattr(request, request.method).dict()

    organisation = data.get('organisation')
    if organisation:
        try:
            organisation = Organisation.objects.get(slug=organisation).pk
        except Organisation.DoesNotExist:
            details = {'organisation': ["L'organisation n'existe pas."]}
            raise GenericException(details=details)

    data_form = {
        'username': data.get('username'),
        'first_name': data.get('first_name'),
        'last_name': data.get('last_name'),
        'email': data.get('email'),
        'phone': data.get('phone'),
        'organisation': organisation,
        'password1': data.get('password'),
        'password2': data.get('password'),
    }

    if username:
        form = UpdateAccountForm(data_form, instance=user)
    else:
        form = SignUpForm(data_form, unlock_terms=True)
    if not form.is_valid():
        raise GenericException(details=form._errors)
    try:
        with transaction.atomic():
            if username:
                phone = form.cleaned_data.pop('phone', None)
                for k, v in form.cleaned_data.items():
                    setattr(user, k, v)
                user.save()
                if phone:
                    user.profile.phone = phone
                    user.profile.save
                CkanHandler.update_user(user)
            else:
                user = User.objects.create_user(**form.cleaned_user_data)
                profile_data = {
                    **form.cleaned_profile_data,
                    **{
                        'user': user,
                        'is_active': True
                    }
                }
                Profile.objects.create(**profile_data)
                CkanHandler.add_user(user,
                                     form.cleaned_user_data['password'],
                                     state='active')
    except (ValidationError, CkanBaseError) as e:
        raise GenericException(details=e.__str__())

    return user
Beispiel #5
0
    def get(self, request):
        user = request.user

        return render(request, self.template,
                      {'form': UpdateAccountForm(instance=user)})