Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activité : ajouter un lien dans l'affichage admin #1926

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions activity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse
from django.utils import timezone

from core.utils import slack
Expand Down Expand Up @@ -77,17 +78,49 @@ class Meta:
verbose_name = "Événement"
verbose_name_plural = "Événements"

@property
def get_event_object_admin_absolute_url(self):
if self.event_object_type == "QUESTION":
return reverse("questions:detail_view", kwargs={"pk": self.event_object_id})
elif self.event_object_type == "QUIZ":
return reverse("quizs:detail_view", kwargs={"pk": self.event_object_id})
return ""

@property
def display_html(self) -> str:
if self.event_object_type in ["QUESTION", "QUIZ"]:
# Prénom Nom a créé la question 'Question'
return self.display_full
return self.display_question_quiz_with_admin_url_html
elif self.event_object_type in ["USER"]:
return f"{self.display_event_emoji} Nouveau contributeur ! <strong>{self.event_object_name}</strong>"
return self.display_new_user_html

@property
def display_question_quiz_html(self) -> str:
"""
💡 <i>Prénom Nom</i> a créé la question <strong>test</strong>
"""
return (
f"{self.display_event_emoji} "
f"<i>{self.actor_name}</i> a {self.get_event_verb_display().lower()} "
f"{self.display_event_object_type_prefix} {self.get_event_object_type_display().lower()} "
f"<strong>{self.event_object_name}</strong>"
)

@property
def display_question_quiz_with_admin_url_html(self) -> str:
"""
💡 <i>Prénom Nom</i> a créé la question <a href="/questions/<id>/view/">test</a>
"""
return (
f"{self.display_event_emoji} "
f"<i>{self.actor_name}</i> a {self.get_event_verb_display().lower()} "
f"{self.display_event_object_type_prefix} {self.get_event_object_type_display().lower()} "
f'<a href="{self.get_event_object_admin_absolute_url}">{self.event_object_name}</a>'
)

@property
def display_full(self) -> str:
return f"{self.display_event_emoji} <i>{self.actor_name}</i> a {self.get_event_verb_display().lower()} {self.display_event_object_type_prefix} {self.get_event_object_type_display().lower()} <strong>{self.event_object_name}</strong>" # noqa
def display_new_user_html(self) -> str:
return f"{self.display_event_emoji} Nouveau contributeur ! <strong>{self.event_object_name}</strong>"

@property
def display_event_object_type_prefix(self) -> str:
Expand All @@ -112,4 +145,4 @@ def display_event_emoji(self):
@receiver(post_save, sender=Event)
def send_event_to_slack(sender, instance, created, **kwargs):
if created:
slack.send_message_to_webhook(instance.display_full, webhook_url=settings.ACTIVITY_EVENT_WEBHOOK_URL)
slack.send_message_to_webhook(instance.display_html, webhook_url=settings.ACTIVITY_EVENT_WEBHOOK_URL)
2 changes: 1 addition & 1 deletion activity/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def create_event(user: User, event_verb: str, event_object, created=timezone.now
event_object_name = event_object.text
elif event_object_type in ["USER"]:
event_object_name = event_object.full_name
else:
else: # "QUIZ"
event_object_name = event_object.name
if len(event_object_name) > 150:
event_object_name = event_object_name[:147] + "..."
Expand Down