diff --git a/apimail/api/views.py b/apimail/api/views.py index e62579d61cc0c57f8640d227cad18c254b27502b..761ec872f1716e894a12d7f88b6535b0a6d1dfd0 100644 --- a/apimail/api/views.py +++ b/apimail/api/views.py @@ -109,6 +109,7 @@ class StoredMessageRetrieveAPIView(RetrieveAPIView): class StoredMessageUpdateReadAPIView(UpdateAPIView): """Updates the read field (M2M to user) in StoredMessage.""" + queryset = StoredMessage.objects.all() permission_classes = (IsAdminUser,) serializer_class = StoredMessageSerializer lookup_field = 'uuid' @@ -117,5 +118,5 @@ class StoredMessageUpdateReadAPIView(UpdateAPIView): def partial_update(self, request, *args, **kwargs): instance = self.get_object() instance.read_by.add(request.user) - instance.save(update_fields=['read_by']) + instance.save() return Response() diff --git a/apimail/migrations/0007_auto_20200116_1955.py b/apimail/migrations/0007_auto_20200116_1955.py new file mode 100644 index 0000000000000000000000000000000000000000..ee0e77c208418dc96404dd0b3214e58e0ceabdbe --- /dev/null +++ b/apimail/migrations/0007_auto_20200116_1955.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.8 on 2020-01-16 18:55 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('apimail', '0006_storedmessage_read_by'), + ] + + operations = [ + migrations.AlterField( + model_name='storedmessage', + name='read_by', + field=models.ManyToManyField(blank=True, related_name='_storedmessage_read_by_+', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/apimail/models/stored_message.py b/apimail/models/stored_message.py index 75688cd5fc6fa8686fa94338a2a4ebbf513de8d4..ffcf290553c50c3f5a16582cee091cbb4b4668e6 100644 --- a/apimail/models/stored_message.py +++ b/apimail/models/stored_message.py @@ -26,7 +26,10 @@ class StoredMessage(models.Model): editable=False) data = JSONField(default=dict) datetimestamp = models.DateTimeField(default=timezone.now) - read_by = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='+') + read_by = models.ManyToManyField( + settings.AUTH_USER_MODEL, + blank=True, + related_name='+') objects = StoredMessageQuerySet.as_manager() diff --git a/apimail/static/apimail/assets/vue/components/MessageContent.vue b/apimail/static/apimail/assets/vue/components/MessageContent.vue index 28cd814a79e5d822b076ac16bd3af0cb5f8e766d..b42c2db01507753352493ac66234fa746417bd53 100644 --- a/apimail/static/apimail/assets/vue/components/MessageContent.vue +++ b/apimail/static/apimail/assets/vue/components/MessageContent.vue @@ -51,36 +51,47 @@ </template> <script> - export default { - name: "message-content", - props: { - message: { - type: Object, - required: true - }, - }, - computed: { - sanitized_html() { - if (this.message.data["body-html"]) { - return this.$sanitize(this.message.data["body-html"]) - } - return this.$sanitize(this.message.data["body-plain"]) - } - }, - filters: { - toDatestring(unixtimestamp) { - return new Date(1000 * unixtimestamp).toISOString() - } - }, - mounted () { - if (!this.message.read) { - console.log('uuid: ' + this.message.uuid) - fetch('/mail/api/stored_message/' + this.message.uuid + '/mark_as_read', - { method: 'PATCH', } - ).then((res) => res.json()) - .then((data) => console.log(data)) - .catch((err)=>console.log(err)) - } - } - } +import Cookies from 'js-cookie' + +var csrftoken = Cookies.get('csrftoken'); + +export default { + name: "message-content", + props: { + message: { + type: Object, + required: true + }, + }, + computed: { + sanitized_html() { + if (this.message.data["body-html"]) { + return this.$sanitize(this.message.data["body-html"]) + } + return this.$sanitize(this.message.data["body-plain"]) + } + }, + filters: { + toDatestring(unixtimestamp) { + return new Date(1000 * unixtimestamp).toISOString() + } + }, + mounted () { + if (!this.message.read) { + console.log('uuid: ' + this.message.uuid) + fetch('/mail/api/stored_message/' + this.message.uuid + '/mark_as_read', + { method: 'PATCH', + headers: { + "X-CSRFToken": csrftoken, + } + } + ).then(function(response) { + if (!response.ok) { + throw new Error('HTTP error, status = ' + response.status); + } + }); + this.message.read = true + } + } +} </script> diff --git a/package.json b/package.json index a2b7bc6b97ce03a37f5054c5f05ba15fdbbd1dca..73230ec7798b82160671bf9a69df0ba5b43dd410 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "bootstrap-vue": "^2.1.0", "expose-loader": "^0.7.5", "fibers": "^4.0.2", + "js-cookie": "^2.2.1", "mem": "^6.0.0", "mini-css-extract-plugin": "^0.8.0", "nan": "^2.14.0",