From 29816137000b78f6bc14674d37c4e7cd39f7f9be Mon Sep 17 00:00:00 2001 From: Sofia Papagiannaki Date: Thu, 4 Oct 2012 13:20:28 +0300 Subject: [PATCH] Turn emails to case-insensitive. Refs: #2883 --- snf-astakos-app/astakos/im/forms.py | 4 + .../im/migrations/0025_case_insensitive_emails.py | 177 ++++++++++++++++++++ snf-astakos-app/astakos/im/models.py | 11 +- snf-astakos-app/astakos/im/target/local.py | 3 +- snf-astakos-app/astakos/im/util.py | 2 +- 5 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 snf-astakos-app/astakos/im/migrations/0025_case_insensitive_emails.py diff --git a/snf-astakos-app/astakos/im/forms.py b/snf-astakos-app/astakos/im/forms.py index a37e768..8ba1423 100644 --- a/snf-astakos-app/astakos/im/forms.py +++ b/snf-astakos-app/astakos/im/forms.py @@ -323,6 +323,10 @@ class LoginForm(AuthenticationForm): self.fields.keyOrder.extend(['recaptcha_challenge_field', 'recaptcha_response_field', ]) + def clean_username(self): + if 'username' in self.cleaned_data: + return self.cleaned_data['username'].lower() + def clean_recaptcha_response_field(self): if 'recaptcha_challenge_field' in self.cleaned_data: self.validate_captcha() diff --git a/snf-astakos-app/astakos/im/migrations/0025_case_insensitive_emails.py b/snf-astakos-app/astakos/im/migrations/0025_case_insensitive_emails.py new file mode 100644 index 0000000..7346d8b --- /dev/null +++ b/snf-astakos-app/astakos/im/migrations/0025_case_insensitive_emails.py @@ -0,0 +1,177 @@ +# encoding: utf-8 +import datetime +from south.db import db +from south.v2 import DataMigration +from django.db import models + +class Migration(DataMigration): + + def forwards(self, orm): + "Write your forwards methods here." + for u in orm.AstakosUser: + u.email = u.email.lower() + u.save() + + def backwards(self, orm): + "Write your backwards methods here." + + + 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'}) + }, + 'im.additionalmail': { + 'Meta': {'object_name': 'AdditionalMail'}, + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"}) + }, + 'im.approvalterms': { + 'Meta': {'object_name': 'ApprovalTerms'}, + 'date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 10, 4, 9, 47, 13, 40029)', 'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'location': ('django.db.models.fields.CharField', [], {'max_length': '255'}) + }, + 'im.astakosgroup': { + 'Meta': {'object_name': 'AstakosGroup', '_ormbases': ['auth.Group']}, + 'approval_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 10, 4, 9, 47, 13, 34050)'}), + 'desc': ('django.db.models.fields.TextField', [], {'null': 'True'}), + 'estimated_participants': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}), + 'expiration_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'group_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.Group']", 'unique': 'True', 'primary_key': 'True'}), + 'homepage': ('django.db.models.fields.URLField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'issue_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'kind': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.GroupKind']"}), + 'moderation_enabled': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'policy': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosGroupQuota']", 'blank': 'True'}) + }, + 'im.astakosgroupquota': { + 'Meta': {'unique_together': "(('resource', 'group'),)", 'object_name': 'AstakosGroupQuota'}, + 'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']", 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'limit': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}), + 'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"}), + 'uplimit': ('django.db.models.fields.BigIntegerField', [], {'null': 'True'}) + }, + 'im.astakosuser': { + 'Meta': {'unique_together': "(('provider', 'third_party_identifier'),)", 'object_name': 'AstakosUser', '_ormbases': ['auth.User']}, + 'activation_sent': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'affiliation': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}), + 'astakos_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.AstakosGroup']", 'symmetrical': 'False', 'through': "orm['im.Membership']", 'blank': 'True'}), + 'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}), + 'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'date_signed_terms': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'email_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'has_credits': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'has_signed_terms': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'invitations': ('django.db.models.fields.IntegerField', [], {'default': '100'}), + 'is_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'level': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'owner': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'owner'", 'null': 'True', 'to': "orm['im.AstakosGroup']"}), + 'policy': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.Resource']", 'null': 'True', 'through': "orm['im.AstakosUserQuota']", 'symmetrical': 'False'}), + 'provider': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}), + 'third_party_identifier': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'updated': ('django.db.models.fields.DateTimeField', [], {}), + 'user_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True', 'primary_key': 'True'}) + }, + 'im.astakosuserquota': { + 'Meta': {'unique_together': "(('resource', 'user'),)", 'object_name': 'AstakosUserQuota'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'limit': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}), + 'resource': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Resource']"}), + 'uplimit': ('django.db.models.fields.BigIntegerField', [], {'null': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"}) + }, + 'im.emailchange': { + 'Meta': {'object_name': 'EmailChange'}, + 'activation_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '40', 'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'new_email_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'}), + 'requested_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2012, 10, 4, 9, 47, 13, 41566)'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'emailchange_user'", 'unique': 'True', 'to': "orm['im.AstakosUser']"}) + }, + 'im.groupkind': { + 'Meta': {'object_name': 'GroupKind'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}) + }, + 'im.invitation': { + 'Meta': {'object_name': 'Invitation'}, + 'code': ('django.db.models.fields.BigIntegerField', [], {'db_index': 'True'}), + 'consumed': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'inviter': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'invitations_sent'", 'null': 'True', 'to': "orm['im.AstakosUser']"}), + 'is_consumed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'realname': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}) + }, + 'im.membership': { + 'Meta': {'unique_together': "(('person', 'group'),)", 'object_name': 'Membership'}, + 'date_joined': ('django.db.models.fields.DateField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}), + 'date_requested': ('django.db.models.fields.DateField', [], {'default': 'datetime.datetime(2012, 10, 4, 9, 47, 13, 37772)', 'blank': 'True'}), + 'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosGroup']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'person': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.AstakosUser']"}) + }, + 'im.resource': { + 'Meta': {'object_name': 'Resource'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'meta': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['im.ResourceMetadata']", 'symmetrical': 'False'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}), + 'service': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['im.Service']"}) + }, + 'im.resourcemetadata': { + 'Meta': {'object_name': 'ResourceMetadata'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}), + 'value': ('django.db.models.fields.CharField', [], {'max_length': '255'}) + }, + 'im.service': { + 'Meta': {'object_name': 'Service'}, + 'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}), + 'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'icon': ('django.db.models.fields.FilePathField', [], {'max_length': '100', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}), + 'url': ('django.db.models.fields.FilePathField', [], {'max_length': '100'}) + } + } + + complete_apps = ['im'] diff --git a/snf-astakos-app/astakos/im/models.py b/snf-astakos-app/astakos/im/models.py index e86aaa2..c30a8e4 100644 --- a/snf-astakos-app/astakos/im/models.py +++ b/snf-astakos-app/astakos/im/models.py @@ -46,7 +46,8 @@ from django.contrib.auth.models import User, UserManager, Group from django.utils.translation import ugettext as _ from django.core.exceptions import ValidationError from django.db import transaction -from django.db.models.signals import pre_save, post_save, post_syncdb, post_delete +from django.db.models.signals import (pre_save, post_save, post_syncdb, + post_delete) from django.dispatch import Signal from django.db.models import Q @@ -256,7 +257,9 @@ class AstakosUser(User): astakos_groups = models.ManyToManyField( AstakosGroup, verbose_name=_('agroups'), blank=True, - help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in."), + help_text=_("""In addition to the permissions manually assigned, this + user will also get all permissions granted to each group + he/she is in."""), through='Membership') __has_signed_terms = False @@ -330,6 +333,7 @@ class AstakosUser(User): self.username = username if not self.provider: self.provider = 'local' + self.email = self.email.lower() self.validate_unique_email_isactive() if self.is_active and self.activation_sent: # reset the activation sent @@ -518,7 +522,8 @@ class EmailChangeManager(models.Manager): class EmailChange(models.Model): - new_email_address = models.EmailField(_(u'new e-mail address'), help_text=_(u'Your old email address will be used until you verify your new one.')) + new_email_address = models.EmailField(_(u'new e-mail address'), + help_text=_(u'Your old email address will be used until you verify your new one.')) user = models.ForeignKey( AstakosUser, unique=True, related_name='emailchange_user') requested_at = models.DateTimeField(default=datetime.now()) diff --git a/snf-astakos-app/astakos/im/target/local.py b/snf-astakos-app/astakos/im/target/local.py index 5e31d4d..fb3ddaf 100644 --- a/snf-astakos-app/astakos/im/target/local.py +++ b/snf-astakos-app/astakos/im/target/local.py @@ -58,8 +58,7 @@ def login(request, on_failure='im/login.html'): was_limited = getattr(request, 'limited', False) form = LoginForm(data=request.POST, was_limited=was_limited, - request=request - ) + request=request) next = get_query(request).get('next', '') if not form.is_valid(): return render_to_response(on_failure, diff --git a/snf-astakos-app/astakos/im/util.py b/snf-astakos-app/astakos/im/util.py index 52f5a3c..9ade1af 100644 --- a/snf-astakos-app/astakos/im/util.py +++ b/snf-astakos-app/astakos/im/util.py @@ -172,7 +172,7 @@ def reverse_lazy(*args, **kwargs): def reserved_email(email): - return AstakosUser.objects.filter(email=email).count() != 0 + return AstakosUser.objects.filter(email__iexact=email).count() != 0 def get_query(request): -- 1.7.10.4