From 2e991224d846b5148418e5a639d45634eb0c4bff Mon Sep 17 00:00:00 2001
From: olechrib <olechrib@stud.ntnu.no>
Date: Fri, 11 Mar 2022 07:07:01 +0100
Subject: [PATCH] added django app for content created models, seralizers and
 views for the app added Pillow to requirements for image support

---
 backend/secfit/content/__init__.py            |   0
 backend/secfit/content/admin.py               |   5 ++
 backend/secfit/content/apps.py                |   5 ++
 .../secfit/content/migrations/0001_initial.py |  29 ++++++++++++
 backend/secfit/content/migrations/__init__.py |   0
 backend/secfit/content/models.py              |  15 ++++++
 backend/secfit/content/serializers.py         |  11 +++++
 backend/secfit/content/tests.py               |   3 ++
 backend/secfit/content/urls.py                |   8 ++++
 backend/secfit/content/views.py               |  44 ++++++++++++++++++
 backend/secfit/requirements.txt               | Bin 1192 -> 1222 bytes
 backend/secfit/secfit/settings.py             |   1 +
 backend/secfit/secfit/urls.py                 |   1 +
 requirements.txt                              | Bin 1192 -> 1218 bytes
 14 files changed, 122 insertions(+)
 create mode 100644 backend/secfit/content/__init__.py
 create mode 100644 backend/secfit/content/admin.py
 create mode 100644 backend/secfit/content/apps.py
 create mode 100644 backend/secfit/content/migrations/0001_initial.py
 create mode 100644 backend/secfit/content/migrations/__init__.py
 create mode 100644 backend/secfit/content/models.py
 create mode 100644 backend/secfit/content/serializers.py
 create mode 100644 backend/secfit/content/tests.py
 create mode 100644 backend/secfit/content/urls.py
 create mode 100644 backend/secfit/content/views.py

