# HG changeset patch # User dellsystem # Date 1348327103 14400 # Node ID 5ab229c9d3489ed72011ef9db36368098b8e46e3 # Parent be8fec1e85d927c64aef0eef59ba90ad98ecc09c Add back ability to edit account settings * Account/profile editing is now done with a ModelForm, instead of processing the data manually. Help text and verbose names have been moved to the Profile model and to the UserForm (in the latter case, it's because we can't edit the User model directly). * The profile page now shows pygments style information and sections of the profile that are not filled are now hidden. * Some parts of apps/profiles/views.py have been rewritten (the getprofile() method has been removed, as it's no longer necessary, and the editprofile() view was rewritten in accordance with the switch to using ModelForms). * The styling for forms has been modified slightly. diff -r be8fec1e85d9 -r 5ab229c9d348 apps/profile/forms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/apps/profile/forms.py Sat Sep 22 11:18:23 2012 -0400 @@ -0,0 +1,25 @@ +from django.forms import ModelForm +from django.contrib.auth.models import User +from django.utils.translation import ugettext as _ + +from agora.apps.profile.models import Profile + + +class ProfileForm(ModelForm): + class Meta: + model = Profile + exclude = ('user',) + + +class UserForm(ModelForm): + class Meta: + model = User + fields = ('first_name', 'last_name') + + def __init__(self, *args, **kwargs): + super(UserForm, self).__init__(*args, **kwargs) + + # Change the help text for names + name_text = _("We will display your name according to most Western \ + European conventions, your given name(s) followed by your surname(s).") + self.fields['first_name'].help_text = name_text diff -r be8fec1e85d9 -r 5ab229c9d348 apps/profile/models.py --- a/apps/profile/models.py Sat Sep 22 11:12:30 2012 -0400 +++ b/apps/profile/models.py Sat Sep 22 11:18:23 2012 -0400 @@ -1,5 +1,6 @@ from django.db import models from django.contrib.auth.models import User +from django.utils.translation import ugettext as _ from agora.apps.free_license.models import FreeLicense from agora.apps.pygments_style.models import PygmentsStyle @@ -7,10 +8,23 @@ class Profile(models.Model): user = models.OneToOneField(User) - preferred_license = models.ForeignKey(FreeLicense, default=1) - interests = models.CharField(max_length=512, null=True, blank=True) - blurb = models.TextField(max_length=16384, null=True, blank=True) - pygments_style = models.ForeignKey(PygmentsStyle, default=1) + preferred_license = models.ForeignKey(FreeLicense, help_text=_("\ + By default, all of your submissions will be under the following \ + license, and this license will be displayed next to your \ + submissions. Find out more."), + default=1) + interests = models.CharField(max_length=512, null=True, help_text=_("\ + Tell us about your research interests (e.g. \ + signal processing, hyperbolic PDEs). These \ + keywords will be used when searching for submissions."), blank=True, + verbose_name=_("Research interests")) + blurb = models.TextField(max_length=16384, null=True, help_text=_("\ + Finally, anything else you would like to say about yourself."), + blank=True) + pygments_style = models.ForeignKey(PygmentsStyle, default=1, + verbose_name=_('Syntax highlighting style'), help_text=_("\ + Choose a stylesheet for displayed syntax-highlighted code. Most of \ + these stylesheets are based off of default Pygments stylesheets.")) def __unicode__(self): return self.user.username diff -r be8fec1e85d9 -r 5ab229c9d348 apps/profile/urls.py --- a/apps/profile/urls.py Sat Sep 22 11:12:30 2012 -0400 +++ b/apps/profile/urls.py Sat Sep 22 11:18:23 2012 -0400 @@ -2,5 +2,5 @@ urlpatterns = patterns('agora.apps.profile.views', url(r'^editprofile$', 'editprofile', name='edit_profile'), - url(r'^(?P\w*)/$', 'showprofile', name='show_profile'), + url(r'^(?P\w*)/$', 'showprofile', name='show_profile'), ) diff -r be8fec1e85d9 -r 5ab229c9d348 apps/profile/views.py --- a/apps/profile/views.py Sat Sep 22 11:12:30 2012 -0400 +++ b/apps/profile/views.py Sat Sep 22 11:18:23 2012 -0400 @@ -1,81 +1,57 @@ -from django.shortcuts import render_to_response, get_object_or_404 +from django.shortcuts import render, get_object_or_404, redirect from django.contrib.auth.models import User -from django.http import Http404, HttpResponseRedirect -from django.core.urlresolvers import reverse from django.contrib.auth.decorators import login_required -from django.views.generic.simple import direct_to_template from agora.apps.free_license.models import FreeLicense from agora.apps.bundle.models import Bundle from agora.apps.snippet.models import Snippet from agora.apps.profile.models import Profile -from agora.middleware.http import Http403 +from agora.apps.profile.forms import UserForm, ProfileForm -def getprofile(user): - u = get_object_or_404(User, username=user) - - #Inactive users "don't exist" - if not u.is_active: - raise Http404 +def showprofile(request, username): + user = get_object_or_404(User, username=username) + profile = user.get_profile() - #Get profile or create a default if none exists - try: - p = u.get_profile() - except Profile.DoesNotExist: - #At least one FreeLicense *must* exist. - p = Profile(user=u, preferred_license=FreeLicense.objects.get(id=1)) - p.save() - - return [u,p] + if user.first_name or user.last_name: + name = user.get_full_name() + else: + name = user.username -def showprofile(request, user): - [u,p] = getprofile(user) - - if u.first_name or u.last_name: - n = u.get_full_name() - else: - n = u.username + b = Bundle.objects.filter(uploader=user) + s = Snippet.objects.filter(author=user) - b = Bundle.objects.filter(uploader=u) - s = Snippet.objects.filter(author=u) + context = { + 'profile': user.get_profile, + 'name': name, + 'bundles': Bundle.objects.filter(uploader=user), + 'snippets': Snippet.objects.filter(author=user), + } - return direct_to_template(request, 'profile/user.djhtml', - { - 'profile' : p, - 'bundles' : b, - 'snippets' : s, - 'name' : n, - }, - ) + return render(request, 'profile/user.djhtml', context) + @login_required def editprofile(request): - [u,p] = getprofile(request.user) + user = request.user + profile = user.get_profile() - if request.method=='POST': - u.first_name = request.POST['first-name'] - u.last_name = request.POST['last-name'] - u.save() - - try: - p.preferred_license = \ - FreeLicense.objects.get(id=request.POST['license']) - except: - p.preferred_license = FreeLicense.objects.get(id=1) + if request.method == 'POST': + user_form = UserForm(request.POST, instance=user) + profile_form = ProfileForm(request.POST, instance=profile) - p.interests = request.POST['interests'] - p.blurb = request.POST['blurb'] - p.save() - return HttpResponseRedirect(reverse( - 'agora.apps.profile.views.showprofile', - args=(u,)) - ) + if user_form.is_valid() and profile_form.is_valid(): + user_form.save() + profile_form.save() + return redirect(user) + else: + user_form = UserForm(instance=user) + profile_form = ProfileForm(instance=profile) - licenses = FreeLicense.objects.all() - return direct_to_template(request, 'profile/edit-user.djhtml', - { - 'profile' : p, - 'licenses' : licenses, - }, - ) + context = { + 'profile': profile, + 'user_form': user_form, + 'profile_form': profile_form, + } + + return render(request, 'profile/edit-user.djhtml', context) diff -r be8fec1e85d9 -r 5ab229c9d348 static/css/agora.less --- a/static/css/agora.less Sat Sep 22 11:12:30 2012 -0400 +++ b/static/css/agora.less Sat Sep 22 11:18:23 2012 -0400 @@ -313,11 +313,27 @@ label { float: left; text-align: right; + padding-right: 20px; + padding-top: 10px; + font-weight: bold; + width: 180px; } } .form-input { - margin-left: 180px; + margin-left: 200px; + padding: 5px 0; + input[type=text] { + width: @fieldWidth; + height: 20px; + } + textarea { + width: @fieldWidth; + } + p { + padding-top: 10px; + padding-bottom: 0; + } } .errors { diff -r be8fec1e85d9 -r 5ab229c9d348 static/css/variables.less --- a/static/css/variables.less Sat Sep 22 11:12:30 2012 -0400 +++ b/static/css/variables.less Sat Sep 22 11:18:23 2012 -0400 @@ -28,3 +28,4 @@ @nonSidebarWidth: @fixedWidth - @sidebarWidth - @sidebarLeftSpace; @inputPadding: 5px; @sidebarPadding: 10px; +@fieldWidth: 400px; diff -r be8fec1e85d9 -r 5ab229c9d348 templates/field.djhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/field.djhtml Sat Sep 22 11:18:23 2012 -0400 @@ -0,0 +1,15 @@ +{% if field.errors %} +
+ {{ field.errors }} +
+{% endif %} + +
+ {{ field.label_tag }} +
+ {{ field }} + {% if field.help_text %} +

