From dbe70fdf2fb8318f412dfbdf38391a52ddbcf32b Mon Sep 17 00:00:00 2001
From: Robin Tissot <tissotrobin@gmail.com>
Date: Wed, 6 Jan 2021 14:20:18 +0100
Subject: [PATCH] Adds a contact message creation date and fixes sending it
 through.

---
 app/apps/imports/tasks.py                     |  2 +-
 app/apps/users/admin.py                       |  6 ++++-
 .../migrations/0013_auto_20210106_1305.py     | 25 +++++++++++++++++++
 app/apps/users/models.py                      | 11 ++++----
 app/escriptorium/utils.py                     | 13 +++++-----
 5 files changed, 43 insertions(+), 14 deletions(-)
 create mode 100644 app/apps/users/migrations/0013_auto_20210106_1305.py

diff --git a/app/apps/imports/tasks.py b/app/apps/imports/tasks.py
index 6ecfc50f..cb810a72 100644
--- a/app/apps/imports/tasks.py
+++ b/app/apps/imports/tasks.py
@@ -151,6 +151,6 @@ def document_export(task, file_format, user_pk, document_pk, part_pks,
     send_email('export/email/ready_subject.txt',
                'export/email/ready_message.txt',
                'export/email/ready_html.html',
-               user.email,
+               (user.email,),
                context={'domain': Site.objects.get_current().domain,
                         'export_uri': rel_path})
diff --git a/app/apps/users/admin.py b/app/apps/users/admin.py
index 423dac64..d44bb4ca 100644
--- a/app/apps/users/admin.py
+++ b/app/apps/users/admin.py
@@ -60,8 +60,12 @@ class InvitationAdmin(admin.ModelAdmin):
         ) % count, messages.SUCCESS)
 
 
+class ContactUsAdmin(admin.ModelAdmin):
+    readonly_fields = ('created_at',)
+
+
 admin.site.register(User, MyUserAdmin)
 admin.site.register(ResearchField)
 admin.site.register(Invitation, InvitationAdmin)
-admin.site.register(ContactUs)
+admin.site.register(ContactUs, ContactUsAdmin)
 admin.site.register(GroupOwner)
diff --git a/app/apps/users/migrations/0013_auto_20210106_1305.py b/app/apps/users/migrations/0013_auto_20210106_1305.py
new file mode 100644
index 00000000..474cb1f0
--- /dev/null
+++ b/app/apps/users/migrations/0013_auto_20210106_1305.py
@@ -0,0 +1,25 @@
+# Generated by Django 2.1.4 on 2021-01-06 13:05
+
+from django.db import migrations, models
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('users', '0012_create_group_owners'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='contactus',
+            name='created_at',
+            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
+            preserve_default=False,
+        ),
+        migrations.AlterField(
+            model_name='user',
+            name='onboarding',
+            field=models.BooleanField(default=True, verbose_name='Show onboarding'),
+        ),
+    ]
diff --git a/app/apps/users/models.py b/app/apps/users/models.py
index 91264d71..3ef8cc9f 100644
--- a/app/apps/users/models.py
+++ b/app/apps/users/models.py
@@ -114,7 +114,7 @@ class Invitation(models.Model):
             "team": self.group.name,
             "accept_link": accept_url,
         }
-        self.send_email(self.recipient.email, context)
+        self.send_email((self.recipient.email,), context)
 
     def send_invitation_to_service(self, request):
         accept_url = request.build_absolute_uri(
@@ -127,7 +127,7 @@ class Invitation(models.Model):
             "team": self.group and self.group.name,
             "accept_link": accept_url,
         }
-        self.send_email(self.recipient_email, context)
+        self.send_email((self.recipient_email,), context)
 
     def send_email(self, to, context):
         send_email(
@@ -163,7 +163,6 @@ class Invitation(models.Model):
         return True
 
 
-
 class ContactUs(models.Model):
     """
     Represents Contact Us form
@@ -174,10 +173,11 @@ class ContactUs(models.Model):
         max_length=255,
     )
     message = models.TextField()
+    created_at = models.DateTimeField(auto_now_add=True, editable=False)
 
     class Meta:
-        verbose_name = "message"
-        verbose_name_plural = "messages"
+        verbose_name = "Contact message"
+        verbose_name_plural = "Contact messages"
 
     def __str__(self):
         return "from {}({})".format(self.name, self.email)
@@ -197,7 +197,6 @@ class ContactUs(models.Model):
             context=context,
             result_interface=None
         )
-
         super().save(*args, **kwargs)
 
 
diff --git a/app/escriptorium/utils.py b/app/escriptorium/utils.py
index b50a8f97..0da34866 100644
--- a/app/escriptorium/utils.py
+++ b/app/escriptorium/utils.py
@@ -5,23 +5,24 @@ from users.tasks import async_email
 
 
 def send_email(subject_template, txt_template, html_template,
-               recipient, context=None,
+               recipients,
+               context=None,
                result_interface=None):
     """
     Higher level interface to send a multipart/aternative email with text/plain + text/html
-    
+
     Send an email to the given recipient using given templates and context
     Queue with celery if settings.USE_CELERY == True
-    
+
     result_interface is used by the task to update an instance if the mail was sent or got an error
     it's a tuple (app_name, model_name, instance_id) which needs to present .email_sent() and .email_error(msg) methods
     """
     subject_tmplt = get_template(subject_template)
     txt_tmplt = get_template(txt_template)
     html_tmplt = get_template(html_template)
-    
+
     subject = subject_tmplt.render(context=context).strip('\n')
     msg = txt_tmplt.render(context=context)
     html = html_tmplt.render(context=context)
-    
-    async_email.delay(subject, msg, (recipient,), html=html, result_interface=result_interface)
+
+    async_email.delay(subject, msg, recipients, html=html, result_interface=result_interface)
-- 
GitLab