From 2b052a5c0fe58e839eff1193a149d7b4a1d1cc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mats=20Astrup=20Schj=C3=B8lberg?= Date: Sun, 18 Dec 2016 23:01:44 +0100 Subject: [PATCH 1/3] Moved intern.models.InternGroup to core.models.Group to fix #78 --- core/fixtures/group.json | 1 + core/migrations/0008_group.py | 31 +++++++ core/models.py | 8 ++ core/rest.py | 9 +- core/serializers.py | 12 ++- intern/admin.py | 6 +- intern/fixtures/groups.json | 92 -------------------- intern/migrations/0002_auto_20161218_2243.py | 27 ++++++ intern/migrations/0003_auto_20161218_2255.py | 20 +++++ intern/migrations/0004_auto_20161218_2309.py | 22 +++++ intern/models.py | 14 +-- intern/rest.py | 9 +- intern/serializers.py | 16 +--- scripts/dump_fixtures.sh | 2 +- scripts/load_fixtures.sh | 4 +- 15 files changed, 142 insertions(+), 131 deletions(-) create mode 100644 core/fixtures/group.json create mode 100644 core/migrations/0008_group.py delete mode 100644 intern/fixtures/groups.json create mode 100644 intern/migrations/0002_auto_20161218_2243.py create mode 100644 intern/migrations/0003_auto_20161218_2255.py create mode 100644 intern/migrations/0004_auto_20161218_2309.py diff --git a/core/fixtures/group.json b/core/fixtures/group.json new file mode 100644 index 00000000..548b2655 --- /dev/null +++ b/core/fixtures/group.json @@ -0,0 +1 @@ +[{"model": "core.group", "pk": 1, "fields": {"name": "Hovedstyret", "leader": 1, "description": "HS sitt ansvar er \u00e5 drive foreningen"}}, {"model": "core.group", "pk": 2, "fields": {"name": "Kjellerstyret", "leader": 2, "description": "Kjellerstyret styrer Escape"}}, {"model": "core.group", "pk": 3, "fields": {"name": "\u00d8konomi", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 4, "fields": {"name": "Arrangementsgruppen", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 5, "fields": {"name": "Promotering", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 6, "fields": {"name": "Bar", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 7, "fields": {"name": "Kaf\u00e9", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 8, "fields": {"name": "DJ", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 9, "fields": {"name": "Arkivgruppen", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 10, "fields": {"name": "Webgruppa", "leader": 1, "description": ""}}] \ No newline at end of file diff --git a/core/migrations/0008_group.py b/core/migrations/0008_group.py new file mode 100644 index 00000000..0332d04c --- /dev/null +++ b/core/migrations/0008_group.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-12-18 21:41 +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 = [ + ('core', '0007_auto_20160713_1508'), + ('intern', '0002_auto_20161218_2243') + ] + + state_operations = [ + migrations.CreateModel( + name='Group', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50, unique=True)), + ('description', models.CharField(blank=True, max_length=300, null=True)), + ('leader', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] + + operations = [ + migrations.SeparateDatabaseAndState(state_operations=state_operations) + ] diff --git a/core/models.py b/core/models.py index 90e9e1c6..42554806 100644 --- a/core/models.py +++ b/core/models.py @@ -114,3 +114,11 @@ def __str__(self): if not self.user: return self.card_uid return "%s (%s)" % (self.card_uid, self.user) + +class Group(models.Model): + name = models.CharField(max_length=50, unique=True) + leader = models.ForeignKey(User) + description = models.CharField(max_length=300, null=True, blank=True) + + def __str__(self): + return self.name \ No newline at end of file diff --git a/core/rest.py b/core/rest.py index 733d458e..45010878 100644 --- a/core/rest.py +++ b/core/rest.py @@ -5,8 +5,8 @@ from core.serializers import CardCreateSerializer, CardSerializer, UserExtendedSerializer, NfcCardCreateSerializer, \ - NfcCardSerializer -from core.models import Card, User, NfcCard + NfcCardSerializer, GroupSerializer +from core.models import Card, User, NfcCard, Group from core.filters import CardFilter, UserFilter, NfcCardFilter from core.permissions import CardPermission @@ -85,3 +85,8 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): def get_serializer_class(self): return UserExtendedSerializer + +class GroupViewSet(viewsets.ModelViewSet): + permission_classes = (permissions.DjangoModelPermissions) + serializer_class = GroupSerializer + queryset = Group.objects.all() diff --git a/core/serializers.py b/core/serializers.py index 55145363..f6ef4bab 100644 --- a/core/serializers.py +++ b/core/serializers.py @@ -1,5 +1,5 @@ from rest_framework import serializers -from core.models import User, Semester, Card, NfcCard +from core.models import Group, User, Semester, Card, NfcCard class UserSimpleGuestSerializer(serializers.ModelSerializer): @@ -60,3 +60,13 @@ class Meta: model = NfcCard fields = ('card_uid', 'user', 'intern', 'comment') extra_kwargs = {'user': {'default': None}, 'intern': {'default': False}, 'comment': {'default': None}} + + +class GroupSerializer(serializers.ModelSerializer): + leader = UserSerializer() + + class Meta: + model = Group + fields = ( + 'id', 'name', 'leader', 'description' + ) diff --git a/intern/admin.py b/intern/admin.py index 21529ed0..1347c164 100644 --- a/intern/admin.py +++ b/intern/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from intern.models import Intern, InternCard, InternGroup, Role, AccessLevel, InternRole, InternLogEntry +from intern.models import Intern, InternCard, Role, AccessLevel, InternRole, InternLogEntry class InternAdmin(admin.ModelAdmin): @@ -10,9 +10,6 @@ class RoleAdmin(admin.ModelAdmin): list_display = ('name', 'description') -class InternGroupAdmin(admin.ModelAdmin): - list_display = ('name', 'leader', 'description') - class AccessLevelAdmin(admin.ModelAdmin): list_display = ('name', 'uio_name', 'description') @@ -33,7 +30,6 @@ class InternLogEntryAdmin(admin.ModelAdmin): # Register your models here. admin.site.register(Intern, InternAdmin) admin.site.register(Role, RoleAdmin) -admin.site.register(InternGroup, InternGroupAdmin) admin.site.register(AccessLevel, AccessLevelAdmin) admin.site.register(InternRole) admin.site.register(InternCard, InternCardAdmin) diff --git a/intern/fixtures/groups.json b/intern/fixtures/groups.json deleted file mode 100644 index 799ea268..00000000 --- a/intern/fixtures/groups.json +++ /dev/null @@ -1,92 +0,0 @@ -[ - { - "model": "intern.interngroup", - "pk": 1, - "fields": { - "name": "Hovedstyret", - "leader": 1, - "description": "HS sitt ansvar er \u00e5 drive foreningen" - } - }, - { - "model": "intern.interngroup", - "pk": 2, - "fields": { - "name": "Kjellerstyret", - "leader": 2, - "description": "Kjellerstyret styrer Escape" - } - }, - { - "model": "intern.interngroup", - "pk": 3, - "fields": { - "name": "\u00d8konomi", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 4, - "fields": { - "name": "Arrangementsgruppen", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 5, - "fields": { - "name": "Promotering", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 6, - "fields": { - "name": "Bar", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 7, - "fields": { - "name": "Kaf\u00e9", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 8, - "fields": { - "name": "DJ", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 9, - "fields": { - "name": "Arkivgruppen", - "leader": 1, - "description": "" - } - }, - { - "model": "intern.interngroup", - "pk": 10, - "fields": { - "name": "Webgruppa", - "leader": 1, - "description": "" - } - } -] diff --git a/intern/migrations/0002_auto_20161218_2243.py b/intern/migrations/0002_auto_20161218_2243.py new file mode 100644 index 00000000..4edf0b84 --- /dev/null +++ b/intern/migrations/0002_auto_20161218_2243.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-12-18 21:43 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('intern', '0001_initial'), + ] + + database_operations = [ + migrations.AlterModelTable('interngroup', 'core_group') + ] + + state_operations = [ + + ] + + operations = [ + migrations.SeparateDatabaseAndState( + database_operations=database_operations, + state_operations=state_operations + ) + ] diff --git a/intern/migrations/0003_auto_20161218_2255.py b/intern/migrations/0003_auto_20161218_2255.py new file mode 100644 index 00000000..653a8a10 --- /dev/null +++ b/intern/migrations/0003_auto_20161218_2255.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-12-18 21:55 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('intern', '0002_auto_20161218_2243'), + ] + + operations = [ + migrations.AlterField( + model_name='role', + name='groups', + field=models.ManyToManyField(related_name='roles', to='core.Group'), + ), + ] diff --git a/intern/migrations/0004_auto_20161218_2309.py b/intern/migrations/0004_auto_20161218_2309.py new file mode 100644 index 00000000..705d071c --- /dev/null +++ b/intern/migrations/0004_auto_20161218_2309.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-12-18 22:09 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('intern', '0003_auto_20161218_2255'), + ] + + state_operations = [ + migrations.DeleteModel( + name='InternGroup', + ), + ] + + operations = [ + migrations.SeparateDatabaseAndState(state_operations=state_operations) + ] diff --git a/intern/models.py b/intern/models.py index 80511b92..0cd5b2df 100644 --- a/intern/models.py +++ b/intern/models.py @@ -1,7 +1,7 @@ from django.db import models from django.utils import timezone -from core.models import User, Semester, Card +from core.models import User, Semester, Card, Group from core.utils import get_semester from members.models import Member @@ -15,20 +15,10 @@ class AccessLevel(models.Model): def __str__(self): return self.name - -class InternGroup(models.Model): - name = models.CharField(max_length=50, unique=True) - leader = models.ForeignKey(User) - description = models.CharField(max_length=300, null=True, blank=True) - - def __str__(self): - return self.name - - class Role(models.Model): name = models.CharField(max_length=50, unique=True) description = models.CharField(max_length=300, null=True, blank=True) - groups = models.ManyToManyField(InternGroup, related_name='roles') + groups = models.ManyToManyField(Group, related_name='roles') access_levels = models.ManyToManyField(AccessLevel) def __str__(self): diff --git a/intern/rest.py b/intern/rest.py index 5a9b74d5..c9cd5fa6 100644 --- a/intern/rest.py +++ b/intern/rest.py @@ -8,9 +8,11 @@ from rest_framework.response import Response from core.utils import get_semester +from core.models import Group +from core.serializers import GroupSerializer from intern.models import * -from intern.serializers import InternRoleFullSerializer, InternSerializer, AccessLevelSerializer, InternGroupSerializer, \ +from intern.serializers import InternRoleFullSerializer, InternSerializer, AccessLevelSerializer, \ RoleSerializer, AddInternRoleSerializer, InternCardSerializer, AddInternCardSerializer @@ -24,9 +26,10 @@ class InternViewSet(viewsets.ModelViewSet): class InternGroupViewSet(viewsets.ModelViewSet): + """Deprecated. You should use api/core/groups instead""" permission_classes = (DjangoModelPermissions,) - serializer_class = InternGroupSerializer - queryset = InternGroup.objects.all() + serializer_class = GroupSerializer + queryset = Group.objects.all() class AccessLevelViewSet(viewsets.ReadOnlyModelViewSet): diff --git a/intern/serializers.py b/intern/serializers.py index 1d61708a..5620ab00 100644 --- a/intern/serializers.py +++ b/intern/serializers.py @@ -1,7 +1,7 @@ from rest_framework import serializers -from core.serializers import UserSerializer, CardSerializer, SemesterSerializer -from intern.models import AccessLevel, Intern, InternCard, InternGroup, InternRole, Role, InternLogEntry +from core.serializers import UserSerializer, CardSerializer, SemesterSerializer, GroupSerializer +from intern.models import AccessLevel, Intern, InternCard, InternRole, Role, Intern, InternLogEntry class AccessLevelSerializer(serializers.ModelSerializer): @@ -12,18 +12,8 @@ class Meta: ) -class InternGroupSerializer(serializers.ModelSerializer): - leader = UserSerializer() - - class Meta: - model = InternGroup - fields = ( - 'id', 'name', 'leader', 'description' - ) - - class RoleSerializer(serializers.ModelSerializer): - groups = InternGroupSerializer(many=True) + groups = GroupSerializer(many=True) access_levels = AccessLevelSerializer(many=True) class Meta: diff --git a/scripts/dump_fixtures.sh b/scripts/dump_fixtures.sh index 35837e18..16b2099a 100755 --- a/scripts/dump_fixtures.sh +++ b/scripts/dump_fixtures.sh @@ -15,6 +15,7 @@ ./manage.py dumpdata --format json core.user >core/fixtures/user.json ./manage.py dumpdata --format json core.semester >core/fixtures/semester.json ./manage.py dumpdata --format json core.nfccard >core/fixtures/nfccard.json +./manage.py dumpdata --format json core.group >core/fixtures/group.json ./manage.py dumpdata --format json cal.event >cal/fixtures/event.json @@ -26,7 +27,6 @@ ./manage.py dumpdata --format json intern.accesslevel >intern/fixtures/accesslevels.json ./manage.py dumpdata --format json intern.role >intern/fixtures/roles.json -./manage.py dumpdata --format json intern.interngroup >intern/fixtures/groups.json ./manage.py dumpdata --format json intern.intern >intern/fixtures/interns.json ./manage.py dumpdata --format json intern.internlogentry >intern/fixtures/internlogs.json ./manage.py dumpdata --format json intern.internrole >intern/fixtures/internroles.json diff --git a/scripts/load_fixtures.sh b/scripts/load_fixtures.sh index 2dd62c3f..91700e7d 100755 --- a/scripts/load_fixtures.sh +++ b/scripts/load_fixtures.sh @@ -4,10 +4,10 @@ # see dump_fixtures.sh # load core first as most stuff have dependencies to it -./manage.py loaddata --app core user semester nfccard +./manage.py loaddata --app core user semester nfccard group ./manage.py loaddata --app cal events ./manage.py loaddata --app members members ./manage.py loaddata --app varer varer ./manage.py loaddata --app voucher voucher -./manage.py loaddata --app intern accesslevels roles groups interns internlogs internroles +./manage.py loaddata --app intern accesslevels roles interns internlogs internroles From 38c3e14c18230052dd87b24e188d20e599fd9a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mats=20Astrup=20Schj=C3=B8lberg?= Date: Sun, 18 Dec 2016 23:50:29 +0100 Subject: [PATCH 2/3] Fixed core/group api --- core/admin.py | 3 ++- core/rest.py | 2 +- core/urls.py | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/admin.py b/core/admin.py index b2e5b6a9..3f314f4c 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,7 +1,7 @@ from django import forms from django.contrib import admin from django.contrib.auth.admin import UserAdmin as _UserAdmin -from core.models import User, Semester, Card, NfcCard +from core.models import User, Semester, Card, NfcCard, Group from django.utils.translation import ugettext_lazy as _ @@ -46,3 +46,4 @@ class UserAdmin(_UserAdmin): admin.site.register(Semester) admin.site.register(Card) admin.site.register(NfcCard) +admin.site.register(Group) diff --git a/core/rest.py b/core/rest.py index 45010878..0edd7d1b 100644 --- a/core/rest.py +++ b/core/rest.py @@ -87,6 +87,6 @@ def get_serializer_class(self): return UserExtendedSerializer class GroupViewSet(viewsets.ModelViewSet): - permission_classes = (permissions.DjangoModelPermissions) + permission_classes = (permissions.DjangoModelPermissions,) serializer_class = GroupSerializer queryset = Group.objects.all() diff --git a/core/urls.py b/core/urls.py index 2125c00d..459f8068 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,7 +1,7 @@ from django.conf.urls import url from core.views import me -from core.rest import CardViewSet, UserViewSet, NfcCardViewSet +from core.rest import CardViewSet, UserViewSet, NfcCardViewSet, GroupViewSet from core.utils import SharedAPIRootRouter # SharedAPIRootRouter is automatically imported in global urls config @@ -9,6 +9,7 @@ router.register(r'core/users', UserViewSet, base_name='users') router.register(r'core/cards', CardViewSet, base_name='voucher_cards') router.register(r'core/nfc', NfcCardViewSet) +router.register(r'core/groups', GroupViewSet) urlpatterns = [ url(r'^api/me$', me, name='me'), From 96e7539096f0ba5dbbb6a27994c77c67f7bd6b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mats=20Astrup=20Schj=C3=B8lberg?= Date: Mon, 19 Dec 2016 11:28:47 +0100 Subject: [PATCH 3/3] Changes from feedback. Pretty printed group fixtures. Fixed some small code style stuff in core.models --- core/fixtures/group.json | 93 +++++++++++++++++++++++++++++++++++++++- core/models.py | 3 +- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/core/fixtures/group.json b/core/fixtures/group.json index 548b2655..f8560128 100644 --- a/core/fixtures/group.json +++ b/core/fixtures/group.json @@ -1 +1,92 @@ -[{"model": "core.group", "pk": 1, "fields": {"name": "Hovedstyret", "leader": 1, "description": "HS sitt ansvar er \u00e5 drive foreningen"}}, {"model": "core.group", "pk": 2, "fields": {"name": "Kjellerstyret", "leader": 2, "description": "Kjellerstyret styrer Escape"}}, {"model": "core.group", "pk": 3, "fields": {"name": "\u00d8konomi", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 4, "fields": {"name": "Arrangementsgruppen", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 5, "fields": {"name": "Promotering", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 6, "fields": {"name": "Bar", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 7, "fields": {"name": "Kaf\u00e9", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 8, "fields": {"name": "DJ", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 9, "fields": {"name": "Arkivgruppen", "leader": 1, "description": ""}}, {"model": "core.group", "pk": 10, "fields": {"name": "Webgruppa", "leader": 1, "description": ""}}] \ No newline at end of file +[ + { + "model": "core.group", + "pk": 1, + "fields": { + "name": "Hovedstyret", + "leader": 1, + "description": "HS sitt ansvar er \u00e5 drive foreningen" + } + }, + { + "model": "core.group", + "pk": 2, + "fields": { + "name": "Kjellerstyret", + "leader": 2, + "description": "Kjellerstyret styrer Escape" + } + }, + { + "model": "core.group", + "pk": 3, + "fields": { + "name": "\u00d8konomi", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 4, + "fields": { + "name": "Arrangementsgruppen", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 5, + "fields": { + "name": "Promotering", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 6, + "fields": { + "name": "Bar", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 7, + "fields": { + "name": "Kaf\u00e9", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 8, + "fields": { + "name": "DJ", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 9, + "fields": { + "name": "Arkivgruppen", + "leader": 1, + "description": "" + } + }, + { + "model": "core.group", + "pk": 10, + "fields": { + "name": "Webgruppa", + "leader": 1, + "description": "" + } + } +] \ No newline at end of file diff --git a/core/models.py b/core/models.py index 42554806..7559eee0 100644 --- a/core/models.py +++ b/core/models.py @@ -115,10 +115,11 @@ def __str__(self): return self.card_uid return "%s (%s)" % (self.card_uid, self.user) + class Group(models.Model): name = models.CharField(max_length=50, unique=True) leader = models.ForeignKey(User) description = models.CharField(max_length=300, null=True, blank=True) def __str__(self): - return self.name \ No newline at end of file + return self.name