Example #1
0
    def from_lib_task(cls, controller, lib_task, depth=None):
        visual_task = VisualTaskData()
        visual_task.controller = controller

        visual_task.tid = lib_task.tid
        visual_task.uid = lib_task.uid
        visual_task.pid = lib_task.pid
        visual_task.parent_tid = lib_task.parent_tid
        visual_task.title = lib_task.title
        visual_task.description = lib_task.description
        visual_task.priority = lib_task.priority
        visual_task.status = lib_task.status
        visual_task.notificate_supposed_start = lib_task.notificate_supposed_start
        visual_task.notificate_supposed_end = lib_task.notificate_supposed_end
        visual_task.notificate_deadline = lib_task.notificate_deadline

        visual_task.supposed_start_time = cls.timestamp_to_display(
            lib_task.supposed_start_time)
        visual_task.supposed_end_time = cls.timestamp_to_display(
            lib_task.supposed_end_time)
        visual_task.deadline_time = cls.timestamp_to_display(
            lib_task.deadline_time)

        if lib_task.parent_tid is not None:
            parent_tasks = TaskController(controller).fetch_tasks(
                tid=lib_task.parent_tid)
            if parent_tasks is not None and len(parent_tasks) != 0:
                visual_task.parent = parent_tasks[0]

        child_tasks = TaskController(controller).fetch_tasks(
            parent_tid=lib_task.tid)
        if child_tasks is not None and len(child_tasks) != 0:
            visual_task.childs = child_tasks

        if depth is not None:
            visual_task.depth = depth

        plans = PlanController(controller).get_plan_for_common_task(
            lib_task.tid)
        if plans is not None and len(plans) != 0:
            visual_task.plan = VisualPlanData.from_lib_plan(
                controller, plans[0])
            visual_task.common = True
        edit_plans = PlanController(controller).get_plan_for_edit_repeat_task(
            lib_task.tid)
        if edit_plans is not None and len(edit_plans) != 0:
            visual_task.plan = VisualPlanData.from_lib_plan(
                controller, edit_plans[0])
            visual_task.edit = True

        plans = plans if plans is not None and len(plans) != 0 else edit_plans
        if plans is not None and len(plans) != 0:
            repeat = PlanController(controller).get_repeat_number_for_task(
                plans[0].plan_id, lib_task)
            visual_task.repeat = repeat

        return visual_task
Example #2
0
def delete_task(controller, request, task_id):
    parts = task_id.split('_')
    if len(parts) == 1:

        task_id = int(task_id)
        tasks = TaskController(controller).fetch_tasks(tid=task_id)
        if tasks is None or len(tasks) == 0:
            raise Http404

        TaskController(controller).remove_task(task_id)

        pid = request.session.get('pid')
        if pid is not None:
            return HttpResponseRedirect(
                reverse('core:project_tasks', args=[pid]))
        else:
            return HttpResponseRedirect(reverse('core:projects_list'))
    if len(parts) == 3:
        parts = task_id.split('_')
        repeat = None
        plan_id = None
        if len(parts) == 1:
            tid = int(tid)
        if len(parts) == 3:
            tid = int(parts[0])
            plan_id = int(parts[1])
            repeat = int(parts[2])

        task_id = int(parts[0])
        tasks = TaskController(controller).fetch_tasks(tid=task_id)
        if tasks is None or len(tasks) == 0:
            raise Http404

        repeat = int(parts[1])

        plans = PlanController(controller).get_plan_for_common_task(task_id)
        if plans is None or len(plans) == 0:
            raise Http404

        PlanController(controller).delete_repeats_from_plan_by_number(
            plan_id, repeat)

        pid = request.session.get('pid')
        if pid is not None:
            return HttpResponseRedirect(
                reverse('core:project_tasks', args=[pid]))
        else:
            return HttpResponseRedirect(reverse('core:projects_list'))
