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) signup_form = SignUpForm(instance=request.user) print(signup_form) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return redirect('dashboard') else: return render( request, 'settings.html', { 'user_form': user_form, 'profile_form': profile_form, 'signup_form': signup_form, }) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) signup_form = SignUpForm(instance=request.user) print('This is what you got') print(signup_form) return render( request, 'settings.html', { 'user_form': user_form, 'profile_form': profile_form, 'signup_form': signup_form, })
def participant(request): if request.user.is_authenticated(): submitMsg = "" instance = Profile.objects.get(username=request.user.profile.username) if (request.method == 'POST'): form = ProfileForm(request.POST,instance=instance) if form.is_valid(): form.save() submitMsg="Changes updated" else: submitMsg = "Invalid Entry!" form = ProfileForm(instance=instance) template = loader.get_template('login/participant.html') studyProfiles = Profile.objects.filter(isStudy=True) suggestedStudies=[] for i in studyProfiles: if i.isSimilar(request.user.profile): suggestedStudies.append(i.username) name = request.user.username dateOfBirth = request.user.profile.dateOfBirth context = {'username':name, 'suggestedStudies':suggestedStudies, 'form':form, 'submitMsg':submitMsg} return HttpResponse(template.render(context,request)) else: return redirect('..')
def make_study(request): if request.user.is_authenticated(): errorMsg = "" successMsg = "" if request.method == "POST": formResults = StudyForm(request.POST) if formResults.is_valid(): currentStudies=Studies.objects.filter(creator=request.user) studyName = formResults.cleaned_data['name'] # If the study already exists in this researcher's studies then return back to the form and print the error message for i in currentStudies: if i.name == studyName: errorMsg = "Study with that name already exists!" template = loader.get_template('login/make_study.html') context = {'user':request.user,'form':StudyForm(),'errorMsg':errorMsg} return HttpResponse(template.render(context,request)) # description = formResults.cleaned_data['description'] # inclusion = formResults.cleaned_data['inclusion'] # exclusion = formResults.cleaned_data['exclusion'] # sampleProfile = Profile.objects.create(username=(request.user.username+'/'+studyName)) # study = Studies(creator=request.user ,name=studyName, description =description, inclusion=inclusion, exclusion = exclusion,profile=sampleProfile) study = StudyForm(request.POST).save() study.creator = request.user.username sampleProfile = ProfileForm(request.POST).save() sampleProfile.isStudy = True sampleProfile.username = request.user.username+'/'+study.name sampleProfile.save() # return HttpResponse(study.name) study.profile= sampleProfile # Try to save the study to the database, if it doesn't work set errorMsg to the appropriate message # try: study.save() successMsg = "Study created!" # except: # successMsg = "" # errorMsg = "Ooops, couldn't write your study to the database..." template = loader.get_template('login/make_study.html') context = {'user':request.user,'form':formResults,'successMsg':successMsg, "errorMsg":errorMsg} return HttpResponse(template.render(context,request)) else: errorMsg = "Couldn't validate study criteria input, check that your input is valid." template = loader.get_template('login/make_study.html') studyForm = StudyForm() profileForm = ProfileForm() context = {'studyForm':studyForm,'profileForm':profileForm, 'errorMsg':errorMsg, 'successMsg':successMsg} return HttpResponse(template.render(context, request)) 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) signup_form = SignUpForm(instance=request.user) current_profile = Profile.objects.filter(pk=request.user.id)[0] print('CHECK =', request.FILES) if 'myfile' in request.FILES and request.FILES['myfile']: myfile = request.FILES['myfile'] fs = FileSystemStorage() filename = fs.save( myfile.name + '-' + datetime.datetime.now().isoformat(), myfile) uploaded_file_url = fs.url(filename) user = User.objects.filter(pk=request.user.id) current_profile = Profile.objects.get(user=user) current_profile.image = uploaded_file_url current_profile.save() print('UPDATED =', uploaded_file_url) return render(request, 'profile.html', {}) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return redirect('settings') else: return render( request, 'settings.html', { 'user_form': user_form, 'signup_form': signup_form, 'profile': current_profile, }) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) signup_form = SignUpForm(instance=request.user) current_profile = Profile.objects.filter(pk=request.user.id)[0] return render( request, 'settings.html', { 'user_form': user_form, 'signup_form': signup_form, 'profile': current_profile })
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('profile') 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, 'profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def login(request): form = ProfileForm(request.POST) #print(form.errors) if form.is_valid(): eid = form["emp_id"].value() pas = form["emp_pas"].value() for p in useradmin.objects.raw('select * from useradmin'): if p.Employee_Id == eid and p.password == pas: user_list = [eid] request.session['user_list'] = user_list return redirect('adminprofile/') for p in Employee.objects.raw('select * from Employee'): if p.Employee_Id == eid and p.password == pas: user_list = [eid] request.session['user_list'] = user_list return redirect('profile/') return render(request, 'login/home.html', {'form': form})
def test_profileForm_case(self): c = Client() tester = User.objects.create(username='******', password='******', is_active=True, is_staff=True, is_superuser=True) tester.save() c.login(username='******', password='******') form = ProfileForm({ 'first_name': "Joe", 'last_name': "Joey", 'username': "******", 'phone_number': "3018523444", 'year': "Fourth", 'bio': "testing", 'location': "Inactive" }, instance=tester.profile) self.assertTrue(form.is_valid()) form.save() self.assertNotEqual(tester.profile.first_name, "joe") self.assertNotEqual(tester.profile.last_name, "joey") self.assertNotEqual(tester.profile.username, "tester") self.assertEqual(tester.profile.phone_number, "3018523444") tester.delete()
def profile(request): dicts['profile'] = models.Profile.objects.get( user=auth.get_user(request).id) dicts['form'] = ProfileForm(request.POST or None, instance=dicts['profile']) return render(request, 'profile.html', dicts)