From a2a9a759396739442b971f7aac2f053b634efcc4 Mon Sep 17 00:00:00 2001
From: rlacko <rlacko@sch.bme.hu>
Date: Sat, 8 Feb 2020 03:14:57 +0100
Subject: [PATCH] image upload for main site

---
 src/document/models.py                        |  2 +-
 src/document/views.py                         | 18 +++++++++++-----
 src/images/__init__.py                        |  0
 src/images/admin.py                           |  5 +++++
 src/images/apps.py                            |  5 +++++
 src/images/migrations/0001_initial.py         | 21 +++++++++++++++++++
 .../migrations/0002_auto_20200208_0254.py     | 18 ++++++++++++++++
 src/images/migrations/__init__.py             |  0
 src/images/models.py                          | 10 +++++++++
 src/images/serializers.py                     |  9 ++++++++
 src/images/tests.py                           |  0
 src/images/urls.py                            |  8 +++++++
 src/images/views.py                           | 14 +++++++++++++
 src/kszkepzes/settings/base.py                |  3 ++-
 src/kszkepzes/urls.py                         |  1 +
 src/mentors/views.py                          |  8 +++++--
 16 files changed, 113 insertions(+), 9 deletions(-)
 create mode 100644 src/images/__init__.py
 create mode 100644 src/images/admin.py
 create mode 100644 src/images/apps.py
 create mode 100644 src/images/migrations/0001_initial.py
 create mode 100644 src/images/migrations/0002_auto_20200208_0254.py
 create mode 100644 src/images/migrations/__init__.py
 create mode 100644 src/images/models.py
 create mode 100644 src/images/serializers.py
 create mode 100644 src/images/tests.py
 create mode 100644 src/images/urls.py
 create mode 100644 src/images/views.py

diff --git a/src/document/models.py b/src/document/models.py
index 093434c..f4103dc 100644
--- a/src/document/models.py
+++ b/src/document/models.py
@@ -10,7 +10,7 @@ from common.validators import FileSizeValidator
 
 def document_file_name(instance, filename):
     return '/'.join([
-        'document',
+        'public/document',
         instance.solution.task.title,
         instance.uploaded_by.full_name,
         filename
diff --git a/src/document/views.py b/src/document/views.py
index 3744e3b..e2e5d30 100644
--- a/src/document/views.py
+++ b/src/document/views.py
@@ -7,6 +7,7 @@ from django.http import HttpResponse, Http404
 from rest_framework.decorators import action
 import os
 
+
 class DocumentViewSet(viewsets.ModelViewSet):
     serializer_class = serializers.DocumentSerializer
     permission_classes = (permissions.IsStaffOrStudent, )
@@ -25,7 +26,10 @@ class DocumentViewSet(viewsets.ModelViewSet):
         profile_id = self.request.query_params.get('profileID', None)
         solution_id = self.request.query_params.get('solutionID', None)
         if profile_id is not None and solution_id is not None:
-            return queryset.filter(uploaded_by=profile_id, solution=solution_id)
+            return queryset.filter(
+                uploaded_by=profile_id,
+                solution=solution_id
+            )
         if profile_id is not None:
             return queryset.filter(uploaded_by=profile_id)
         if solution_id is not None:
@@ -38,19 +42,23 @@ class DocumentViewSet(viewsets.ModelViewSet):
         if solution_id is not None:
             return queryset.filter(solution=solution_id)
         return queryset
-    
+
     def perform_create(self, serializer):
         kwargs = {
             'uploaded_by': self.request.user.profile
         }
- 
+
         serializer.save(**kwargs)
 
     @action(detail=True, methods=["get"])
     def download(self, request, pk):
         document = self.get_object()
         with document.file.open() as fh:
-            response = HttpResponse(fh.read(), content_type="application/media")
-            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(document.file.name)
+            response = HttpResponse(
+                fh.read(),
+                content_type="application/media"
+            )
+            response['Content-Disposition'] = \
+                'inline; filename=' + os.path.basename(document.file.name)
             return response
         raise Http404
diff --git a/src/images/__init__.py b/src/images/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/images/admin.py b/src/images/admin.py
new file mode 100644
index 0000000..e545953
--- /dev/null
+++ b/src/images/admin.py
@@ -0,0 +1,5 @@
+from django.contrib import admin
+from .models import Images
+
+admin.site.register(Images)
+# Register your models here.
diff --git a/src/images/apps.py b/src/images/apps.py
new file mode 100644
index 0000000..a873d1a
--- /dev/null
+++ b/src/images/apps.py
@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class ImagesConfig(AppConfig):
+    name = 'images'
diff --git a/src/images/migrations/0001_initial.py b/src/images/migrations/0001_initial.py
new file mode 100644
index 0000000..2319c14
--- /dev/null
+++ b/src/images/migrations/0001_initial.py
@@ -0,0 +1,21 @@
+# Generated by Django 2.2.4 on 2020-02-07 22:44
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Images',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('image', models.ImageField(blank=True, null=True, upload_to='images/')),
+            ],
+        ),
+    ]
diff --git a/src/images/migrations/0002_auto_20200208_0254.py b/src/images/migrations/0002_auto_20200208_0254.py
new file mode 100644
index 0000000..045c2a1
--- /dev/null
+++ b/src/images/migrations/0002_auto_20200208_0254.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.2.4 on 2020-02-08 01:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('images', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='images',
+            name='image',
+            field=models.ImageField(blank=True, null=True, upload_to='public/images/'),
+        ),
+    ]
diff --git a/src/images/migrations/__init__.py b/src/images/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/images/models.py b/src/images/models.py
new file mode 100644
index 0000000..d515aef
--- /dev/null
+++ b/src/images/models.py
@@ -0,0 +1,10 @@
+from django.db import models
+
+
+class Images(models.Model):
+    image = models.ImageField(
+        upload_to='public/images/', null=True, blank=True
+        )
+
+    def __str__(self):
+        return self.id
diff --git a/src/images/serializers.py b/src/images/serializers.py
new file mode 100644
index 0000000..e8985bd
--- /dev/null
+++ b/src/images/serializers.py
@@ -0,0 +1,9 @@
+from images.models import Images
+from rest_framework import serializers
+
+
+class ImagesSerializer(serializers.ModelSerializer):
+
+    class Meta:
+        model = Images
+        fields = ('id', 'image')
diff --git a/src/images/tests.py b/src/images/tests.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/images/urls.py b/src/images/urls.py
new file mode 100644
index 0000000..b16c854
--- /dev/null
+++ b/src/images/urls.py
@@ -0,0 +1,8 @@
+from rest_framework import routers
+from images import views
+
+
+router = routers.DefaultRouter()
+router.register(r'images', views.ImagesViewSet, base_name='images')
+
+urlpatterns = router.urls
diff --git a/src/images/views.py b/src/images/views.py
new file mode 100644
index 0000000..162a8da
--- /dev/null
+++ b/src/images/views.py
@@ -0,0 +1,14 @@
+from common.permissions import IsStaffOrStudent, \
+    IsStaffOrReadOnlyForAuthenticated
+from rest_framework import viewsets
+from images.models import Images
+from images.serializers import ImagesSerializer
+
+
+class ImagesViewSet(viewsets.ModelViewSet):
+    serializer_class = ImagesSerializer
+    permission_classes = (
+        IsStaffOrReadOnlyForAuthenticated,
+        IsStaffOrStudent,
+    )
+    queryset = Images.objects.all()
diff --git a/src/kszkepzes/settings/base.py b/src/kszkepzes/settings/base.py
index 1e9a301..b040f8c 100644
--- a/src/kszkepzes/settings/base.py
+++ b/src/kszkepzes/settings/base.py
@@ -49,6 +49,7 @@ INSTALLED_APPS = [
     'news',
     'document',
     'mentors',
+    'images',
     'groups',
     'drf_yasg',
 ]
