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

Feature/drf versioning #61

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions kerckhoff/kerckhoff/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

router = routers.DefaultRouter()
router.register(r"packageSet", package_views.PackageSetViewSet)
router.register(r"package", package_views.PackageViewSet)
router.register(r"packageVersion", package_views.PackageVersionViewSet)

urlpatterns = [
url(r"^api/v2/", include(router.urls)),
Expand Down
42 changes: 42 additions & 0 deletions kerckhoff/packages/migrations/0009_auto_20181121_0245.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 2.1 on 2018-11-21 02:45

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('packages', '0008_merge_20181013_0514'),
]

operations = [
migrations.AddField(
model_name='package',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='package',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='packageversion',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='packageversion',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='package',
name='latest_version',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='versions', to='packages.PackageVersion'),
),
]
7 changes: 7 additions & 0 deletions kerckhoff/packages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Package(models.Model):
last_fetched_date = models.DateTimeField(null=True, blank=True)
package_set = models.ForeignKey(PackageSet, on_delete=models.PROTECT)
_content_type = models.CharField(max_length=2, choices=CONTENT_TYPE_CHOICES, blank=True, default="")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

# Versioning
latest_version = models.ForeignKey('PackageVersion', related_name='versions', on_delete=models.CASCADE, null=True, blank=True)
Expand Down Expand Up @@ -192,3 +194,8 @@ class PackageVersion(models.Model):
version_description = models.TextField(blank=True)
article_data = models.TextField(blank=True)
data = JSONField(blank=True, default=dict)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

#TODO
# Add package stateEnum for future (freeze should change state)
14 changes: 13 additions & 1 deletion kerckhoff/packages/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from rest_framework import serializers, viewsets

from .models import PackageSet
from .models import PackageSet, Package, PackageVersion


class PackageSetSerializer(serializers.ModelSerializer):
class Meta:
model = PackageSet
fields = ("slug", "drive_folder_id", "drive_folder_url", "default_content_type")

class PackageSerializer(serializers.ModelSerializer):
class Meta:
model = Package
fields = "__all__"

class PackageVersionSerializer(serializers.ModelSerializer):
package_slug = serializers.ReadOnlyField(source='package.slug')

class Meta:
model = PackageVersion
fields = "__all__"
65 changes: 61 additions & 4 deletions kerckhoff/packages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,79 @@
from django.views.decorators.http import require_GET, require_http_methods, require_POST
from elasticsearch_dsl import Q, Search
from elasticsearch_dsl.search import Response
from rest_framework import viewsets

from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

from kerckhoff.exceptions import UserError
from kerckhoff.util.decorators import api_login_required
from search.indexes import PackageIndex
from search.search import elasticsearch_client

from .forms import PackageForm
from .models import Package, PackageSet
from .serializers import PackageSetSerializer

from .models import Package, PackageSet, PackageVersion
from .serializers import PackageSetSerializer, PackageSerializer, PackageVersionSerializer

class PackageSetViewSet(viewsets.ModelViewSet):
queryset = PackageSet.objects.all().order_by("-slug")
serializer_class = PackageSetSerializer

class PackageViewSet(viewsets.ModelViewSet):
queryset = Package.objects.all().order_by("-updated_at")
serializer_class = PackageSerializer

# Just do update_package, but first do nested-url so we can fetch id
# AND pset_slug. (id is pk aka primarykey)

# DRF extra action 1
# @require_POST
# @api_login_required()
# def update_package(request, pset_slug, id):
# package = Package.objects.get(package_set__slug=pset_slug, slug=id)
# res = package.fetch_from_gdrive(request.user)
# return JsonResponse(model_to_dict(res))
@action(detail=True, methods=['post'])
def update_package(self, request, pset_slug, id):
package = queryset.filter(package_set__slug=pset_slug, slug=id)
serializer = serializer_class(data=request.data)
if serializer.is_valid():
res = package.fetch_from_gdrive(request.user)
return Response(model_to_dict(res))
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)

# DRF extra action 2
# @require_POST
# @api_login_required()
# def push_to_live(request: HttpRequest, pset_slug: str, id: str):
# package = Package.objects.get(package_set__slug=pset_slug, slug=id)
# res = package.push_to_live()
# if res:
# return HttpResponse(status=200)
# return HttpResponse(status=400)
@action(detail=True, methods=['post'])
def push_to_live(self, request: HttpRequest, pset_slug: str, id: str):
package = queryset.filter(package_set__slug=pset_slug, slug=id)
serializer = serializer_class(data=request.data)
if serializer.is_valid():
res = package.push_to_live()
if res:
return Response({'status': 'push_to_live() ok'},
status=status.HTTP_200_OK)
else:
return Response({'status': 'push_to_live() bad request'},
status=status.HTTP_400_BAD_REQUEST)
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)

class PackageVersionViewSet(viewsets.ModelViewSet):
queryset = PackageVersion.objects.all().order_by("-updated_at")
serializer_class = PackageVersionSerializer
#TODO Overwrite default drf create method, custom user input when we create a new PV instance


@require_http_methods(["GET", "POST"])
@api_login_required()
Expand Down