Skip to content

Commit

Permalink
Add author to source
Browse files Browse the repository at this point in the history
  • Loading branch information
CFarcy committed Dec 11, 2023
1 parent 362bf77 commit bf3fb2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
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 @@ -125,6 +128,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
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

0 comments on commit bf3fb2c

Please sign in to comment.