Permalink
Browse files

Migrate Invites to using FKs instead of LDAP

  • Loading branch information...
1 parent 8e148d5 commit 6b13f610d012d5b78615785a0ed9d20e92a9a23a @davedash davedash committed Dec 17, 2011
@@ -41,7 +41,8 @@ def process_request(self, request):
'is missing from your settings.py')
raise ImproperlyConfigured(msg)
- if request.user.is_authenticated():
+ if (request.user.is_authenticated() and
+ hasattr(request.user, 'ldap_user')):
_populate(request)
def process_response(self, request, response):
@@ -165,5 +165,12 @@ def clean_recipient(self):
'already been vouched.'))
return recipient
+ def save(self, inviter):
+ invite = super(InviteForm, self).save(commit=False)
+ invite.inviter = inviter
+ invite.save()
+ return invite
+
class Meta:
model = Invite
+ exclude = ('redeemer', 'inviter')
@@ -0,0 +1,33 @@
+# 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):
+ # This was just a reaname of the model field, the db field stays the
+ # same
+ pass
+
+ def backwards(self, orm):
+ # This was just a reaname of the model field, the db field stays the
+ # same.
+
+ pass
+
+ models = {
+ 'phonebook.invite': {
+ 'Meta': {'object_name': 'Invite', 'db_table': "'invite'"},
+ 'code': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'inviter_old': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32', 'db_column': "'inviter'"}),
+ 'recipient': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+ 'redeemed': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+ 'redeemer_old': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32', 'db_column': "'redeemer'"})
+ }
+ }
+
+ complete_apps = ['phonebook']
@@ -0,0 +1,102 @@
+# 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 field 'Invite.inviter'
+ db.add_column('invite', 'inviter', self.gf('django.db.models.fields.related.ForeignKey')(related_name='invites', null=True, to=orm['users.UserProfile']), keep_default=False)
+
+ # Adding field 'Invite.redeemer'
+ db.add_column('invite', 'redeemer', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['users.UserProfile'], unique=True, null=True), keep_default=False)
+
+
+ def backwards(self, orm):
+
+ # Deleting field 'Invite.inviter'
+ db.delete_column('invite', 'inviter_id')
+
+ # Deleting field 'Invite.redeemer'
+ db.delete_column('invite', 'redeemer_id')
+
+
+ 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'})
+ },
+ 'groups.group': {
+ 'Meta': {'object_name': 'Group', 'db_table': "'group'"},
+ 'always_auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'}),
+ 'system': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+ 'url': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'})
+ },
+ 'phonebook.invite': {
+ 'Meta': {'object_name': 'Invite', 'db_table': "'invite'"},
+ 'code': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
+ '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': "'invites'", 'null': 'True', 'to': "orm['users.UserProfile']"}),
+ 'inviter_old': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32', 'db_column': "'inviter'"}),
+ 'recipient': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+ 'redeemed': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+ 'redeemer': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['users.UserProfile']", 'unique': 'True', 'null': 'True'}),
+ 'redeemer_old': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32', 'db_column': "'redeemer'"})
+ },
+ 'users.userprofile': {
+ 'Meta': {'object_name': 'UserProfile', 'db_table': "'profile'"},
+ 'bio': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255'}),
+ 'confirmation_code': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
+ 'display_name': ('django.db.models.fields.CharField', [], {'max_length': '30'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['groups.Group']", 'symmetrical': 'False'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ircname': ('django.db.models.fields.CharField', [], {'max_length': '63', 'blank': 'True'}),
+ 'is_confirmed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_vouched': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'photo': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}),
+ 'vouched_by': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['users.UserProfile']", 'null': 'True'}),
+ 'website': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True'})
+ }
+ }
+
+ complete_apps = ['phonebook']
@@ -0,0 +1,116 @@
+# encoding: utf-8
+import datetime
+
+from django.db import models
+from django.db.utils import IntegrityError
+
+import commonware.log
+from south.db import db
+from south.v2 import DataMigration
+
+from users.models import UserProfile
+
+log = commonware.log.getLogger('m.migrator')
+
+
+class Migration(DataMigration):
+
+ def forwards(self, orm):
+ "Write your forwards methods here."
+ for i in orm.Invite.objects.all():
+ try:
+ i.inviter_id = UserProfile.objects.get_by_unique_id(
+ i.inviter_old).id
+ if i.redeemer_old:
+ i.redeemer_id = UserProfile.objects.get_by_unique_id(
+ i.redeemer_old).id
+ i.save()
+ log.debug('Invite %d converted' % i.pk)
+ except UserProfile.DoesNotExist:
+ log.warning('Invite %d not converted' % i.pk)
+ except IntegrityError as e:
+ if 'redeemer_id' in e.args[1]:
+ log.warning('Bogus looking invite, deleting %d' % i.pk)
+ i.delete()
+ else:
+ raise
+ def backwards(self, orm):
+ "Write your backwards methods here."
+ # No going back from LDAP
+
+ 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'})
+ },
+ 'groups.group': {
+ 'Meta': {'object_name': 'Group', 'db_table': "'group'"},
+ 'always_auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'auto_complete': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'}),
+ 'system': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+ 'url': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'})
+ },
+ 'phonebook.invite': {
+ 'Meta': {'object_name': 'Invite', 'db_table': "'invite'"},
+ 'code': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
+ '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': "'invites'", 'null': 'True', 'to': "orm['users.UserProfile']"}),
+ 'inviter_old': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32', 'db_column': "'inviter'"}),
+ 'recipient': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+ 'redeemed': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+ 'redeemer': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['users.UserProfile']", 'unique': 'True', 'null': 'True'}),
+ 'redeemer_old': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '32', 'db_column': "'redeemer'"})
+ },
+ 'users.userprofile': {
+ 'Meta': {'object_name': 'UserProfile', 'db_table': "'profile'"},
+ 'bio': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255'}),
+ 'confirmation_code': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}),
+ 'display_name': ('django.db.models.fields.CharField', [], {'max_length': '30'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['groups.Group']", 'symmetrical': 'False'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ircname': ('django.db.models.fields.CharField', [], {'max_length': '63', 'blank': 'True'}),
+ 'is_confirmed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_vouched': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'photo': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}),
+ 'vouched_by': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['users.UserProfile']", 'null': 'True'}),
+ 'website': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True'})
+ }
+ }
+
+ complete_apps = ['phonebook']
Oops, something went wrong.

0 comments on commit 6b13f61

Please sign in to comment.