add Profile

This commit is contained in:
James Turk 2015-04-06 18:35:51 -04:00
parent cd02ca4684
commit 22b2bb0b82
7 changed files with 70 additions and 0 deletions

1
profile/__init__.py Normal file
View File

@ -0,0 +1 @@
default_app_config = 'profile.apps.ProfileConfig'

18
profile/apps.py Normal file
View File

@ -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)

View File

@ -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)),
],
),
]

View File

15
profile/models.py Normal file
View File

@ -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'

12
profile/tests.py Normal file
View File

@ -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

View File

@ -21,6 +21,7 @@ INSTALLED_APPS = (
'django.contrib.messages',
'django.contrib.staticfiles',
'django_gravatar',
'profile',
'lifting',
)