From 96248962de09b475b610e8252be56af60a8a0307 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bodor=20M=C3=A1t=C3=A9?= <bodor.mate@kszk.bme.hu>
Date: Mon, 21 Jan 2019 15:53:53 +0100
Subject: [PATCH] Refactor profile validation

---
 src/account/serializers.py                    | 27 +++++++++++++------
 src/common/email.py                           |  4 +--
 .../migrations/0002_auto_20190121_1332.py     | 20 ++++++++++++++
 .../migrations/0003_auto_20190121_1335.py     | 20 ++++++++++++++
 src/document/models.py                        |  3 ++-
 src/homework/serializers.py                   |  8 +++++-
 src/kszkepzes/settings/local.py               |  4 +--
 7 files changed, 72 insertions(+), 14 deletions(-)
 create mode 100644 src/document/migrations/0002_auto_20190121_1332.py
 create mode 100644 src/document/migrations/0003_auto_20190121_1335.py

diff --git a/src/account/serializers.py b/src/account/serializers.py
index fd5f20f..1ceaf2d 100644
--- a/src/account/serializers.py
+++ b/src/account/serializers.py
@@ -31,17 +31,28 @@ class ProfileSerializer(serializers.ModelSerializer):
             'role',
         )
 
-    def validate(self, data):
+    def validate_updated_at(self, value):
         deadline = models.Deadline.get_solo().deadline
-        if data['signed'] is False:
-            raise serializers.ValidationError("You cannot join without signed")
-        if deadline is not None and data['updated_at'] > deadline:
+        if deadline is not None and value > deadline:
             raise serializers.ValidationError("You cannot join after the deadline")
+        return value
+
+    def validate_role(self, value):
         modifier_role = CurrentUserMiddleware.get_current_user_profile().role
-        role = data['role']
-        if role is not None and modifier_role != 'Staff':
-            raise serializers.ValidationError("You don't have permission to change role")
-        return data
+        if value != modifier_role and modifier_role != "Staff":
+            raise serializers.ValidationError("You don't have permission change role")
+        return value
+
+    def validate_signed(self, value):
+        if value is False:
+            raise serializers.ValidationError("You cannot join without signed")
+        return value
+
+    def validate_id(self, value):
+        modifier= CurrentUserMiddleware.get_current_user_profile()
+        if value != modifier.id and modifier.role != "Staff":
+            raise serializers.ValidationError("You don't have permission")
+        return value
 
     def get_full_name(self, obj):
         return obj.full_name
diff --git a/src/common/email.py b/src/common/email.py
index 67ca7d6..1c4151e 100644
--- a/src/common/email.py
+++ b/src/common/email.py
@@ -19,11 +19,11 @@ def denied(email):
     send_mail(subject, message, 'noreply@devteam.sch.bme.hu', [email, ])
 
 
-def new_homework(email):
+def new_homework(emails):
     subject = "NEW HOMEWORK TEST"
     message = "Szia!\nEgy Ăşj hĂĄzi lett kiadva, ha tĂ­z percen belĂźl megoldod akkor fasza gyerek vagy," \
               " ha nem ĂŠleted vĂŠgĂŠig bĂĄnnifogod..."
-    send_mail(subject, message, 'noreply@devteam.sch.bme.hu', [email, ])
+    send_mail(subject, message, 'noreply@devteam.sch.bme.hu', emails)
 
 
 def homework_corrected(email):
diff --git a/src/document/migrations/0002_auto_20190121_1332.py b/src/document/migrations/0002_auto_20190121_1332.py
new file mode 100644
index 0000000..5cf755b
--- /dev/null
+++ b/src/document/migrations/0002_auto_20190121_1332.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.0.1 on 2019-01-21 12:32
+
+import common.validators
+import django.core.validators
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('document', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='document',
+            name='file',
+            field=models.FileField(blank=True, default='', upload_to='', validators=[django.core.validators.FileExtensionValidator(['png', 'jpeg', 'jpg', 'zip']), common.validators.FileSizeValidator(size_limit=52428800)]),
+        ),
+    ]
diff --git a/src/document/migrations/0003_auto_20190121_1335.py b/src/document/migrations/0003_auto_20190121_1335.py
new file mode 100644
index 0000000..fa4d1c2
--- /dev/null
+++ b/src/document/migrations/0003_auto_20190121_1335.py
@@ -0,0 +1,20 @@
+# Generated by Django 2.0.1 on 2019-01-21 12:35
+
+import common.validators
+import django.core.validators
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('document', '0002_auto_20190121_1332'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='document',
+            name='file',
+            field=models.FileField(blank=True, upload_to='', validators=[django.core.validators.FileExtensionValidator(['png', 'jpeg', 'jpg', 'zip']), common.validators.FileSizeValidator(size_limit=52428800)]),
+        ),
+    ]
diff --git a/src/document/models.py b/src/document/models.py
index 3401e64..d86febb 100644
--- a/src/document/models.py
+++ b/src/document/models.py
@@ -20,7 +20,8 @@ class Document(models.Model):
                 'zip',
             ]),
             FileSizeValidator(size_limit=52428800),  # 52428800 - 50MiB
-        ]
+        ],
+        blank=True,
     )
     solution = models.ForeignKey(Solution, related_name='files', on_delete=models.DO_NOTHING, blank=True, null=True)
 
diff --git a/src/homework/serializers.py b/src/homework/serializers.py
index cb9ff5b..c6acde3 100755
--- a/src/homework/serializers.py
+++ b/src/homework/serializers.py
@@ -1,7 +1,8 @@
 from rest_framework import serializers
 from django.utils import timezone
-
+from account.models import Profile
 from . import models
+from common.email import new_homework
 from common.middleware import CurrentUserMiddleware
 
 
@@ -16,6 +17,11 @@ class TaskSerializer(serializers.ModelSerializer):
             raise serializers.ValidationError('Please, enter appropriate deadline.')
         return data
 
+    def create(self, validated_data):
+        emails = Profile.objects.filter(role="Student").exclude(user__email='').values_list('user__email', flat=True)
+        new_homework(emails)
+        return self.Meta.model.objects.create(**validated_data)
+
 
 class SolutionSerializer(serializers.ModelSerializer):
     class Meta:
diff --git a/src/kszkepzes/settings/local.py b/src/kszkepzes/settings/local.py
index 2cb6909..53eb49a 100644
--- a/src/kszkepzes/settings/local.py
+++ b/src/kszkepzes/settings/local.py
@@ -5,5 +5,5 @@ EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
 EMAIL_HOST = 'smtp.gmail.com'
 EMAIL_USE_TLS = True
 EMAIL_PORT = 587
-EMAIL_HOST_USER = 'bmate711kamu@gmail.com'
-EMAIL_HOST_PASSWORD = '1IronxDog'
+EMAIL_HOST_USER = os.getenv('EMAIL')
+EMAIL_HOST_PASSWORD = os.getenv('EMAIL_PASSWORD')
-- 
GitLab