def create_user(): form = UserForm() if form.validate_on_submit(): try: username = form.username.data email = form.email.data password = form.password.data user = User(username=username, email=email, password=bcrypt.generate_password_hash( password).decode('utf-8')) db.session.add(user) db.session.commit() return redirect("/") except: flash('Введенный email уже существует') return render_template("signup.html", form=form)
def setup_1_confirm_profile(request): user = request.user profile = request.user.profile if request.method == 'GET': user_form = UserForm(instance=user) profile_form = ConfirmProfileForm(instance=profile) context = { 'user': user, 'profile': profile, 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'get_together/new_user/setup_1_confirm_profile.html', context) elif request.method == 'POST': user_form = UserForm(request.POST, instance=user) profile_form = ConfirmProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and profile_form.is_valid(): saved_user = user_form.save() profile_form.save() if saved_user.email is not None and saved_user.email != '' and not saved_user.account.is_email_confirmed: # Call the view to trigger sending a confirmation email, but ignore it's response user_send_confirmation_email(request) return redirect('setup-2-pick-categories') else: return redirect('home')
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_image' in request.FILES: profile.profile_image = request.FILES['profile_image'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render( request, 'events/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def edit_profile(request): if not request.user.is_authenticated: messages.add_message( request, messages.WARNING, message=_('You must be logged in to edit your profile.')) return redirect('login') user = request.user profile = request.user.profile account = request.user.account if request.method == 'GET': user_form = UserForm(instance=user) profile_form = UserProfileForm(instance=profile) context = { 'user': user, 'profile': profile, 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'get_together/users/edit_profile.html', context) elif request.method == 'POST': old_email = request.user.email user_form = UserForm(request.POST, instance=user) profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() profile = profile_form.save() if user.email != old_email: if user.email is None or user.email == "": messages.add_message( request, messages.ERROR, message=_('Your email address has been removed.')) account.is_email_confirmed = False account.save() else: messages.add_message( request, messages.WARNING, message= _('Your email address has changed, please confirm your new address.' )) return redirect('send-confirm-email') return redirect('show-profile', profile.id) else: context = { 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'get_together/users/edit_profile.html', context) else: return redirect('home')
def edit_profile(request): if not request.user.is_authenticated: messages.add_message( request, messages.WARNING, message=_("You must be logged in to edit your profile."), ) return redirect("login") user = request.user profile = request.user.profile account = request.user.account if request.method == "GET": user_form = UserForm(instance=user) profile_form = UserProfileForm(instance=profile) context = { "user": user, "profile": profile, "user_form": user_form, "profile_form": profile_form, } return render(request, "get_together/users/edit_profile.html", context) elif request.method == "POST": old_email = request.user.email user_form = UserForm(request.POST, instance=user) profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() profile = profile_form.save() if user.email != old_email: if user.email is None or user.email == "": messages.add_message( request, messages.ERROR, message=_("Your email address has been removed."), ) account.is_email_confirmed = False account.save() else: messages.add_message( request, messages.WARNING, message= _("Your email address has changed, please confirm your new address." ), ) return redirect("send-confirm-email") return redirect("show-profile", profile.id) else: context = {"user_form": user_form, "profile_form": profile_form} return render(request, "get_together/users/edit_profile.html", context) else: return redirect("home")
def setup_1_confirm_profile(request): user = request.user profile = request.user.profile if request.method == "GET": user_form = UserForm(instance=user) profile_form = ConfirmProfileForm(instance=profile) context = { "user": user, "profile": profile, "user_form": user_form, "profile_form": profile_form, } return render( request, "get_together/new_user/setup_1_confirm_profile.html", context ) elif request.method == "POST": user_form = UserForm(request.POST, instance=user) profile_form = ConfirmProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and profile_form.is_valid(): saved_user = user_form.save() profile_form.save() if ( saved_user.email is not None and saved_user.email != "" and not saved_user.account.is_email_confirmed ): # Call the view to trigger sending a confirmation email, but ignore it's response user_send_confirmation_email(request) # Mark the account as completed setup, the rest of the steps are optional request.user.account.setup_complete() return redirect("setup-2-pick-categories") else: context = { "user": user, "profile": profile, "user_form": user_form, "profile_form": profile_form, } return render( request, "get_together/new_user/setup_1_confirm_profile.html", context ) else: return redirect("home")
def edit_profile(request): if not request.user.is_authenticated: messages.add_message( request, messages.WARNING, message=_('You must be logged in to edit your profile.')) return redirect('login') user = request.user profile = request.user.profile if request.method == 'GET': user_form = UserForm(instance=user) profile_form = UserProfileForm(instance=profile) context = { 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'get_together/users/edit_profile.html', context) elif request.method == 'POST': user_form = UserForm(request.POST, instance=user) profile_form = UserProfileForm(request.POST, instance=profile) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() profile = profile_form.save() return redirect('home') else: context = { 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'get_together/users/edit_profile.html', context) else: return redirect('home')
def register(request): #test if cookies work if request.session.test_cookie_worked(): print ">>>> TEST COOKIE WORKED!" request.session.delete_test_cookie() # 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) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_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() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.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, profile_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() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'events/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)