@@ -160,4 +161,4 @@ STATIC_URL = '/staticfiles/'
 STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
 
 MEDIA_URL = "/mediafiles/"
-MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
+MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
\ No newline at end of file
diff --git a/src/kszkepzes/urls.py b/src/kszkepzes/urls.py
index 0760d77..c07e90e 100644
--- a/src/kszkepzes/urls.py
+++ b/src/kszkepzes/urls.py
@@ -37,6 +37,7 @@ urlpatterns = [
     url(r'^api/v1/', include('document.urls')),
     url(r'^api/v1/', include('groups.urls')),
     url(r'^api/v1/', include('mentors.urls')),
+    url(r'^api/v1/', include('images.urls')),
     url(r'^api/v1/logout/$', auth_views.LogoutView.as_view(), name='logout'),
 ]
 
diff --git a/src/mentors/views.py b/src/mentors/views.py
index 28fdcc0..825eccb 100644
--- a/src/mentors/views.py
+++ b/src/mentors/views.py
@@ -1,4 +1,5 @@
-from common.permissions import IsStaffOrStudent
+from common.permissions import IsStaffOrStudent, \
+    IsStaffOrReadOnlyForAuthenticated
 from rest_framework import viewsets
 from mentors.models import Mentor
 from mentors.serializers import MentorSerializer
@@ -6,7 +7,10 @@ from mentors.serializers import MentorSerializer
 
 class MentorsViewSet(viewsets.ModelViewSet):
     serializer_class = MentorSerializer
-    permission_classes = (IsStaffOrStudent,)
+    permission_classes = (
+        IsStaffOrReadOnlyForAuthenticated,
+        IsStaffOrStudent,
+    )
     queryset = Mentor.objects.all().order_by('name')
 
     def perform_create(self, serializer):
-- 
GitLab