コード例 #1
0
ファイル: views.py プロジェクト: abulnes16/adopta_un_foraneo
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)
コード例 #2
0
ファイル: views.py プロジェクト: ccunnin8/django
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"))
コード例 #3
0
ファイル: views.py プロジェクト: diegofdoruiz/diuproject
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})
コード例 #4
0
ファイル: views.py プロジェクト: ccunnin8/django
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"))
コード例 #5
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)
コード例 #6
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)
コード例 #7
0
ファイル: new.py プロジェクト: kamalhg/time-sheet
def New(request):
    template_file = "user-new.html"
    context = RequestContext(request)

    form = UserForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        mymodel = form.save()

        msg = u"User successfully created"
        messages.add_message(request, messages.SUCCESS, msg)
        return redirect("user:home")

    params = {"form": form}

    return render_to_response(template_file, params, context_instance=context)
コード例 #8
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)
コード例 #9
0
ファイル: views.py プロジェクト: aftaberski/plangenius
def sign_up(request):
    # Like before, get the request's context.
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Update our variable to tell the template registration was successful.
            registered = True

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print user_form.errors

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()

    # Render the template depending on the context.
    return render_to_response(
            'users/sign_up.html',
            {'user_form': user_form, 'registered': registered},
            context)
コード例 #10
0
ファイル: views.py プロジェクト: diegofdoruiz/diuproject
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})
コード例 #11
0
ファイル: edit.py プロジェクト: kamalhg/time-sheet
def Edit(request,user_id):
    template_file = "user-edit.html"
    context = RequestContext(request)

    obj = User.objects.get(pk=user_id)

    form = UserForm(request.POST or None, request.FILES or None,instance=obj)
    if form.is_valid():
        mymodel = form.save()

        msg = u"User saved successfully"
        messages.add_message(request, messages.SUCCESS, msg)
        return redirect("user:home")

    params = {
        'form': form,
        'user_id': user_id,
    }

    return render_to_response (
        template_file,
        params,
        context_instance = context
    )
コード例 #12
0
    async def patch(self, *args, **kwargs):
        user = self.current_user
        res = {}
        data = self.request.body.decode("utf-8")
        data = json.loads(data)
        form = UserForm.from_json(data)
        if form.validate():

            username = form.username.data
            #判断用户名是否重复
            query = UserProfile.select().where(
                UserProfile.username == username)
            count = await self.application.objects.count(query)
            if username == user.username:
                count = 0

            if count:
                res = {"username": "******"}
                self.set_status(400)

            else:
                user.gender = form.gender.data
                user.bio = form.bio.data
                user.birthday = form.birthday.data
                user.address = form.address.data
                user.username = username
                await self.application.objects.update(user)
                res = {
                    "id": user.id,
                    "username": user.username,
                    "gender": user.gender,
                    "bio": user.bio,
                    "birthday": user.birthday,
                    "address": user.address,
                    "icon": user.icon
                }

        else:
            self.set_status(400)
            for field in form.errors:
                res[field] = form.errors[field][0]

        self.finish(json.dumps(res, default=utils.json_serial))
コード例 #13
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
            })
コード例 #14
0
ファイル: views.py プロジェクト: dmallcott/C-Nutra
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
            }
        )
コード例 #15
0
ファイル: views.py プロジェクト: ccunnin8/django
def edit(req, id):
    edit_user = UserForm()
    return render(req, "users/edit.html", {"id": id, "edit_user": edit_user})
コード例 #16
0
ファイル: views.py プロジェクト: ccunnin8/django
def new(req):
    create_user = UserForm()
    return render(req, 'users/new.html', {"new_user": create_user})