Example #3
0
def edit_task(controller, request, task_id):
    parts = task_id.split('_')
    repeat = None
    plan_id = None
    if len(parts) == 1:
        task_id = int(task_id)
    if len(parts) == 3:
        task_id = int(parts[0])
        plan_id = int(parts[1])
        repeat = int(parts[2])

    tasks = TaskController(controller).fetch_tasks(tid=task_id)
    if tasks is None or len(tasks) == 0:
        raise Http404
    initial_task = tasks[0]

    def on_task_edited(title, description, priority, supposed_start,
                       supposed_end, deadline, project_pid,
                       shift_milliseconds):
        if plan_id is None:
            if initial_task.status == Status.OVERDUE:
                TaskController(controller).edit_task(
                    task_id,
                    pid=project_pid,
                    title=title,
                    description=description,
                    priority=priority,
                    supposed_start_time=supposed_start,
                    supposed_end_time=supposed_end,
                    deadline_time=deadline,
                    status=Status.PENDING)
            else:
                TaskController(controller).edit_task(
                    task_id,
                    pid=project_pid,
                    title=title,
                    description=description,
                    priority=priority,
                    supposed_start_time=supposed_start,
                    supposed_end_time=supposed_end,
                    deadline_time=deadline)

            if shift_milliseconds is not None and shift_milliseconds != 0:
                plans = PlanController(controller).get_plan_for_common_task(
                    task_id)
                if plans is None or len(plans) == 0:
                    tid = TaskController(controller).get_last_saved_task_tid()
                    PlanController(controller).attach_plan(
                        tid, shift_milliseconds)
                else:
                    plan = plans[0]
                    PlanController(controller).edit_plan(
                        plan.plan_id, shift=shift_milliseconds)
        else:
            if Priority.to_str(initial_task.priority) != priority:
                PlanController(controller).edit_repeat_by_number(
                    plan_id, repeat, priority=priority)

    return _task_change_common(controller, request, on_task_edited, plan_id,
                               repeat, initial_task)
Example #4
0
    def on_task_created(title, description, priority, supposed_start,
                        supposed_end, deadline, project_pid,
                        shift_milliseconds):
        parent_task = request.session.get('parent_task')
        TaskController(controller).save_task(pid=project_pid,
                                             parent_tid=parent_task,
                                             title=title,
                                             description=description,
                                             priority=priority,
                                             supposed_start=supposed_start,
                                             supposed_end=supposed_end,
                                             deadline_time=deadline)

        if shift_milliseconds is not None and shift_milliseconds != 0:
            tid = TaskController(controller).get_last_saved_task_tid()
            PlanController(controller).attach_plan(tid, shift_milliseconds)
Example #5
0
    def on_task_edited(title, description, priority, supposed_start,
                       supposed_end, deadline, project_pid,
                       shift_milliseconds):
        if plan_id is None:
            if initial_task.status == Status.OVERDUE:
                TaskController(controller).edit_task(
                    task_id,
                    pid=project_pid,
                    title=title,
                    description=description,
                    priority=priority,
                    supposed_start_time=supposed_start,
                    supposed_end_time=supposed_end,
                    deadline_time=deadline,
                    status=Status.PENDING)
            else:
                TaskController(controller).edit_task(
                    task_id,
                    pid=project_pid,
                    title=title,
                    description=description,
                    priority=priority,
                    supposed_start_time=supposed_start,
                    supposed_end_time=supposed_end,
                    deadline_time=deadline)

            if shift_milliseconds is not None and shift_milliseconds != 0:
                plans = PlanController(controller).get_plan_for_common_task(
                    task_id)
                if plans is None or len(plans) == 0:
                    tid = TaskController(controller).get_last_saved_task_tid()
                    PlanController(controller).attach_plan(
                        tid, shift_milliseconds)
                else:
                    plan = plans[0]
                    PlanController(controller).edit_plan(
                        plan.plan_id, shift=shift_milliseconds)
        else:
            if Priority.to_str(initial_task.priority) != priority:
                PlanController(controller).edit_repeat_by_number(
                    plan_id, repeat, priority=priority)
