Skip to content

Commit

Permalink
Merge pull request breatheco-de#1410 from jefer94/fix/mentorship-survey
Browse files Browse the repository at this point in the history
fix mentorship survey, update signals, add vs extensions to codespace…
  • Loading branch information
tommygonzaleza authored Jul 17, 2024
2 parents 21d9201 + 5a94582 commit 463453f
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 173 deletions.
9 changes: 7 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
"eamodio.gitlens",
"gruntfuggly.todo-tree",
"redhat.vscode-yaml",
"bungcip.better-toml",
"ms-python.black-formatter",
"ms-python.isort",
"janisdd.vscode-edit-csv",
"tamasfe.even-better-toml",
"ms-python.flake8",
"donjayamanne.githistory",
"TabNine.tabnine-vscode",
"github.vscode-github-actions"
"ms-python.debugpy"
]
}
},
Expand Down
13 changes: 6 additions & 7 deletions breathecode/assessment/signals.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this:"""

from django import dispatch
from task_manager.django.dispatch import Emisor

assessment_updated = dispatch.Signal()
userassessment_status_updated = dispatch.Signal()
emisor = Emisor("breathecode.assessment")

assessment_updated = emisor.signal("assessment_updated")
userassessment_status_updated = emisor.signal("userassessment_status_updated")
27 changes: 25 additions & 2 deletions breathecode/assignments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@
from breathecode.services.learnpack import LearnPack

from .actions import sync_student_tasks
from .models import AssignmentTelemetry, CohortProxy, FinalProject, LearnPackWebhook, Task, UserAttachment, UserProxy
from .models import (
AssignmentTelemetry,
CohortProxy,
FinalProject,
LearnPackWebhook,
RepositoryDeletionOrder,
RepositoryWhiteList,
Task,
UserAttachment,
UserProxy,
)

# Register your models here.
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -125,7 +135,6 @@ def async_process_hook(modeladmin, request, queryset):
def process_hook(modeladmin, request, queryset):
# stay this here for use the poor mocking system
for hook in queryset.all().order_by("created_at"):
print(f"Procesing hook: {hook.id}")
client = LearnPack()
try:
client.execute_action(hook.id)
Expand Down Expand Up @@ -166,3 +175,17 @@ def from_status(s):
return ""

return format_html(f"<div><span class='badge'>{obj.status}</span></div><small>{obj.status_text}</small>")


@admin.register(RepositoryDeletionOrder)
class RepositoryDeletionOrderAdmin(admin.ModelAdmin):
list_display = ("provider", "status", "repository_user", "repository_name")
search_fields = ["repository_user", "repository_name"]
list_filter = ["provider", "status"]


@admin.register(RepositoryWhiteList)
class RepositoryWhiteListAdmin(admin.ModelAdmin):
list_display = ("provider", "repository_user", "repository_name")
search_fields = ["repository_user", "repository_name"]
list_filter = ["provider"]
15 changes: 7 additions & 8 deletions breathecode/assignments/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this:"""

from django import dispatch
from task_manager.django.dispatch import Emisor

assignment_created = dispatch.Signal()
assignment_status_updated = dispatch.Signal()
revision_status_updated = dispatch.Signal()
emisor = Emisor("breathecode.assignments")

assignment_created = emisor.signal("assignment_created")
assignment_status_updated = emisor.signal("assignment_status_updated")
revision_status_updated = emisor.signal("revision_status_updated")
16 changes: 9 additions & 7 deletions breathecode/authenticate/signals.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django import dispatch
from task_manager.django.dispatch import Emisor

emisor = Emisor("breathecode.authenticate")

# UserInvite accepted
invite_status_updated = dispatch.Signal()
invite_status_updated = emisor.signal("invite_status_updated")
# ProfileAcademy accepted
academy_invite_accepted = dispatch.Signal()
profile_academy_saved = dispatch.Signal()
academy_invite_accepted = emisor.signal("academy_invite_accepted")
profile_academy_saved = emisor.signal("profile_academy_saved")

# post_delete and post_save for User, ProfileAcademy and MentorProfileMentorProfile
user_info_updated = dispatch.Signal()
user_info_deleted = dispatch.Signal()
user_info_updated = emisor.signal("user_info_updated")
user_info_deleted = emisor.signal("user_info_deleted")

cohort_user_deleted = dispatch.Signal()
cohort_user_deleted = emisor.signal("cohort_user_deleted")
3 changes: 2 additions & 1 deletion breathecode/feedback/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging

from django.apps import AppConfig

logger = logging.getLogger(__name__)
Expand All @@ -8,5 +9,5 @@ class FeedbackConfig(AppConfig):
name = "breathecode.feedback"

def ready(self):
logger.debug("Loading feedback.receivers")
from . import receivers # noqa: F401
from . import supervisors # noqa: F401
6 changes: 3 additions & 3 deletions breathecode/feedback/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from breathecode.admissions.models import CohortUser
from breathecode.admissions.signals import student_edu_status_updated
from breathecode.mentorship.models import MentorshipSession
from breathecode.mentorship.signals import mentorship_session_status
from breathecode.mentorship.signals import mentorship_session_saved

