diff --git a/profile/__init__.py b/profile/__init__.py new file mode 100644 index 0000000..09cda9b --- /dev/null +++ b/profile/__init__.py @@ -0,0 +1 @@ +default_app_config = 'profile.apps.ProfileConfig' diff --git a/profile/apps.py b/profile/apps.py new file mode 100644 index 0000000..b434e52 --- /dev/null +++ b/profile/apps.py @@ -0,0 +1,18 @@ +from django.apps import AppConfig +from django.dispatch import receiver +from django.db.models.signals import post_save +from django.contrib.auth.models import User + + +def create_profile(sender, created, instance, **kwargs): + from .models import Profile + if created: + Profile.objects.create(user=instance) + + +class ProfileConfig(AppConfig): + name = 'profile' + app_label = 'profile' + + def ready(self): + post_save.connect(create_profile, sender=User) diff --git a/profile/migrations/0001_initial.py b/profile/migrations/0001_initial.py new file mode 100644 index 0000000..92d412d --- /dev/null +++ b/profile/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Profile', + fields=[ + ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True, serialize=False)), + ('lifting_units', models.CharField(max_length=1, choices=[('m', 'Metric (kg)'), ('i', 'Imperial (lbs)')])), + ('user', models.OneToOneField(related_name='config', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/profile/migrations/__init__.py b/profile/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/profile/models.py b/profile/models.py new file mode 100644 index 0000000..d1ac288 --- /dev/null +++ b/profile/models.py @@ -0,0 +1,15 @@ +from django.db import models +from django.contrib.auth.models import User + +UNITS = ( + ('m', 'Metric (kg)'), + ('i', 'Imperial (lbs)'), +) + +class Profile(models.Model): + user = models.OneToOneField(User, related_name='profile') + + lifting_units = models.CharField(max_length=1, choices=UNITS) + + def metric(self): + return self.lifting_units == 'm' diff --git a/profile/tests.py b/profile/tests.py new file mode 100644 index 0000000..aaa1f54 --- /dev/null +++ b/profile/tests.py @@ -0,0 +1,12 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from .models import Profile + + +class TestProfile(TestCase): + + def test_signal(self): + + u = User.objects.create_user(username='test', email='test@example.com', password='test') + + assert Profile.objects.filter(user=u).count() == 1 diff --git a/web/settings.py b/web/settings.py index a3931ab..c5d5908 100644 --- a/web/settings.py +++ b/web/settings.py @@ -21,6 +21,7 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'django_gravatar', + 'profile', 'lifting', )