def toggle_done(request, task_id: int) -> HttpResponse: """Toggle the completed status of a task from done to undone, or vice versa. Redirect to the list from which the task came. Переключите статус выполненной задачи с "done" на "undone" или наоборот. Перенаправление в список, из которого пришло задание. """ if request.method == "POST": task = get_object_or_404(Task, pk=task_id) redir_url = reverse( "todo:list_detail", kwargs={ "list_id": task.task_list.id, "list_slug": task.task_list.slug }, ) # Permissions if not ((task.created_by == request.user) or (request.user.is_superuser) or (task.assigned_to == request.user) or (task.task_list.group in request.user.groups.all())): raise PermissionDenied toggle_task_completed(task.id) messages.success(request, "Task status changed for '{}'".format(task.title)) return redirect(redir_url) else: raise PermissionDenied
def toggle_done(request, task_id: int) -> HttpResponse: """Toggle the completed status of a task from done to undone, or vice versa. Redirect to the list from which the task came. """ task = get_object_or_404(Task, pk=task_id) # Permissions if not ((request.user.is_superuser) or (task.created_by == request.user) or (task.assigned_to == request.user) or (task.task_list.group in request.user.groups.all())): raise PermissionDenied toggle_task_completed(task.id) messages.success(request, "Task status changed for '{}'".format(task.title)) return redirect( reverse( "todo:list_detail", kwargs={ "list_id": task.task_list.id, "list_slug": task.task_list.slug }, ))
def toggle_done(request, task_id: int) -> HttpResponse: """Toggle the completed status of a task from done to undone, or vice versa. Redirect to the list from which the task came. """ if request.method == "POST": task = get_object_or_404(Task, pk=task_id) redir_url = reverse( "todo:list_detail", kwargs={"list_id": task.task_list.id, "list_slug": task.task_list.slug}, ) # Permissions if not ( (task.created_by == request.user) or (request.user.is_superuser) or (task.assigned_to == request.user) or (task.task_list.group in request.user.groups.all()) ): raise PermissionDenied toggle_task_completed(task.id) messages.success(request, "Task status changed for '{}'".format(task.title)) return redirect(redir_url) else: raise PermissionDenied
def toggle_done(request, task_id: int) -> HttpResponse: """Toggle the completed status of a task from done to undone, or vice versa. Redirect to the list from which the task came. """ if request.method == "POST": task = get_object_or_404(Task, pk=task_id) # Permissions # pai if not user_can_toggle_task_done(request.user, task): # raise PermissionDenied messages.error( request, _("Can not change completion status for ") + '"' + task.title + '".') return redirect(reverse("todo:task_detail", args=(task.pk, ))) _redir_url = reverse("todo:list_detail", kwargs={ "list_id": task.task_list.id, "list_slug": task.task_list.slug }) # prevent toggle for specific actions if (request.GET.get('done', False) and task.completed) or \ (request.GET.get('not_done', False) and not task.completed): # task has changed completion status while user was looking at page messages.error( request, _("Can not change completion status for ") + '"' + task.title + '".') redir_url = _redir_url else: try: toggle_task_completed(task.id, user=request.user) except Exception as e: messages.error( request, _("Can not change completion status.") + ' ' + str(e)) # redirect to task detail redir_url = reverse("todo:task_detail", args=(task.pk, )) else: messages.success( request, _("Task completion status changed for ") + '"' + task.title + '".') redir_url = request.GET.get('next', _redir_url) return redirect(redir_url) else: return HttpResponseNotAllowed(permitted_methods=('post', 'options'))
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).order_by('-date') # 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 not user_can_read_task(task, request.user): raise PermissionDenied # Handle task merging if not HAS_TASK_MERGE: merge_form = None else: class MergeForm(forms.Form): merge_target = forms.ModelChoiceField( queryset=Task.objects.all(), widget=autocomplete.ModelSelect2( url=reverse("todo:task_autocomplete", kwargs={"task_id": task_id}) ), ) # Handle task merging if not request.POST.get("merge_task_into"): merge_form = MergeForm() else: merge_form = MergeForm(request.POST) if merge_form.is_valid(): merge_target = merge_form.cleaned_data["merge_target"] if not user_can_read_task(merge_target, request.user): raise PermissionDenied task.merge_into(merge_target) return redirect(reverse( "todo:task_detail", kwargs={"task_id": merge_target.pk} )) # Save submitted comments handle_add_comment(request, task) # Save task edits if not request.POST.get("add_edit_task"): form = AddEditTaskForm( request.user, instance=task, initial={"task_list": task.task_list} ) else: 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, ) # 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, "merge_form": merge_form, "thedate": thedate, "comment_classes": getattr(settings, 'TODO_COMMENT_CLASSES', []), } return render(request, "todo/task_detail.html", context)
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)
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).order_by("-date") # Ensure user has permission to view task. Superusers can view all tasks. # Get the group this task belongs to, and check whether current user is a member of that group. if not user_can_read_task(task, request.user): raise PermissionDenied # Handle task merging if not HAS_TASK_MERGE: merge_form = None else: class MergeForm(forms.Form): merge_target = forms.ModelChoiceField( queryset=Task.objects.all(), widget=autocomplete.ModelSelect2( url=reverse("todo:task_autocomplete", kwargs={"task_id": task_id}) ), ) # Handle task merging if not request.POST.get("merge_task_into"): merge_form = MergeForm() else: merge_form = MergeForm(request.POST) if merge_form.is_valid(): merge_target = merge_form.cleaned_data["merge_target"] if not user_can_read_task(merge_target, request.user): raise PermissionDenied task.merge_into(merge_target) return redirect(reverse("todo:task_detail", kwargs={"task_id": merge_target.pk})) # Save submitted comments handle_add_comment(request, task) # Save task edits if not request.POST.get("add_edit_task"): form = AddEditTaskForm(request.user, instance=task, initial={"task_list": task.task_list}) else: 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 ) # 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() # Handle uploaded files if request.FILES.get("attachment_file_input"): file = request.FILES.get("attachment_file_input") if file.size > defaults("TODO_MAXIMUM_ATTACHMENT_SIZE"): messages.error(request, f"File exceeds maximum attachment size.") return redirect("todo:task_detail", task_id=task.id) name, extension = os.path.splitext(file.name) if extension not in defaults("TODO_LIMIT_FILE_ATTACHMENTS"): messages.error(request, f"This site does not allow upload of {extension} files.") return redirect("todo:task_detail", task_id=task.id) Attachment.objects.create( task=task, added_by=request.user, timestamp=datetime.datetime.now(), file=file ) messages.success(request, f"File attached successfully") return redirect("todo:task_detail", task_id=task.id) context = { "task": task, "comment_list": comment_list, "form": form, "merge_form": merge_form, "thedate": thedate, "comment_classes": defaults("TODO_COMMENT_CLASSES"), "attachments_enabled": defaults("TODO_ALLOW_FILE_ATTACHMENTS"), } return render(request, "todo/task_detail.html", context)
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).order_by("-date") # Ensure user has permission to view task. Superusers can view all tasks. # Убедитесь, что у пользователя есть разрешение на просмотр задачи. Суперпользователи могут просматривать все задачи. # Get the group this task belongs to, and check whether current user is a member of that group. # Получите группу, к которой принадлежит эта задача, и проверьте, является ли текущий пользователь членом этой группы. if not user_can_read_task(task, request.user): raise PermissionDenied # Handle task merging # Обрабатывать слияние задач (скрыто) if not HAS_TASK_MERGE: merge_form = None else: class MergeForm(forms.Form): merge_target = forms.ModelChoiceField( queryset=Task.objects.all(), widget=autocomplete.ModelSelect2( url=reverse("todo:task_autocomplete", kwargs={"task_id": task_id}) ), ) # Handle task merging # Обрабатывать слияние задач (скрыто) if not request.POST.get("merge_task_into"): merge_form = MergeForm() else: merge_form = MergeForm(request.POST) if merge_form.is_valid(): merge_target = merge_form.cleaned_data["merge_target"] if not user_can_read_task(merge_target, request.user): raise PermissionDenied task.merge_into(merge_target) return redirect(reverse("todo:task_detail", kwargs={"task_id": merge_target.pk})) # Save submitted comments # Сохранить отправленные комментарии handle_add_comment(request, task) # Save task edits # Сохранение изменений задачи if not request.POST.get("add_edit_task"): form = AddEditTaskForm(request.user, instance=task, initial={"task_list": task.task_list}) else: 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.title = bleach.clean(form.cleaned_data["title"], 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 ) # 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() # Handle uploaded files # Обрабатывать загруженные файлы if request.FILES.get("attachment_file_input"): file = request.FILES.get("attachment_file_input") if file.size > defaults("TODO_MAXIMUM_ATTACHMENT_SIZE"): messages.error(request, f"File exceeds maximum attachment size.") return redirect("todo:task_detail", task_id=task.id) name, extension = os.path.splitext(file.name) if extension not in defaults("TODO_LIMIT_FILE_ATTACHMENTS"): messages.error(request, f"This site does not allow upload of {extension} files.") return redirect("todo:task_detail", task_id=task.id) Attachment.objects.create( task=task, added_by=request.user, timestamp=datetime.datetime.now(), file=file ) messages.success(request, f"File attached successfully") return redirect("todo:task_detail", task_id=task.id) context = { "task": task, "comment_list": comment_list, "form": form, "merge_form": merge_form, "thedate": thedate, "comment_classes": defaults("TODO_COMMENT_CLASSES"), "attachments_enabled": defaults("TODO_ALLOW_FILE_ATTACHMENTS"), } return render(request, "todo/task_detail.html", context)