Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 27, 2024
1 parent e0588f2 commit b04f66b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
17 changes: 12 additions & 5 deletions canopeum_backend/canopeum_backend/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from datetime import datetime

from django.conf import settings
from django.db import models
from datetime import datetime


class Announcement(models.Model):
Expand Down Expand Up @@ -84,30 +84,35 @@ class FertilizertypeInternationalization(models.Model):
class Mulchlayertype(models.Model):
name = models.ForeignKey("MulchlayertypeInternationalization", models.DO_NOTHING, blank=True, null=True)


class MulchlayertypeInternationalization(models.Model):
en = models.TextField(db_column="EN", blank=True, null=True)
fr = models.TextField(db_column="FR", blank=True, null=True)


def upload_to(instance, filename):
now = datetime.now().strftime('%Y%m%d%H%M%S%f')
return f'{now}{filename}'
now = datetime.now().strftime("%Y%m%d%H%M%S%f")
return f"{now}{filename}"


class Asset(models.Model):
asset = models.FileField(upload_to=upload_to, null=False)


class Post(models.Model):
site = models.ForeignKey("Site", models.DO_NOTHING, blank=True, null=True)
body = models.TextField(blank=True, null=True)
like_count = models.IntegerField(blank=True, null=True)
share_count = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
media = models.ManyToManyField(Asset, through='PostAsset')
media = models.ManyToManyField(Asset, through="PostAsset")


class PostAsset(models.Model):
post = models.ForeignKey(Post, models.DO_NOTHING, null=False)
asset = models.ForeignKey(Asset, models.DO_NOTHING, null=False)


class Site(models.Model):
name = models.TextField(blank=True, null=True)
site_type = models.ForeignKey("Sitetype", models.DO_NOTHING, blank=True, null=True)
Expand All @@ -121,10 +126,12 @@ class Site(models.Model):
announcement = models.ForeignKey(Announcement, models.DO_NOTHING, blank=True, null=True)
image = models.ForeignKey(Asset, models.DO_NOTHING, blank=True, null=True)


class Siteadmin(models.Model):
auth_user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING, blank=True, null=True)
site = models.ForeignKey(Site, models.DO_NOTHING, blank=True, null=True)


class Sitetreespecies(models.Model):
site = models.ForeignKey(Site, models.DO_NOTHING, blank=True, null=True)
tree_type = models.ForeignKey("Treetype", models.DO_NOTHING, blank=True, null=True)
Expand Down
29 changes: 24 additions & 5 deletions canopeum_backend/canopeum_backend/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,28 @@ def get_en(self, obj):
def get_fr(self, obj):
return TreeTypeSerializer(obj.tree_type).data.get("fr", None)


class AssetSerializer(serializers.ModelSerializer):
asset = serializers.FileField()

class Meta:
model = Asset
fields = ("asset",)

def to_internal_value(self, data):
# Map 'image' field to 'asset' field in incoming data
if 'image' in data:
data['asset'] = data['image']
del data['image']
if "image" in data:
data["asset"] = data["image"]
del data["image"]
return super().to_internal_value(data)


class SitePostSerializer(serializers.ModelSerializer):
class Meta:
model = Site
fields = ("id", "name", "description", "image")


class SiteSerializer(serializers.ModelSerializer):
site_type = SiteTypeSerializer()
coordinate = CoordinatesSerializer()
Expand All @@ -145,6 +149,7 @@ class Meta:
def get_site_tree_species(self, obj):
return SitetreespeciesSerializer(obj.sitetreespecies_set.all(), many=True).data


class SiteSocialSerializer(serializers.ModelSerializer):
site_type = SiteTypeSerializer()
contact = ContactSerializer()
Expand Down Expand Up @@ -372,6 +377,7 @@ def get_latitude(self, obj):
def get_longitude(self, obj):
return obj.dd_longitude


class SiteMapSerializer(serializers.ModelSerializer):
site_type = SiteTypeSerializer()
coordinates = serializers.SerializerMethodField()
Expand All @@ -385,19 +391,21 @@ class Meta:
def get_coordinates(self, obj):
return CoordinatesMapSerializer(obj.coordinate).data


