def set_new_title(self, title): query = {'_id': self._id} update = {'title': title} Database.update_one(Checklist.collection, query, update) updatedChecklist = Checklist.get_by_id(self._id).dict_from_class() return updatedChecklist
def update(self, updates): query = {'_id': self._id} update = {**updates, "isEdited": True, "action": Log.actions['UPDATE']} Database.update_one(Log.collection, query, update) updatedCursor = Log.get_by_id(self._id) return updatedCursor
def assign_board(self, boardId): curentTeamId = self._id query = {"_id": curentTeamId} addBoard = {"boards": boardId} Database.update_push('teams', query, addBoard)
def delete_comment(self, _id): result = Database.delete_one('comments', {'_id': self._id}) isDocumentExist = Database.find_one('comments', {'_id': self._id}) if result.deleted_count is 1 and isDocumentExist is None: return True return False
def add_item(self, item): itemDict = Item(**item).dict_from_class() query = {'_id': self._id} newItem = {'items': itemDict} Database.update_push(Checklist.collection, query, newItem) updatedClass = Checklist.get_by_id(self._id).dict_from_class() return updatedClass
def add_label(self, label): Database.update_one('cards', {'_id': self._id}, {"labels": []}) if type(label) is dict: Database.update_push('cards', {'_id': self._id}, {"labels": label}) elif type(label) is list: [ Database.update_push('cards', {'_id': self._id}, {"labels": lebl}) for lebl in label ]
def for_list(self, listId, listTitle): prepareData = { **self.rest, 'listId': listId, "logType": Log.logType['LIST'], "action": Log.actions['CREATE'], 'body': listTitle } Database.insert(Log.collection, prepareData)
def remove_item(self, itemId): query = {'_id': self._id} document = {'items': {'_id': itemId}} Database.delete_one_from_array(Checklist.collection, query, document) cursor = Database.find_one(Checklist.collection, query) isRemovedtItemExist = list( filter(lambda c: c['_id'] == itemId, cursor['items'])) if len(isRemovedtItemExist) == 0: return True return False
def add_attachment(self, file, content_type, file_name): filedId = FS.put(file, content_type, file_name) query = {'_id': self._id} if len(self.attachments['files']) == 0: Database.update_one(Card.collection, query, {'attachments.assigned': filedId}) Database.update_push(Card.collection, query, {'attachments.files': filedId}) _, cursorCard = Card.get_card_by_id(self._id) return cursorCard, filedId
def register_user(email, password, name): user_data = Database.find_one('users', {"email": email}) print("There is current user {}".format(user_data)) if user_data is not None: raise err.UserIsAlreadyExist("The user with {} is already exist".format(email)) if not Utils.email_is_valid(email): raise err.InvalidEmail("Please, write a valid email") userId = User(email, Utils.hash_password(password), name).save() userCursor = Database.find_one('users', {'_id': userId}) return True, userCursor
def assign_attachment_file(self, fileId): query = {'_id': self._id} if fileId == self.attachments['assigned']: Database.update_one(Card.collection, query, {'attachments.assigned': ''}) else: Database.update_one(Card.collection, query, {'attachments.assigned': fileId}) cursorCard = Database.find_one(Card.collection, query) return { **cursorCard, 'attachments': { 'assigned': cursorCard['attachments']['assigned'] } }
def get_board(cls, _id): cursorBoard = Database.find_one('boards', {'_id': _id}) if cursorBoard is not None: return cls(**cursorBoard), cursorBoard else: raise error.BoardIsNotExistInDatabase( "The board with this Id is not exist in Database")
def update_team(self, updates): updatedClass = Database.update_one(Team.collection, {'_id': self._id}, {**updates}) if updatedClass.raw_result['nModified'] == 1: return {**updates, '_id': self._id} return "THERE IS NOTHINK TO UPDATE"
def update(self, updates): isUpdatedResult = Database.update_one(Board.collection, {'_id': self._id}, {**updates}) if isUpdatedResult.raw_result['nModified'] == 1: return {'_id': self._id, **updates, 'reletedTo': self.reletedTo} return "Nothink to update"
def is_login_valid(email, password): user_data = Database.find_one('users', {"email": email}) if user_data is None: raise err.UserNotExist("User is not exist") if not Utils.check_hashed_password(password, user_data['password']): raise err.IncorectPassword("Wrond password") return True
def update_comment(self, updates): commentId = Database.update_one('comments', {'_id': self._id}, { 'description': updates, 'isEdited': True }) if commentId.acknowledged: cursor, _ = Comment.get_by_id(self._id) return cursor return "throw error cause nothink to update actually"
def update_user_info(self, updates): updatedClass = Database.update_one(User.collection, {'_id': self._id}, {**updates}) if updatedClass.raw_result['nModified'] == 1: return {**updates, '_id' : self._id} # userWutkUpdatedData = User.get_user_by_id_cursor(self._id) return "We dont uppdate actualy nothink"
def delete_attachment(self, fileId): query = {'_id': self._id} toRemoveFile = {'attachments.files': fileId} isRemoveFromFs = FS.delete(fileId) try: if isRemoveFromFs: if fileId == self.attachments['assigned']: clearAssignment = {'attachments.assigned': ''} Database.update_one(Card.collection, query, clearAssignment) Database.delete_one_from_array(Card.collection, query, toRemoveFile) return {**query, 'forList': self.forList, 'deletedFile': fileId} except: raise "The file is not exist with — {} | id".format(fileId)
def add_checklist(self, newChecklist): checklistId = Checklist(**newChecklist).save() updatedCardId = Database.update_push('cards', {'_id': self._id}, {'checklists': checklistId}) justCreatedCheckList = Checklist.get_by_id( checklistId).dict_from_class() return justCreatedCheckList
def create_for_board(self): prepareData = { **self.rest, "teamId": self.teamId, "logType": Log.logType['BOARD'], "action": Log.actions['ASSIGN'], "cardId": None, "body": None } logId = Database.insert(Log.collection, prepareData) return logId
def assign_to(self, logType, addonsInfo=dict()): prepareData = { **self.rest, "action": self._action(action='a'), "logType": logType, **addonsInfo } logId = Database.insert(Log.collection, prepareData) return logId
def create_team(self): teamNameFromDb = Database.find_one("teams", {"teamName": self.teamName}) if teamNameFromDb is None: teamId = Team(teamName=self.teamName, authorId=self.authorId, boards=self.boards).save() _, team = Team.get_team_by_id(teamId) return team else: raise err.TeamIsAlreadyExist( "The team with this name is already exist")
def for_comment(self, cardId, body, commentId, action='c'): if action == 'u': commentAction = Log.actions['UPDATE'] elif action == 'd': commentAction = Log.actions['DELETE'] else: commentAction = Log.actions['CREATE'] prepareData = { **self.rest, "logType": Log.logType['COMMENT'], "action": commentAction, "cardId": cardId, "commentId": commentId, "body": body } logId = Database.insert(Log.collection, prepareData) return logId
def is_login_valid(email, password): ''' This method verifies that an email-password combo (as sent by site forms) is valid or not. Checks that email exists, and that password associated to that email is correct :param email: The user's email :param password: A sha512 hashed password :return: True if valid, False otherwise ''' user_data = Database.find_one( UserConstants.COLLECTION, {'email': email}) # Password in sha512 --> pbkdf2_sha512 if user_data is None: # Tell the user their email does not exist raise UserErrors.UserNotExistsError("Your user does not exist!") if not Utils.check_hashed_password(password, user_data['password']): # Tell the user their password is wrong raise UserErrors.IncorrectPasswordError("Your password was wrong!") return True
def register_user(email, password): ''' Registers a user using an email & password. Password already comes hashed as sha-512. :param email: user's email (might be invalid) :param password: sha-512 hashed password :return: True if registered, False otherwise (exceptions can als be raised) ''' user_data = Database.find_one(UserConstants.COLLECTION, {"email": email}) if user_data is not None: raise UserErrors.UserAlreadyRegisteredError( "User email already exists") if not Utils.email_is_valid(email): raise UserErrors.InvalidEmailError("Invalid email format!") User(email, Utils.hash_password(password)).save_to_mongo() return True
def assign_list_to_board(cls, listId): print("The assign_list_to_board — {}".format(cls._id)) Database.update_push('boards', {'_id': cls._id}, {"lists": listId})
def remove_board(cls, boardId, removeFromTeamCollection): _, boardCursor = Board.get_board(boardId) teamId = boardCursor['reletedTo']['teamId'] deletedBoard = Database.delete_one('boards', {"_id": boardId}) removeFromTeamCollection(teamId, boardId)
def toggleBoardImportant(self, isImportant): Database.update_one('boards', {'_id': self._id}, {'isImportant': isImportant}) cursorBoard = Database.find_one('boards', {'_id': self._id}) return cursorBoard
def get_boards_by_author(cls, authorId): cursorBoards = Database.find('boards', {'authorId': authorId}) return [board for board in cursorBoards]
def save(self): return Database.insert('boards', self.json())