예제 #1
0
def show_notifications_in_console(tasks):
    print('///////////////////////////////////////////')
    print('\t\tNOTIFICATIONS')
    print('///////////////////////////////////////////')
    print('')

    data = [[str(task.tid), task.title, task.description, 
            timestamp_to_display(task.supposed_start_time), 
            timestamp_to_display(task.supposed_end_time),
            timestamp_to_display(task.deadline_time)] for task in tasks]
    data.insert(0, ['TID', 'TITLE', 'DESCRIPTION', 'SUPPOSED_START', 'SUPPOSED_END', 'DEADLINE', 'NUMBER IN PLANS'])

    plan_controller = PlanController()
    for i in range(len(tasks)):
        task = tasks[i]

        plans = plan_controller.get_plan_for_common_task(task.tid)
        edited_plans = plan_controller.get_plan_for_edit_repeat_task(task.tid)

        all_plans = plans + edited_plans
        if len(all_plans) > 0:
            repeats = []
            for plan in all_plans:
                repeat = plan_controller.get_repeat_number_for_task(plan.plan_id, task)
                if repeat is not None:
                    repeats.append(str(repeat))
            numbers = ','.join(repeats)
            data[i+1].append(numbers)
        else:
            data[i+1].append(str(None))

    print_in_groups(data, 0)
예제 #2
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
예제 #3
0
def show_tasks_in_console(tasks, shift=0):
    data = [[str(task.tid), str(task.pid), str(task.parent_tid), task.title, task.description, 
            timestamp_to_display(task.supposed_start_time), 
            timestamp_to_display(task.supposed_end_time),
            timestamp_to_display(task.deadline_time),
            Priority.to_str(task.priority),
            Status.to_str(task.status),
            task.notificate_supposed_start,
            task.notificate_supposed_end,
            task.notificate_deadline] for task in tasks]
    data.insert(0, ['TID', 'PID', 'PARENT', 'TITLE', 'DESCRIPTION', 'SUPPOSED_START', 'SUPPOSED_END', 'DEADLINE',
                    'PRIORITY', 'STATUS', 'NOTIFICATE START', 'NOTIFICATE END', 'NOTIFICATE DEADLINE', 
                    'PLANNED (plan id)', 'EDIT PLANS (plan id)', 'NUMBER IN PLANS'])
    
    for i in range(len(tasks)):
        task = tasks[i]
        projects = ProjectController.fetch_projects(pid=task.pid)
        if projects is not None and len(projects) != 0:
            projects_str = ','.join([project.name for project in projects])
            data[i+1][1] += ' ({})'.format(projects_str)

    plan_controller = PlanController()
    for i in range(len(tasks)):
        task = tasks[i]
        plans = plan_controller.get_plan_for_common_task(task.tid)
        if len(plans) > 0:
            plans_ids = ','.join([str(plan.plan_id) for plan in plans])
            data[i+1].append(plans_ids)
        else:
            data[i+1].append(str(None))
        edited_plans = plan_controller.get_plan_for_edit_repeat_task(task.tid)
        if len(edited_plans) > 0:
            plans_ids = ','.join([str(plan.plan_id) for plan in edited_plans])
            data[i+1].append(plans_ids)
        else:
            data[i+1].append(str(None))

        all_plans = plans + edited_plans
        if len(all_plans) > 0:
            repeats = []
            for plan in all_plans:
                repeat = plan_controller.get_repeat_number_for_task(plan.plan_id, task)
                if repeat is not None:
                    repeats.append(str(repeat))
            numbers = ','.join(repeats)
            data[i+1].append(numbers)
        else:
            data[i+1].append(str(None))
        
    print_in_groups(data, shift)
예제 #4
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'))
예제 #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)
예제 #6
0
    def from_lib_plan(cls, controller, lib_plan):
        visual_plan = VisualPlanData()
        visual_plan.controller = controller

        visual_plan.plan_id = lib_plan.plan_id
        visual_plan.shift = lib_plan.shift
        visual_plan.end = lib_plan.end

        if lib_plan.exclude is not None and len(lib_plan.exclude) != 0:
            visual_plan.excludes = []
            for exclude in lib_plan.exclude:
                exclude_obj = cls.Exclude()
                exclude_obj.repeat = exclude
                exclude_obj.kind = PlanController(controller).get_exclude_type(
                    lib_plan.plan_id, exclude)
                if exclude_obj.kind == Plan.PlanExcludeKind.EDITED:
                    exclude_obj.tid = PlanController(
                        controller).get_tid_for_edit_repeat(
                            lib_plan.plan_id, exclude)
                    if exclude_obj.tid is not None:
                        exclude_obj.tid = exclude_obj.tid.tid
                time_range = PlanController(controller).get_time_for_repeat(
                    lib_plan.plan_id, exclude)
                if len(time_range) == 2:
                    exclude_obj.time = (VisualTaskData.timestamp_to_display(
                        time_range[0]),
                                        VisualTaskData.timestamp_to_display(
                                            time_range[1]))
                else:
                    exclude_obj.time = (VisualTaskData.timestamp_to_display(
                        time_range[0]), )
                visual_plan.excludes.append(exclude_obj)

        if visual_plan.shift is not None:
            visual_plan.shift = cls.shift_to_display(visual_plan.shift)

        return visual_plan
