Exemple #1
0
def test_send_email_to_thread_participants(todo_setup, django_user_model,
                                           email_backend_setup):
    """For a given task authored by one user, add comments by two other users.
    Notification email should be sent to all three users.

    Для данной задачи, созданной одним пользователем, добавьте комментарии двух других пользователей.
    Уведомление по электронной почте должно быть отправлено всем трем пользователям.
    """

    u1 = django_user_model.objects.get(username="******")
    task = Task.objects.filter(created_by=u1).first()

    u3 = django_user_model.objects.create_user(username="******",
                                               password="******",
                                               email="*****@*****.**")
    u4 = django_user_model.objects.create_user(username="******",
                                               password="******",
                                               email="*****@*****.**")
    Comment.objects.create(author=u3, task=task, body="Hello")
    Comment.objects.create(author=u4, task=task, body="Hello")

    send_email_to_thread_participants(task, "test body", u1)
    assert len(mail.outbox) == 1  # One message to multiple recipients
    assert "*****@*****.**" in mail.outbox[0].recipients()
    assert "*****@*****.**" in mail.outbox[0].recipients()
    assert "*****@*****.**" in mail.outbox[0].recipients()
Exemple #2
0
def test_send_email_to_thread_participants(todo_setup, django_user_model,
                                           email_backend_setup):
    """For a given task authored by one user, add comments by two other users.
    Notification email should be sent to all three users."""

    u1 = django_user_model.objects.get(username="******")
    task = Task.objects.filter(created_by=u1).first()

    u3 = django_user_model.objects.create_user(username="******",
                                               password="******",
                                               email="*****@*****.**")
    u4 = django_user_model.objects.create_user(username="******",
                                               password="******",
                                               email="*****@*****.**")
    Comment.objects.create(
        author=u3,
        task=task,
        body="Hello",
    )
    Comment.objects.create(
        author=u4,
        task=task,
        body="Hello",
    )

    send_email_to_thread_participants(task, "test body", u1)
    assert len(mail.outbox) == 1  # One message to multiple recipients
    assert '*****@*****.**' in mail.outbox[0].recipients()
    assert '*****@*****.**' in mail.outbox[0].recipients()
    assert '*****@*****.**' in mail.outbox[0].recipients()
Exemple #3
0
def handle_add_comment(request, task):
    if not request.POST.get("add_comment"):
        return

    Comment.objects.create(
        author=request.user, task=task, body=bleach.clean(request.POST["comment-body"], strip=True)
    )

    send_email_to_thread_participants(
        task,
        request.POST["comment-body"],
        request.user,
        subject='New comment posted on task "{}"'.format(task.title),
    )

    messages.success(request, "Comment posted. Notification email sent to thread participants.")
Exemple #4
0
def test_send_email_to_thread_participants(todo_setup, django_user_model, email_backend_setup):
    """For a given task authored by one user, add comments by two other users.
    Notification email should be sent to all three users."""

    u1 = django_user_model.objects.get(username="******")
    task = Task.objects.filter(created_by=u1).first()

    u3 = django_user_model.objects.create_user(
        username="******", password="******", email="*****@*****.**"
    )
    u4 = django_user_model.objects.create_user(
        username="******", password="******", email="*****@*****.**"
    )
    Comment.objects.create(author=u3, task=task, body="Hello")
    Comment.objects.create(author=u4, task=task, body="Hello")

    send_email_to_thread_participants(task, "test body", u1)
    assert len(mail.outbox) == 1  # One message to multiple recipients
    assert "*****@*****.**" in mail.outbox[0].recipients()
    assert "*****@*****.**" in mail.outbox[0].recipients()
    assert "*****@*****.**" in mail.outbox[0].recipients()
