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 add_attachment_to_card(cardId): image_file = request.files['attachment'] contentType = image_file.content_type fileName = image_file.filename classCard, _ = Card.get_card_by_id(cardId) updatedCard, newImageId = classCard.add_attachment(image_file, contentType, fileName) fsClass = FS.get(newImageId) imageStr = Utils.prepareImage(fsClass) imgAssigned = updatedCard['attachments']['assigned'] if imgAssigned == newImageId: assignedImage = True else: assignedImage = False responseSchema = { '_id': updatedCard['_id'], 'forList': updatedCard['forList'], 'boardId': updatedCard['boardId'], 'attachments': { 'file_id': newImageId, 'image': imageStr, 'assigned': assignedImage, 'filename': fsClass.filename, 'uploadDate': fsClass.uploadDate } } return jsonify(responseSchema)
def user_login(): if request.method == "POST" and request.is_json: CONTENT = request.get_json() email = CONTENT['email'] password = CONTENT['password'] try: if User.is_login_valid(email, password): session['email'] = email user = User.get_user_by_email(email) # if isinstance(user['photo'], ObjectId): # fsClass = FS.get(user['photo']) # photo = Utils.prepareImage(fsClass) # user['photo'] = photo # return jsonify(user) if user['photo']: fsClass = FS.get(user['photo']) photo = Utils.prepareImage(fsClass) user['photo'] = photo return jsonify(user) except UserErrors.UserError as e: return jsonify(error=e.message) return "login form"
def get_attachment_in_string(cursors): cardWithImage = list() if len(cursors['attachments']['files']) is not 0: images = list() for ids in cursors['attachments']['files']: fsClass = FS.get(ids) filename = fsClass.filename uploadDate = fsClass.uploadDate imgStr = Utils.prepareImage(fsClass) imageDict = { 'file_id': ids, 'image': imgStr, 'filename': filename, 'uploadDate': uploadDate } images.append(imageDict) cardWithImage.append({ **cursors, 'attachments': { 'files': images, 'assigned': cursors['attachments']['assigned'] } }) else: cardWithImage.append({**cursors}) return cardWithImage
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 upload_photo_for_team(teamId): image_file = request.files['photo'] contentType = image_file.content_type fileName = image_file.filename teamClass, _ = Team.get_team_by_id(teamId) isUploaded = teamClass.upload_photo(image_file, contentType, fileName) fsCLass = FS.get(isUploaded) img_str = Utils.prepareImage(fsCLass) return jsonify(img_str)
def update_user_photo(): if request.method == 'POST': image_file = request.files['photo'] contentType = image_file.content_type fileName = image_file.filename user = User.get_user_by_email(session['email']) classUser = User(**user) isUploaded = classUser.upload_photo(image_file, contentType, fileName) fsCLass = FS.get(isUploaded) img_str = Utils.prepareImage(fsCLass) return jsonify(img_str)
def return_with_assigned_files(cardsCursor): result = list() for card in cardsCursor: if card['attachments']['assigned']: fsClass = FS.get(card['attachments']['assigned']) imgStr = Utils.prepareImage(fsClass) prepareAttach = { **card['attachments'], 'assigned': imgStr, 'file_id': card['attachments']['assigned'] } result.append({**card, "attachments": prepareAttach}) else: result.append({ **card, "attachments": { **card['attachments'], 'assigned': None, 'file_id': card['attachments']['assigned'] } }) return result
def isHaveImage(team): if team['photo']: fsClass = FS.get(team['photo']) photo = Utils.prepareImage(fsClass) return {**team, 'photo': photo} return team