from .models import Answer
from .signals import survey_answered
Expand All @@ -33,9 +33,9 @@ def post_save_cohort_user(sender, instance, **kwargs):
process_student_graduation.delay(instance.cohort.id, instance.user.id)


@receiver(mentorship_session_status, sender=MentorshipSession)
@receiver(mentorship_session_saved, sender=MentorshipSession)
def post_mentorin_session_ended(sender: Type[MentorshipSession], instance: MentorshipSession, **kwargs):
if instance.status == "COMPLETED":
if instance.status == "COMPLETED" and Answer.objects.filter(mentorship_session__id=instance.id).exists() is False:
duration = timedelta(seconds=0)
if instance.started_at is not None and instance.ended_at is not None:
duration = instance.ended_at - instance.started_at
Expand Down
11 changes: 5 additions & 6 deletions breathecode/feedback/signals.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
For each signal you want other apps to be able to receive, you have to
declare a new variable here like this:
"""
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this:"""

from django import dispatch
from task_manager.django.dispatch import Emisor

emisor = Emisor("breathecode.feedback")

# when a student answers one particular questions of a survey
survey_answered = dispatch.Signal()
survey_answered = emisor.signal("survey_answered")
40 changes: 28 additions & 12 deletions breathecode/feedback/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from datetime import timedelta

from django.contrib.auth.models import User
from django.core.cache import cache
from django.utils import timezone
from django_redis import get_redis_connection
from redis.exceptions import LockError
from task_manager.core.exceptions import AbortTask, RetryTask
from task_manager.django.decorators import task

Expand All @@ -11,6 +14,7 @@
from breathecode.mentorship.models import MentorshipSession
from breathecode.notify import actions as notify_actions
from breathecode.utils import TaskPriority, getLogger
from breathecode.utils.redis import Lock
from capyc.rest_framework.exceptions import ValidationException

from . import actions
Expand All @@ -23,6 +27,7 @@
ADMIN_URL = os.getenv("ADMIN_URL", "")
API_URL = os.getenv("API_URL", "")
ENV = os.getenv("ENV", "")
IS_DJANGO_REDIS = hasattr(cache, "delete_pattern")


def build_question(answer):
Expand Down Expand Up @@ -291,19 +296,30 @@ def send_mentorship_session_survey(session_id, **_):
if not session.service:
raise AbortTask("Mentorship session doesn't have a service associated with it")

answer = Answer.objects.filter(mentorship_session__id=session.id).first()
if answer is None:
answer = Answer(mentorship_session=session, academy=session.mentor.academy, lang=session.service.language)
question = build_question(answer)
answer.title = question["title"]
answer.lowest = question["lowest"]
answer.highest = question["highest"]
answer.user = session.mentee
answer.status = "SENT"
answer.save()
client = None
if IS_DJANGO_REDIS:
client = get_redis_connection("default")

elif answer.status == "ANSWERED":
raise AbortTask(f"This survey about MentorshipSession {session.id} was answered")
try:
with Lock(client, f"lock:session:{session.id}:answer", timeout=30, blocking_timeout=30):
answer = Answer.objects.filter(mentorship_session__id=session.id).first()
if answer is None:
answer = Answer(
mentorship_session=session, academy=session.mentor.academy, lang=session.service.language
)
question = build_question(answer)
answer.title = question["title"]
answer.lowest = question["lowest"]
answer.highest = question["highest"]
answer.user = session.mentee
answer.status = "SENT"
answer.save()

elif answer.status == "ANSWERED":
raise AbortTask(f"This survey about MentorshipSession {session.id} was answered")

except LockError:
raise RetryTask("Could not acquire lock for activity, operation timed out.")

if not session.mentee.email:
message = f"Author not have email, this survey cannot be send by {session.mentee.id}"
Expand Down
7 changes: 5 additions & 2 deletions breathecode/mentorship/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,13 @@ def save(self, *args, **kwargs):

is_creating = self.pk is None

super().save(*args, **kwargs) # Call the "real" save() method.
super().save(*args, **kwargs)

if is_creating or self.__old_status != self.status:
signals.mentorship_session_status.send_robust(instance=self, sender=MentorshipSession)
signals.mentorship_session_status.delay(instance=self, sender=MentorshipSession)

# we're testing why there hasn't any mentorship session with a survey changing mentorship_session_status by mentorship_session_saved
signals.mentorship_session_saved.delay(instance=self, sender=MentorshipSession)

self.__old_status = self.status

Expand Down
11 changes: 8 additions & 3 deletions breathecode/mentorship/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from django import dispatch
"""For each signal you want other apps to be able to receive, you have to declare a new variable here like this:"""

mentorship_session_status = dispatch.Signal()
mentor_profile_saved = dispatch.Signal()
from task_manager.django.dispatch import Emisor

emisor = Emisor("breathecode.mentorship")

mentorship_session_status = emisor.signal("mentorship_session_status")
mentor_profile_saved = emisor.signal("mentor_profile_saved")
mentorship_session_saved = emisor.signal("mentorship_session_saved")
Loading

0 comments on commit 463453f

Please sign in to comment.