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 adding image storage #75

Merged
merged 5 commits into from
Apr 1, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by Django 5.0.3 on 2024-03-24 00:48

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


class Migration(migrations.Migration):

dependencies = [
('canopeum_backend', '0005_remove_batchsupportedspecies_quantity_and_more'),
]

operations = [
migrations.CreateModel(
name='Asset',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('asset', models.FileField(upload_to='assets/')),
],
),
migrations.RemoveField(
model_name='siteimage',
name='image',
),
migrations.RemoveField(
model_name='postimage',
name='image',
),
migrations.RemoveField(
model_name='site',
name='image',
),
migrations.RemoveField(
model_name='postimage',
name='post',
),
migrations.RemoveField(
model_name='siteimage',
name='site',
),
migrations.CreateModel(
name='PostAsset',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('asset', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='canopeum_backend.asset')),
('post', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='canopeum_backend.post')),
],
),
migrations.CreateModel(
name='SiteAsset',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('asset', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='canopeum_backend.asset')),
('site', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='canopeum_backend.site')),
],
),
migrations.DeleteModel(
name='Image',
),
migrations.DeleteModel(
name='Postimage',
),
migrations.DeleteModel(
name='Siteimage',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.3 on 2024-03-24 01:12

import canopeum_backend.models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('canopeum_backend', '0006_asset_remove_siteimage_image_remove_postimage_image_and_more'),
]

operations = [
migrations.AddField(
model_name='site',
name='image',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='canopeum_backend.asset'),
),
migrations.AlterField(
model_name='asset',
name='asset',
field=models.FileField(upload_to=canopeum_backend.models.upload_to),
),
migrations.DeleteModel(
name='SiteAsset',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.3 on 2024-03-24 02:31

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('canopeum_backend', '0007_site_image_alter_asset_asset_delete_siteasset'),
]

operations = [
migrations.AlterField(
model_name='asset',
name='asset',
field=models.FileField(upload_to=''),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.3 on 2024-03-24 02:45

import canopeum_backend.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('canopeum_backend', '0008_alter_asset_asset'),
]

operations = [
migrations.AlterField(
model_name='asset',
name='asset',
field=models.FileField(upload_to=canopeum_backend.models.upload_to),
),
]
31 changes: 17 additions & 14 deletions canopeum_backend/canopeum_backend/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

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

Expand Down Expand Up @@ -59,7 +61,7 @@ class Contact(models.Model):
facebook_link = models.URLField(blank=True, null=True)
x_link = models.URLField(blank=True, null=True)
instagram_link = models.URLField(blank=True, null=True)
linkedin_link = models.URLField(blank=True, null=True)
linkedIn_link = models.URLField(blank=True, null=True)


class Coordinate(models.Model):
Expand All @@ -79,10 +81,6 @@ class FertilizertypeInternationalization(models.Model):
fr = models.TextField(db_column="FR", blank=True, null=True)


class Image(models.Model):
path = models.TextField(blank=True, null=True)


class Mulchlayertype(models.Model):
name = models.ForeignKey("MulchlayertypeInternationalization", models.DO_NOTHING, blank=True, null=True)

Expand All @@ -92,23 +90,32 @@ class MulchlayertypeInternationalization(models.Model):
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}"


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")


class Postimage(models.Model):
image = models.ForeignKey(Image, models.DO_NOTHING, blank=True, null=True)
post = models.ForeignKey(Post, models.DO_NOTHING, blank=True, null=True)
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)
image = models.ForeignKey(Image, models.DO_NOTHING, blank=True, null=True)
coordinate = models.ForeignKey(Coordinate, models.DO_NOTHING, blank=True, null=True)
description = models.TextField(blank=True, null=True)
size = models.TextField(blank=True, null=True)
Expand All @@ -117,18 +124,14 @@ class Site(models.Model):
visitor_count = models.IntegerField(blank=True, null=True)
contact = models.ForeignKey(Contact, models.DO_NOTHING, blank=True, null=True)
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 Siteimage(models.Model):
image = models.ForeignKey(Image, 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
49 changes: 47 additions & 2 deletions canopeum_backend/canopeum_backend/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .models import (
Announcement,
Asset,
Batch,
Batchfertilizer,
BatchSeed,
Expand Down Expand Up @@ -112,12 +113,34 @@ 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"]
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()
site_tree_species = serializers.SerializerMethodField()
contact = ContactSerializer()
announcement = AnnouncementSerializer()
image = AssetSerializer()

class Meta:
model = Site
Expand All @@ -134,6 +157,7 @@ class SiteSocialSerializer(serializers.ModelSerializer):
announcement = AnnouncementSerializer()
widget = serializers.SerializerMethodField()
sponsors = serializers.SerializerMethodField()
image = AssetSerializer()

class Meta:
model = Site
Expand Down Expand Up @@ -249,6 +273,7 @@ class Meta:
"name",
"size",
"soil_condition",
"sponsor",
"fertilizers",
"mulch_layers",
"supported_species",
Expand Down Expand Up @@ -384,6 +409,7 @@ def get_longitude(self, obj):
class SiteMapSerializer(serializers.ModelSerializer):
site_type = SiteTypeSerializer()
coordinates = serializers.SerializerMethodField()
image = AssetSerializer()

class Meta:
model = Site
Expand All @@ -395,19 +421,38 @@ def get_coordinates(self, obj):


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

class Meta:
model = Site
fields = ("id", "name")
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()
has_liked = serializers.SerializerMethodField()
media = AssetSerializer(many=True)

class Meta:
model = Post
fields = ("id", "site", "created_at", "body", "like_count", "share_count", "comment_count", "has_liked")
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 Down
5 changes: 5 additions & 0 deletions canopeum_backend/canopeum_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@

STATIC_URL = "static/"

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

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

APPEND_SLASH = False
4 changes: 4 additions & 0 deletions canopeum_backend/canopeum_backend/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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
Expand Down Expand Up @@ -63,3 +65,5 @@
path("api/schema/swagger-ui/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
path("api/schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Loading
Loading