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)
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})
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)
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)
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)
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)
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})
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 )
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 })
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 } )