def __format_images(self, content): content_in_html = content img_tag = content_in_html.find("<img") while img_tag != -1: img_tag_src_starts = img_tag + 10 img_tag_src_ends = content_in_html.find('"', img_tag_src_starts) img_src = content_in_html[img_tag_src_starts:img_tag_src_ends] img_id = get_id(img_src) if img_id: image = self.image_service.get('id', get_id(img_src)) img_string = "data:image/"+image.filetype+";base64,"+image.image width = image.width if image.width < self.__active_width - \ 500 else self.__active_width - 500 content_in_html = content_in_html[:img_tag_src_ends+1] + \ ' width="'+str(width)+'" ' + \ content_in_html[img_tag_src_ends+1:] content_in_html = content_in_html[:img_tag_src_starts] + \ img_string+content_in_html[img_tag_src_ends:] img_tag = content_in_html.find("<img", img_tag_src_ends) return content_in_html
def test_create_without_valid_author_returns_none(self): imgsrc = path.join(path.dirname(__file__), '..', './testdata', 'testimage1.png') imgsrc = path.normpath(imgsrc) image = self.image_service.create(get_id('608bd901507fc317f17ab300'), 'testimage1', imgsrc, 600) self.assertIsNone(image) self.assertEqual(self.image_service.count(), self.before)
def test_count_unvalid_id_returns_zero(self): imgsrc = path.join(path.dirname(__file__), '..', './testdata', 'testimage1.png') imgsrc = path.normpath(imgsrc) for i in range(1, 11): self.image_service.create(self.author.id, 'testimage' + str(i), imgsrc, 600) self.assertEqual( self.image_service.count('id', get_id('608bd901507fc317f17ab300')), 0)
def test_get_unvalid_id_returns_none(self): imgsrc = path.join(path.dirname(__file__), '..', './testdata', 'testimage1.png') imgsrc = path.normpath(imgsrc) image = self.image_service.create(self.author.id, 'testimage1', imgsrc, 600) images = self.image_service.get('id', get_id('608bd901507fc317f17ab300')) self.assertIsNone(images)
def test_count_author_works(self): imgsrc = path.join(path.dirname(__file__), '..', './testdata', 'testimage1.png') imgsrc = path.normpath(imgsrc) for i in range(1, 6): self.image_service.create(self.author.id, 'testimage' + str(i), imgsrc, 600) for i in range(6, 11): self.image_service.create(get_id('608bd901507fc317f17ab300'), 'testimage' + str(i), imgsrc, 600) self.assertEqual(self.image_service.count('author', self.author.id), 5)
def __get_id(self, search_term): """__get_id is function for get function to call, when requested user with specific id. Handles finding the user with given id. Args: search_term (ObjectId): search term given by get function Returns: Union([User, None]): return user object if found, None if none users with given id in the database. """ if isinstance(search_term, get_type_id()): return self.__get_all_users()(id=get_id(search_term)).first() return None
def test_get_author_returns_images(self): imgsrc = path.join(path.dirname(__file__), '..', './testdata', 'testimage1.png') imgsrc = path.normpath(imgsrc) image = self.image_service.create(self.author.id, 'testimage1', imgsrc, 600) second_image = self.image_service.create(self.author.id, 'testimage2', imgsrc, 600) third_image = self.image_service.create( get_id('608bd901507fc317f17ab301'), 'otherimage3', imgsrc, 600) images = self.image_service.get('author', self.author.id) self.assertEqual(len(images), 2) self.assertEqual(images[0], image) self.assertEqual(images[1], second_image)
def test_get_id_returns_only_one(self): imgsrc = path.join(path.dirname(__file__), '..', './testdata', 'testimage1.png') imgsrc = path.normpath(imgsrc) image = self.image_service.create(self.author.id, 'testimage1', imgsrc, 600) second_image = self.image_service.create(self.author.id, 'testimage2', imgsrc, 600) third_image = self.image_service.create( get_id('608bd901507fc317f17ab300'), 'otherimage3', imgsrc, 600) images = self.image_service.get('id', image.id) self.assertEqual(images, image) self.assertEqual(images.id, image.id) self.assertEqual(images.image, image.image) self.assertEqual(images.name, image.name)
def test_count_not_valid_id_gives_zero(self): result = self.memo_service.count('id', get_id()) self.assertEqual(result, 0)
def test_count_not_valid_id_returns_zero(self): result = self.memorepo.count('id', get_id()) self.assertEqual(result, 0)
def test_get_unvalid_id(self): queried_memo = self.memorepo.get('id', get_id()) self.assertIsNone(queried_memo)
def test_count_unvalid_id_returns_zero(self): result = self.user_service.count('id', get_id()) self.assertEqual(result, 0)
def test_get_unvalid_id_returns_none(self): queried_user = self.user_service.get("id", get_id()) self.assertIsNone(queried_user)
def test_remove_returns_false_with_unvalid_id(self): before = self.user_service.count() result = self.user_service.remove(get_id()) after = self.user_service.count() self.assertFalse(result) self.assertEqual(after, before)
def test_remove_unvalid_id(self): image = get_test_image_obj() image.id = get_id() res = self.imagerepo.remove(image) self.assertFalse(res)
def import_from_file(self, author_id, src): """import from file is used to import memos from markdown files Args: author_id: memo's author's id as ObjectId. src: source location where imported content is currently as string. Returns: Union(Memo, None): returns Memo if import were successful. Returns None if imported file were empty or no file found. """ memo = { "imported": "", "filename": "", "file_ext": "", "title": "", "img_src": "", "img_name": "", "index": None, "img_src_end": None, "status": None, "img_tag": "", } try: memo["imported"] = self.file_repository.open_file(src) if not memo["imported"]: raise ValueError("File was empty") memo["filename"], memo["file_ext"] = os.path.splitext(src) memo["filename"] = memo["filename"].split("/") memo["filename"] = memo["filename"][len(memo["filename"]) - 1] + memo["file_ext"] src = src[:src.find(memo["filename"])] index = memo["imported"].find('![](') while index != -1: memo["img_src_end"] = memo["imported"].find(')', index) memo["img_src"] = memo["imported"][index + 4:memo["img_src_end"]] img_name = memo["img_src"].split('/') img_name = img_name[len(img_name) - 1] img_name = memo["filename"] + "/" + img_name img_name = img_name if len(img_name) < 50 else img_name[:50] memo["img_src"] = os.path.normpath( os.path.join(src, memo["img_src"])) img = self.image_service.get('name', img_name) if len(img) > 0: i = 0 while i < len(img): imgs = img[i] memo["status"] = True if imgs.author != self.user_repository.get( 'id', get_id(author_id)): memo["status"] = False if imgs.image != self.image_service.convert_image( memo["img_src"]): memo["status"] = False if memo["status"]: img = imgs break i += 1 else: img = None img = img if img else self.image_service.create( author_id, img_name, memo["img_src"], 600) memo["img_tag"] = "" if img: memo["img_tag"] = "![](" + str(img.id) + ')' memo["imported"] = memo["imported"][:index] + \ memo["img_tag"]+memo["imported"][memo["img_src_end"]+1:] index = memo["imported"].find('![](', index + 1) memo["title"] = "Imported from " + memo["filename"] memo["title"] = memo["title"] if len( memo["title"]) < 50 else memo["title"][:50] return self.create(author_id, memo["title"], memo["imported"]) except ValueError: return None
def test_remove_returns_false_with_unvalid_id(self): result = self.image_service.remove(get_id('608bd901507fc317f17ab301')) self.assertFalse(result)
def test_get_not_valid_id_gives_zero(self): queried_memo = self.memo_service.get('id', get_id()) self.assertIsNone(queried_memo)