From 969d8a0eac0cd5ddf9e1a50276cb1c01aa75521f Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Fri, 25 Dec 2020 16:42:56 +0200 Subject: [PATCH 1/4] provide notifications on commenting --- cms/settings.py | 3 ++- files/methods.py | 38 +++++++++++++++++++++++++++++--------- files/models.py | 8 ++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/cms/settings.py b/cms/settings.py index b5de473..d523332 100644 --- a/cms/settings.py +++ b/cms/settings.py @@ -173,7 +173,8 @@ MINIMUM_RESOLUTIONS_TO_ENCODE = [240, 360] USERS_NOTIFICATIONS = { "MEDIA_ADDED": True, # in use "MEDIA_ENCODED": False, # not implemented - "MEDIA_REPORTED": False, # not implemented + "MEDIA_REPORTED": True, # in use + "COMMENT_ADDED": True, # in use } ADMINS_NOTIFICATIONS = { diff --git a/files/methods.py b/files/methods.py index 2bd81f0..3ca3a53 100644 --- a/files/methods.py +++ b/files/methods.py @@ -149,22 +149,32 @@ def notify_users(friendly_token=None, action=None, extra=None): media_url = settings.SSL_FRONTEND_HOST + media.get_absolute_url() if action == "media_reported" and media: - if settings.ADMINS_NOTIFICATIONS.get("MEDIA_REPORTED", False): - title = "[{}] - Media was reported".format(settings.PORTAL_NAME) - msg = """ + msg = """ Media %s was reported. Reason: %s\n -Total times this media has been reported: %s - """ % ( - media_url, - extra, - media.reported_times, - ) +Total times this media has been reported: %s\n +Media becomes private if it gets reported %s times %s\n + """ % ( + media_url, + extra, + media.reported_times, + settings.REPORTED_TIMES_THRESHOLD, + ) + + if settings.ADMINS_NOTIFICATIONS.get("MEDIA_REPORTED", False): + title = "[{}] - Media was reported".format(settings.PORTAL_NAME) d = {} d["title"] = title d["msg"] = msg d["to"] = settings.ADMIN_EMAIL_LIST notify_items.append(d) + if settings.USERS_NOTIFICATIONS.get("MEDIA_REPORTED", False): + title = "[{}] - Media was reported".format(settings.PORTAL_NAME) + d = {} + d["title"] = title + d["msg"] = msg + d["to"] = media.user.email + notify_items.append(d) if action == "media_added" and media: if settings.ADMINS_NOTIFICATIONS.get("MEDIA_ADDED", False): @@ -194,6 +204,16 @@ URL: %s d["to"] = [media.user.email] notify_items.append(d) + if action == "comment_added" and media: + if settings.USERS_NOTIFICATIONS.get("COMMENT_ADDED", False): + d = {} + title = f"[{settings.PORTAL_NAME}] - Comment was added" + msg = f"A comment was added on media {media_url}\n" + d["title"] = title + d["msg"] = msg + d["to"] = media.user.username + notify_items.append(d) + for item in notify_items: email = EmailMessage( item["title"], item["msg"], settings.DEFAULT_FROM_EMAIL, item["to"] diff --git a/files/models.py b/files/models.py index 65ec6b4..df5e86e 100644 --- a/files/models.py +++ b/files/models.py @@ -1712,3 +1712,11 @@ def encoding_file_delete(sender, instance, **kwargs): instance.media.post_encode_actions(encoding=instance, action="delete") # delete local chunks, and remote chunks + media file. Only when the # last encoding of a media is complete + + +@receiver(post_save, sender=Comment) +def comment_save(sender, instance, created, **kwargs): + if created: + notify_users( + friendly_token=instance.media.friendly_token, action="comment_added" + ) From e6361cecb10f2440b3b74e33d3361d01ebb1dfda Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Fri, 25 Dec 2020 16:58:53 +0200 Subject: [PATCH 2/4] fix notification upon comments --- files/methods.py | 4 ++-- files/views.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/files/methods.py b/files/methods.py index 3ca3a53..ac6b552 100644 --- a/files/methods.py +++ b/files/methods.py @@ -173,7 +173,7 @@ Media becomes private if it gets reported %s times %s\n d = {} d["title"] = title d["msg"] = msg - d["to"] = media.user.email + d["to"] = [media.user.email] notify_items.append(d) if action == "media_added" and media: @@ -211,7 +211,7 @@ URL: %s msg = f"A comment was added on media {media_url}\n" d["title"] = title d["msg"] = msg - d["to"] = media.user.username + d["to"] = [media.user.username] notify_items.append(d) for item in notify_items: diff --git a/files/views.py b/files/views.py index 6f7e7df..5160e65 100644 --- a/files/views.py +++ b/files/views.py @@ -1112,7 +1112,7 @@ class CommentDetail(APIView): Delete comment (DELETE) """ - permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsUserOrEditor) + permission_classes = (IsAuthorizedToAdd,) parser_classes = (JSONParser, MultiPartParser, FormParser, FileUploadParser) def get_object(self, friendly_token): From 40eaffef9451281578d67bedae75189170e79407 Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Fri, 25 Dec 2020 17:08:15 +0200 Subject: [PATCH 3/4] removes redundant arguement --- files/methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/methods.py b/files/methods.py index ac6b552..e64bf06 100644 --- a/files/methods.py +++ b/files/methods.py @@ -153,7 +153,7 @@ def notify_users(friendly_token=None, action=None, extra=None): Media %s was reported. Reason: %s\n Total times this media has been reported: %s\n -Media becomes private if it gets reported %s times %s\n +Media becomes private if it gets reported %s times\n """ % ( media_url, extra, From 7513870778d4b66809b12f0ee443fac0113cc8e3 Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Fri, 25 Dec 2020 17:24:42 +0200 Subject: [PATCH 4/4] fix error in permissions --- cms/permissions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cms/permissions.py b/cms/permissions.py index f456dbe..1556164 100644 --- a/cms/permissions.py +++ b/cms/permissions.py @@ -24,7 +24,7 @@ class IsUserOrManager(permissions.BasePermission): if is_mediacms_manager(request.user): return True - return obj == request.user + return obj.user == request.user class IsUserOrEditor(permissions.BasePermission): @@ -41,7 +41,7 @@ class IsUserOrEditor(permissions.BasePermission): if is_mediacms_editor(request.user): return True - return obj == request.user + return obj.user == request.user def user_allowed_to_upload(request):