def remove_task(self, tid):
        plan_storage = PlanStorageAdapter(self.db_file, self.db)
        plans = plan_storage.get_plans(common_tid=tid)
        if plans is not None and len(plans) != 0:
            for plan in plans:
                success = plan_storage.remove_plan(plan.plan_id)
                print(success)
                if not success:
                    return success

        plans = plan_storage.get_plans(edit_repeat_tid=tid)
        if plans is not None and len(plans) != 0:
            for plan in plans:
                number = plan_storage.get_number_for_edit_repeat_by_tid(plan.plan_id, tid)
                success = plan_storage.restore_plan_repeat(plan.plan_id, number)
                print(success)
                if not success:
                    return False

        tasks_to_delete = [tid]
        while len(tasks_to_delete) != 0:
            tid_to_delete = tasks_to_delete.pop(0)
            rows_deleted = TaskTableModel.delete().where(TaskTableModel.tid == tid_to_delete).execute()
            if rows_deleted == 0:
                return False
            else:
                logging.get_logger(self._log_tag).info('Task was removed: {}'.format(tid_to_delete))

            child_tasks = TaskTableModel.select().where(TaskTableModel.parent_tid == tid_to_delete)
            for child_task in child_tasks:
                tasks_to_delete.append(child_task.tid)

        return True
 def remove_project(self, pid):
     ProjectRelationsTableModel.delete().where(ProjectRelationsTableModel.pid == pid).execute()
     rows_deleted = ProjectTableModel.delete().where(ProjectTableModel.pid == pid).execute()
     success = rows_deleted != 0
     if success:
         logging.get_logger(self._log_tag).info('Project {} was removed'.format(pid))
     return success
 def restore_all_repeats(self, plan_id):
     conditions = ((PlanRelationsTableModel.plan_id == plan_id) 
             & (PlanRelationsTableModel.kind != PlanRelationsTableModel.Kind.COMMON))
     rows_deleted = PlanRelationsTableModel.delete().where(conditions).execute()
     success = rows_deleted != 0
     if success:
         logging.get_logger(self._log_tag).info('All repeats in plan {} were restore'.format(plan_id))
     return success
 def restore_plan_repeat(self, plan_id, number):
     conditions = ((PlanRelationsTableModel.plan_id == plan_id)
         & (PlanRelationsTableModel.number == number))
     rows_deleted = PlanRelationsTableModel.delete().where(conditions).execute()
     success = rows_deleted != -1
     if success:
         logging.get_logger(self._log_tag).info('Repeat {} in plan {} was restored'.format(number, plan_id))    
     return success
 def add_guest_to_project(self, pid, uid):
     project_raltion_model = ProjectRelationsTableModel(pid=pid, 
                                 uid=uid, kind=ProjectRelationsTableModel.Kind.GUEST)
     rows_modified = project_raltion_model.save()
     success = rows_modified == 1
     if success:
         logging.get_logger(self._log_tag).info('Guest {} was invited in project {}'.format(uid, pid))
     return success
 def remove_guest_from_project(self, pid, uid):
     conditions = (ProjectRelationsTableModel.pid == pid & ProjectRelationsTableModel.uid == uid
                     & ProjectRelationsTableModel.kind == ProjectRelationsTableModel.Kind.GUEST)
     rows_deleted = ProjectRelationsTableModel.delete().where(conditions).execute()
     success = rows_deleted == 1
     if success:
         logging.get_logger(self._log_tag).info('Guest {} was removed from project {}'.format(uid, pid))
     return success
 def save_user(self, user):
     user_to_save = UserTableModel(login=user.login,
                         password=user.password,
                         online=user.online)
     rows_modified = user_to_save.save()
     success = rows_modified == 1
     if success:
         logging.get_logger(self._log_tag).info('User {} was saved'.format(user_to_save.__data__))
     return success
 def delete_plan_repeat(self, plan_id, number):
     plan_deleted_relations = PlanRelationsTableModel(plan_id=plan_id,
                             tid=None,
                             number=number,
                             kind=PlanRelationsTableModel.Kind.DELETED)
     rows_modified = plan_deleted_relations.save()
     success = rows_modified == 1
     if success:
         logging.get_logger(self._log_tag).info('Repeat {} was deleted in plan {}'.format(number, plan_id))
     return success
    def delete_user(self, uid):
        task_adapter = TaskStorageAdapter(self.db_file, self.db)
        filter = TaskStorageAdapter.Filter()
        filter.uid(uid)
        tasks = task_adapter.get_tasks(filter)
        for task in tasks:
            task_adapter.remove_task(task.tid)

        rows_deleted = UserTableModel.delete().where(UserTableModel.uid == uid).execute()
        success = rows_deleted == 1
        if success:
            logging.get_logger(self._log_tag).info('User {} was deleted'.format(uid))
        return success
 def edit_plan_repeat(self, plan_id, number, tid):
     type = self.get_exclude_type(plan_id, number)
     if type != None:
         self.restore_plan_repeat(plan_id, number)
     plan_deleted_relations = PlanRelationsTableModel(plan_id=plan_id,
                             tid=tid,
                             number=number,
                             kind=PlanRelationsTableModel.Kind.EDITED)
     rows_modified = plan_deleted_relations.save()
     success = rows_modified == 1
     if success:
         logging.get_logger(self._log_tag).info('Repeat {} in plan {} was edited: {}'\
             .format(number, plan_id, plan_deleted_relations.__data__))
     return success
    def remove_plan(self, plan_id):
        rows_deleted = PlanTableModel.delete().where(PlanTableModel.plan_id == plan_id).execute()
        relations = PlanRelationsTableModel.select().where(PlanRelationsTableModel.plan_id == plan_id)
        if len(relations) != 0:
            task_storage = TaskStorageAdapter(self.db_file, self.db)
            for relation in relations:
                if relation.tid is not None:
                    TaskTableModel.delete().where(TaskTableModel.tid == relation.tid).execute()

        PlanRelationsTableModel.delete().where(PlanRelationsTableModel.plan_id == plan_id).execute()
        success = rows_deleted == 1
        if success:
            logging.get_logger(self._log_tag).info('Plan {} was deleted'.format(plan_id))
        return success
    def edit_project(self, project_fields_dict):
        pid = project_fields_dict[Project.Field.pid]
        project_models = ProjectTableModel.select().where(ProjectTableModel.pid == pid)
        if len(project_models) == 0:
            return False
        project_model = project_models[0]
        if Project.Field.name in project_fields_dict:
            project_model.name = project_fields_dict[Project.Field.name]
            rows_modified = project_model.save()
            success = rows_modified == 1
            if success:
                logging.get_logger(self._log_tag).info('Project {} was edited'.format(project_model.__data__))
            return success

        return True
    def edit_user(self, user_field_dict):
        uid = user_field_dict[User.Field.uid]
        users_to_edit = UserTableModel.select().where(UserTableModel.uid == uid)
        if len(users_to_edit) == 0:
            return False
        user_to_edit = users_to_edit[0]
        for field, value in user_field_dict.items():
            if field == Task.Field.uid:
                continue
            user_to_edit.map_user_attr(field, value)
        rows_modified = user_to_edit.save()

        success = rows_modified == 1
        if success:
            logging.get_logger(self._log_tag).info('User {} was edited'.format(user_to_edit.__data__))
        return success
 def save_task(self, task, auto_tid=True):
     task_to_save = TaskTableModel(uid=task.uid,
                         pid=task.pid,
                         parent_tid=task.parent_tid,
                         title=task.title, 
                         description=task.description,
                         supposed_start_time=task.supposed_start_time,
                         supposed_end_time=task.supposed_end_time,
                         deadline_time=task.deadline_time,
                         priority=task.priority,
                         status=task.status,
                         notificate_supposed_start=task.notificate_supposed_start,
                         notificate_supposed_end=task.notificate_supposed_end,
                         notificate_deadline=task.notificate_deadline)
     rows_modified = task_to_save.save()
     success = rows_modified == 1
     if success:
         logging.get_logger(self._log_tag).info('Task was saved: {}'.format(task_to_save.__data__))
     return success
 def edit_task_from_model(self, task):
     task_to_edit = TaskTableModel.select().where(TaskTableModel.tid == task.tid)[0]
     task_to_edit.pid = task.pid
     task_to_edit.uid = task.uid
     task_to_edit.parent_tid = task.parent_tid
     task_to_edit.title = task.title
     task_to_edit.description = task.description
     task_to_edit.supposed_start_time = task.supposed_start_time
     task_to_edit.supposed_end_time = task.supposed_end_time
     task_to_edit.deadline_time = task.deadline_time
     task_to_edit.priority = task.priority
     task_to_edit.status = task.status
     task_to_edit.notificate_supposed_start = task.notificate_supposed_start
     task_to_edit.notificate_supposed_end = task.notificate_supposed_end
     task_to_edit.notificate_deadline = task.notificate_deadline
     
     rows_modified = task_to_edit.save()
     success = rows_modified == 1
     if success:
         logging.get_logger(self._log_tag).info('Task was edited: %s', task_to_edit.__data__)
     return success
    def recalculate_exclude_when_start_time_shifted(self, plan_id, start_time_shift):
        plan_models = PlanTableModel.select().where(PlanTableModel.plan_id == plan_id)
        shift = plan_models[0].shift
        if start_time_shift % shift == 0:
            logging.get_logger(self._log_tag).info(('For {} excludes were recalculated '
                'due start time shift changed').format(plan_id))
            conditions = ((PlanRelationsTableModel.plan_id == plan_id) 
                & (PlanRelationsTableModel.kind != PlanRelationsTableModel.Kind.COMMON))
            relations = PlanRelationsTableModel.select().where(conditions)
            for relation in relations:
                relation.number -= start_time_shift / shift
                if relation.number < 0:
                    relation.delete_instance()
                else:
                    rows_modified = relation.save()
                    if rows_modified != 1:
                        return False
        else:
            return self.restore_all_repeats(plan_id)

        return True
    def remove_admin_from_project(self, pid, uid):
        filter = TaskStorageAdapter.Filter()
        filter.pid(pid)
        filter.uid(uid)
        task_storage = TaskStorageAdapter(self.db_file, self.db)
        tasks = task_storage.get_tasks(filter)
        if tasks is not None and len(tasks) != 0:
            projects = self.get_projects(uid=uid, pid=pid)
            project = projects[0]
            for task in tasks:
                task.uid = project.creator
                success = task_storage.edit_task_from_model(task)
                if not success:
                    return False

        conditions = (ProjectRelationsTableModel.pid == pid & ProjectRelationsTableModel.uid == uid
                        & ProjectRelationsTableModel.kind == ProjectRelationsTableModel.Kind.ADMIN)
        rows_deleted = ProjectRelationsTableModel.delete().where(conditions).execute()
        success = rows_deleted == 1
        if success:
            logging.get_logger(self._log_tag).info('Admin {} was removed from project {}'.format(uid, pid))
        return success
    def save_plan(self, plan):
        plan_to_save = PlanTableModel(end=plan.end,
                                shift=plan.shift)
        rows_modified = plan_to_save.save()
        if rows_modified != 1:
            return False
        logging.get_logger(self._log_tag).info('Plan was saved: {}'.format(plan_to_save.__data__))

        plan_common_relation = PlanRelationsTableModel(plan_id=plan_to_save.plan_id,
                                                    tid=plan.tid,
                                                    number=None,
                                                    kind=PlanRelationsTableModel.Kind.COMMON)
        rows_modified = plan_common_relation.save()
        if rows_modified != 1:
            return False
        logging.get_logger(self._log_tag).info('Plan common relation was added: {}'.format(plan_common_relation.__data__))

        if plan.exclude is not None and len(plan.exclude) != 0:
            for exclude_number in plan.exclude:
                success = self.delete_plan_repeat(plan_to_save.plan_id, exclude_number)
                if not success:
                    return False
        
        return True
    def edit_plan(self, plan_field_dict):
        plan_id = plan_field_dict[Plan.Field.plan_id]

        if Plan.Field.end in plan_field_dict:
            end = plan_field_dict[Plan.Field.end]
            plan_models = PlanTableModel.select().where(PlanTableModel.plan_id == plan_id)
            for plan_model in plan_models:
                plan_model.end = end
                rows_modified = plan_model.save()
                if rows_modified != 1:
                    return False
                logging.get_logger(self._log_tag).info('End of plan {} was changed to {}'.format(plan_id, end))

        if Plan.Field.shift in plan_field_dict:
            shift = plan_field_dict[Plan.Field.shift]
            plan_models = PlanTableModel.select().where(PlanTableModel.plan_id == plan_id)
            for plan_model in plan_models:
                old_shift = plan_model.shift
                plan_model.shift = shift
                rows_modified = plan_model.save()
                if rows_modified != 1:
                    return False
                logging.get_logger(self._log_tag).info('Shift of plan {} was changed to {}'.format(plan_id, shift))
            
            conditions = ((PlanRelationsTableModel.plan_id == plan_id)
                & (PlanRelationsTableModel.kind != PlanRelationsTableModel.Kind.COMMON))
            relations = PlanRelationsTableModel.select().where(conditions)
            for relation in relations:
                number = relation.number
                if (number * old_shift) % shift == 0:
                    relation.number = (number * old_shift) / shift
                    rows_modified = relation.save()
                    if rows_modified != 1:
                        return False
                    logging.get_logger(self._log_tag).info('Repeat {} was shifted'.format(number))
                else:
                    relation.delete_instance()
                    logging.get_logger(self._log_tag).info('Repeat {} was removed'.format(number))

        return True