{{ field.help_text|safe }}

+ {% endif %} +
+
diff -r be8fec1e85d9 -r 5ab229c9d348 templates/profile/edit-user.djhtml --- a/templates/profile/edit-user.djhtml Sat Sep 22 11:12:30 2012 -0400 +++ b/templates/profile/edit-user.djhtml Sat Sep 22 11:18:23 2012 -0400 @@ -1,76 +1,34 @@ -{% extends "profile/user.djhtml" %} +{% extends "base.djhtml" %} -{% block boxtitle %} -Edit your profile -{% endblock boxtitle %} +{% block breadcrubs %} +Viewing your profile +{% endblock %} -{% block boxcontents %} -

+{% block content %} +

Edit your account settings ({{ user }})

+

You can provide some extra optional information about yourself. We recommend that you provide us with a real name. If you do, it will be displayed next to all of your contributions instead of your Agora username.

-
- - {% csrf_token %} + + {% if form.non_field_errors %} + {{ form.non_field_errors }} + {% endif %} + {% csrf_token %} -

- We will display your name according to most Western European - conventions, your given name(s) followed by your surname(s). -

- Given name(s) -
- Surname(s) -
- -

- By default, all of your submissions will be under the following - license, and will be displayed next to your submissions. - - Here is a thorough explanation of the available licenses. - -

+ {% for field in user_form %} + {% include "field.djhtml" %} + {% endfor %} - Preferred license - + {% for field in profile_form %} + {% include "field.djhtml" %} + {% endfor %} -

- Tell us about your research interests (e.g. signal processing, - hyperbolic PDEs). These keywords will be used when - searching for submissions. -

- Research interests -
- -

- Finally, anything else you would like to say about yourself. -

- - Blurb - - -
- - +
+ + +
-{% endblock boxcontents %} +{% endblock %} diff -r be8fec1e85d9 -r 5ab229c9d348 templates/profile/user.djhtml --- a/templates/profile/user.djhtml Sat Sep 22 11:12:30 2012 -0400 +++ b/templates/profile/user.djhtml Sat Sep 22 11:18:23 2012 -0400 @@ -17,15 +17,22 @@
Preferred license:
{{ profile.preferred_license }}
+ +
Syntax highlighting style:
+
{{ profile.pygments_style }}
+{% if profile.interests %}

Interests

{{ profile.interests }}

+{% endif %} +{% if profile.blurb %}

About {{ name }}

{{ profile.blurb }}

+{% endif %} {% endblock %} {% block content-related %}