def save(self, obj): """ This function is used to store given task to the database. Note, that tasks can be with similar names and dates (but on that case I have a task below) :param obj: type with fields: - creator_uid - uid - pid - status - deadline_time - title - priority - parent with tid or None - comment - realization_time or None - creation_time :return: None """ try: task = Task.select().where((Task.tid == obj.tid) & ((Task.receiver == self.uid) | (Task.creator == self.uid))).get() TaskAdapter._update(task, obj) logger.debug('task updated') return except DoesNotExist: logger.debug('adding task...') try: # check that if receiver is not us, that we have rights to do smt in # the project if obj.pid is not None: # But we hope that we are already checked this pass table_task = Task.create(creator=obj.creator_uid, receiver=obj.uid, project=obj.pid, status=obj.status, parent=obj.parent, title=obj.title, priority=obj.priority, deadline_time=obj.deadline, comment=obj.comment, realization_time=obj.realization_time, creation_time=obj.creation_time, period=obj.period) except IntegrityError: # if you are guest raise db_e.InvalidLoginError(db_e.TaskMessages.DO_NOT_HAVE_RIGHTS) logger.debug('task\'s parent %s', table_task.parent) logger.debug('task saved to database')
def _init_project_tasks_table(): for task in _PROJECT_TASKS: Task.create(creator=task.creator_uid, receiver=task.uid, project=task.pid, status=task.status, parent=task.parent, title=task.title, priority=task.priority, deadline_time=task.deadline, comment=task.comment, realization_time=task.realization_time, creation_time=task.creation_time)
def get_task_by_id(self, tid): """ This function finds task by id and current user in database and returns it, or raise error due to incorrect request :param tid: Task id to find :type tid: Int :return: Task """ task = Task.select().where((Task.tid == tid) & ( (Task.creator == self.uid) | (Task.receiver == self.uid))) try: return TaskInstance.make_task(task.get()) except DoesNotExist: logger.info( 'There is no such tid %s in the database for your user', tid) try: # if we can find such tid, then we don't have rights self._get_available_tasks().select().\ where(Task.tid == tid).get() raise db_e.InvalidTidError( db_e.TaskMessages.DO_NOT_HAVE_RIGHTS) except DoesNotExist: raise db_e.InvalidTidError( db_e.TaskMessages.TASK_DOES_NOT_EXISTS)
def _get_available_tasks(self): # get all tasks in project related to user and his personal tasks query_projects = UserProjectRelation. \ select().where(UserProjectRelation.user == self.uid) projects = [rel.project.pid for rel in query_projects] query = Task.select().where((Task.project << projects) | (Task.creator == self.uid)) return query
def remove_project_by_id(self, pid): """ This function removes project from id snd clears all relations between users and passed project. Or it raises an exception if you don't have rights or pid do not exists :param pid: Project's id :return: None """ if not self.has_rights(pid): raise db_e.InvalidLoginError( db_e.ProjectMessages.DO_NOT_HAVE_RIGHTS) try: UserProjectRelation.delete(). \ where(UserProjectRelation.project == pid).execute() Task.delete().where(Task.project == pid).execute() Project.delete().where(Project.pid == pid).execute() logger.debug('project fully removed') except DoesNotExist: db_e.InvalidPidError(db_e.ProjectMessages.PROJECT_DOES_NOT_EXISTS)
def last_id(): """ This function gets last id to add task on it's place TODO: This function finds the first unused id in the table to add new task row on that place :return: Int """ query = Task.select().order_by(Task.tid.desc()) logger.debug('getting last id from query...') try: # task query return query.get().tid except DoesNotExist: return 0
def has_rights(self, tid): """ This function returns bool value for current user. It checks if he can modify this task or not :param tid: Task's id :return: Bool """ try: task = Task.select().where(Task.tid == tid).get() except DoesNotExist: raise db_e.InvalidTidError(db_e.TaskMessages.TASK_DOES_NOT_EXISTS) if task.receiver == self.uid: return True elif task.project: project = Project.select().where(Project.pid == task.project).get() return project.admin == self.uid else: return task.creator == self.uid