Skip to content

Views

hypha.apply.todo.views

TodoListView

Bases: ListView

model class-attribute instance-attribute

model = Task

template_name class-attribute instance-attribute

template_name = 'todo/todolist_dropdown.html'

get_queryset

get_queryset()
Source code in hypha/apply/todo/views.py
def get_queryset(self):
    tasks = render_task_templates_for_user(self.request, self.request.user)
    return tasks

get_context_data

get_context_data(**kwargs)
Source code in hypha/apply/todo/views.py
def get_context_data(self, **kwargs):
    ctx = super().get_context_data(**kwargs)
    ctx["my_tasks"] = {"data": self.get_queryset()}
    return ctx

TaskRemovalView

Bases: View

dispatch

dispatch(request, *args, **kwargs)
Source code in hypha/apply/todo/views.py
def dispatch(self, request, *args, **kwargs):
    self.task = get_object_or_404(Task, id=self.kwargs.get("pk"))
    if self.task.user == request.user or set(self.task.user_group.all()) == set(
        request.user.groups.all()
    ):
        return super().dispatch(request, *args, **kwargs)
    raise PermissionDenied("You can remove the tasks that are assigned to you.")

delete

delete(*args, **kwargs)
Source code in hypha/apply/todo/views.py
def delete(self, *args, **kwargs):
    source = self.task.related_object
    from hypha.apply.activity.models import Activity
    from hypha.apply.determinations.models import Determination
    from hypha.apply.projects.models import Invoice
    from hypha.apply.review.models import Review

    if isinstance(self.task.related_object, Invoice):
        source = self.task.related_object.project
    elif isinstance(self.task.related_object, Determination) or isinstance(
        self.task.related_object, Review
    ):
        source = self.task.related_object.submission
    elif isinstance(self.task.related_object, Activity):
        source = self.task.related_object.source
    messenger(
        MESSAGES.REMOVE_TASK,
        user=self.request.user,
        request=self.request,
        source=source,
        related=self.task,
    )
    self.task.delete()
    tasks = render_task_templates_for_user(self.request, self.request.user)
    response = render(
        self.request,
        "dashboard/includes/my-tasks.html",
        context={"my_tasks": {"data": tasks}},
    )
    trigger_client_event(response, "taskListUpdated", {})
    return response

add_task_to_user

add_task_to_user(code, user, related_obj)

Add task for a user input: code: TASKS_CODE_CHOICES.keys() user: User object related_obj: Object - Submission, Project, Invoice, Report output: task - Task object / None in case of no creation

Source code in hypha/apply/todo/views.py
def add_task_to_user(code, user, related_obj):
    """
    Add task for a user
    input:
        code: TASKS_CODE_CHOICES.keys()
        user: User object
        related_obj: Object - Submission, Project, Invoice, Report
    output: task - Task object / None in case of no creation
    """
    user_uniqueness = validate_user_uniqueness(
        code=code, user=user, related_obj=related_obj
    )
    if user_uniqueness:
        task = Task.objects.create(code=code, user=user, related_object=related_obj)
        return task
    return None

add_task_to_user_group

add_task_to_user_group(code, user_group, related_obj)

Add task for user_groups input: code: TASKS_CODE_CHOICES.keys() user_group: Queryset - Group objects related_obj: Object - Submission, Project, Invoice, Report output: task - Task object / None in case of no creation

Source code in hypha/apply/todo/views.py
def add_task_to_user_group(code, user_group, related_obj):
    """
    Add task for user_groups
    input:
        code: TASKS_CODE_CHOICES.keys()
        user_group: Queryset - Group objects
        related_obj: Object - Submission, Project, Invoice, Report
    output: task - Task object / None in case of no creation
    """
    user_groups_uniqueness = validate_user_groups_uniqueness(
        code=code, user_groups=user_group, related_obj=related_obj
    )
    if user_groups_uniqueness:
        task = Task.objects.create(code=code, related_object=related_obj)
        groups = [Group.objects.filter(id=group.id).first() for group in user_group]
        task.user_group.add(*groups)
        return task
    return None

remove_tasks_for_user

remove_tasks_for_user(code, user, related_obj)

