def save(obj): """ This function if checking is current user exists, and if so we are raising an exception. Or we are adding it to the database. 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: - login - nickname, - about :return: None """ try: user = User.select().where(User.uid == obj.uid).get() UserAdapter._update(user, obj) logger.debug('user updated') return except DoesNotExist: logger.debug('adding user...') User.create(uid=obj.uid, login=obj.login, about=obj.about, nickname=obj.nickname)
def _create_sqlite_database(): User.create_table() Project.create_table() UserProjectRelation.create_table() Task.create_table() Message.create_table() logger.debug("Database created")
def remove_from_project_by_id(self, uid, pid): """ This function removes user from the project by it's id :param pid: Project's id :param uid: User's id :return: None """ try: # we get task where current user is admin and where project id is # matching Project.select().where( (self.uid == Project.admin) & (Project.pid == pid)).get() logger.debug('such project exists') # if an admin tries to delete himself we deny it if uid == self.uid: raise db_e.InvalidPidError( db_e.ProjectMessages.DO_NOT_HAVE_RIGHTS) except DoesNotExist: # not admin can try to delete himself if uid != self.uid: raise db_e.InvalidPidError( db_e.ProjectMessages.DO_NOT_HAVE_RIGHTS) # now we try to find and delete user rows = UserProjectRelation.delete().where( (UserProjectRelation.project == pid) & (UserProjectRelation.user == uid)).\ execute() if rows == 0: raise db_e.InvalidUidError(db_e.LoginMessages.NO_USER_TO_DELETE)
def remove_message(self, mid): try: message = Message.select().where((Message.mid == mid) & (Message.uid == self.uid)).get() message.delete_instance() logger.debug('Message has been removed') except DoesNotExist: raise db_e.InvalidMidError(db_e.MidMessages.DO_NOT_HAVE_RIGHTS)
def _init_psql_tables(): if not User.table_exists(): User.create_table() if not Project.table_exists(): Project.create_table() if not UserProjectRelation.table_exists(): UserProjectRelation.create_table() if not Task.table_exists(): Task.create_table() if not Message.table_exists(): Message.create_table() logger.debug("Database tables initialized")
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 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 is_user_in_project(uid, pid): """ This function checks if passed user exists in the selected project :param uid: User's id :param pid: Project's id :return: Bool """ logger.debug('the uid is %s', uid) try: UserProjectRelation.select(). \ where((UserProjectRelation.project == pid) & (UserProjectRelation.user == uid)).get() logger.debug('user exists in project') return True except DoesNotExist: raise db_e.InvalidPidError(db_e.LoginMessages.USER_DOES_NOT_EXISTS)
def save(self, obj): """ This function is used to store given project in database :param obj: type with fields: - pid - admin_uid - description - title :return: None """ try: if not self.has_rights(obj.pid): raise db_e.InvalidLoginError( db_e.ProjectMessages.DO_NOT_HAVE_RIGHTS) project = Project.select().where(Project.pid == obj.pid).get() self._update(project, obj) return except db_e.InvalidPidError: logger.debug('adding project...') try: project = Project(admin=obj.admin_uid, description=obj.description, title=obj.title) relation = UserProjectRelation(user=self.uid, project=project) # only if everything is ok we try save project to our database project.save() relation.save() except IntegrityError: # if you are guest raise db_e.InvalidLoginError( db_e.ProjectMessages.DO_NOT_HAVE_RIGHTS) logger.debug('project saved to database')
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 add_user_to_project_by_id(self, uid, pid): """ This function adds user to the Project relationship table to make user a member of the project, if both exists. :param uid: User's id :param pid: Project's id :return: None """ try: # we get task where current user is admin and where project id is # matching project = Project.select(). \ where((self.uid == Project.admin) & (Project.pid == pid)).get() logger.debug('such project exists') except DoesNotExist: raise db_e.InvalidPidError( db_e.ProjectMessages.DO_NOT_HAVE_RIGHTS) # NOTE: due to we don't have access to the users db, we BELIEVE, that # user exists in the database (you should check that on the auth level) try: # and now we are checking if selected user already in the project. # If the exception DoesNotExist was not raised, that means that user # already in project and it's bad UserProjectRelation.select(). \ where((UserProjectRelation.project == pid) & (UserProjectRelation.user == uid)).get() logger.debug('such relation exists') raise db_e.InvalidLoginError( db_e.ProjectMessages.USER_ALREADY_EXISTS) except DoesNotExist: logger.debug('such user is not in project') UserProjectRelation.create(user=uid, project=project) logger.info('user_project relation created')
def send_message(uid, message_str): Message.create(uid=uid, content=message_str) logger.debug('Message has been sent')