class SiteOverviewSerializer(serializers.ModelSerializer):
image = AssetSerializer()

class Meta:
model = Site
fields = ("id", "name", "image")



class PostPostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ("site", "body")


class PostSerializer(serializers.ModelSerializer):
site = SiteOverviewSerializer()
comment_count = serializers.SerializerMethodField()
Expand All @@ -406,7 +414,17 @@ class PostSerializer(serializers.ModelSerializer):

class Meta:
model = Post
fields = ("id", "site", "created_at", "body", "like_count", "share_count", "comment_count", "has_liked", "media")
fields = (
"id",
"site",
"created_at",
"body",
"like_count",
"share_count",
"comment_count",
"has_liked",
"media",
)

@extend_schema_field(int) # pyright: ignore[reportArgumentType]
def get_comment_count(self, obj):
Expand All @@ -416,6 +434,7 @@ def get_comment_count(self, obj):
def get_has_liked(self, obj):
return self.context.get("has_liked")


class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
Expand Down
4 changes: 2 additions & 2 deletions canopeum_backend/canopeum_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@

STATIC_URL = "static/"

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'canopeum_backend/media'
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "canopeum_backend/media"

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down
6 changes: 2 additions & 4 deletions canopeum_backend/canopeum_backend/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView

from .settings import STATIC_URL

from . import views

urlpatterns = [
Expand Down
24 changes: 10 additions & 14 deletions canopeum_backend/canopeum_backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.parsers import FormParser, MultiPartParser
from rest_framework.permissions import AllowAny
from rest_framework.parsers import MultiPartParser, FormParser, FileUploadParser, JSONParser
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import Announcement, Batch, Comment, Contact, Post, Site, Widget
from rest_framework.parsers import FileUploadParser
from .models import Announcement, Batch, Comment, Contact, Like, Post, Site, Widget
from .serializers import (
AnnouncementSerializer,
Expand Down Expand Up @@ -67,6 +65,7 @@ def post(self, request):
request.user.auth_token.delete()
return Response(status=status.HTTP_200_OK)


class SiteListAPIView(APIView):
@extend_schema(responses=SiteSerializer(many=True), operation_id="site_all")
def get(self, request):
Expand All @@ -75,27 +74,25 @@ def get(self, request):
return Response(serializer.data)

parser_classes = (MultiPartParser, FormParser)

@extend_schema(request=SiteSerializer, responses=SiteSerializer, operation_id="site_create")
def post(self, request):
asset = AssetSerializer(data=request.data)
if not asset.is_valid():
return Response(
data=asset.errors,
status=status.HTTP_400_BAD_REQUEST
)
return Response(data=asset.errors, status=status.HTTP_400_BAD_REQUEST)
asset = asset.save()
serializer = SitePostSerializer(data=request.data)
if serializer.is_valid():
post = serializer.save(image=asset)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class SiteDetailAPIView(APIView):
@extend_schema(request=SiteSerializer, responses=SiteSerializer, operation_id="site_detail")
def get(self, request, siteId):
try:
site = Site.objects.prefetch_related('image').get(pk=siteId)
site = Site.objects.prefetch_related("image").get(pk=siteId)
except Site.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

Expand Down Expand Up @@ -214,6 +211,7 @@ def get(self, request):
return Response(serializer.data)

parser_classes = (MultiPartParser, FormParser)

@extend_schema(request=PostPostSerializer, responses=PostSerializer, operation_id="post_create")
def post(self, request):
assets = request.data.getlist("media")
Expand All @@ -223,10 +221,7 @@ def post(self, request):
q.update({"image": asset})
asset = AssetSerializer(data=q)
if not asset.is_valid():
return Response(
data=asset.errors,
status=status.HTTP_400_BAD_REQUEST
)
return Response(data=asset.errors, status=status.HTTP_400_BAD_REQUEST)
asset.save()
saved_assets.append(asset)
serializer = PostPostSerializer(data=request.data)
Expand All @@ -237,6 +232,7 @@ def post(self, request):
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class CommentListAPIView(APIView):
@extend_schema(responses=CommentSerializer(many=True), operation_id="comment_all")
def get(self, request, postId):
Expand Down

0 comments on commit b04f66b

Please sign in to comment.