def post(self, request): user = request.user if hasattr(user, 'profile'): profile = user.profile profile_form = ProfileForm(request.POST, request.FILES, instance=profile) else: profile_form = ProfileForm(request.POST, request.FILES) user_form = UserEditForm(request.POST, instance=user) if user_form.is_valid() and profile_form.is_valid(): if hasattr(user, 'profile'): profile.save() else: phone = profile_form.cleaned_data.get('phone') city = profile_form.cleaned_data.get('city') description = profile_form.cleaned_data.get('description') photo = profile_form.cleaned_data.get('photo') Profile.objects.create(user=user, phone=phone, city=city, photo=photo, description=description) user.save() return HttpResponseRedirect(reverse('profile')) return render(request, 'blog/profile_edit.html', context={ 'user_form': user_form, 'profile_form': profile_form })
def register(request): if request.method == 'POST': userform = UserCreationForm(request.POST) proform = ProfileForm(request.POST,request.FILES) # if userform.is_valid(): # new_user = userform.save() # else: # for f in userform: # mylog(str(f)) # if proform.is_valid(): # new_pro = proform.save() # return HttpResponseRedirect("/") if userform.is_valid() and proform.is_valid(): new_user = userform.save() nickname = proform.cleaned_data['nickname'] photo = proform.cleaned_data['photo'] birthday = proform.cleaned_data['birthday'] about = proform.cleaned_data['about'] new_pro = Profile( user=new_user, nickname=nickname, photo=photo, birthday=birthday, about=about ) new_pro.save() return HttpResponseRedirect("/") else: userform = UserCreationForm() proform = ProfileForm() return render_to_response("blog/register.html", {'userform': userform,'proform':proform }, context_instance=RequestContext(request))
def EditProfile(request, pk): document_files = [] detail = Profile.objects.get(pk=pk) saved = False if request.method == "POST": MyProfileForm = ProfileForm(request.POST, request.FILES) if MyProfileForm.is_valid(): #MyProfileForm.cleaned_data["agree"] #profile.picture = MyProfileForm.cleaned_data["picture"] if 'name' in request.POST: detail.name = MyProfileForm.cleaned_data["name"] if 'email' in request.POST: detail.email = MyProfileForm.cleaned_data["email"] if 'city' in request.POST: detail.city = request.POST["city"] if 'gender' in request.POST: detail.gender = request.POST["gender"] if 'age' in request.POST: detail.age = request.POST["age"] if 'summary' in request.POST: detail.summary = request.POST["summary"] if 'picture' in request.FILES: ts = time.time() pic_name = datetime.datetime.fromtimestamp(ts).strftime( '%Y_%m_%d_%H_%M_%S_') + request.FILES['picture']._get_name( ) pic_name = 'static/pictures/' + pic_name.replace(' ', '_').lower() if save_file(request.FILES['picture'], pic_name): detail.picture = pic_name if 'documents' in request.FILES: ts = time.time() for document in request.FILES.getlist('documents'): document_file = datetime.datetime.fromtimestamp( ts).strftime( '%Y_%m_%d_%H_%M_%S_') + document._get_name() document_file = 'static/documents/' + document_file.replace( ' ', '_').lower() if save_file(document, document_file): document_files.append(document_file) if len(document_files) > 0: detail.document = ','.join(document_files) if request.POST.has_key('bank'): detail.banks = ','.join(request.POST.getlist('bank')) detail.save() saved = True else: MyProfileForm = ProfileForm() #return HttpResponse(detail.city) #return HttpResponse(json.dumps(document_files)) #return HttpResponseRedirect('/thanks/') # Redirect return render(request, 'blog/edit_profile.html', locals())
def test_profile_form(self): # profile form field are all optional profile_form = ProfileForm({ 'city': '', 'phone': '', 'description': '', 'photo': None }) self.assertTrue(profile_form.is_valid())
def signUp(request): error = False if request.method == 'POST': form = UserForm(request.POST) profile_form = ProfileForm(request.POST, request.FILES) if all((form.is_valid(), profile_form.is_valid())): data_user = { 'username': form.cleaned_data['username'], 'password': form.cleaned_data['password'], 'email': form.cleaned_data['email'], 'first_name': form.cleaned_data['first_name'], 'last_name': form.cleaned_data['last_name'] } try: user = requests.post(BASE_URL + '/api/users/create/', data=data_user) if user.status_code != 201: raise Exception('User already exist') user = user.json() data_profile = {'user': user['id']} if 'picture' in profile_form.cleaned_data: file_profile = { 'picture': profile_form.cleaned_data['picture'] } else: file_profile = {} data_put = { 'first_name': form.cleaned_data['first_name'], 'last_name': form.cleaned_data['last_name'] } r = login_api(user['username'], form.cleaned_data['password']) request.session['token'] = r.json()['auth_token'] request.session['me'] = requests.get( BASE_URL + '/api/users/me/', headers=get_header(request)).json() profile = requests.post(BASE_URL + '/api/profile/', data=data_profile, files=file_profile, headers=get_header(request)).json() user = requests.put(BASE_URL + '/api/user/' + str(user['id']) + '/', data=data_put, headers=get_header(request)).json() return redirect('home') except Exception as e: error = True form = UserForm() profile_form = ProfileForm() # if request.user.is_authenticated(): # redirect('home') return render(request, 'blog/signUp.html', locals())
def get(self, request): user = request.user if hasattr(user, 'profile'): profile = user.profile profile_form = ProfileForm(instance=profile) else: profile_form = ProfileForm() user_form = UserEditForm(instance=user) return render(request, 'blog/profile_edit.html', context={ 'user_form': user_form, 'profile_form': profile_form })
def profile(request, slug, template="profile.html"): profile = get_object_or_404(Profile, user__username__iexact=slug) posts = Post.objects.filter(user=profile.user).order_by("-date") is_editable = request.user == profile.user if request.method == "POST": profileform = ProfileForm(instance=profile, data=request.POST, files=request.FILES) if profileform.is_valid(): profileform.save() return redirect("profile", slug) else: profileform = ProfileForm(instance=profile) ctx = {"profile": profile, "posts": posts, "profileform": profileform, "is_editable": is_editable} return render(request, template, ctx)
def save_profile(request): saved = False if request.method == "POST": #Get the posted form MyProfileForm = ProfileForm(request.POST, request.FILES) if MyProfileForm.is_valid(): profile = Profile() profile.name = MyProfileForm.cleaned_data["name"] profile.picture = MyProfileForm.cleaned_data["picture"] profile.save() saved = True else: MyProfileForm = Profileform() return render(request, 'saved.html', locals())
def dispatch(self, request, *args, **kwargs): form = ProfileForm(instance=self.get_profile(request.user)) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=self.get_profile(request.user)) if form.is_valid(): form.instance.user = request.user form.save() messages.success(request, u"Профиль успешно обновлен!") return redirect(reverse("profile")) return render(request, self.template_name, {'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) 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', user_id=request.user.id) 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, 'profiles/profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def update_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() messages.success(request, _('Your profile was successfully updated!')) #return redirect('settings:profile') return HttpResponseRedirect( reverse('profile-detail', args=[str(request.user.profile.id)])) 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, 'blog/profile_update.html', { 'user_form': user_form, 'profile_form': profile_form, })