예제 #7
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)
예제 #8
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
    })
예제 #9
0
 def setUp(self):
     self.controller = PlanController()
예제 #10
0
class TestPlanController(unittest.TestCase):

    def setUp(self):
        self.controller = PlanController()

    def test_delete_repeats_from_plan_by_time_range(self):

        class PlanStorageAdapterMock():
            
            _deleted_numbers = []

            def get_plans(self, plan_id):
                plan = Plan()
                plan.tid = 1
                plan.plan_id = 1
                plan.shift = utils.create_shift_in_millis(datetime.timedelta(days=3))
                return [plan]

            def delete_plan_repeat(self, plan_id, number):
                if plan_id == 1:
                    self._deleted_numbers.append(number)
                    return True
                return False

            def get_tid_for_edit_repeat(self, plan_id, number):
                return None

            def get_exclude_type(self, plan_id, number):
                return None

        class TaskStorageAdapterMock():

            def get_tasks(self, filter):
                task = Task()
                task.tid = 1
                task.supposed_start_time = utils.datetime_to_milliseconds(datetime.datetime.today())
                task.supposed_end_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=3))
                return [task]

        class ProjectStorageAdapterMock():

            def get_projects(self, uid, name=None, pid=None):
                project = Project()
                project.creator = 1
                project.name = Project.default_project_name
                return [project]

        class UserStorageAdapterMock():

            def get_users(self, uid):
                user = User()
                return [user]

        Controller.init_storage_adapters(PlanStorageAdapterMock, 
            TaskStorageAdapterMock, UserStorageAdapterMock, ProjectStorageAdapterMock)
        Controller.authentication(1)

        time_range = (utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=10)),
                      utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=20)))
        success = self.controller.delete_repeats_from_plan_by_time_range(1, time_range)
        self.assertEqual(success, True)

        deleted_numbers = PlanStorageAdapterMock._deleted_numbers
        self.assertEqual(deleted_numbers, [3, 4, 5, 6])

    def test_get_repeats_by_time_range(self):

        class PlanStorageAdapterMock():
            
            _deleted_numbers = []

            def get_plans(self, plan_id):
                plan = Plan()
                plan.tid = 1
                plan.plan_id = 1
                plan.shift = utils.create_shift_in_millis(datetime.timedelta(days=3))
                plan.exclude = [4, 5]
                return [plan]

        class TaskStorageAdapterMock():

            def get_tasks(self, filter):
                task = Task()
                task.tid = 1
                task.supposed_start_time = utils.datetime_to_milliseconds(datetime.datetime.today())
                task.supposed_end_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=3))
                return [task]

        class ProjectStorageAdapterMock():

            def get_projects(self, uid, name=None, pid=None):
                project = Project()
                project.creator = 1
                project.name = Project.default_project_name
                return [project]

        class UserStorageAdapterMock():

            def get_users(self, uid):
                user = User()
                return [user]

        Controller.init_storage_adapters(PlanStorageAdapterMock, 
            TaskStorageAdapterMock, UserStorageAdapterMock, ProjectStorageAdapterMock)
        Controller.authentication(1)

        time_range = (utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=10)),
                      utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=20)))
        repeats = self.controller.get_repeats_by_time_range(1, time_range)
        self.assertEqual(repeats, [3, 6])

    def test_edit_repeat_by_number(self):

        class PlanStorageAdapterMock():
            
            _plan_id = None
            _number = None
            _tid = None

            def get_plans(self, plan_id=None, common_tid=None):
                plan = Plan()
                plan.tid = 1
                plan.plan_id = 1
                plan.shift = utils.create_shift_in_millis(datetime.timedelta(days=3))
                plan.exclude = [5]
                return [plan]

            def edit_plan_repeat(self, plan_id, number, tid):
                self.__class__._plan_id = plan_id
                self.__class__._number = number
                self.__class__._tid = tid
                return True

            def get_tid_for_edit_repeat(self, plan_id, number):
                return 2

        class TaskStorageAdapterMock():

            _saved_task = None

            _removed_tid = None

            class Filter():

                def tid(self, tid):
                    self._tid = tid

                def uid(self, uid):
                    self._uid = uid

            def get_tasks(self, filter):
                tid = getattr(filter, '_tid', None)
                if tid is not None and tid == 1:
                    task = Task()
                    task.tid = 1
                    task.status = Status.PENDING
                    task.supposed_start_time = utils.datetime_to_milliseconds(datetime.datetime.today())
                    task.supposed_end_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=2))
                    task.notificate_supposed_start=False
                    return [task]
                if tid is not None and tid == 2:
                    task = Task()
                    task.tid = 2
                    task.status = Status.PENDING
                    task.supposed_start_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=9))
                    task.supposed_end_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=11))
                    task.notificate_deadline=True
                    return [task]
                return []

            def remove_task(self, tid):
                self.__class__._removed_tid = tid
                return True

            def save_task(self, task):
                self.__class__._saved_task = task
                return True

            def get_last_saved_task(self):
                self._saved_task.tid = 3
                return self._saved_task

        class ProjectStorageAdapterMock():

            def get_projects(self, uid, name=None, pid=None):
                project = Project()
                project.creator = 1
                project.name = Project.default_project_name
                return [project]

        class UserStorageAdapterMock():

            def get_users(self, uid):
                user = User()
                return [user]

        Controller.init_storage_adapters(PlanStorageAdapterMock, 
            TaskStorageAdapterMock, UserStorageAdapterMock, ProjectStorageAdapterMock)
        Controller.authentication(1)

        success = self.controller.edit_repeat_by_number(1, 3, status=Status.COMPLETED,
                                              notificate_supposed_start=True)
        self.assertEqual(success, True)

        task = Task()
        task.tid = 3
        task.status = Status.COMPLETED
        task.supposed_start_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=9))
        task.supposed_end_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=11))
        task.notificate_supposed_start=True

        if task.supposed_start_time - TaskStorageAdapterMock._saved_task.supposed_start_time == 1:
            task.supposed_start_time -= 1
        if task.supposed_end_time - TaskStorageAdapterMock._saved_task.supposed_end_time == 1:
            task.supposed_end_time -= 1

        self.assertEqual(TaskStorageAdapterMock._saved_task.__dict__, task.__dict__)
        self.assertEqual(TaskStorageAdapterMock._removed_tid, 2)
        self.assertEqual(PlanStorageAdapterMock._plan_id, 1)
        self.assertEqual(PlanStorageAdapterMock._number, 3)
        self.assertEqual(PlanStorageAdapterMock._tid, 3)

    def test_get_repeats_by_time_range(self):

        class PlanStorageAdapterMock():
            
            def get_plans(self, plan_id):
                plan = Plan()
                plan.tid = 1
                plan.plan_id = 1
                plan.shift = utils.create_shift_in_millis(datetime.timedelta(days=3))
                plan.exclude = [5]
                plan.end = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=10))
                return [plan]

        class TaskStorageAdapterMock():

            _saved_task = None

            def get_tasks(self, filter):
                task = Task()
                task.tid = 1
                task.status = Status.PENDING
                task.supposed_start_time = utils.datetime_to_milliseconds(datetime.datetime.today())
                task.supposed_end_time = utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=2))
                task.notificate_supposed_start=False
                return [task]

        class ProjectStorageAdapterMock():

            def get_projects(self, uid, name=None, pid=None):
                project = Project()
                project.creator = 1
                project.name = Project.default_project_name
                return [project]

        class UserStorageAdapterMock():

            def get_users(self, uid):
                user = User()
                return [user]

        Controller.init_storage_adapters(PlanStorageAdapterMock, 
            TaskStorageAdapterMock, UserStorageAdapterMock, ProjectStorageAdapterMock)
        Controller.authentication(1)

        time_range = (utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=8)),
                    utils.shift_datetime_in_millis(datetime.datetime.today(), datetime.timedelta(days=30)))
        repeats = self.controller.get_repeats_by_time_range(1, time_range)
        self.assertEqual(repeats, [2, 3])
예제 #11
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
        })
예제 #12
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
    })
예제 #13
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})