Commits

Keegan Carruthers-Smith committed 575e4d4 Draft

Add a UserProfile storing an auth token.

Comments (0)

Files changed (3)

 from django.contrib import admin
-from mcpweb.models import TronGame
+from mcpweb.models import TronGame, UserProfile
 
 
 class TronGameAdmin(admin.ModelAdmin):
                     'date_created', 'last_played')
     radio_fields = {'winner': admin.HORIZONTAL}
 
+
+class UserProfileAdmin(admin.ModelAdmin):
+    readonly_fields = ('user',)
+
 admin.site.register(TronGame, TronGameAdmin)
+admin.site.register(UserProfile, UserProfileAdmin)

mcpweb/migrations/0006_auto__add_userprofile.py

+# -*- coding: utf-8 -*-
+from south.db import db
+from south.v2 import SchemaMigration
+from django.utils.crypto import get_random_string
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding model 'UserProfile'
+        db.create_table('mcpweb_userprofile', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['auth.User'], unique=True)),
+            ('auth_token', self.gf('django.db.models.fields.CharField')(default='', max_length=10)),
+        ))
+        db.send_create_signal('mcpweb', ['UserProfile'])
+
+        if not db.dry_run:
+            UserProfile = orm['mcpweb.UserProfile']
+            for user in orm['auth.User'].objects.all():
+                UserProfile.objects.create(user=user,
+                                           auth_token=get_random_string())
+
+    def backwards(self, orm):
+        # Deleting model 'UserProfile'
+        db.delete_table('mcpweb_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'})
+        },
+        'mcpweb.trongame': {
+            'Meta': {'ordering': "['-date_created']", 'object_name': 'TronGame'},
+            'date_created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'description': ('django.db.models.fields.CharField', [], {'default': "'Waiting for player 1 to go.'", 'max_length': '50'}),
+            'game_history': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'game_state': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'last_played': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'player1': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'player1'", 'to': "orm['auth.User']"}),
+            'player1_token': ('django.db.models.fields.CharField', [], {'default': "'0msspxc3rMw4'", 'max_length': '10'}),
+            'player2': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'player2'", 'to': "orm['auth.User']"}),
+            'player2_token': ('django.db.models.fields.CharField', [], {'default': "'MEgbD7LkTp1d'", 'max_length': '10'}),
+            'turn': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'winner': ('django.db.models.fields.CharField', [], {'default': "u'inprogress'", 'max_length': '10'})
+        },
+        'mcpweb.userprofile': {
+            'Meta': {'object_name': 'UserProfile'},
+            'auth_token': ('django.db.models.fields.CharField', [], {'default': "'S1nLmzV6UhQv'", 'max_length': '10'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'})
+        }
+    }
+
+    complete_apps = ['mcpweb']
 
 from datetime import datetime
 
+from django.contrib.auth.models import User
 from django.db import models
-from django.contrib.auth.models import User
+from django.db.models.signals import post_save
 from django.utils.crypto import get_random_string
 
 
             winners = u' and '.join(map(unicode, self.winners))
             game_str += u' won by %s.' % winners
         return u'%s %s' % (game_str, players_str)
+
+
+class UserProfile(models.Model):
+    user = models.OneToOneField(User)
+    auth_token = models.CharField(max_length=10, default=get_random_string)
+
+    def __unicode__(self):
+        return unicode(self.user)
+
+
+def create_user_profile(sender, instance, created, **kwargs):
+    if created:
+        UserProfile.objects.create(user=instance)
+
+
+post_save.connect(create_user_profile, sender=User)