Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user profile (WIP) #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
32 changes: 29 additions & 3 deletions vol/admin.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions vol/migrations/0027_profile.py
Original file line number Diff line number Diff line change
@@ -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)),
],
),
]
20 changes: 20 additions & 0 deletions vol/migrations/0028_auto_20171114_2215.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
27 changes: 25 additions & 2 deletions vol/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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()