Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
user settings form: save profile now works
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Robles Elvira committed Jun 2, 2013
1 parent 0d5d200 commit 4cf8cc4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 31 deletions.
26 changes: 22 additions & 4 deletions agora_site/agora_core/forms/user.py
Expand Up @@ -174,6 +174,8 @@ class UserSettingsForm(django_forms.ModelForm):

email = django_forms.EmailField(required=False)

username = django_forms.CharField(required=False)

email_updates = django_forms.BooleanField(required=False)

old_password = django_forms.CharField(required=False)
Expand All @@ -196,7 +198,7 @@ def save(self, *args, **kwargs):
profile.mugshot.delete()
profile.mugshot = None
elif 'use_initials' in self.data:
r = requests.get("https://unitials.com/mugshot/50/%s.png" %\
r = requests.get("https://unitials.com/mugshot/170/%s.png" %\
user.get_profile().get_initials())
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(r.content)
Expand All @@ -212,6 +214,9 @@ def save(self, *args, **kwargs):
if 'email' in self.data:
user.email = self.cleaned_data['email']

if 'username' in self.data:
user.username = self.cleaned_data['username']

if 'email_updates' in self.data:
profile.email_updates = self.cleaned_data['email_updates']

Expand All @@ -228,12 +233,25 @@ def clean_email(self):
if 'email' not in self.data:
return None

if User.objects.filter(email__iexact=self.cleaned_data['email']).exclude(
email__iexact=self.instance.email).exists():
if User.objects.filter(email__iexact=self.cleaned_data['email']
).exclude(pk=self.instance.id).exists():
raise django_forms.ValidationError(_(u'This email is already in '
u'use. Please supply a different email.'))
return self.cleaned_data['email']

def clean_username(self):
'''
Validate that the username is not already registered with another user
'''
if 'username' not in self.data:
return None

if User.objects.filter(username__iexact=self.cleaned_data['username']
).exclude(pk=self.instance.id).exists():
raise django_forms.ValidationError(_(u'This username is already in '
u'use. Please supply a different username.'))
return self.cleaned_data['username']

def clean_old_password(self):
'''
Clean old password if needed
Expand Down Expand Up @@ -273,7 +291,7 @@ def clean(self):

class Meta:
model = User
fields = ('first_name', 'last_name', 'username')
fields = ('first_name', 'last_name')

@staticmethod
def static_get_form_kwargs(request, data, *args, **kwargs):
Expand Down
9 changes: 9 additions & 0 deletions agora_site/agora_core/resources/user.py
Expand Up @@ -425,5 +425,14 @@ class UserSettingsResource(UserResource):
Resource representing users.
'''
email = fields.CharField()
biography = fields.CharField()
email_updates = fields.BooleanField()

def dehydrate_email(self, bundle):
return bundle.obj.email

def dehydrate_biography(self, bundle):
return bundle.obj.get_profile().biography

def dehydrate_email_updates(self, bundle):
return bundle.obj.get_profile().email_updates
50 changes: 24 additions & 26 deletions agora_site/agora_core/templates/agora_core/user_settings.html
Expand Up @@ -29,27 +29,21 @@
<div id="tab-profile" class="tab-pane active form-data admin-form span7">
<form>
<fieldset>
<legend><%= gettext("Profile") %></legend>

<div class="control-group" id="div_id_first_name">
<label class="control-label" for="id_first_name">
<%= gettext("First name") %>
<%= gettext("Full name") %>
</label>

<div class="controls">
<input class="textinput textInput" id="id_first_name"
maxlength="30" name="first_name" type="text"
value="edulix">
</div>
</div>

<div class="control-group" id="div_id_last_name">
<label class="control-label" for="id_last_name">
<%= gettext("Last name") %>
</label>
value="<%= first_name %>">

<div class="controls">
<input class="textinput textInput" id="id_last_name"
maxlength="30" name="last_name" type="text">
<p class="help-block" id="hint_id_first_name">
<%= gettext("Example: Pepito de los Palotes") %>
</p>
</div>
</div>

Expand All @@ -61,7 +55,8 @@
<div class="controls">
<input class="textinput textInput"
id="id_short_description" maxlength="140"
name="short_description" type="text" value="" />
name="short_description" type="text"
value="<%= short_description %>" />

<p class="help-block" id="hint_id_short_description">
<%= gettext("Say something about yourself (140 chars max)") %>
Expand All @@ -76,6 +71,7 @@
<div class="controls">
<textarea class="textarea" cols="40" id="id_biography"
name="biography" rows="10">
<%= biography %>
</textarea>

<p class="help-block" id="hint_id_biography">
Expand All @@ -87,7 +83,7 @@
<div class="control-group">
<div class="controls">
<button class="btn btn-large btn-block btn-success"
id="save_profile" type="button">
id="save_profile" type="submit">
<%= gettext("Save profile") %>
</button>
</div>
Expand All @@ -100,8 +96,7 @@
<div class='row user-picture'>
<div class='span3'>
<div class='large-user-avatar avatar-canvas' id='avatar-preview'>
<img alt="Fadb64d123868b634eb5d9541e8d899f" src=
"https://secure.gravatar.com/avatar/fadb64d123868b634eb5d9541e8d899f.png?d=none&amp;r=pg&amp;s=170"></div>
<img alt="" src="https://secure.gravatar.com/avatar/fadb64d123868b634eb5d9541e8d899f.png?d=none&amp;r=pg&amp;s=170"></div>
</div>

<div class='span3'>
Expand All @@ -113,7 +108,7 @@

<div class='medium-user-avatar avatar-canvas' id=
'image-option-gravatar'>
<span><img alt="Fadb64d123868b634eb5d9541e8d899f" src=
<span><img alt="" src=
"https://secure.gravatar.com/avatar/fadb64d123868b634eb5d9541e8d899f.png?d=none&amp;r=pg&amp;s=35"></span>
</div>

Expand Down Expand Up @@ -174,17 +169,17 @@
<div class="btn-group" id="id_language">
<a class="btn btn-block dropdown-toggle" data-toggle="dropdown" href="#">
<div class="current-item inline" data-id="es">
Spanish
<%= gettext("Spanish") %>
</div>
<span class="caret inline"></span>
</a>
<ul class="dropdown-menu span7">
<li data-id="es" data-usertext="Spanish">
<a href="#">Spanish</a>
<li data-id="es" data-usertext="<%= gettext("Spanish") %>">
<a href="#"><%= gettext("Spanish") %></a>
</li>
<li class="divider"></li>
<li data-id="en" data-usertext="English">
<a href="#">English</a>
<li data-id="en" data-usertext="<%= gettext("English") %>">
<a href="#"><%= gettext("English") %></a>
</li>
</ul>
</div>
Expand All @@ -203,7 +198,8 @@

<div class="controls">
<input class="required textinput textInput" id="id_email"
maxlength="75" name="email" type="text">
maxlength="75" name="email" type="text"
value="<%= email %>"/>

<p class="help-block" id="hint_id_email">
<%= gettext("Email is visible by agora admins") %>
Expand All @@ -214,9 +210,11 @@
<div class="control-group" id="div_id_email_updates">
<div class="controls">
<label class="checkbox" for="id_email_updates">
<input checked="checked" class= "checkboxinput"
<input
<%= (email_updates) ? 'checked="checked"' : '' %>
class= "checkboxinput"
id="id_email_updates" name="email_updates"
type="checkbox">
type="checkbox" />
<%= gettext("Receive email updates") %>
</label>
</div>
Expand All @@ -225,7 +223,7 @@
<div class="control-group">
<div class="controls">
<button class="btn btn-large btn-block btn-success"
id="save_profile" type="button">
id="save_profile" type="submit">
<%= gettext("Save email settings") %>
</button>
</div>
Expand Down
45 changes: 44 additions & 1 deletion agora_site/static/js/agora/views/user_settings.js
Expand Up @@ -10,11 +10,54 @@
this.template = _.template($("#template-user-settings-form").html());
this.render();

var metrics_profile = [
['#id_first_name', 'presence', gettext('This field is required')],
['#id_first_name', 'between:4:140', gettext('Must be between 4 and 140 characters long')],
];
this.$el.find("#tab-profile form").nod(metrics_profile,
{silentSubmit: true, broadcastError: true});
this.$el.find("#tab-profile form").on('silentSubmit', this.saveProfile);
this.$el.find("#tab-profile form").submit(function (e) { e.preventDefault(); });

return this.$el;
},