Exemple #5
0
def task_detail(request, task_id: int) -> HttpResponse:
    """View task details. Allow task details to be edited. Process new comments on task.
    """

    task = get_object_or_404(Task, pk=task_id)
    comment_list = Comment.objects.filter(task=task_id)

    # Ensure user has permission to view task. Admins can view all tasks.
    # Get the group this task belongs to, and check whether current user is a member of that group.
    if task.task_list.group not in request.user.groups.all(
    ) and not request.user.is_staff:
        raise PermissionDenied

    # Save submitted comments
    if request.POST.get("add_comment"):
        Comment.objects.create(
            author=request.user,
            task=task,
            body=bleach.clean(request.POST["comment-body"], strip=True),
        )

        send_email_to_thread_participants(
            task,
            request.POST["comment-body"],
            request.user,
            subject='New comment posted on task "{}"'.format(task.title),
        )
        messages.success(
            request,
            "Comment posted. Notification email sent to thread participants.")

    # Save task edits
    if request.POST.get("add_edit_task"):
        form = AddEditTaskForm(request.user,
                               request.POST,
                               instance=task,
                               initial={"task_list": task.task_list})

        if form.is_valid():
            item = form.save(commit=False)
            item.note = bleach.clean(form.cleaned_data["note"], strip=True)
            item.save()
            messages.success(request, "The task has been edited.")
            return redirect("todo:list_detail",
                            list_id=task.task_list.id,
                            list_slug=task.task_list.slug)
    else:
        form = AddEditTaskForm(request.user,
                               instance=task,
                               initial={"task_list": task.task_list})

    # Mark complete
    if request.POST.get("toggle_done"):
        results_changed = toggle_task_completed(task.id)
        if results_changed:
            messages.success(request,
                             f"Changed completion status for task {task.id}")

        return redirect("todo:task_detail", task_id=task.id)

    if task.due_date:
        thedate = task.due_date
    else:
        thedate = datetime.datetime.now()

    context = {
        "task": task,
        "comment_list": comment_list,
        "form": form,
        "thedate": thedate
    }

    return render(request, "todo/task_detail.html", context)
Exemple #6
0
def task_detail(request, task_id: int) -> HttpResponse:
    """View task details. Allow task details to be edited. Process new comments on task.
    """

    task = get_object_or_404(Task, pk=task_id)
    comment_list = Comment.objects.filter(task=task_id)

    # Ensure user has permission to view task. Admins can view all tasks.
    # Get the group this task belongs to, and check whether current user is a member of that group.
    if task.task_list.group not in request.user.groups.all(
    ) and not request.user.is_staff:
        raise PermissionDenied

    # Save submitted comments
    if request.POST.get('add_comment'):
        Comment.objects.create(
            author=request.user,
            task=task,
            body=request.POST['comment-body'],
        )

        email_subject = render_to_string("todo/email/newcomment_subject.txt",
                                         {'task': task.title})
        send_email_to_thread_participants(task,
                                          request.POST['comment-body'],
                                          request.user,
                                          subject=email_subject)
        messages.success(
            request,
            _("Comment posted. Notification email sent to thread participants."
              ))

    # Save task edits
    if request.POST.get('add_edit_task'):
        form = AddEditTaskForm(request.user,
                               request.POST,
                               instance=task,
                               initial={'task_list': task.task_list})

        if form.is_valid():
            form.save()
            messages.success(request, _("The task has been edited."))
            return redirect('todo:list_detail',
                            list_id=task.task_list.id,
                            list_slug=task.task_list.slug)
    else:
        form = AddEditTaskForm(request.user,
                               instance=task,
                               initial={'task_list': task.task_list})

    # Mark complete
    if request.POST.get('toggle_done'):
        results_changed = toggle_done([
            task.id,
        ])
        for res in results_changed:
            messages.success(request, res)

        return redirect(
            'todo:task_detail',
            task_id=task.id,
        )

    if task.due_date:
        thedate = task.due_date
    else:
        thedate = datetime.datetime.now()

    context = {
        "task": task,
        "comment_list": comment_list,
        "form": form,
        "thedate": thedate,
    }

    return render(request, 'todo/task_detail.html', context)