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

Improve source views #493

Merged
merged 9 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 docs/source/others/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Changelog
**Improvements:**

- Add "In progress" status to source
- Source list endpoint now also return ids of related layer
- Add author to Source

**Documentation:**

Expand Down
26 changes: 26 additions & 0 deletions project/geosource/migrations/0012_source_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.1.13 on 2023-12-05 08:26

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("geosource", "0011_sourcereporting_source_status_alter_source_report"),
]

operations = [
migrations.AddField(
model_name="source",
name="author",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="sources",
to=settings.AUTH_USER_MODEL,
),
),
]
6 changes: 6 additions & 0 deletions project/geosource/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from celery.result import AsyncResult
from celery.utils.log import LoggingProxy
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.gis.gdal.error import GDALException
from django.contrib.gis.geos import GEOSGeometry
Expand All @@ -33,6 +34,8 @@
from .mixins import CeleryCallMethodsMixin
from .signals import refresh_data_done

User = get_user_model()

# Decimal fields must be returned as float
DEC2FLOAT = psycopg2.extensions.new_type(
psycopg2.extensions.DECIMAL.values,
Expand Down Expand Up @@ -126,6 +129,9 @@ class Status(models.IntegerChoices):
status = models.PositiveSmallIntegerField(
choices=Status.choices, default=Status.NEED_SYNC
)
author = models.ForeignKey(
User, related_name="sources", blank=True, null=True, on_delete=models.SET_NULL
)

SOURCE_GEOM_ATTRIBUTE = "_geom_"
MAX_SAMPLE_DATA = 5
Expand Down
3 changes: 3 additions & 0 deletions project/geosource/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class SourceSerializer(PolymorphicModelSerializer):
fields = FieldSerializer(many=True, required=False)
slug = serializers.SlugField(max_length=255, read_only=True)
report = SourceReportingSerializer(read_only=True)
layers = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

class Meta:
fields = "__all__"
Expand Down Expand Up @@ -163,6 +164,7 @@ class SourceListSerializer(serializers.ModelSerializer):
_type = serializers.SerializerMethodField()
report = SourceReportingSerializer(read_only=True)
status = serializers.SerializerMethodField()
layers = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

class Meta:
model = Source
Expand All @@ -173,6 +175,7 @@ class Meta:
"name",
"geom_type",
"report",
"layers",
)

def get__type(self, instance):
Expand Down
7 changes: 7 additions & 0 deletions project/geosource/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth import get_user_model
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.parsers import JSONParser
Expand All @@ -10,6 +11,8 @@
from .permissions import SourcePermission
from .serializers import SourceListSerializer, SourceSerializer

User = get_user_model()


class SourceModelViewset(ModelViewSet):
parser_classes = (JSONParser, NestedMultipartJSONParser)
Expand All @@ -32,6 +35,10 @@ def get_serializer_class(self):
def get_queryset(self):
return Source.objects.all().order_by("-id")

def perform_create(self, serializers):
user = User.objects.get(email=self.request.user)
serializers.save(author=user)

@action(detail=True, methods=["get"])
def refresh(self, request, pk):
"""Schedule a refresh now"""
Expand Down
Loading