Remove task for a user input: code: TASKS_CODE_CHOICES.keys() user: User object related_obj: Object - Submission, Project, Invoice, Report output: None

Source code in hypha/apply/todo/views.py
def remove_tasks_for_user(code, user, related_obj):
    """
    Remove task for a user
    input:
        code: TASKS_CODE_CHOICES.keys()
        user: User object
        related_obj: Object - Submission, Project, Invoice, Report
    output: None
    """
    task = Task.objects.filter(
        code=code,
        user=user,
        related_content_type=ContentType.objects.get_for_model(related_obj).id,
        related_object_id=related_obj.id,
    ).first()
    if task:
        task.delete()
    return None

remove_tasks_for_user_group

remove_tasks_for_user_group(code, user_group, related_obj)

Remove task for user_groups input: code: TASKS_CODE_CHOICES.keys() user_group: Queryset - Group objects related_obj: Object - Submission, Project, Invoice, Report output: None

Source code in hypha/apply/todo/views.py
def remove_tasks_for_user_group(code, user_group, related_obj):
    """
    Remove task for user_groups
    input:
        code: TASKS_CODE_CHOICES.keys()
        user_group: Queryset - Group objects
        related_obj: Object - Submission, Project, Invoice, Report
    output: None
    """
    matching_tasks = Task.objects.filter(
        code=code,
        related_content_type=ContentType.objects.get_for_model(related_obj).id,
        related_object_id=related_obj.id,
    )
    user_group_matching_tasks = matching_tasks.annotate(
        group_count=Count("user_group")
    ).filter(group_count=len(user_group.all()))
    for group in user_group.all():
        user_group_matching_tasks = user_group_matching_tasks.filter(
            user_group__id=group.id
        )
    if user_group_matching_tasks.exists():
        user_group_matching_tasks.delete()
    return None
remove_tasks_of_related_obj_for_specific_code(code, related_obj)

Remove all tasks of a related object with the provide code irrespective to their users input: code: TASKS_CODE_CHOICES.keys() related_obj: Object - Submission, Project, Invoice, Report

Source code in hypha/apply/todo/views.py
def remove_tasks_of_related_obj_for_specific_code(code, related_obj):
    """
    Remove all tasks of a related object with the provide code irrespective to their users
    input:
        code: TASKS_CODE_CHOICES.keys()
        related_obj: Object - Submission, Project, Invoice, Report
    """
    Task.objects.filter(
        code=code,
        related_content_type=ContentType.objects.get_for_model(related_obj).id,
        related_object_id=related_obj.id,
    ).delete()
    return None
remove_tasks_of_related_obj(related_obj)

Remove all tasks of a related object irrespective of their code and users input: related_obj: Object - Submission, Project, Invoice, Report

Source code in hypha/apply/todo/views.py
def remove_tasks_of_related_obj(related_obj):
    """
    Remove all tasks of a related object irrespective of their code and users
    input:
        related_obj: Object - Submission, Project, Invoice, Report
    """
    Task.objects.filter(
        related_content_type=ContentType.objects.get_for_model(related_obj).id,
        related_object_id=related_obj.id,
    ).delete()
    return None

get_tasks_for_user

get_tasks_for_user(user)
Source code in hypha/apply/todo/views.py
def get_tasks_for_user(user):
    user_tasks = Task.objects.filter(user=user).annotate(
        group_count=Count("user_group")
    )
    user_group_tasks = Task.objects.annotate(group_count=Count("user_group")).filter(
        group_count=len(user.groups.all())
    )
    for group in user.groups.all():
        user_group_tasks = user_group_tasks.filter(user_group__id=group.id)

    return user_tasks.union(user_group_tasks)

render_task_templates_for_user

render_task_templates_for_user(request, user)
[{

"text":"", "icon":"", "url":"", "type":"",

     },
    ]
Source code in hypha/apply/todo/views.py
def render_task_templates_for_user(request, user):
    """
    input: request (HttpRequest)
    input: user   (User object)

    output: [{
                 "text":"",
                 "icon":"",
                 "url":"",
                 "type":"",
             },
            ]
    """
    tasks = get_tasks_for_user(user)
    templates = [get_task_template(request, task=task) for task in tasks]

    return list(filter(None, templates))