Example #6
0
def project_tasks(controller, request, project_id):
    request.session['pid'] = project_id

    if request.session.get('parent_task') is not None:
        request.session.pop('parent_task')

    if request.method == "POST":
        up_status = request.POST.get('status_up')
        if up_status is not None:
            status, tid = tuple(up_status.split(','))
            status = Status.to_str(Status.raise_status(
                Status.from_str(status)))
            TaskController(controller).edit_task(tid, status=status)
        down_status = request.POST.get('status_down')
        if down_status is not None:
            status, tid = tuple(down_status.split(','))
            status = Status.to_str(
                Status.downgrade_status(Status.from_str(status)))
            TaskController(controller).edit_task(tid, status=status)
        back = request.POST.get('back')
        if back is not None:
            return HttpResponseRedirect(reverse('core:projects_list'))
        add_subtask = request.POST.get('add_subtask')
        if add_subtask is not None:
            request.session['parent_task'] = add_subtask
            return HttpResponseRedirect(reverse('core:add_task'))

    projects = ProjectController(controller).fetch_projects(pid=project_id)
    back_text = None
    if projects is not None and len(projects) != 0:
        back_text = projects[0].name

    tasks = TaskController(controller).fetch_tasks(pid=project_id)
    tasks = list(reversed(tasks))
    visual_tasks = [
        VisualTaskData.from_lib_task(controller, task) for task in tasks
    ]
    return render(request, 'core/projects_task_list.html', {
        'tasks': visual_tasks,
        'back_text': back_text
    })
Example #7
0
def show_task(controller, request, task_id):
    if request.session.get('parent_task') is not None:
        request.session.pop('parent_task')

    tasks = TaskController(controller).fetch_tasks(tid=task_id)
    if tasks is None or len(tasks) == 0:
        raise Http404

    main_task = VisualTaskData.from_lib_task(controller, tasks[0])

    if request.method == "POST":
        up_status = request.POST.get('status_up')
        if up_status is not None:
            status, tid = tuple(up_status.split(','))
            status = Status.to_str(Status.raise_status(
                Status.from_str(status)))
            TaskController(controller).edit_task(tid, status=status)
        down_status = request.POST.get('status_down')
        if down_status is not None:
            status, tid = tuple(down_status.split(','))
            status = Status.to_str(
                Status.downgrade_status(Status.from_str(status)))
            TaskController(controller).edit_task(tid, status=status)
        add_subtask = request.POST.get('add_subtask')
        if add_subtask is not None:
            request.session['parent_task'] = add_subtask
            return HttpResponseRedirect(reverse('core:add_task'))
        restore = request.POST.get('restore')
        if restore is not None:
            success = PlanController(controller).restore_repeat(
                main_task.plan.plan_id, int(restore))
            main_task = VisualTaskData.from_lib_task(controller, tasks[0])

    child_tasks = []
    build_task_tree(controller, main_task, child_tasks)

    return render(request, 'core/show_task.html', {
        'main_task': main_task,
        'child_tasks': child_tasks
    })
 def setUp(self):
     self.controller = TaskController()
