def people_edit(request, username): u = get_object_or_404(UserProfile, user__username=username) TagFormSet = inlineformset_factory(UserProfile, Tag, extra=1, max_num=10) image_form = ProfileImageForm(instance=u) tag_formset = TagFormSet(instance=u) psw_form = PasswordChangeForm(request.user) email_form = UserEmailForm(instance=request.user) email_notif_form = ProfileEmailNotificationForm(instance=u) if request.method == "POST": if request.POST["form-type"] == "image-form": image_form = ProfileImageForm(request.POST, request.FILES, instance=u) if image_form.is_valid(): image_form.save() # resize and save image under same filename im_path = join(MEDIA_ROOT, u.avatar.name) im = Image.open(im_path) im.thumbnail((138, 138), Image.ANTIALIAS) im.save(im_path, "JPEG") elif request.POST["form-type"] == "tag-form": tag_formset = TagFormSet(request.POST, instance=u) if tag_formset.is_valid(): tag_formset.save() return HttpResponseRedirect(reverse("hip_engine.views.people_edit", args=(request.user.username,))) elif request.POST["form-type"] == "email-form": email_form = UserEmailForm(request.POST, instance=u) if email_form.is_valid(): email_form.save() return HttpResponseRedirect(reverse("hip_engine.views.people_edit", args=(request.user.username,))) elif request.POST["form-type"] == "email-notif-form": email_notif_form = ProfileEmailNotificationForm(request.POST, instance=u) if email_notif_form.is_valid(): email_notif_form.save() return HttpResponseRedirect(reverse("hip_engine.views.people_edit", args=(request.user.username,))) else: psw_form = PasswordChangeForm(request.user, request.POST) if psw_form.is_valid(): psw_form.save() return HttpResponseRedirect(reverse("hip_engine.views.people_edit", args=(request.user.username,))) if request.user.username == username: u = get_object_or_404(UserProfile, user__username=username) return render_to_response( "hip_engine/profile_edit.html", { "user_focused": u, "image_form": image_form, "tag_formset": tag_formset, "psw_form": psw_form, "email_form": email_form, "email_notif_form": email_notif_form, }, context_instance=RequestContext(request), ) else: return index(request)
def profile_edit(request, username): if (username == request.user.username): username_form = UserUsernameForm(instance=request.user) email_form = UserEmailForm(instance=request.user) email_notif_form = ProfileEmailNotificationForm(instance=request.user.get_profile()) image_form = ProfileImageForm(instance=request.user.get_profile()) psw_form = PasswordChangeForm(request.user) tracklist_form = TracklistForm(username=request.user.username) track_form = TrackForm() if request.method == "POST": if request.POST['form-type'] == "image-form": image_form = ProfileImageForm(request.POST, request.FILES, instance=request.user.get_profile()) if image_form.is_valid(): image_form.save() # resize and save image under same filename im_path = join(MEDIA_ROOT, request.user.get_profile().avatar.name) im = Image.open(im_path) im = rescale_square(im, 256) im.save(im_path, "JPEG") elif request.POST['form-type'] == "username-form": username_form = UserUsernameForm(request.POST, instance=request.user) if username_form.is_valid(): username_form.save() return HttpResponseRedirect(reverse('hip_engine.views.profile_edit', args=(request.user.username,))) elif request.POST['form-type'] == "email-form": email_form = UserEmailForm(request.POST, instance=request.user) if email_form.is_valid(): email_form.save() return HttpResponseRedirect(reverse('hip_engine.views.profile_edit', args=(request.user.username,))) elif request.POST['form-type'] == "email-notif-form": email_notif_form = ProfileEmailNotificationForm(request.POST, instance=request.user.get_profile()) if email_notif_form.is_valid(): email_notif_form.save() return HttpResponseRedirect(reverse('hip_engine.views.profile_edit', args=(request.user.username,))) else: psw_form = PasswordChangeForm(request.user, request.POST) if psw_form.is_valid(): psw_form.save() return HttpResponseRedirect(reverse('hip_engine.views.profile_edit', args=(request.user.username,))) context = { 'email_form':email_form, 'email_notif_form':email_notif_form, 'image_form':image_form, 'psw_form':psw_form, 'username_form':username_form, } context.update(get_profile_context(username)) context.update(get_generic_context(request)) context.update(get_rankings(request)) # tracklist_formset = TracklistFormset(initial=[{'owner': request.user.get_profile(), 'bundlego':bundlego}]) return render_to_response('hip_engine/profile_edit.html', context, context_instance=RequestContext(request)) else: return HttpResponseRedirect(reverse('hip_engine.views.feed'))