allow username setting and tagline in profile

started work on user profiles, with "tagline" field
the first a user signs in this google, they are redirected to a page
where they can set their username
pull/1/head
eric 2011-10-03 12:36:04 -04:00
parent 77eb1db0ce
commit 73c4ebde29
12 changed files with 281 additions and 3 deletions

View File

@ -0,0 +1,131 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'UserProfile'
db.create_table('core_userprofile', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], unique=True)),
('tagline', self.gf('django.db.models.fields.CharField')(max_length=140, blank=True)),
))
db.send_create_signal('core', ['UserProfile'])
def backwards(self, orm):
# Deleting model 'UserProfile'
db.delete_table('core_userprofile')
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'core.author': {
'Meta': {'object_name': 'Author'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
'openlibrary_id': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True'}),
'works': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'authors'", 'symmetrical': 'False', 'to': "orm['core.Work']"})
},
'core.campaign': {
'Meta': {'object_name': 'Campaign'},
'amazon_receiver': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'deadline': ('django.db.models.fields.DateTimeField', [], {}),
'description': ('django.db.models.fields.CharField', [], {'max_length': '10000'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
'paypal_receiver': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True'}),
'target': ('django.db.models.fields.FloatField', [], {}),
'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'campaigns'", 'to': "orm['core.Work']"})
},
'core.edition': {
'Meta': {'object_name': 'Edition'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'default': "''", 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'isbn_10': ('django.db.models.fields.CharField', [], {'max_length': '10', 'null': 'True'}),
'isbn_13': ('django.db.models.fields.CharField', [], {'max_length': '13', 'null': 'True'}),
'openlibrary_id': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True'}),
'publication_date': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'publisher': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '1000'}),
'work': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'editions'", 'null': 'True', 'to': "orm['core.Work']"})
},
'core.editioncover': {
'Meta': {'object_name': 'EditionCover'},
'edition': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'covers'", 'to': "orm['core.Edition']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'openlibrary_id': ('django.db.models.fields.IntegerField', [], {})
},
'core.subject': {
'Meta': {'object_name': 'Subject'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
'works': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'subjects'", 'symmetrical': 'False', 'to': "orm['core.Work']"})
},
'core.userprofile': {
'Meta': {'object_name': 'UserProfile'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'tagline': ('django.db.models.fields.CharField', [], {'max_length': '140', 'blank': 'True'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'unique': 'True'})
},
'core.wishlist': {
'Meta': {'object_name': 'Wishlist'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'wishlist'", 'unique': 'True', 'to': "orm['auth.User']"}),
'works': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'wishlists'", 'symmetrical': 'False', 'to': "orm['core.Work']"})
},
'core.work': {
'Meta': {'object_name': 'Work'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'openlibrary_id': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '1000'})
}
}
complete_apps = ['core']

View File

@ -85,5 +85,8 @@ class Wishlist(models.Model):
user = models.OneToOneField(User, related_name='wishlist')
works = models.ManyToManyField('Work', related_name='wishlists')
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
tagline = models.CharField(max_length=140, blank=True)
from regluit.core import signals

38
frontend/forms.py Normal file
View File

@ -0,0 +1,38 @@
from django import forms
from django.db import models
#from django.forms import Form, ModelForm, Textarea, CharField, ValidationError, RegexField
from regluit.core.models import UserProfile
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
class ProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = 'user'
widgets = {
'tagline': forms.Textarea(attrs={'cols': 70, 'rows': 2}),
}
class UserData(forms.Form):
username = forms.RegexField(
label=_("New Username"),
max_length=30,
regex=r'^[\w.@+-]+$',
help_text = _("30 characters or less."),
error_messages = {
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
}
)
# oldusername = forms.CharField(max_length=30)
def clean_username(self):
username = self.data["username"]
oldusername = self.data["oldusername"]
if username != oldusername:
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(_("Another user with that username already exists."))
raise forms.ValidationError(_("Your username is already "+oldusername))

View File

@ -1,6 +1,7 @@
<!DOCTYPE html>
{# raw url references raise test errors in tests for django registration; this is a workaround #}
{% url privacy as privacyurl %}
{% url regluit.frontend.views.edit_user as editurl %}
<html>
<head>
<title>unglue.it {% block title %}{% endblock %}</title>
@ -45,9 +46,12 @@
</div>
<div id="footer">
<ul class="menu">
<li><a href="{{privacyurl}}"><span>Privacy</span></a></li>
</ul>
<p>
<a href="{{privacyurl}}"><span>Privacy</span></a>&nbsp;&nbsp;&nbsp;&nbsp;
{% if user.is_authenticated %}
<a href="{{editurl}}"><span>Settings</span></a>&nbsp;&nbsp;&nbsp;&nbsp;
{% endif %}
</p>
</div>
</body>

View File

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block title %}Create Profile{% endblock %}
{% block content %}
<div id="registration">
<h1>Create a new profile for {{ user }} </h1>
<form method="POST" action="">
{% csrf_token %}
{{ form }}
<input type="submit" name="submit" value="Save" id="submit">
</form>
</div>
{% endblock content %}

View File

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block title %}Edit Profile{% endblock %}
{% block content %}
<div id="registration">
<h1>Edit profile for {{ user }} </h1>
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="submit" value="Update" id="submit">
</form>
{{ debug }}
</div>
{% endblock content %}

View File

@ -0,0 +1,13 @@
{% extends "base.html" %}
{% block title %}Edit Profile{% endblock %}
{% block content %}
<div id="registration">
<h1>Profile for {{ user }} </h1>
<p><b>TagLine:</b> {{ profile.tagline }}</p>
<p><a href="/profiles/edit/">Edit Again</a></p>
<p><a href="/supporter/{{user|urlencode}}/">{{user}}'s Unglue.it Page</a></p>
</div>
{% endblock content %}

View File

@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block title %}Profile List{% endblock %}
{% block content %}
<div id="registration">
Sorry, you can't list the profiles on this site.
</div>
{% endblock content %}

View File

@ -0,0 +1,19 @@
{% extends "registration/registration_base.html" %}
{% block title %}Change User Data{% endblock %}
{% block content %}
<div id="registration">
<h1>Changing your Username.</h1>
<p> If you change your Username, the web address for your profile page will change as well.</p>
<p> <b>Your current Username:</b> {{ user.username }}</p>
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="submit" value="Change" id="submit">
</form>
<p><a href="/profiles/edit/">Edit Your Profile</a></p>
</div>
{% endblock %}

View File

@ -1,5 +1,6 @@
from django.template import RequestContext
from django.contrib.auth.models import User
# from django.contrib.auth.forms import UserChangeForm
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
@ -10,6 +11,8 @@ from django.shortcuts import render, render_to_response, get_object_or_404
from regluit.core import models, bookloader
from regluit.core.search import gluejar_search
from regluit.frontend.forms import UserData
def home(request):
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('supporter',
@ -25,6 +28,24 @@ def supporter(request, supporter_username):
}
return render(request, 'supporter.html', context)
def edit_user(request):
form=UserData()
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('auth_login'))
oldusername=request.user.username
if request.method == 'POST':
# surely there's a better way to add data to the POST data?
postcopy=request.POST.copy()
postcopy['oldusername']=oldusername
form = UserData(postcopy)
if form.is_valid(): # All validation rules pass, go and change the username
request.user.username=form.cleaned_data['username']
request.user.save()
return HttpResponseRedirect(reverse('home')) # Redirect after POST
return render(request,'registration/user_change_form.html', {'form': form},)
def search(request):
q = request.GET.get('q', None)
results = gluejar_search(q)