Example #9
0
def _task_change_common(controller,
                        request,
                        on_task_changed,
                        plan_id=None,
                        repeat=None,
                        initial_task=None):
    def to_utc(datetime_object):
        if datetime_object is not None:
            return datetime_object.replace(tzinfo=None)

    def timestamp_to_display(timestamp):
        if timestamp is not None:
            return datetime.datetime.utcfromtimestamp(
                timestamp / 1000.0).strftime('%d-%m-%Y %H:%M')

    projects = ProjectController(controller).fetch_projects()
    visual_projects = [
        VisualProjectData.from_lib_project(controller, project)
        for project in projects
    ]
    VisualProjectData.normalize_visual_names(visual_projects, controller)

    if request.method == "POST":
        form = TaskForm(data=request.POST)

        cancel_parent = request.POST.get('cancel_parent')
        if cancel_parent is not None:
            request.session.pop('parent_task')
        elif form.is_valid():
            title = form.cleaned_data.get('title')
            description = form.cleaned_data.get('description')
            priority = form.cleaned_data.get('priority')
            supposed_start = form.cleaned_data.get('supposed_start')
            supposed_end = form.cleaned_data.get('supposed_end')
            deadline = form.cleaned_data.get('deadline')
            project_pid = int(form.cleaned_data.get('project'))

            shift_milliseconds = 0
            plan_minute = form.cleaned_data.get('plan_minute')
            if plan_minute is not None:
                plan_minute = int(plan_minute)
                shift_milliseconds += plan_minute * 60000
            plan_hour = form.cleaned_data.get('plan_hour')
            if plan_hour is not None:
                plan_hour = int(plan_hour)
                shift_milliseconds += plan_hour * 60 * 60000
            plan_day = form.cleaned_data.get('plan_day')
            if plan_day is not None:
                plan_day = int(plan_day)
                shift_milliseconds += plan_day * 24 * 60 * 60000
            plan_month = form.cleaned_data.get('plan_month')
            if plan_month is not None:
                plan_month = int(plan_month)
                shift_milliseconds += plan_month * 30 * 24 * 60 * 60000
            plan_year = form.cleaned_data.get('plan_year')
            if plan_year is not None:
                plan_year = int(plan_year)
                shift_milliseconds += plan_year * 365 * 30 * 24 * 60 * 60000

            print('t', supposed_end)

            supposed_start = to_utc(supposed_start)
            supposed_end = to_utc(supposed_end)
            deadline = to_utc(deadline)

            print('t', supposed_end)

            supposed_start = lib_utils.datetime_to_milliseconds(supposed_start)
            supposed_end = lib_utils.datetime_to_milliseconds(supposed_end)
            deadline = lib_utils.datetime_to_milliseconds(deadline)

            print('t', timestamp_to_display(supposed_end))

            on_task_changed(title, description, priority, supposed_start,
                            supposed_end, deadline, project_pid,
                            shift_milliseconds)

            return HttpResponseRedirect(
                reverse('core:project_tasks', args=[project_pid]))
    else:
        form = TaskForm()
        if initial_task is not None:
            form.fields['title'].initial = initial_task.title
            form.fields['description'].initial = initial_task.description
            form.fields['priority'].initial = Priority.to_str(
                initial_task.priority)

            supposed_start = VisualTaskData.timestamp_to_display(
                initial_task.supposed_start_time)
            supposed_end = VisualTaskData.timestamp_to_display(
                initial_task.supposed_end_time)
            deadline = VisualTaskData.timestamp_to_display(
                initial_task.deadline_time)

            form.fields['supposed_start'].initial = supposed_start
            form.fields['supposed_end'].initial = supposed_end
            form.fields['deadline'].initial = deadline

            form.fields['project'].initial = str(initial_task.pid)

            plans = PlanController(controller).get_plan_for_common_task(
                initial_task.tid)
            if plans is not None and len(plans) != 0:
                years, months, days, hours, minutes = VisualPlanData.parse_shift(
                    plans[0].shift)
                if minutes != 0:
                    form.fields['plan_minute'].initial = minutes
                if hours != 0:
                    form.fields['plan_hour'].initial = hours
                if days != 0:
                    form.fields['plan_day'].initial = days
                if months != 0:
                    form.fields['plan_month'].initial = months
                if years != 0:
                    form.fields['plan_year'].initial = years
        else:
            pid = request.session.get('pid')
            if pid is not None:
                form.fields['project'].initial = str(pid)
            else:
                for project in projects:
                    if project.name == Project.default_project_name:
                        form.fields['project'].initial = str(project.pid)
                        break

    parent_task = None
    if initial_task is None:
        parent_tid = request.session.get('parent_task')
        if parent_tid is not None:
            lib_task = TaskController(controller).fetch_tasks(tid=parent_tid)
            if lib_task is not None and len(lib_task) != 0:
                parent_task = VisualTaskData.from_lib_task(
                    controller, lib_task[0])
    else:
        if request.session.get('parent_task') is not None:
            request.session.pop('parent_task')

    return render(
        request, 'core/add_task.html', {
            'form': form,
            'projects': visual_projects,
            'parent_task': parent_task,
            'repeat': repeat
        })
