def signup(request, template="main/register.html"): """ Signup form. """ """ Login form. """ login_form = LoginForm(prefix="login") signup_form = ProfileForm() if request.method == "POST": login_form = LoginForm(request.POST, prefix="login") signup_form = ProfileForm(request.POST) if not login_form.has_changed() and not request.POST.get( "from_popup", False): login_form = LoginForm(prefix="login") if not signup_form.has_changed(): signup_form = ProfileForm() if login_form.is_valid(): authenticated_user = login_form.save() messages.info(request, "Successfully logged in") auth_login(request, authenticated_user) return redirect('/') if signup_form.has_changed() and signup_form.is_valid(): #import pdb;pdb.set_trace() new_user = signup_form.save() messages.info(request, "Successfully signed up") auth_login(request, new_user) return redirect("/") context = {"login_form": login_form, "signup_form": signup_form} return render(request, template, context)
def manage_profile(request): profile, created = Profile.objects.get_or_create(user=request.user) form = ProfileForm(request.POST or None, instance=profile) if form.is_valid(): form.instance.save() return redirect(reverse('dashboard')) return render(request, 'main/profile/manage_profile.html', {'form': form})
def request_post_method_processing(request, args): post = copy.deepcopy(request.POST) if 'username' in post: post['username'] = post['username'].lower() user_form = UserCreationForm(post) profile_form = ProfileForm(post) email_form = UserChangeEmailForm(post) if user_form.is_valid() and profile_form.is_valid( ) and email_form.is_valid(): user = user_form.save(commit=False) profile = profile_form.save(commit=False) profile.user = user user.email = request.POST.get('email', '') user.save() profile.save() birth_date = datetime.datetime.strptime(post['birthday'], '%d.%m.%Y').date() birthday = BirthDate() birthday.birthday = birth_date birthday.profile = profile birthday.save() profile_photo = ProfilePhoto() profile_photo.profile = profile profile_photo.photo = 'images/photo.svg' profile_photo.save() # Убрать, если не нужна автоматическая авторизация после регистрации пользователя auth.login(request, user) return redirect('/') else: args['user_form'] = user_form args['profile_form'] = profile_form args['email_form'] = email_form
def post(self, request): form = ProfileForm(request.POST, request.FILES, instance=request.user) if 'save' in request.POST: if form.is_valid(): form.save() username = form.cleaned_data.get('username') request.user.username = username request.user.bio = form.cleaned_data.get('bio') request.user.profile_img = form.cleaned_data.get('profile_img') request.user.save() return redirect('account')
def profile_settings(request): if request.method == "POST": profileform = ProfileForm(request.POST, instance=request.user.profile) if profileform.is_valid(): profileform.save() messages.success(request, _("Profile settings successfully updated")) return redirect("profile_settings") messages.error(request, _("Please fix these errors and resubmit the form")) else: profileform = ProfileForm(instance=request.user.profile) return render_to_response("user/profile_settings.jinja", locals())
def profile(request, template='main/account/profile.html'): instance = request.user.driver.car if hasattr(request.user.driver,'car') else None carform = CarForm(prefix='car', user=request.user, instance=instance) if request.method == "POST": userform = ProfileForm(request.POST, prefix='user', instance=request.user) if userform.is_valid(): user = userform.save() messages.info(request, _("Your personal page has been updated!")) else: userform = ProfileForm(prefix='user', instance=request.user) context = {"userform": userform, 'carform': carform} return render(request, template, context)
def settings(request): user = request.user if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.email = form.cleaned_data.get('email') user.save() messages.add_message(request, messages.SUCCESS, 'Your profile was successfully edited.') else: form = ProfileForm(instance=user) return render(request, 'main/settings.html', {'form': form})
def create_profile(request): if Profile.objects.filter(user=request.user).exists(): messages.add_message(request, messages.INFO, 'A Profile already exists for user %s' % request.user.username) return HttpResponseRedirect(reverse('admin:index')) if request.method == "POST": profile_form = ProfileForm(request.POST, request.FILES) if profile_form.is_valid(): profile_form.save(user_id=request.user.pk) messages.add_message(request, messages.INFO, '%s, your Profile has been successfully created.' % request.user.username) return HttpResponseRedirect(reverse('admin:index')) else: profile_form = ProfileForm args = {'profile_form': profile_form} return render(request, 'profile/createprofile.html', args)
def profile_creation(username): form = ProfileForm() if form.validate_on_submit(): username = flask.session['user'] first_name = flask.session['first_name'] last_name = flask.session['last_name'] email = flask.session['email'] password = flask.session['password'] user = User(username=username, first_name=first_name, last_name=last_name, email=email, password=password) db.session.add(user) db.session.commit() user_id = user.id profile = Profile(weight=form.weight.data, height=form.height.data, goal=form.goal.data, age=form.age.data, gender=form.gender.data, location=form.location.data, quote=form.quote.data, user_id=user_id) db.session.add(profile) set_blank_macros(user_id) set_blank_schedule(user_id) db.session.commit() login_user(user) flash(f'You have successfully created your account!', 'success') return redirect(url_for('fitness_page')) return render_template('profile-creation.html', title="Create Your Profile", form=form)
def index(request): if Profile.objects.first(): budget = Profile.objects.first().budget expenses = Expenses.objects.all() total_expenses = 0 prices = [] for entry in expenses: total_expenses += entry.price prices.append(f'{entry.price:.2f}') context = { 'profiles': Profile.objects.all(), 'expenses': expenses, 'budget': f'{budget:.2f}', 'left': f'{(budget - total_expenses):.2f}', 'prices': prices, } return render(request, 'home-with-profile.html', context) if request.method == "GET": form = ProfileForm() return render(request, 'home-no-profile.html', {'form': form}) else: form = ProfileForm(request.POST) if form.is_valid(): # new_profile = form.save() # new_profile.save() form.save() return redirect('index') return render(request, 'home-no-profile.html', {'form': form})
def update_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) # image_form=ImageUploadForm(request.POST) if user_form.is_valid() and profile_form.is_valid() and image_form.is_valid(): user_form.save() profile_form.save() # m = ExampleModel.objects.get(pk=course_id) # m.model_pic = image_form.cleaned_data['image'] # m.save() # messages.success(request, _('Your profile was successfully updated!')) return redirect('home') # else: # messages.error(request, _('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) # image_form=ImageUploadForm() return render(request, 'profile.html', { 'user_form': user_form, 'profile_form': profile_form, # 'image_form': image_form }) # def upload_pic(request): # if request.method == 'POST': # form = ImageUploadForm(request.POST, request.FILES) # if form.is_valid(): # m = ExampleModel.objects.get(pk=course_id) # m.model_pic = form.cleaned_data['image'] # m.save() # return HttpResponse('image upload success') # return HttpResponseForbidden('allowed only via POST')
def update_profile(request): """ Update the current profile with a POST request. """ room_index = request.POST.get('room_index') profile_index = request.POST.get('profile_index') form = ProfileForm(request.POST, instance=get_object_or_404(Profile, room_index=room_index, profile_index=profile_index)) if form.is_valid(): form.save() config = Configuration.objects.get() profile = Profile.objects.get(room_index=room_index, profile_index=profile_index) config.active_profile = profile config.save() return HttpResponse(json.dumps({}), content_type="application/json")
def signup(request, template="main/register.html"): """ Signup form. """ """ Login form. """ login_form = LoginForm(prefix="login") #DriverImageFormSet = formset_factory(CarImageForm, max_num=6) signup_form = ProfileForm() #formset = DriverImageFormSet if request.method == "POST": #import pdb;pdb.set_trace() login_form = LoginForm(request.POST, prefix="login") signup_form = ProfileForm(request.POST, request.FILES) #import pdb;pdb.set_trace() if not login_form.has_changed() and not request.POST.get("from_popup",False): login_form = LoginForm(prefix="login") if not signup_form.has_changed(): signup_form = ProfileForm() if login_form.is_valid(): authenticated_user = login_form.save() messages.info(request, _("Successfully logged in")) auth_login(request, authenticated_user) return redirect('/') if signup_form.has_changed() and signup_form.is_valid(): #formset = DriverImageFormSet(request.POST, request.FILES) new_user = signup_form.save() Driver(user=new_user).save() #import pdb;pdb.set_trace() # #data = signup_form.cleaned_data #import pdb;pdb.set_trace() # f_img = Image.objects.create(image=request.FILES.get('featured_image')) # driver = Driver.objects.create(user=new_user, mobile=data.get('mobile',None), featured_image=f_img, sex=data.get('gender'))#set mobile and featured image # if formset.is_valid(): # for form in formset: # cd = form.cleaned_data # if cd: # dci = DriverImage(driver=driver, image=cd.get('image')) # dci.save() messages.info(request, _("Successfully signed up!")) auth_login(request, new_user) return redirect("/") #import pdb;pdb.set_trace() context = {"login_form": login_form, "signup_form": signup_form} return render(request, template, context)
def signup(request, template="main/register.html"): """ Signup form. """ loginform = LoginForm(prefix="login") signup_form = ProfileForm(prefix="signup") #import pdb; pdb.set_trace() if request.method == "POST": signup_form = ProfileForm(request.POST, prefix="signup") # if signup_form.is_valid(): new_user = signup_form.save() d = Driver(user=new_user) d.save() messages.info(request, _("Successfully signed up!")) auth_login(request, new_user) return redirect("home") context = {"loginform": loginform, "signup_form": signup_form} return render(request, template, context)
def edit_profile(request, id=None): """Edit profile page rendering""" form = ProfileForm(request.POST or None, instance=id and Profile.objects.get(id=id)) # Save edited profile if request.method == 'POST': valid = form.is_valid() if valid: form.save() if (request.is_ajax()): rdict = {'bad': 'false'} if not valid: rdict.update({'bad': 'true'}) d = {} for e in form.errors.iteritems(): d.update({e[0]: unicode(e[1])}) rdict.update({'errs': d}) json = simplejson.dumps(rdict, ensure_ascii=False) return HttpResponse(json, mimetype='application/javascript') else: return HttpResponseRedirect(reverse('main.views.index')) return render_to_response('edit_profile.html', {'form': form}, context_instance=RequestContext(request))
def edit_profile(request): user = Profile.objects.first() if request.method == "GET": form = ProfileForm(instance=user) return render(request, 'profile-edit.html', {'form': form}) else: form = ProfileForm(request.POST, instance=user) if form.is_valid(): # user = form.save() # user.save() form.save() return redirect('index') return render(request, 'profile-edit.html', {'form': form})
def update_profile(request): if request.method == 'POST': form1 = ProfileForm(request.POST, request.FILES, instance=request.user.profile) form2 = UpdateProfile(request.POST, instance=request.user) if form1.is_valid() and form2.is_valid(): form1.save() form2.save() messages.info(request, 'บันทึกข้อมูลแล้ว') return redirect('index') else: form1 = ProfileForm(instance=request.user.profile) form2 = UpdateProfile(instance=request.user) context = {'form1': form1, 'form2': form2} return render(request, 'profile.html', context=context)
def update_user(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, ('Your profile was sucessfull updated!')) else: messages.error(request, ('Please correct the error(s) below:')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'main/update_user.html', { 'user_form': user_form, 'profile_form': profile_form })
def profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return redirect('home') else: messages.error(request, 'invalid sign up') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def profile(request): courses = Course.objects.filter(is_active=True) vcourses = Vcourse.objects.filter(owner=request.user) profile = UserProfile.objects.get(pk=request.user.pk) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=profile) if form.is_valid(): form.save() return redirect('my_profile') else: form = ProfileForm(instance=profile) lessons = Subscriber2Lesson.objects.filter(user=request.user) context = { 'form': form, 'profile': profile, 'lessons': lessons, 'courses': courses, 'vcourses': vcourses } return render_to_response('profile.html', context, RequestContext(request))
def user_register(request): if auth.get_user(request).is_authenticated: return redirect('/{}/'.format(get_user_profile(request).id)) args = { 'user_form': UserCreationForm(), 'profile_form': ProfileForm(), 'email_form': UserChangeEmailForm(), 'birth_date_form': BirthDateForm, 'title': "Регистрация" } if request.is_ajax(): result_ajax = request_ajax_processing(request) if result_ajax is not None: return result_ajax if request.method == 'POST': result_post = request_post_method_processing(request, args) if result_post is not None: return result_post return render(request, 'main/no_login/register.html', args)
def get(self, request): form = ProfileForm(instance=request.user) context = {'form': form} if request.user.is_authenticated: return render(request, self.template_name, context) return redirect('login')
def edit(request): user = request.user profile = user.profile first_time = hasattr(profile, 'candidate') and profile.candidate.peer_teaching is None if request.method != "POST": personal_dict = model_to_dict(user) personal_dict['middle_name'] = profile.middle_name user_personal_form = UserPersonalForm(instance=user, initial=personal_dict) profile_dict = model_to_dict(profile) if profile.graduation_term is not None: profile_dict.update({ 'graduation_quarter': profile.graduation_term.quarter, 'graduation_year': profile.graduation_term.year }) if not first_time: profile_form = ProfileForm(initial=profile_dict) else: profile_form = FirstProfileForm(initial=profile_dict) tutoring_form = TutoringPreferencesForm() shirt_form = ShirtForm() peer_teaching_form = PeerTeachingForm() else: user_personal_form = UserPersonalForm(request.POST, instance=user) if not first_time: profile_form = ProfileForm(request.POST, request.FILES, instance=profile) valid_forms = [form.is_valid() for form in (user_personal_form, profile_form)] else: profile_form = FirstProfileForm(request.POST, instance=profile) tutoring_form = TutoringPreferencesForm(request.POST) shirt_form = ShirtForm(request.POST, instance=profile.candidate) peer_teaching_form = PeerTeachingForm(request.POST) valid_forms = [form.is_valid() for form in (user_personal_form, profile_form, tutoring_form, shirt_form, peer_teaching_form)] if all(valid_forms): term, created = Term.objects.get_or_create(quarter=profile_form.cleaned_data['graduation_quarter'], year=profile_form.cleaned_data['graduation_year']) user_personal_form.save() profile.middle_name = user_personal_form.cleaned_data['middle_name'] profile.graduation_term = term profile_form.save() if not first_time: if profile.position == Profile.CANDIDATE: update_professor_interview_and_resume(profile.candidate) return redirect(profile_view) else: candidate = profile.candidate candidate.peer_teaching = PeerTeaching.objects.create(profile=profile, term=Settings.objects.term()) candidate.peer_teaching.tutoring = Tutoring.with_weeks(profile=profile, term=Settings.objects.term()) tutoring_form = TutoringPreferencesForm(request.POST, instance=candidate.peer_teaching.tutoring) tutoring_form.save() # If the candidate did NOT select tutoring, then we will set the attributes # of their tutoring object to hidden/frozen so that their object does not # interfere with anything else. if peer_teaching_form.cleaned_data['requirement_choice'] != PeerTeaching.TUTORING: candidate.peer_teaching.tutoring.frozen = True candidate.peer_teaching.tutoring.hidden = True candidate.peer_teaching.tutoring.save() peer_teaching_form = PeerTeachingForm(request.POST, instance=candidate.peer_teaching) peer_teaching_form.save() shirt_form.save() return redirect(add) if not first_time: return render_profile_page(request, 'edit.html', {'user_personal_form': user_personal_form, 'profile_form': profile_form}) else: return render_profile_page(request, 'first_edit.html', {'user_personal_form': user_personal_form, 'profile_form': profile_form, 'tutoring_form': tutoring_form, 'shirt_form': shirt_form, 'peer_teaching_form' : peer_teaching_form})
def profile(request): ''' Shows profile page ''' edit_profile = False edit_user = False edit_user_form = EditUserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) fb_form = FbUsernameForm(instance=request.user.profile) # The users has facebook profile? try: fb_profile = FacebookProfile.objects.get(user=request.user) if fb_profile.facebook_id is not None: connected_via_facebook = True else: connected_via_facebook = False except FacebookProfile.DoesNotExist: connected_via_facebook = False if request.method == 'POST': # Pull from facebook if user was logged via facebook if request.POST.get('use_fb_photo'): profile = request.user.profile profile.use_fb_photo = True profile.use_bethunting_photo = False profile.save() messages.add_message(request, messages.INFO, 'Your account has been succesfully updated') # Pull from facebook if user was logged via bethunting elif request.POST.get('fb_username') == '': return render(request, "profile.html", { 'profile_form': profile_form, 'edit_user_form': edit_user_form, 'fb_form': fb_form, 'connected_via_facebook': connected_via_facebook, }) elif request.POST.get('fb_username'): profile = request.user.profile profile.use_fb_photo = True profile.fb_photo = "http://graph.facebook.com/%s/picture?type=large" % request.POST.get('fb_username') profile.use_bethunting_photo = False profile.save() messages.add_message(request, messages.INFO, 'Your account has been succesfully updated') else: edit_user_form = EditUserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if edit_user_form.is_valid(): user = edit_user_form.save(commit=False) user.username = edit_user_form.cleaned_data.get('email') user.save() edit_user = True if profile_form.is_valid(): profile = profile_form.save() edit_profile = True if edit_profile or edit_user: messages.add_message(request, messages.INFO, 'Your account has been succesfully updated') return render(request, "profile.html", { 'profile_form': profile_form, 'edit_user_form': edit_user_form, 'fb_form': fb_form, 'connected_via_facebook': connected_via_facebook, })
def edit(request): user = request.user profile = user.profile first_time = hasattr( profile, 'candidate') and profile.candidate.peer_teaching is None if request.method != "POST": personal_dict = model_to_dict(user) personal_dict['middle_name'] = profile.middle_name user_personal_form = UserPersonalForm(instance=user, initial=personal_dict) profile_dict = model_to_dict(profile) if profile.graduation_term is not None: profile_dict.update({ 'graduation_quarter': profile.graduation_term.quarter, 'graduation_year': profile.graduation_term.year }) if not first_time: profile_form = ProfileForm(initial=profile_dict) else: profile_form = FirstProfileForm(initial=profile_dict) tutoring_form = TutoringPreferencesForm() shirt_form = ShirtForm() peer_teaching_form = PeerTeachingForm() else: user_personal_form = UserPersonalForm(request.POST, instance=user) if not first_time: profile_form = ProfileForm(request.POST, request.FILES, instance=profile) valid_forms = [ form.is_valid() for form in (user_personal_form, profile_form) ] else: profile_form = FirstProfileForm(request.POST, instance=profile) tutoring_form = TutoringPreferencesForm(request.POST) shirt_form = ShirtForm(request.POST, instance=profile.candidate) peer_teaching_form = PeerTeachingForm(request.POST) valid_forms = [ form.is_valid() for form in (user_personal_form, profile_form, tutoring_form, shirt_form, peer_teaching_form) ] if all(valid_forms): term, created = Term.objects.get_or_create( quarter=profile_form.cleaned_data['graduation_quarter'], year=profile_form.cleaned_data['graduation_year']) user_personal_form.save() profile.middle_name = user_personal_form.cleaned_data[ 'middle_name'] profile.graduation_term = term profile_form.save() if not first_time: if profile.position == Profile.CANDIDATE: update_professor_interview_and_resume(profile.candidate) return redirect(profile_view) else: candidate = profile.candidate candidate.peer_teaching = PeerTeaching.objects.create( profile=profile, term=Settings.objects.term()) candidate.peer_teaching.tutoring = Tutoring.with_weeks( profile=profile, term=Settings.objects.term()) tutoring_form = TutoringPreferencesForm( request.POST, instance=candidate.peer_teaching.tutoring) tutoring_form.save() # If the candidate did NOT select tutoring, then we will set the attributes # of their tutoring object to hidden/frozen so that their object does not # interfere with anything else. if peer_teaching_form.cleaned_data[ 'requirement_choice'] != PeerTeaching.TUTORING: candidate.peer_teaching.tutoring.frozen = True candidate.peer_teaching.tutoring.hidden = True candidate.peer_teaching.tutoring.save() peer_teaching_form = PeerTeachingForm( request.POST, instance=candidate.peer_teaching) peer_teaching_form.save() shirt_form.save() return redirect(add) if not first_time: return render_profile_page(request, 'edit.html', { 'user_personal_form': user_personal_form, 'profile_form': profile_form }) else: return render_profile_page( request, 'first_edit.html', { 'user_personal_form': user_personal_form, 'profile_form': profile_form, 'tutoring_form': tutoring_form, 'shirt_form': shirt_form, 'peer_teaching_form': peer_teaching_form })