diff --git a/backend/secfit/content/__init__.py b/backend/secfit/content/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/backend/secfit/content/admin.py b/backend/secfit/content/admin.py
new file mode 100644
index 0000000..2e5edc9
--- /dev/null
+++ b/backend/secfit/content/admin.py
@@ -0,0 +1,5 @@
+from django.contrib import admin
+from .models import Content
+
+# Register your models here.
+admin.site.register(Content)
\ No newline at end of file
diff --git a/backend/secfit/content/apps.py b/backend/secfit/content/apps.py
new file mode 100644
index 0000000..3a9bd48
--- /dev/null
+++ b/backend/secfit/content/apps.py
@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class ContentConfig(AppConfig):
+    name = 'content'
diff --git a/backend/secfit/content/migrations/0001_initial.py b/backend/secfit/content/migrations/0001_initial.py
new file mode 100644
index 0000000..e5d7bf1
--- /dev/null
+++ b/backend/secfit/content/migrations/0001_initial.py
@@ -0,0 +1,29 @@
+# Generated by Django 3.1 on 2022-03-11 05:50
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('groups', '0004_delete_content'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Content',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('title', models.TextField(max_length=100)),
+                ('description', models.TextField()),
+                ('image', models.ImageField(blank=True, upload_to='images')),
+                ('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ownedByGroup', to='groups.group')),
+                ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contentOwner', to=settings.AUTH_USER_MODEL)),
+            ],
+        ),
+    ]
diff --git a/backend/secfit/content/migrations/__init__.py b/backend/secfit/content/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/backend/secfit/content/models.py b/backend/secfit/content/models.py
new file mode 100644
index 0000000..e6c4b5c
--- /dev/null
+++ b/backend/secfit/content/models.py
@@ -0,0 +1,15 @@
+from django.db import models
+from groups.models import Group
+from django.contrib.auth import get_user_model
+
+# Create your models here.
+class Content(models.Model):
+    owner = models.ForeignKey(
+        get_user_model(), on_delete=models.CASCADE, related_name="contentOwner"
+    )
+    group = models.ForeignKey(
+        Group, on_delete=models.CASCADE, related_name="ownedByGroup"
+    )
+    title = models.TextField(max_length=100)
+    description = models.TextField()
+    image = models.ImageField(upload_to='images', blank=True)
\ No newline at end of file
diff --git a/backend/secfit/content/serializers.py b/backend/secfit/content/serializers.py
new file mode 100644
index 0000000..083611e
--- /dev/null
+++ b/backend/secfit/content/serializers.py
@@ -0,0 +1,11 @@
+from rest_framework import serializers
+from .models import Content
+
+class ContentSerializer(serializers.ModelSerializer):
+    """Serializer for a group content instance
+
+    Serialized fields: id, member, group
+    """
+    class Meta:
+        model = Content
+        fields = ["id", "owner", "group", "title", "description", "image"]
\ No newline at end of file
diff --git a/backend/secfit/content/tests.py b/backend/secfit/content/tests.py
new file mode 100644
index 0000000..7ce503c
--- /dev/null
+++ b/backend/secfit/content/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/backend/secfit/content/urls.py b/backend/secfit/content/urls.py
new file mode 100644
index 0000000..b7a7e6f
--- /dev/null
+++ b/backend/secfit/content/urls.py
@@ -0,0 +1,8 @@
+from django.urls import path
+from .views import ContentView, ContentDetail
+
+
+urlpatterns = [
+    path("api/content/", ContentView.as_view(), name="content-view"),
+    path("api/content/<int:pk>/", ContentDetail.as_view(), name="contentDetail-view"),
+]
\ No newline at end of file
diff --git a/backend/secfit/content/views.py b/backend/secfit/content/views.py
new file mode 100644
index 0000000..6ccbdf3
--- /dev/null
+++ b/backend/secfit/content/views.py
@@ -0,0 +1,44 @@
+from rest_framework import generics, mixins
+from .models import Content
+from rest_framework import permissions
+from .serializers import ContentSerializer
+
+# Create your views here.
+class ContentView(
+    mixins.CreateModelMixin,
+    mixins.RetrieveModelMixin,
+    mixins.ListModelMixin,
+    generics.GenericAPIView,
+):
+    """
+    View getting and adding content to a group
+
+    HTTP methods: GET, POST
+    """
+    queryset = Content.objects.all()
+    serializer_class = ContentSerializer
+
+    def get(self, request, *args, **kwargs):
+        return self.list(request, *args, **kwargs)
+
+    def post(self, request, *args, **kwargs):
+        return self.create(request, *args, **kwargs)
+
+class ContentDetail(
+    mixins.RetrieveModelMixin,
+    mixins.UpdateModelMixin,
+    generics.GenericAPIView,
+):
+    """
+    View getting and adding content to a group
+
+    HTTP methods: GET, POST
+    """
+    queryset = Content.objects.all()
+    serializer_class = ContentSerializer
+
+    def get(self, request, *args, **kwargs):
+        return self.retrieve(request, *args, **kwargs)
+
+    def put(self, request, *args, **kwargs):
+        return self.update(request, *args, **kwargs)
\ No newline at end of file
diff --git a/backend/secfit/requirements.txt b/backend/secfit/requirements.txt
index 9feb375bde1e8fb7befe6c102dd29beeee7c6940..88fa935213723454c324824ac92816923797c869 100644
GIT binary patch
delta 38
pcmZ3%d5m+z3KqElhD?SWAk1edXRrlAO9nj#10XhJ;AP-q006g$2KfL0

delta 7
OcmX@cxq@@U3KjqicLLo2

diff --git a/backend/secfit/secfit/settings.py b/backend/secfit/secfit/settings.py
index 640c563..a572d28 100644
--- a/backend/secfit/secfit/settings.py
+++ b/backend/secfit/secfit/settings.py
@@ -62,6 +62,7 @@ INSTALLED_APPS = [
     "users.apps.UsersConfig",
     "comments.apps.CommentsConfig",
     "groups.apps.GroupsConfig",
+    "content.apps.ContentConfig",
     "corsheaders",
 ]
 
diff --git a/backend/secfit/secfit/urls.py b/backend/secfit/secfit/urls.py
index 227073f..a92ac29 100644
--- a/backend/secfit/secfit/urls.py
+++ b/backend/secfit/secfit/urls.py
@@ -23,6 +23,7 @@ urlpatterns = [
     path("", include("workouts.urls")),
     path("", include("meals.urls")),
     path("", include("groups.urls")),
+    path("", include("content.urls")),
 ]
 
 urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
diff --git a/requirements.txt b/requirements.txt
index 9feb375bde1e8fb7befe6c102dd29beeee7c6940..3336c06f830c08f4048e40e65c3fd3dbfd5b6d8b 100644
GIT binary patch
delta 34
lcmZ3%d5Ckv3KppVhD?SWAk1edXRrlAO9nj#10XhJ005fI2HF4s

delta 7
OcmX@axq@@U3KjqiTLRet

-- 
GitLab