def profile_view(request): user = request.user if request.POST: form = ProfileForm(request.POST) if form.is_valid(): data = form.cleaned_data if len(data['phone_number']) != 11 or not (data['phone_number']).isdigit(): return render(request, 'account/register.html', {'validation': 'not valid'}) else: Customer.objects.filter(username=request.user.username).update( first_name=data['first_name'], last_name=data['last_name']) user.phone_number = data['phone_number'] user.address = data['address'] user.save() if request.is_ajax(): message = {'message': 'edited your profile successfully!'} return JsonResponse(message) print(user) return render(request, 'account/profile.html', {'form': form}) else: data = { 'first_name': user.first_name, 'last_name': user.last_name, 'phone_number': user.phone_number, 'address': user.address } return render(request, 'account/profile.html', {'form': ProfileForm(initial=data)})
def profile_edit(request): current_user = request.user user_object = User.objects.get(pk=current_user.id) current_profile = current_user.get_profile() profile_object = UserProfile.objects.get(pk=current_profile.id) if request.method == 'POST': form = ProfileForm(request.POST, logged_user=user_object, user_profile=profile_object) if form.is_valid(): form.save_user() form.save_profile() return HttpResponseRedirect('/account/profile/') else: profile = { "first_name": current_user.first_name, "last_name": current_user.last_name, "email": current_user.email, "address": current_profile.address, "phone": current_profile.phone, } form = ProfileForm(initial=profile) return render_to_response('account/profile/edit.html', {"form": form, }, context_instance=RequestContext(request))
def register_view(request): reg_form = UserRegistrationForm(request.POST) profile_form = ProfileForm(request.POST) login_form = LoginForm() if request.method == 'POST': if reg_form.is_valid() and profile_form.is_valid(): new_user = reg_form.save(commit=False) new_user_profile = profile_form.save(commit=False) new_user.set_password(reg_form.cleaned_data['password']) new_user.is_active = True new_user.save() new_user_profile.user = new_user new_user_profile.save() messages.success(request, 'Account created successfully!') return redirect('index') else: messages.error(request, f'{reg_form.errors}') return redirect('index') else: messages.error(request, 'Error registering, please try again.') return redirect('index')
def post(self, request, *args, **kwargs): user = User.objects.get(username=request.user.username) userform = UserForm(request.POST, instance=user) profileform = ProfileForm(request.POST, instance=user.profile) if request.POST.get('email')!=user.email: user.profile.isVerified=False user.profile.save() if not (userform.is_valid() and profileform.is_valid()): kwargs['userform'] = userform kwargs['profileform'] = profileform return super(CProfile, self).post(request, *args, **kwargs) if userform.cleaned_data.get('email')!=user.email: user.profile.isVerified=False user.profile.save() profileform.save() userform.save() password = userform.cleaned_data.get('password') if password: user.set_password(password) user.save() log = authenticate(username=user.username, password=password) login(request, log) messages.success(request, '會員資料修改成功') return redirect(reverse('account:profile'))
def profile_edit(request): current_user = request.user user_object = User.objects.get(pk=current_user.id) current_profile = current_user.get_profile() profile_object = UserProfile.objects.get(pk=current_profile.id) if request.method == 'POST': form = ProfileForm(request.POST, logged_user=user_object, user_profile=profile_object) if form.is_valid(): form.save_user() form.save_profile() return HttpResponseRedirect('/account/profile/') else: profile = { "first_name": current_user.first_name, "last_name": current_user.last_name, "email": current_user.email, "address": current_profile.address, "phone": current_profile.phone, } form = ProfileForm(initial=profile) return render_to_response('account/profile/edit.html', { "form": form, }, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': user_form = UserRegistrationForm(request.POST) profile_form = ProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): # Создаем нового пользователя, но пока не сохраняем в базу данных. new_user = user_form.save(commit=False) # Задаем пользователю зашифрованный пароль. new_user.set_password(user_form.cleaned_data['password']) # Создание профиля пользователя. profile = Profile( user=new_user, date_of_birth=profile_form.cleaned_data['date_of_birth']) if request.FILES: profile.photo = request.FILES['photo'] # Сохраняем пользователя в базе данных. new_user.save() profile.save() create_action(new_user, 'has created an account') return render(request, 'account/register_done.html', {'new_user': new_user}) else: user_form = UserRegistrationForm() profile_form = ProfileForm() return render(request, 'account/register.html', { 'user_form': user_form, 'profile_form': profile_form, })
def post(self, request, *args, **kwargs): user = User.objects.get(username=request.user.username) userform = UserForm(request.POST, instance=user) profileform = ProfileForm(request.POST, instance=user.profile) if request.POST.get("email") != user.email: user.profile.isVerified = False user.profile.save() if not (userform.is_valid() and profileform.is_valid()): kwargs["userform"] = userform kwargs["profileform"] = profileform return super(CProfile, self).post(request, *args, **kwargs) if userform.cleaned_data.get("email") != user.email: user.profile.isVerified = False user.profile.save() profileform.save() userform.save() password = userform.cleaned_data.get("password") if password: user.set_password(password) user.save() log = authenticate(username=user.username, password=password) login(request, log) messages.success(request, "會員資料修改成功") return redirect(reverse("account:profile"))
def account(request): context = { 'user': request.user, 'authenticated': request.user.is_authenticated(), } if request.user.is_authenticated(): profile = Profile.objects.get(user=request.user) if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): default_term = form.cleaned_data['default_term'] learning_community = form.cleaned_data['learning_community'] preferred_name = form.cleaned_data['preferred_name'] profile.default_term = default_term profile.learning_community = learning_community profile.preferred_name = preferred_name profile.save() else: form = ProfileForm() if profile.default_term: form.fields['default_term'].initial = profile.default_term if profile.learning_community: form.fields['learning_community'].initial = profile.learning_community if profile.preferred_name: form.fields['preferred_name'].initial = profile.preferred_name else: form.fields['preferred_name'].initial = request.user.first_name context['form'] = form follows = FollowEntry.objects.filter(user=request.user) if len(follows) > 0: context['followed_courses'] = follow_get_courses(follows) else: if 'next' in request.GET: context['next'] = request.GET['next'] return render(request, 'account/account.html', context)
def profile(request): if request.method == 'POST': usr = User.objects.get(id=request.user.id) form = ProfileForm(request.POST, request.FILES, instance = request.user) if form.is_valid() and user_data_changed(usr, form.cleaned_data, request): usr.username = form.cleaned_data['username'] usr.first_name = form.cleaned_data['first_name'] usr.last_name = form.cleaned_data['last_name'] usr.email = form.cleaned_data['email'] usr.is_active = True usr.save() if ('avatar' in form.cleaned_data): avatar = form.cleaned_data['avatar'] ue = UserExt.objects.filter(user=usr.id).get() ue.avatar = avatar #transform = Image.open(avatar.file) #ue.avatar_mini = transform.resize((50,50)) ue.save() messages.add_message(request, messages.SUCCESS, 'The user `%s` was changed successfully.' % (usr.username)) else: form = ProfileForm(instance = request.user) context = get_base_context(request, 'home', ROLE_ACCOUNT, None, '', (_('profile').capitalize(),)) context['form'] = form avatar = request.user.userext.avatar context['avatar_url'] = avatar.url if avatar and type(avatar) == ImageFieldFile else '/static/Default-avatar.jpg' if request.user.userext.avatar_mini and type(request.user.userext.avatar_mini) == ImageFieldFile: context['avatar_mini_url'] = request.user.userext.avatar_mini.url else: context['avatar_mini_url'] = '/static/Default-avatar.jpg' context['hide_add_item_input'] = True return render(request, 'account/profile.html', context)
def edit_profile(request): if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=request.user) if form.is_valid(): form.save() form = ProfileForm(instance=request.user) return render(request, 'pages/edit-profile.html', context={ "form": form })
def update_profile(request, update_sucess_url="home", template="account/update_profile.html"): form = ProfileForm(instance=request.user.get_profile() ,data=request.POST or None) if form.is_valid(): profile_form=form.save(commit=False) profile_form.user=request.user profile_form.save() return redirect(update_sucess_url) return render(request, template, {'form':form})
def upload_avatar(self): if self.method == 'POST': form = ProfileForm(self.POST, self.FILES) if form.is_valid(): form.save() return redirect('/success') else: form = ProfileForm() return render(self, 'account/profile.html', {'form': form})
def post(self, request): user = request.user profile = Profile.objects.get(account=user) form = ProfileForm(request.POST) if form.is_valid(): profile = form.save(commit=False) profile.account = request.user profile.save() form = ProfileForm() return redirect('index') return render(request, self.template_name, {'form':form, 'profile':profile})
def account(request): name = "Congrat" form = ProfileForm(request.POST or None) if request.method == 'POST' and form.is_valid(): print(request.POST) print(form.cleaned_data) data = form.cleaned_data print(data['name']) new_form = form.save() return render(request, 'base/base.html', locals())
def profileUpdate(request, userID): userProfile = get_object_or_404(UserProfile, id=userID) template = 'account/profileUpdate.html' if request.method=='GET': form = ProfileForm(instance=userProfile) return render(request, template, {'form':form, 'userProfile':userProfile}) # POST form = ProfileForm(request.POST, instance=userProfile) if not form.is_valid(): return render(request, template, {'form':form, 'userProfile':userProfile}) form.save() return redirect('account:account', userID=userID)
def post(self, request): user = UpdateUserForm(request.POST, instance=request.user) profile = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if user.is_valid() and profile.is_valid(): user.save() profile.save() messages.success(request, 'Account details updated...') return redirect('staff:profile') return render(request, self.template_name, context={"userForm": self.user, "profileForm": self.profile})
def edit_profile(request): """Edit your profile""" form = ProfileForm(request.POST or None, user=request.user) if request.method == 'POST' and form.is_valid(): form.save() return redirect('user-home') context = { 'form': form, } return render(request, 'account/edit_profile.html', context)
def user_profile(request): profile = Profile.objects.get(user=request.user) form = ProfileForm(instance=profile) if request.method == 'POST': form = ProfileForm(request.POST, instance=profile) if form.is_valid(): form.save() messages.success(request, "Data updated successfully") form = ProfileForm(instance=profile) context = {'form': form} return render(request, 'account/change_profile.html', context)
def new_view(request): try: request.user.userprofile return redirect(request.GET.get("next") or reverse("account:home")) except ObjectDoesNotExist: form = ProfileForm({"email": request.user.email}) if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): form.save(request.user) _next = request.GET.get("next", '').strip() return redirect(_next or reverse("account:home")) return render_to_response('account/new.html', locals(), context_instance=RequestContext(request))
def profile(request): if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=request.user) if form.is_valid(): user = form.save() login(request, user) messages.success(request, 'ویرایش پروفایل با موفقیت انجام شد') form = ProfileForm(instance=request.user) else: form = ProfileForm(instance=request.user) return render(request, 'basic_form.html', { 'form': form, 'title': 'ویرایش پروفایل' })
def signup(request): if request.method == "POST": form = UsercreatinForm(request.POST) pro_form = ProfileForm(request.POST) if form.is_valid() and pro_form.is_valid(): user = form.save() profile = pro_form.save(commit=False) profile.user = user pro_form.save() auth_login(request, user) return redirect('question:index') else: form = UserCreationForm() pro_form = ProfileForm() return render(request, 'account/signup.html', {'form':form, 'pro_form':pro_form})
def post(self, request): self.class_taken = request.user.profile.timetable profile_form = ProfileForm(instance=request.user.profile) setting_form = PasswordChangeForm(request.user) tags = request.user.profile.interest_tag.all() if 'profile' in request.POST: profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if profile_form.is_valid(): profile_form.save() return redirect("index") else: return render(request, self.template_name, {"profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags}) elif 'setting' in request.POST: setting_form = PasswordChangeForm(user=request.user, data=request.POST) if setting_form.is_valid(): setting_form.save() update_session_auth_hash(request, setting_form.user) return redirect("index") else: return render(request, self.template_name, {"profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags}) elif 'time-table' in request.POST: table = request.POST['table'] profile = request.user.profile profile.timetable = table profile.save() self.class_taken = table return render(request, self.template_name, {"profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags}) elif 'interest' in request.POST: tags = request.POST.getlist('tags') print tags for tag in tags: t, created = Tag.objects.get_or_create(name=tag.lower()) request.user.profile.interest_tag.add(t) return render(request, self.template_name, {"profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags})
def edit_profile(request): my_profile = Profile.objects.get(user=request.user) if request.method == 'POST': profile_form = ProfileForm(request.POST, request.FILES, instance=my_profile) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.avatar = profile_form.cleaned_data['avatar'] profile.save() make_thumbnail(profile.avatar.path) return HttpResponseRedirect('/accounts/profile/') else: profile_form = ProfileForm(instance=my_profile) return render(request, 'account/edit_profile.html', { 'profile_form': profile_form, })
def profile_setup(request): hostname=os.environ['HTTP_HOST'] dest=request.GET.get('continue', reverse('front-page')) user=get_current_user() profile=get_current_profile() if not profile: return HttpResponseRedirect(create_signin_url(reverse('account-edit'))) if request.method == 'POST': # If the form has been submitted... form = ProfileForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass save_profile(profile, form) return HttpResponseRedirect(dest) # Redirect after POST else: form = ProfileForm(initial=profile.profile_details) return(render_to_response('account/profile-setup.html', locals(), context_instance=RequestContext(request)))
def edit_profile(request): profile=get_current_profile() if request.method == 'POST': # If the form has been submitted... form = ProfileForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass save_profile(profile, form) messages.add_message(request, messages.INFO,"Your profile has been saved!") return HttpResponseRedirect('/') # Redirect after POST current_settings={'nickname':profile.nickname, 'email': profile.email, 'subscribe':profile.subscribes, 'link': profile.link} form=ProfileForm(current_settings) return(render_to_response('account/edit.html', locals(), context_instance=RequestContext(request)))
def signup(request): if request.user.is_authenticated: return redirect('question:list') else: if request.method == "POST": # 사용자가 로그인을 요청했으면? form = UserCreationForm(request.POST) pro_form = ProfileForm(request.POST) if form.is_valid() and pro_form.is_valid(): user = form.save() profile = pro_form.save(commit=False) profile.user = user profile.save() auth_login(request, user) return redirect('question:list') else: # 로그인을 위한 폼을 요청했으면? form = UserCreationForm() pro_form = ProfileForm() return render(request,"account/signup.html",{'form':form,"pro_form":pro_form})
def edit(request): if request.method == 'POST': user_form = UserEditForm(instance=request.user, data=request.POST) profile_form = ProfileForm(instance=request.user.profile, data=request.POST, files=request.FILES) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Profile updated successfully') else: messages.error(request, 'Error updating your profile') else: user_form = UserEditForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'account/edit.html', { 'user_form': user_form, 'profile_form': profile_form })
def register_new_doctor(request): # # this view provides an admin with the ability to register other users if request.user.is_superuser: title = "Register Doctor" form = UserRegisterForm(request.POST or None) profile_form = ProfileForm(request.POST or None) if form.is_valid() and profile_form.is_valid(): user = form.save(commit=False) password = form.cleaned_data.get('password') user.set_password(password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() doctor = Doctor() doctor.profile = profile doctor.save() messages.success(request, 'Form submission successful') log = Log(username="******", action=" registered " + doctor.profile.user.username + " as a doctor ") log.save() msg = "Doctor %s was successfully created" % doctor return landing(request, msg) context = { "form": form, "title": title, "prof": profile_form, } return render(request, 'administration/create_user.html', context) else: return redirect('/')
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) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, _('Your profile was successfully updated!')) return redirect('dashboard') else: messages.error(request, _('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'accounts/profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def profile_setup(request): hostname = os.environ['HTTP_HOST'] dest = request.GET.get('continue', reverse('front-page')) user = get_current_user() profile = get_current_profile() if not profile: return HttpResponseRedirect(create_signin_url(reverse('account-edit'))) if request.method == 'POST': # If the form has been submitted... form = ProfileForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass save_profile(profile, form) return HttpResponseRedirect(dest) # Redirect after POST else: form = ProfileForm(initial=profile.profile_details) return (render_to_response('account/profile-setup.html', locals(), context_instance=RequestContext(request)))
def edit_profile(request): profile = get_current_profile() if request.method == 'POST': # If the form has been submitted... form = ProfileForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass save_profile(profile, form) messages.add_message(request, messages.INFO, "Your profile has been saved!") return HttpResponseRedirect('/') # Redirect after POST current_settings = { 'nickname': profile.nickname, 'email': profile.email, 'subscribe': profile.subscribes, 'link': profile.link } form = ProfileForm(current_settings) return (render_to_response('account/edit.html', locals(), context_instance=RequestContext(request)))
def user_profile(request): avatar_form = PhotoProfileForm(instance=request.user.profile) if request.method == 'POST': user_form = UserEditForm(instance=request.user, data=request.POST) profile_form = ProfileForm(instance=request.user.profile, data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Профиль успешно обновлен') else: messages.error(request, 'Что-то пошло не так') else: user_form = UserEditForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render( request, 'account/profile.html', { 'user_form': user_form, 'profile_form': profile_form, 'avatar_form': avatar_form, })
def profile(request, template_name="profile.html"): if request.user.is_authenticated(): if request.method == "POST": if request.POST["action"] == "update": profile_form = ProfileForm(request.POST, instance=request.user.get_profile()) if profile_form.is_valid(): email = profile_form.cleaned_data["email"] firstname = profile_form.cleaned_data["firstname"] lastname = profile_form.cleaned_data["lastname"] if profile.email != email or profile.firstname != firstname or profile.lastname != lastname: profile.email = request.user.email = email profile.firstname = request.user.firstname = firstname profile.lastname = request.user.lastname = lastname request.user.save() profile_form.save() else: profile_form = ProfileForm(instance=request.user.get_profile()) else: profile_form = None return render_to_response(template_name, { "form": profile_form, }, context_instance=RequestContext(request))
def post(self, request, *args, **kwargs): userForm = SignupForm(request.POST) profileform = ProfileForm(request.POST) CForm = CaptchaForm(request.POST) if not (CForm.is_valid() and userForm.is_valid() and profileform.is_valid()): kwargs['userForm'] = userForm kwargs['profileform'] = profileform kwargs['CForm'] = CForm return super(CSignUp, self).get(request, *args, **kwargs) user = userForm.save() password = user.password user.set_password(password) user.save() userProfile = profileform.save(commit=False) userProfile.user = user userProfile.username = user.username userProfile.ip = get_client_ip(request) userProfile.save() log = authenticate(username=user.username, password=password) login(request, log) messages.add_message(request, 50, request.user.username+'會員 謝謝您的註冊', extra_tags='註冊成功') #messages.success(request, '歡迎註冊') return redirect(reverse('main:main'))
def update_profile(request): # get user profile profile = request.user.get_profile() if request.method == 'POST': profile_form = ProfileForm(request.POST, request.FILES, instance=profile) user_form = UserForm(request.POST, instance=request.user) if profile_form.is_valid() and user_form.is_valid(): profile_form.save() user_form.save() messages.success(request, _("Profile updated succesfully.")) return redirect('update_profile') else: profile_form = ProfileForm(instance=profile) user_form = UserForm(instance=request.user) return render(request, 'account/profile.html', { 'profile_form': profile_form, 'user_form': user_form })
def post(self, request, *args, **kwargs): userForm = SignupForm(request.POST) profileform = ProfileForm(request.POST) CForm = CaptchaForm(request.POST) if not (CForm.is_valid() and userForm.is_valid() and profileform.is_valid()): kwargs["userForm"] = userForm kwargs["profileform"] = profileform kwargs["CForm"] = CForm return super(CSignUp, self).get(request, *args, **kwargs) user = userForm.save() password = user.password user.set_password(password) user.save() userProfile = profileform.save(commit=False) userProfile.user = user userProfile.username = user.username userProfile.ip = get_client_ip(request) userProfile.save() log = authenticate(username=user.username, password=password) login(request, log) messages.add_message(request, 50, request.user.username + "會員 謝謝您的註冊", extra_tags="註冊成功") # messages.success(request, '歡迎註冊') return redirect(reverse("main:main"))
def edit_info(request): if request.user.is_authenticated: # gets the user id based on the request user_id = request.user.id # gets the user instance based on the id user_instance = User.objects.get(id=user_id) isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) if isPatient: # if there is data that exists on the user, the forms will be prefilled user_init_data = { 'first_name': user_instance.first_name, 'last_name': user_instance.last_name, 'email': user_instance.email, 'username': user_instance.username } # create a new user form that is tied to the current instance and is prefilled with current data user_form = UserForm(instance=user_instance, initial=user_init_data) # get the profile instance based on the id profile_instance = Profile.objects.get(user_id=user_id) # if there is data that exists on the profile, the forms will be prefilled profile_init_data = { 'streetAddress': profile_instance.streetAddress, 'town': profile_instance.town, 'zipCode': profile_instance.zipCode, 'state': profile_instance.state, 'phoneNumber': profile_instance.phoneNumber, 'hospital_assignment': profile_instance.hospital_assignment } profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) # get the patient instance based on the id patient_instance = Patient.objects.get(profile__user_id=user_id) # if there is data that exists on the patient, the forms will be prefilled patient_init_data = { 'height': patient_instance.height, 'weight': patient_instance.weight, 'age': patient_instance.age, 'hospital_preferred': patient_instance.hospital_preferred } # create a new patient form that is tied to the current instance and is prefilled with current data # if the user is submitting a form, save it if it is valid if request.POST: patient_form = PatientForm(request.POST, instance=patient_instance) profile_form = ProfileForm(request.POST, instance=profile_instance) user_form = UserForm(request.POST, instance=user_instance) if user_form.is_valid() and patient_form.is_valid( ) and profile_form.is_valid(): user_form.save() patient_form.save() profile_form.save() log = Log(username=patient_instance.profile.user.username, action=" made profile changes ") log.save() return redirect('/profile') else: print("error") else: patient_form = PatientForm(instance=patient_instance, initial=patient_init_data) profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) user_form = UserForm(instance=user_instance, initial=user_init_data) appointments = Appointment.objects.filter(patient=patient_instance) return render( request, "account/useredit.html", { "user": request.user, "profile": profile_instance, "patient": patient_instance, "userform": user_form, "patientform": patient_form, "profileform": profile_form, "usertype": "Patient", "appointments": appointments }) elif isDoctor: # if there is data that exists on the user, the forms will be prefilled user_init_data = { 'first_name': user_instance.first_name, 'last_name': user_instance.last_name, 'email': user_instance.email, 'username': user_instance.username } # create a new user form that is tied to the current instance and is prefilled with current data user_form = UserForm(instance=user_instance, initial=user_init_data) # get the profile instance based on the id profile_instance = Profile.objects.get(user_id=user_id) # if there is data that exists on the profile, the forms will be prefilled profile_init_data = { 'streetAddress': profile_instance.streetAddress, 'town': profile_instance.town, 'zipCode': profile_instance.zipCode, 'state': profile_instance.state, 'phoneNumber': profile_instance.phoneNumber, 'hospital_assignment': profile_instance.hospital_assignment } profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) # get the doctor instance based on the id doctor_instance = Doctor.objects.get(profile__user_id=user_id) # if there is data that exists on the patient, the forms will be prefilled doctor_init_data = {'type': doctor_instance.type} # create a new patient form that is tied to the current instance and is prefilled with current data doctor_form = DoctorForm(instance=doctor_instance, initial=doctor_init_data) # if the user is submitting a form, save it if it is valid if request.POST: doctor_form = DoctorForm(request.POST, instance=doctor_instance, initial=doctor_init_data) profile_form = ProfileForm(request.POST, instance=profile_instance, initial=profile_init_data) user_form = UserForm(request.POST, instance=user_instance, initial=user_init_data) if user_form.is_valid() and profile_form.is_valid(): user_form.save() doctor_form.save() profile_form.save() log = Log(username=doctor_instance.profile.user.username, action=" made profile changes ") log.save() return redirect('/profile') else: print("error") appointments = Appointment.objects.filter( doctor=doctor_instance).order_by('-date').reverse() return render( request, "account/useredit.html", { "user": request.user, "profile": profile_instance, "doctor": doctor_instance, "userform": user_form, "doctorform": doctor_form, "profileform": profile_form, "usertype": "Doctor", "appointments": appointments }) elif isNurse: # if there is data that exists on the user, the forms will be prefilled user_init_data = { 'first_name': user_instance.first_name, 'last_name': user_instance.last_name, 'email': user_instance.email, 'username': user_instance.username } # create a new user form that is tied to the current instance and is prefilled with current data user_form = UserForm(instance=user_instance, initial=user_init_data) # get the profile instance based on the id profile_instance = Profile.objects.get(user_id=user_id) # if there is data that exists on the profile, the forms will be prefilled profile_init_data = { 'streetAddress': profile_instance.streetAddress, 'town': profile_instance.town, 'zipCode': profile_instance.zipCode, 'state': profile_instance.state, 'phoneNumber': profile_instance.phoneNumber } profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) # get the nurse instance based on the id nurse_instance = Nurse.objects.get(profile__user_id=user_id) # if there is data that exists on the patient, the forms will be prefilled nurse_init_data = {'type': nurse_instance.type} # create a new nurse form that is tied to the current instance and is prefilled with current data nurse_form = NurseForm(instance=nurse_instance, initial=nurse_init_data) # if the user is submitting a form, save it if it is valid if request.POST: nurse_form = NurseForm(request.POST, instance=nurse_instance, initial=nurse_init_data) profile_form = ProfileForm(request.POST, instance=profile_instance, initial=profile_init_data) user_form = UserForm(request.POST, instance=user_instance, initial=user_init_data) if user_form.is_valid(): user_form.save() nurse_form.save() profile_form.save() log = Log(username=nurse_instance.profile.user.username, action=" made profile changes ") log.save() return redirect('/profile') else: print("error") appointments = Appointment.objects.filter( hospital=profile_instance.hospital_assignment).order_by( '-date').reverse() return render( request, "account/useredit.html", { "user": request.user, "profile": profile_instance, "nurse": nurse_instance, "userform": user_form, "nurseform": nurse_form, "profileform": profile_form, "usertype": "Nurse", "appointments": appointments }) else: return redirect('/login')
def profile_page(request, username): """ Allows a user to update their own profile. """ # capture and set user in session user = request.user if request.method == "GET": if not user.is_authenticated: # very username if exists try_user = User.objects.filter(username=username) if not try_user.exists(): return render(request,'404.html') user_obj = User.objects.get(username=username) return render(request, 'account/profile.html', {'user_obj':user_obj}) else: # check if user is equal to username if user.username == username: # Create the formset, specifying the form and formset we want to use. PictureFormSet = formset_factory(PictureForm) profile_form = ProfileForm(request.POST, user=user) picture_formset = PictureFormSet(request.POST, request.FILES) user_obj = User.objects.get(username=username) profile_form = ProfileForm(user=user) picture_formset = PictureFormSet() return render(request, 'account/profile.html', {'user_obj':user_obj, 'status':True, 'profile_form':profile_form, 'picture_formset':picture_formset}) user_obj = User.objects.get(username=username) return render(request, 'account/profile.html', {'user_obj':user_obj}) if request.method == 'POST': if not user.is_authenticated: return redirect('account:login') else: if user.username == username: # Create the formset, specifying the form and formset we want to use. PictureFormSet = formset_factory(PictureForm) profile_form = ProfileForm(request.POST, user=user) picture_formset = PictureFormSet(request.POST, request.FILES) if profile_form.is_valid() and picture_formset.is_valid(): # save user info user.first_name = profile_form.cleaned_data.get('first_name') user.last_name = profile_form.cleaned_data.get('last_name') user.save() # now save data for picture_form for picture_f in picture_formset.cleaned_data: picture = picture_f.get('pic', None) proffession = picture_f.get('proffession', None) if proffession is not None: if len(proffession) > 0: member_obj = Member.objects.get(user__username=username) member_obj.proffession = proffession member_obj.save() if picture: member_obj = Member.objects.get(user__username=username) member_obj.pic = picture member_obj.save() return redirect('account:profile', username=user.username) else: return render(request, '404.html')
def post(self, request): self.class_taken = request.user.profile.timetable profile_form = ProfileForm(instance=request.user.profile) setting_form = PasswordChangeForm(request.user) tags = request.user.profile.interest_tag.all() if 'profile' in request.POST: profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) if profile_form.is_valid(): profile_form.save() return redirect("index") else: return render( request, self.template_name, { "profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags }) elif 'setting' in request.POST: setting_form = PasswordChangeForm(user=request.user, data=request.POST) if setting_form.is_valid(): setting_form.save() update_session_auth_hash(request, setting_form.user) return redirect("index") else: return render( request, self.template_name, { "profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags }) elif 'time-table' in request.POST: table = request.POST['table'] profile = request.user.profile profile.timetable = table profile.save() self.class_taken = table return render( request, self.template_name, { "profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags }) elif 'interest' in request.POST: tags = request.POST.getlist('tags') print tags for tag in tags: t, created = Tag.objects.get_or_create(name=tag.lower()) request.user.profile.interest_tag.add(t) return render( request, self.template_name, { "profile_form": profile_form, "setting_form": setting_form, 'classes_number': self.classes_number, 'class_taken': self.class_taken, 'tags': tags })