Skip to content

Commit

Permalink
Add Revision model
Browse files Browse the repository at this point in the history
  • Loading branch information
hombit committed Aug 6, 2020
1 parent d28c504 commit aef2238
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
44 changes: 44 additions & 0 deletions akb/migrations/0006_revision.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
16 changes: 13 additions & 3 deletions akb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
10 changes: 8 additions & 2 deletions akb/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions akb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<oid>\d+)/$', views.RevisionObjectViewSet.as_view(), name='revisions_oid'),
url(r'^whoami/$', views.whoami, name="whoami"),
]
17 changes: 15 additions & 2 deletions akb/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down

0 comments on commit aef2238

Please sign in to comment.