From 93d28c656c4e5fde635217b94bc8773fc5cc93d2 Mon Sep 17 00:00:00 2001 From: davidpofo Date: Thu, 5 Aug 2021 11:56:18 -0400 Subject: [PATCH 1/4] get task for the attached object. added some more notes --- discussion/models.py | 12 ++++++++++++ guidedmodules/views.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/discussion/models.py b/discussion/models.py index e60b2a111..9208d7d64 100644 --- a/discussion/models.py +++ b/discussion/models.py @@ -246,6 +246,18 @@ def get_autocompletes(self, user): self._get_autocompletes = self.attached_to_obj.get_discussion_autocompletes(self) return self._get_autocompletes + @property + def get_task(self, discussion): + """ + Retrieves the task for the given discussion's attached object. For example Projects have root_task while TaskAnswer discussion would have a task. + """ + if hasattr(self.discussion.attached_to_obj, 'root_task'): + return discussion.attached_to_obj.root_task + elif hasattr(self.discussion.attached_to_obj, 'task'): + return discussion.attached_to_obj.task + else: + return False + class Comment(models.Model): discussion = models.ForeignKey(Discussion, related_name="comments", on_delete=models.CASCADE, help_text="The Discussion that this comment is attached to.") replies_to = models.ForeignKey('self', blank=True, null=True, related_name="replies", on_delete=models.CASCADE, help_text="If this is a reply to a Comment, the Comment that this is in reply to.") diff --git a/guidedmodules/views.py b/guidedmodules/views.py index e82b117b5..765d514be 100644 --- a/guidedmodules/views.py +++ b/guidedmodules/views.py @@ -2021,14 +2021,14 @@ def start_a_discussion(request): # Get the TaskAnswer for this task. It may not exist yet. tq, isnew = TaskAnswer.objects.get_or_create(**tq_filter) - + # Filter for discussion and return the first entry (if it doesn't exist it returns None) discussion = Discussion.get_for(task.project.organization, tq) if not discussion: # Validate user can create discussion. if not task.has_read_priv(request.user): return JsonResponse({ "status": "error", "message": "You do not have permission!" }) - # Get the Discussion. + # Create a Discussion. discussion = Discussion.get_for(task.project.organization, tq, create=True) return JsonResponse(discussion.render_context_dict(request.user)) From b213fcab231e7d087f704f3990399f9aa7cbc84c Mon Sep 17 00:00:00 2001 From: davidpofo Date: Thu, 5 Aug 2021 14:16:35 -0400 Subject: [PATCH 2/4] fixed signature for getting a task for the attached object. --- discussion/models.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/discussion/models.py b/discussion/models.py index 9208d7d64..3a4096b2c 100644 --- a/discussion/models.py +++ b/discussion/models.py @@ -176,6 +176,11 @@ def post_comment(self, user, text, post_method, is_draft=False): def is_public(self): return getattr(self.attached_to_obj, 'is_discussion_public', lambda : False) + def is_discussion_inactive(self): + + if self.attached_to_obj is not None: + return True if self.attached_to_obj.title == "" else False + def can_comment(self, user): return user is not None and self.is_participant(user) @@ -247,14 +252,14 @@ def get_autocompletes(self, user): return self._get_autocompletes @property - def get_task(self, discussion): + def get_task(self): """ Retrieves the task for the given discussion's attached object. For example Projects have root_task while TaskAnswer discussion would have a task. """ - if hasattr(self.discussion.attached_to_obj, 'root_task'): - return discussion.attached_to_obj.root_task - elif hasattr(self.discussion.attached_to_obj, 'task'): - return discussion.attached_to_obj.task + if hasattr(self.attached_to_obj, 'root_task'): + return self.attached_to_obj.root_task + elif hasattr(self.attached_to_obj, 'task'): + return self.attached_to_obj.task else: return False From d6dd1c8e8d70ad56b89a4ac346be29775a11e6c8 Mon Sep 17 00:00:00 2001 From: davidpofo Date: Thu, 5 Aug 2021 14:41:48 -0400 Subject: [PATCH 3/4] Added get_discussion_autocompletes changes to get the organization from the discussion(should be 1 to 1). If another models wants to implement it they can look at the get_discussion_autocompletes in TaskAnswer --- discussion/models.py | 2 +- guidedmodules/models.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/discussion/models.py b/discussion/models.py index 3a4096b2c..2a022739e 100644 --- a/discussion/models.py +++ b/discussion/models.py @@ -261,7 +261,7 @@ def get_task(self): elif hasattr(self.attached_to_obj, 'task'): return self.attached_to_obj.task else: - return False + return False#TODO: not sure what to do if the model isn't related to a Task class Comment(models.Model): discussion = models.ForeignKey(Discussion, related_name="comments", on_delete=models.CASCADE, help_text="The Discussion that this comment is attached to.") diff --git a/guidedmodules/models.py b/guidedmodules/models.py index 5ba7f454f..33953432f 100644 --- a/guidedmodules/models.py +++ b/guidedmodules/models.py @@ -2008,12 +2008,12 @@ def get_notification_watchers(self): # required to attach a Discussion to it def get_discussion_autocompletes(self, discussion): # Get a list of all users who can be @-mentioned. It includes the discussion - # participants (i.e. people working on the same project and disussion guests) + # participants (i.e. people working on the same project and discussion guests) # plus anyone in the same organization. - organization = self.task.project.organization + organization = discussion.organization mentionable_users = set(discussion.get_all_participants()) \ | set( - User.objects.filter(projectmembership__project__organization=self.task.project.organization).distinct()) + User.objects.filter(projectmembership__project__organization=organization).distinct()) User.preload_profiles(mentionable_users) return { # @-mention participants in the discussion and other From 21e9fa11a63c46d4ad908849ed328717a83d43b8 Mon Sep 17 00:00:00 2001 From: davidpofo Date: Thu, 5 Aug 2021 15:06:31 -0400 Subject: [PATCH 4/4] Added get_discussion_autocompletes changes to get the organization from the discussion(should be 1 to 1). If another models wants to implement it they can look at the get_discussion_autocompletes in TaskAnswer --- discussion/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/discussion/views.py b/discussion/views.py index 6efd63ab7..23a288d9c 100644 --- a/discussion/views.py +++ b/discussion/views.py @@ -92,7 +92,8 @@ def edit_discussion_comment(request): # save comment.save() - + # TODO: fully implement this add it as an attribute to Discussion model perhaps? Otherwise to find out if the text of the + # Current comment is the same as the previous text you would do something like: comment.extra['history'][-1]['previous-text'] != comment.text # Kick the attached object. if hasattr(comment.discussion.attached_to, 'on_discussion_comment_edited'): comment.discussion.attached_to.on_discussion_comment_edited(comment)