View File

@ -107,6 +107,7 @@ INSTALLED_APPS = (
'registration',
'social_auth',
'tastypie',
'profiles',
)
# A sample logging configuration. The only tangible logging
@ -169,6 +170,8 @@ AUTHENTICATION_BACKENDS = (
SOCIAL_AUTH_ENABLED_BACKENDS = ['google', 'facebook', 'twitter']
SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/accounts/edit/'
FACEBOOK_EXTENDED_PERMISSIONS = ['email']
LOGIN_URL = "/accounts/login/"
@ -178,3 +181,5 @@ LOGOUT_URL = "/accounts/logout/"
USER_AGENT = "unglue.it.bot v0.0.1 <http://unglue.it>"
SOUTH_TESTS_MIGRATE = False
AUTH_PROFILE_MODULE = "core.userprofile"

View File

@ -1,11 +1,15 @@
from django.conf.urls.defaults import *
from frontend.forms import ProfileForm
urlpatterns = patterns('',
url(r'^accounts/activate/complete/$','django.contrib.auth.views.login',
{'template_name': 'registration/activation_complete.html'}),
(r'^accounts/edit/$', 'regluit.frontend.views.edit_user'),
(r'^accounts/', include('registration.backends.default.urls')),
(r'^socialauth/', include('social_auth.urls')),
(r'^api/', include('regluit.api.urls')),
('^profiles/edit/$', 'profiles.views.edit_profile', {'form_class': ProfileForm,}),
(r'^profiles/', include('profiles.urls')),
(r'', include('regluit.frontend.urls')),
(r'', include('regluit.payment.urls'))
)