Example #10
0
def search(controller, request):
    def to_utc(datetime_object):
        if datetime_object is not None:
            return datetime_object.replace(tzinfo=None)

    if request.session.get('parent_task') is not None:
        request.session.pop('parent_task')

    projects = ProjectController(controller).fetch_projects()
    visual_projects = [
        VisualProjectData.from_lib_project(controller, project)
        for project in projects
    ]
    VisualProjectData.normalize_visual_names(visual_projects, controller)

    if request.method == "POST":
        up_status = request.POST.get('status_up')
        if up_status is not None:
            status, tid = tuple(up_status.split(','))
            status = Status.to_str(Status.raise_status(
                Status.from_str(status)))
            parts = tid.split('_')
            repeat = None
            plan_id = None
            if len(parts) == 1:
                tid = int(tid)
            if len(parts) == 3:
                tid = int(parts[0])
                plan_id = int(parts[1])
                repeat = int(parts[2])
            if repeat is None:
                TaskController(controller).edit_task(tid, status=status)
            else:
                PlanController(controller).edit_repeat_by_number(plan_id,
                                                                 repeat,
                                                                 status=status)
        down_status = request.POST.get('status_down')
        if down_status is not None:
            status, tid = tuple(down_status.split(','))
            status = Status.to_str(
                Status.downgrade_status(Status.from_str(status)))
            parts = tid.split('_')
            repeat = None
            plan_id = None
            if len(parts) == 1:
                tid = int(tid)
            if len(parts) == 3:
                tid = int(parts[0])
                plan_id = int(parts[1])
                repeat = int(parts[2])
            if repeat is None:
                TaskController(controller).edit_task(tid, status=status)
            else:
                PlanController(controller).edit_repeat_by_number(plan_id,
                                                                 repeat,
                                                                 status=status)
        add_subtask = request.POST.get('add_subtask')
        if add_subtask is not None:
            request.session['parent_task'] = add_subtask
            return HttpResponseRedirect(reverse('core:add_task'))

        form = TaskSearchForm(data=request.POST)
        if form.is_valid():
            title = form.cleaned_data.get('title')
            if len(title) == 0:
                title = None

            description = form.cleaned_data.get('description')
            if len(description) == 0:
                description = None

            priority_list = form.cleaned_data.get('priority')
            if len(priority_list) == 0:
                priority_list = None
            else:
                priority_list = [
                    Priority.from_str(priority) for priority in priority_list
                ]

            status_list = form.cleaned_data.get('status')
            if len(status_list) == 0:
                status_list = None
            else:
                status_list = [
                    Status.from_str(status) for status in status_list
                ]

            supposed_start = form.cleaned_data.get('supposed_start')
            supposed_end = form.cleaned_data.get('supposed_end')
            deadline = form.cleaned_data.get('deadline')

            project_pid = request.POST.getlist('project')
            form.projects = project_pid

            supposed_start = to_utc(supposed_start)
            supposed_end = to_utc(supposed_end)
            deadline = to_utc(deadline)

            supposed_start = lib_utils.datetime_to_milliseconds(supposed_start)
            supposed_end = lib_utils.datetime_to_milliseconds(supposed_end)
            deadline = lib_utils.datetime_to_milliseconds(deadline)

            time_range = utils.get_time_range(supposed_start, supposed_end,
                                              deadline)

            print('TEST', time_range)

            timeless = form.cleaned_data.get('timeless')
            if timeless == 'true':
                timeless = True
            else:
                timeless = None

            if project_pid is None or len(project_pid) == 0:

                tasks = TaskController(controller).fetch_tasks(
                    title=title,
                    description=description,
                    priority=priority_list,
                    status=status_list,
                    time_range=time_range,
                    timeless=timeless)
            else:
                project_pid = [int(p) for p in project_pid]
                tasks = []
                for p in project_pid:
                    tasks += TaskController(controller).fetch_tasks(
                        pid=p,
                        title=title,
                        description=description,
                        priority=priority_list,
                        status=status_list,
                        time_range=time_range,
                        timeless=timeless)
        else:
            tasks = TaskController(controller).fetch_tasks()
    else:
        form = TaskSearchForm()
        tasks = TaskController(controller).fetch_tasks()

    tasks = list(reversed(tasks))
    visual_tasks = [
        VisualTaskData.from_lib_task(controller, task) for task in tasks
    ]

    return render(request, 'core/search.html', {
        'form': form,
        'tasks': visual_tasks,
        'projects': visual_projects
    })