sendingData: false,

saveProfile: function(e) {
e.preventDefault();
if (this.sendingData) {
return;
}

$(".btn[type=submit]").addClass("disabled");
this.sendingData = true;

var json = {
'first_name': $('#id_first_name').val(),
'short_description': $('#id_short_description').val(),
'biography': $('#id_biography').val(),
};
var self = this;
var jqxhr = $.ajax("/api/v1/user/settings/", {
data: JSON.stringifyCompat(json),
contentType : 'application/json',
type: 'PUT',
})
.done(function(e) {
window.location.reload(true);
})
.fail(function() {
self.sendingData = false;
$(".btn[type=submit]").removeClass("disabled");
alert("Error saving settings");
});

return false;
},

render: function() {
this.$el.html(this.template({}));
this.$el.html(this.template(ajax_data.user));
this.delegateEvents();
return this;
}
Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Expand Up @@ -2285,6 +2285,7 @@ Get User settings
"is_active": true,
"last_login": "2013-05-17T17:45:01.597510",
"full_name": "",
"biography": "blah blah",
"short_description": "Is a member of 2 agoras and has emitted 0 direct votes.",
"id": 0,
"date_joined": "2012-11-29T16:08:43.874000"
Expand Down

0 comments on commit 4cf8cc4

Please sign in to comment.