From aef2238caa500825049a1efd99b43a35c92374fe Mon Sep 17 00:00:00 2001 From: Konstantin Malanchev Date: Thu, 6 Aug 2020 16:36:06 +0300 Subject: [PATCH] Add Revision model --- akb/migrations/0006_revision.py | 44 +++++++++++++++++++++++++++++++++ akb/models.py | 16 +++++++++--- akb/serializers.py | 10 ++++++-- akb/urls.py | 2 ++ akb/views.py | 17 +++++++++++-- 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 akb/migrations/0006_revision.py diff --git a/akb/migrations/0006_revision.py b/akb/migrations/0006_revision.py new file mode 100644 index 0000000..f42ffcb --- /dev/null +++ b/akb/migrations/0006_revision.py @@ -0,0 +1,44 @@ +# Generated by Django 3.0.8 on 2020-08-06 13:03 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('akb', '0005_tag_description'), + ] + + operations = [ + migrations.RemoveField( + model_name='object', + name='description', + ), + migrations.RemoveField( + model_name='object', + name='simbadid', + ), + migrations.RemoveField( + model_name='object', + name='tags', + ), + migrations.CreateModel( + name='Revision', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date', models.DateTimeField(auto_now_add=True)), + ('description', models.TextField(blank=True)), + ('simbadid', models.CharField(blank=True, max_length=256)), + ('object', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='akb.Object')), + ('tags', models.ManyToManyField(related_name='tagged_revisions', to='akb.Tag')), + ], + options={ + 'ordering': ['object', '-date', '-id'], + }, + ), + migrations.AddIndex( + model_name='revision', + index=models.Index(fields=['date'], name='akb_revisio_date_a7987f_idx'), + ), + ] diff --git a/akb/models.py b/akb/models.py index a9b69e5..1dddf0c 100644 --- a/akb/models.py +++ b/akb/models.py @@ -14,11 +14,21 @@ class Meta: class Object(models.Model): oid = models.BigIntegerField(unique = True, blank = False) - description = models.TextField(blank = True) - tags = models.ManyToManyField("Tag", related_name="tagged_objects") - simbadid = models.CharField(max_length = 256, blank = True) class Meta: indexes = [ models.Index(fields=['oid']), ] + +class Revision(models.Model): + date = models.DateTimeField(auto_now_add = True, blank = False) + object = models.ForeignKey(Object, on_delete=models.PROTECT) + description = models.TextField(blank = True) + tags = models.ManyToManyField(Tag, related_name="tagged_revisions") + simbadid = models.CharField(max_length=256, blank=True) + + class Meta: + indexes = [ + models.Index(fields=['date']), + ] + ordering = ['object', '-date', '-id'] diff --git a/akb/serializers.py b/akb/serializers.py index 7e93445..57957c4 100644 --- a/akb/serializers.py +++ b/akb/serializers.py @@ -8,11 +8,17 @@ class Meta: fields = ('id', 'name', 'priority', 'description') class ObjectSerializer(serializers.ModelSerializer): + class Meta: + model = models.Object + fields = ('id', 'oid') + +class RevisionSerializer(serializers.ModelSerializer): + object = serializers.SlugRelatedField(many=False, slug_field='oid', queryset=models.Object.objects) tags = serializers.SlugRelatedField(many=True, slug_field='name', queryset=models.Tag.objects, default=[]) class Meta: - model = models.Object - fields = ('id', 'oid', 'description', 'simbadid', 'tags') + model = models.Revision + fields = ('id', 'object', 'date', 'description', 'simbadid', 'tags') class UserSerializer(serializers.ModelSerializer): class Meta: diff --git a/akb/urls.py b/akb/urls.py index a12dd19..44830c2 100644 --- a/akb/urls.py +++ b/akb/urls.py @@ -21,8 +21,10 @@ router = DefaultRouter() router.register(r'tags', views.TagViewSet) router.register(r'objects', views.ObjectViewSet) +router.register(r'revisions', views.RevisionViewSet) urlpatterns = [ url(r'^', include(router.urls)), + url(r'^revisions/oid/(?P\d+)/$', views.RevisionObjectViewSet.as_view(), name='revisions_oid'), url(r'^whoami/$', views.whoami, name="whoami"), ] diff --git a/akb/views.py b/akb/views.py index 1db917a..18cf8e5 100644 --- a/akb/views.py +++ b/akb/views.py @@ -1,5 +1,5 @@ from akb import models, serializers -from rest_framework import viewsets +from rest_framework import generics, viewsets from rest_framework.settings import api_settings from rest_framework.response import Response from rest_framework.decorators import api_view, permission_classes @@ -11,10 +11,23 @@ class TagViewSet(viewsets.ModelViewSet): lookup_field = 'name' class ObjectViewSet(viewsets.ModelViewSet): - queryset = models.Object.objects.prefetch_related('tags').all() + queryset = models.Object.objects serializer_class = serializers.ObjectSerializer lookup_field = 'oid' +class RevisionViewSet(viewsets.ModelViewSet): + queryset = models.Revision.objects.prefetch_related('tags').prefetch_related('object').all() + serializer_class = serializers.RevisionSerializer + +class RevisionObjectViewSet(generics.ListAPIView): + queryset = models.Revision.objects.prefetch_related('tags').prefetch_related('object').all() + serializer_class = serializers.RevisionSerializer + + def get_queryset(self): + oid = self.kwargs['oid'] + object = models.Object.objects.get(oid=oid) + return models.Revision.objects.filter(object=object) + @api_view(['GET']) @permission_classes([IsAuthenticated]) def whoami(request):