Example #11
0
def home(controller, request):
    if request.session.get('parent_task') is not None:
        request.session.pop('parent_task')

    now = lib_utils.datetime_to_milliseconds(lib_utils.now())

    TaskController(controller).find_overdue_tasks(now)

    if request.method == "POST":
        up_status = request.POST.get('status_up')
        if up_status is not None:
            status, tid = tuple(up_status.split(','))
            status = Status.to_str(Status.raise_status(
                Status.from_str(status)))
            parts = tid.split('_')
            repeat = None
            plan_id = None
            if len(parts) == 1:
                tid = int(tid)
            if len(parts) == 3:
                tid = int(parts[0])
                plan_id = int(parts[1])
                repeat = int(parts[2])
            if repeat is None:
                TaskController(controller).edit_task(tid, status=status)
            else:
                PlanController(controller).edit_repeat_by_number(plan_id,
                                                                 repeat,
                                                                 status=status)
        down_status = request.POST.get('status_down')
        if down_status is not None:
            status, tid = tuple(down_status.split(','))
            status = Status.to_str(
                Status.downgrade_status(Status.from_str(status)))
            parts = tid.split('_')
            repeat = None
            plan_id = None
            if len(parts) == 1:
                tid = int(tid)
            if len(parts) == 3:
                tid = int(parts[0])
                plan_id = int(parts[1])
                repeat = int(parts[2])
            if repeat is None:
                TaskController(controller).edit_task(tid, status=status)
            else:
                PlanController(controller).edit_repeat_by_number(plan_id,
                                                                 repeat,
                                                                 status=status)
        add_subtask = request.POST.get('add_subtask')
        if add_subtask is not None:
            request.session['parent_task'] = add_subtask
            return HttpResponseRedirect(reverse('core:add_task'))

    time_range = (lib_utils.datetime_to_milliseconds(lib_utils.today()),
                  lib_utils.shift_datetime_in_millis(
                      lib_utils.today(), datetime.timedelta(days=1)))
    tasks = []
    tasks += TaskController(controller).get_overdue_tasks(now)
    tasks += TaskController(controller).fetch_tasks(time_range=time_range)
    tasks += TaskController(controller).fetch_tasks(timeless=True)
    for i, task in enumerate(tasks):
        for j in range(len(tasks) - 1, i, -1):
            if task.tid == tasks[j].tid and task.status == Status.OVERDUE:
                del tasks[j]

    tasks = filter_status(tasks)

    visual_tasks = [
        VisualTaskData.from_lib_task(controller, task) for task in tasks
    ]

    return render(request, 'core/home.html', {'tasks': visual_tasks})
Example #12
0
def build_task_tree(controller, root, tree, depth=0):
    lib_tasks = TaskController(controller).fetch_tasks(parent_tid=root.tid)
    for task in lib_tasks:
        tree.append(VisualTaskData.from_lib_task(controller, task, 30 * depth))
        build_task_tree(controller, task, tree, depth + 1)