Example #1
0
    def get(self, request, **kwargs):
        user = get_object_or_404(User, id=kwargs['id'])
        form = UserForm(instance=user)

        self.context['form'] = form
        self.context['user'] = user
        return render(request, self.template_name, self.context)
Example #2
0
def signup(request):
    context = {'user_form': UserForm, 'profile_form': ProfileForm}
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save(commit=False)
            user.password = make_password(user.password)
            profile = profile_form.save(commit=False)
            group_role = Role.objects.get(id=profile.role.id)
            if group_role.description == 'Arrendatario':
                user.is_staff = True
                user.save()
                group = Group.objects.get(name='Arrendador')
                group.user_set.add(user)
            user.save()
            profile.user = user
            profile.save()
            return redirect('login')
        else:
            messages.error(
                request,
                'Uno o mas campos requeridos no fueron enviados, completa los campos requeridos'
            )
            return render(request, 'signup.html', context)

    return render(request, 'signup.html', context)
Example #3
0
def create(req):
    try:
        new_user = UserForm(req.POST)
        print(new_user)
        new_user.save()
        return redirect(reverse("users:index"))
    except:
        messages.error(req, "Unable to create new user")
        return redirect(reverse("users:index"))
Example #4
0
def create(request):
    form = UserForm(request.POST or None, user=request.user)
    if request.method == 'POST':
        if form.is_valid():
            user = form.save()
            for g in request.POST.getlist('groups'):
                group = Group.objects.get(pk=g)
                user.groups.add(group)
            return redirect('users:users')
    return render(request, 'user_form.html', {'form': form})
Example #5
0
def update(req):
    try:
        id = req.POST["id"]
        old_instance = User.objects.get(id=id)
        updated = UserForm(req.POST, instance=old_instance)
        updated.save()
        return redirect(reverse("users:index"))
    except:
        messages.error(req, "There was an error updating user")
        return redirect(reverse("users:index"))
Example #6
0
    def post(self, request, **kwargs):
        user = get_object_or_404(User, id=kwargs['id'])
        form = UserForm(request.POST, instance=user)

        if form.is_valid():
            new_user = form.save(commit=False)
            new_user.set_password(request.POST.get('password'))
            new_user.save()
            messages.success(request, 'User successfully updated.')
            return redirect(reverse('user_view', kwargs={'id': user.id}))
        else:
            self.context['form'] = form
            messages.error(request, form.errors)
            return render(request, self.template_name, self.context)
Example #7
0
    def post(self, request):
        form = UserForm(request.POST)

        if form.is_valid():
            new_user = form.save(commit=False)
            new_user.set_password(request.POST.get('password'))
            new_user.save()

            messages.success(request, 'User successfully created.')
            return redirect(reverse('users_directory'))
        else:
            self.context['form'] = form
            messages.error(request, form.errors)
            return render(request, self.template_name, self.context)
Example #8
0
def edit(request, pk):
    instance = get_object_or_404(User, pk=pk)
    form = UserForm(request.POST or None, instance=instance, user=request.user)
    if request.method == 'POST':
        if form.is_valid():
            user = form.save(commit=False)
            password = request.POST.get('password1', '')

            if password == '':
                user.password = instance.password
            else:
                user.set_password(password)
                print(user.username)

            user = form.save()
            user.groups.clear()
            for g in request.POST.getlist('groups'):
                group = Group.objects.get(pk=g)
                user.groups.add(group)
            return redirect('users:users')
    return render(request, 'user_form.html', {'form': form})
Example #9
0
def user_profile(request):
    if request.method == 'POST':
        user = get_user(request)
        # The user-form atribute in the request represents changes were made to
        # the user information
        if "user-form" in request.POST:
            form = UserForm(request.POST)
            # The form is evaluated and if valid its updated
            if form.is_valid():
                username = form.cleaned_data.get('username')
                email = form.cleaned_data.get('email')
                first_name = form.cleaned_data.get('first_name')
                last_name = form.cleaned_data.get('last_name')
                if username:
                    User.objects.filter(id=user.id).update(username=username)
                if email:
                    User.objects.filter(id=user.id).update(email=email)
                if first_name:
                    User.objects.filter(id=user.id).update(
                        first_name=first_name)
                if last_name:
                    User.objects.filter(id=user.id).update(last_name=last_name)
                # A message is added so the user knows his information was
                # updated
                messages.add_message(request,
                                     messages.SUCCESS,
                                     'Perfil actualizado correctamente.',
                                     extra_tags={'user': '******'})
            # If there is an error with the inputs a message is added with the
            # errors
            else:
                for field in form:
                    for error in field.errors:
                        messages.add_message(request, messages.ERROR, error)
        # The profile-form atribute in the request represents changes were made
        # to the user profile
        elif "profile-form" in request.POST:
            form = UserProfileForm(request.POST)
            # The form is evaluated and if valid its updated
            if form.is_valid():
                birthday = form.cleaned_data.get('birthday')
                gender = form.cleaned_data.get('gender')
                height = form.cleaned_data.get('height')
                weight = form.cleaned_data.get('weight')
                elbow_diameter = form.cleaned_data.get('elbow_diameter')
                if birthday:
                    UserProfile.objects.filter(user=user).update(
                        birthday=birthday)
                if gender:
                    UserProfile.objects.filter(user=user).update(gender=gender)
                if height:
                    UserProfile.objects.filter(user=user).update(height=height)
                if weight:
                    UserProfile.objects.filter(user=user).update(weight=weight)
                if elbow_diameter:
                    UserProfile.objects.filter(user=user).update(
                        elbow_diameter=elbow_diameter)
                # A message is added so the user knows his profile was updated
                messages.add_message(request,
                                     messages.SUCCESS,
                                     'Perfil actualizado correctamente.',
                                     extra_tags=('profile'))
            # If there is an error with the inputs a message is added with the
            # errors
            else:
                for field in form:
                    for error in field.errors:
                        messages.add_message(request,
                                             messages.ERROR,
                                             error,
                                             extra_tags=('profile'))
        # User is redirected to the profile page
        return HttpResponseRedirect('/accounts/profile/')
    else:
        # The user profile is obtained and two empy forms for user information
        # and profile are created
        profile = request.user.get_profile()
        form_user = UserForm
        form_profile = UserProfileForm
        # Possible previous messages are collected, processed and added to the
        # context
        messages_temp = get_messages(request)
        profile_messages = False
        for message in messages_temp:
            if 'profile' in message.tags:
                profile_messages = True
        return render(
            request, 'profile/profile.html', {
                'profile': profile,
                'form_profile': form_profile,
                'form_user': form_user,
                'profile_messages': profile_messages
            })
Example #10
0
def edit(req, id):
    edit_user = UserForm()
    return render(req, "users/edit.html", {"id": id, "edit_user": edit_user})
Example #11
0
def new(req):
    create_user = UserForm()
    return render(req, 'users/new.html', {"new_user": create_user})