Skip to content

Commit

Permalink
Cannot save profile edit form - Fixed #268
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbir committed Feb 12, 2014
1 parent 77eb14a commit 62257da
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
25 changes: 21 additions & 4 deletions profiles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@

from profiles.models import Profile

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, HTML

class ProfileForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)

class Meta:
self.helper = FormHelper()
self.helper.form_action = 'profile_edit'
self.helper.layout = Layout(
Fieldset(
'',
HTML("""
<p>Github account, <strong>{{ profile.github_account }}</strong></p>
"""),
'bitbucket_url',
'google_code_url',
),
ButtonHolder(
Submit('edit', 'Edit', css_class='btn btn-default')
)
)

class Meta:
fields = (
'bitbucket_url',
'google_code_url',
)
'bitbucket_url',
'google_code_url',
)
model = Profile
6 changes: 5 additions & 1 deletion profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from profiles import views

urlpatterns = patterns("",
url(r"^edit/$", views.profile_edit, name="profile_edit"),
url(
regex=r"^edit/$",
view=views.ProfileEditUpdateView.as_view(),
name="profile_edit"
),
url(r"^$", views.profile_list, name="profile_list"),
url(r"^(?P<github_account>[-\w]+)/$", views.profile_detail, name="profile_detail"),
)
50 changes: 11 additions & 39 deletions profiles/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.views.generic.edit import UpdateView

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, HTML
from braces.views import LoginRequiredMixin

from social_auth.signals import pre_update
from social_auth.backends.contrib.github import GithubBackend
Expand Down Expand Up @@ -36,45 +35,18 @@ def profile_list(request, template_name="profiles/profiles.html"):
})


@login_required
def profile_edit(request, template_name="profiles/profile_edit.html"):
class ProfileEditUpdateView(LoginRequiredMixin, UpdateView):
model = Profile
form_class = ProfileForm
template_name = "profiles/profile_edit.html"

profile = request.user.get_profile()
form = ProfileForm(request.POST or None, instance=profile)
def get_object(self):
return self.request.user.get_profile()

if form.is_valid():
def form_valid(self, form):
form.save()
msg = 'Profile edited'
messages.add_message(request, messages.INFO, msg)
return HttpResponseRedirect(reverse("profile_detail", kwargs={"github_account": profile.github_account}))

# TODO - move this to a template
github_account = """
<div
id="div_id_github_account"
class="ctrlHolder"><label for="id_github_account" >Github account</label><strong>{0}</strong></div>
""".format(profile.github_account)

helper = FormHelper()
helper.form_class = "profile-edit-form"
helper.layout = Layout(
Fieldset(
'',
HTML(github_account),
'bitbucket_url',
'google_code_url',
),
ButtonHolder(
Submit('edit', 'Edit', css_class="btn btn-default"),
)
)

return render(request, template_name,
{
"profile": profile,
"form": form,
"helper": helper,
})
messages.add_message(self.request, messages.INFO, "Profile Saved")
return HttpResponseRedirect(reverse("profile_detail", kwargs={"github_account": self.get_object()}))


def github_user_update(sender, user, response, details, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion templates/profiles/profile_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

{% block body %}

{% crispy form helper %}
{% crispy form %}

{% endblock body %}

0 comments on commit 62257da

Please sign in to comment.