diff --git a/Makefile b/Makefile index 961456f..0e164e3 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,15 @@ build: docker-compose build +migrations: + docker-compose run web scripts/wait-for-it.sh db:5432 -- pipenv run python manage.py makemigrations + +shell: + docker-compose run web scripts/wait-for-it.sh db:5432 -- pipenv run python manage.py shell + +migrate: + docker-compose run web scripts/wait-for-it.sh db:5432 -- pipenv run python manage.py migrate + test: docker-compose run -e DEBUG="" web scripts/wait-for-it.sh db:5432 -- pipenv run python manage.py test diff --git a/vol/admin.py b/vol/admin.py index 6524b08..cb5d1e0 100644 --- a/vol/admin.py +++ b/vol/admin.py @@ -1,10 +1,36 @@ from django.contrib import admin +from django.contrib.auth.admin import UserAdmin, User -# Register your models here. - -from .models import Labels, Organisation, Site, Job +from .models import Labels, Organisation, Site, Job, Profile admin.site.register(Labels) admin.site.register(Organisation) admin.site.register(Site) admin.site.register(Job) + + +class ProfileInline(admin.StackedInline): + model = Profile + can_delete = False + verbose_name_plural = 'Profile' + fk_name = 'user' + + +class CustomUserAdmin(UserAdmin): + inlines = (ProfileInline,) + list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'get_location') + list_select_related = ('profile',) + + def get_location(self, instance): + return instance.profile.location + + get_location.short_description = 'Location' + + def get_inline_instances(self, request, obj=None): + if not obj: + return list() + return super(CustomUserAdmin, self).get_inline_instances(request, obj) + + +admin.site.unregister(User) +admin.site.register(User, CustomUserAdmin) diff --git a/vol/migrations/0027_profile.py b/vol/migrations/0027_profile.py new file mode 100644 index 0000000..0d1d49c --- /dev/null +++ b/vol/migrations/0027_profile.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2017-11-14 06:25 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('vol', '0026_auto_20171106_2015'), + ] + + operations = [ + migrations.CreateModel( + name='Profile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('bio', models.TextField(blank=True, max_length=500)), + ('location', models.CharField(blank=True, max_length=30)), + ('birth_date', models.DateField(blank=True, null=True)), + ('favorite_jobs', models.ManyToManyField(to='vol.Job')), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/vol/migrations/0028_auto_20171114_2215.py b/vol/migrations/0028_auto_20171114_2215.py new file mode 100644 index 0000000..fd18783 --- /dev/null +++ b/vol/migrations/0028_auto_20171114_2215.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2017-11-14 22:15 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('vol', '0027_profile'), + ] + + operations = [ + migrations.AlterField( + model_name='profile', + name='favorite_jobs', + field=models.ManyToManyField(blank=True, to='vol.Job'), + ), + ] diff --git a/vol/models.py b/vol/models.py index 40cf136..0585c01 100644 --- a/vol/models.py +++ b/vol/models.py @@ -1,6 +1,11 @@ import uuid -from django.db import models + from autoslug import AutoSlugField +from django.conf import settings +from django.contrib.auth.models import User +from django.db import models +from django.db.models.signals import post_save +from django.dispatch import receiver class Labels(models.Model): @@ -56,7 +61,8 @@ class Job(models.Model): title = models.CharField(max_length=200) text = models.TextField() labels = models.ManyToManyField(Labels) - organisation = models.ForeignKey(Organisation, on_delete=models.SET_NULL, null=True) # Not all jobs have a known org + organisation = models.ForeignKey(Organisation, on_delete=models.SET_NULL, + null=True) # Not all jobs have a known org sites = models.ManyToManyField(Site) # Possibly more sites per job country = models.CharField(max_length=70) # TODO: move to country table region = models.CharField(max_length=70, null=True) # TODO: move to region table? TODO: don't use null for char @@ -72,3 +78,20 @@ def __str__(self): class Meta: ordering = ['created_at'] + + +class Profile(models.Model): + user = models.OneToOneField(settings.AUTH_USER_MODEL) + favorite_jobs = models.ManyToManyField(Job, blank=True) + bio = models.TextField(max_length=500, blank=True) + location = models.CharField(max_length=30, blank=True) + birth_date = models.DateField(null=True, blank=True) + + def __str__(self): + return self.user.username + + @receiver(post_save, sender=User) + def create_or_update_user_profile(sender, instance, created, **kwargs): + if created: + Profile.objects.create(user=instance) + instance.profile.save()