• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chiefonboarding / ChiefOnboarding / 6427167771

06 Oct 2023 03:33AM UTC coverage: 93.316% (-0.06%) from 93.378%
6427167771

Pull #364

github

web-flow
Merge 6cef66109 into 80bf55996
Pull Request #364: Allow trigger items on admin task (sequence)

5878 of 6299 relevant lines covered (93.32%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.26
back/admin/admin_tasks/models.py
1
from django.conf import settings
1✔
2
from django.db import models
1✔
3
from django.template.loader import render_to_string
1✔
4
from django.utils.translation import gettext as _
1✔
5

6
from slack_bot.utils import Slack, actions, button, paragraph
1✔
7

8
from .emails import (
1✔
9
    send_email_new_assigned_admin,
10
    send_email_new_comment,
11
    send_email_notification_to_external_person,
12
)
13

14

15
class AdminTask(models.Model):
1✔
16
    class Priority(models.IntegerChoices):
1✔
17
        EMAIL = 1, _("Low")
1✔
18
        MEDIUM = 2, _("Medium")
1✔
19
        HIGH = 3, _("High")
1✔
20

21
    class Notification(models.IntegerChoices):
1✔
22
        NO = 0, _("No")
1✔
23
        EMAIL = 1, _("Email")
1✔
24
        SLACK = 2, _("Slack")
1✔
25

26
    new_hire = models.ForeignKey(
1✔
27
        settings.AUTH_USER_MODEL,
28
        verbose_name=_("New hire"),
29
        on_delete=models.CASCADE,
30
        related_name="new_hire_tasks",
31
    )
32
    assigned_to = models.ForeignKey(
1✔
33
        settings.AUTH_USER_MODEL,
34
        verbose_name=_("Assigned to"),
35
        on_delete=models.CASCADE,
36
        related_name="owner",
37
        blank=True,
38
        null=True,
39
    )
40
    name = models.CharField(verbose_name=_("Name"), max_length=500)
1✔
41
    option = models.IntegerField(
1✔
42
        verbose_name=_("Send email or text to extra user?"),
43
        choices=Notification.choices,
44
    )
45
    slack_user = models.ForeignKey(
1✔
46
        settings.AUTH_USER_MODEL,
47
        verbose_name=_("Slack user"),
48
        on_delete=models.SET_NULL,
49
        related_name="slack_user",
50
        blank=True,
51
        null=True,
52
    )
53
    email = models.EmailField(
1✔
54
        verbose_name=_("Email"), max_length=12500, default="", blank=True
55
    )
56
    completed = models.BooleanField(verbose_name=_("Completed"), default=False)
1✔
57
    date = models.DateField(verbose_name=_("Date"), blank=True, null=True)
1✔
58
    priority = models.IntegerField(
1✔
59
        verbose_name=_("Priority"), choices=Priority.choices, default=Priority.MEDIUM
60
    )
61
    based_on = models.ForeignKey(
1✔
62
        "sequences.PendingAdminTask",
63
        null=True,
64
        on_delete=models.SET_NULL,
65
        help_text="If generated through a sequence, then this will be filled",
66
    )
67

68
    @property
1✔
69
    def get_icon_template(self):
1✔
70
        return render_to_string("_admin_task_icon.html")
1✔
71

72
    def send_notification_third_party(self):
1✔
73
        if self.assigned_to is None:
1✔
74
            # Only happens when a sequence adds this with "manager" or "buddy" is
75
            # choosen option
76
            return
×
77
        if self.option == AdminTask.Notification.EMAIL:
1✔
78
            send_email_notification_to_external_person(self)
1✔
79
        elif self.option == AdminTask.Notification.SLACK:
1✔
80
            blocks = [
1✔
81
                paragraph(
82
                    _(
83
                        "%(name_assigned_to)s needs your help with this task:\n*"
84
                        "%(task_name)s*\n_%(comment)s_"
85
                    )
86
                    % {
87
                        "name_assigned_to": self.assigned_to.full_name,
88
                        "task_name": self.name,
89
                        "comment": self.comment.last().content,
90
                    }
91
                ),
92
                actions(
93
                    [
94
                        button(
95
                            text=_("I have completed this"),
96
                            style="primary",
97
                            value=str(self.pk),
98
                            action_id="admin_task:complete",
99
                        )
100
                    ]
101
                ),
102
            ]
103
            Slack().send_message(blocks, self.slack_user.slack_user_id)
1✔
104

105
    def send_notification_new_assigned(self):
1✔
106
        if self.assigned_to is None:
1✔
107
            # Only happens when a sequence adds this with "manager" or "buddy" is
108
            # choosen option
109
            return
×
110
        if self.assigned_to.has_slack_account:
1✔
111
            comment = ""
1✔
112
            if self.comment.all().exists():
1✔
113
                comment = _("_%(comment)s_\n by _%(name)s_") % {
1✔
114
                    "comment": self.comment.last().content,
115
                    "name": self.comment.last().comment_by.full_name,
116
                }
117

118
            text = _("You have just been assigned to *%(title)s* for *%(name)s*\n") % {
1✔
119
                "title": self.name,
120
                "name": self.new_hire.full_name,
121
            }
122
            text += comment
1✔
123
            blocks = [
1✔
124
                paragraph(text),
125
                actions(
126
                    [
127
                        button(
128
                            text=_("I have completed this"),
129
                            style="primary",
130
                            value=str(self.pk),
131
                            action_id="admin_task:complete",
132
                        )
133
                    ]
134
                ),
135
            ]
136
            Slack().send_message(
1✔
137
                blocks=blocks,
138
                channel=self.assigned_to.slack_user_id,
139
                text=_("New assigned task!"),
140
            )
141
        else:
142
            send_email_new_assigned_admin(self)
1✔
143

144
    def mark_completed(self):
1✔
145
        from admin.sequences.tasks import process_condition
1✔
146

147
        self.completed = True
1✔
148
        self.save()
1✔
149

150
        # Get conditions with this to do item as (part of the) condition
151
        conditions = self.new_hire.conditions.filter(
1✔
152
            condition_admin_tasks=self.based_on
153
        )
154

155
        for condition in conditions:
1✔
156
            condition_admin_tasks_id = condition.condition_admin_tasks.values_list(
1✔
157
                "id", flat=True
158
            )
159

160
            # Check if all admin to do items have been added to new hire and are
161
            # completed. If not, then we know it should not be triggered yet
162
            completed_tasks = AdminTask.objects.filter(
1✔
163
                based_on_id__in=condition_admin_tasks_id,
164
                new_hire=self.new_hire,
165
                completed=True,
166
            )
167

168
            # If the amount matches, then we should process it
169
            if completed_tasks.count() == len(condition_admin_tasks_id):
1✔
170
                # Send notification only if user has a slack account
171
                process_condition(
1✔
172
                    condition.id, self.new_hire.id, self.new_hire.has_slack_account
173
                )
174

175
    class Meta:
1✔
176
        ordering = ["completed", "date"]
1✔
177

178

179
class AdminTaskComment(models.Model):
1✔
180
    admin_task = models.ForeignKey(
1✔
181
        "AdminTask", on_delete=models.CASCADE, related_name="comment"
182
    )
183
    content = models.CharField(max_length=12500)
1✔
184
    date = models.DateTimeField(auto_now_add=True)
1✔
185
    comment_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
1✔
186

187
    class Meta:
1✔
188
        ordering = ["-date"]
1✔
189

190
    def send_notification_new_message(self):
1✔
191
        if self.admin_task.assigned_to != self.comment_by:
1✔
192
            if self.admin_task.assigned_to.has_slack_account:
1✔
193
                blocks = [
1✔
194
                    paragraph(
195
                        _(
196
                            "%(name)s added a message to your task:\n*"
197
                            "%(task_title)s*\n_%(comment)s_"
198
                        )
199
                        % {
200
                            "name": self.comment_by.full_name,
201
                            "task_title": self.admin_task.name,
202
                            "comment": self.content,
203
                        }
204
                    ),
205
                    actions(
206
                        [
207
                            button(
208
                                text=_("I have completed this"),
209
                                style="primary",
210
                                value=str(self.pk),
211
                                action_id="admin_task:complete",
212
                            )
213
                        ]
214
                    ),
215
                ]
216
                Slack().send_message(blocks, self.admin_task.assigned_to.slack_user_id)
1✔
217
            else:
218
                send_email_new_comment(self)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc