def update_post_safety(post: model.Post, safety: str) -> None: assert post safety = util.flip(SAFETY_MAP).get(safety, None) if not safety: raise InvalidPostSafetyError('Safety can be either of %r.' % list(SAFETY_MAP.values())) post.safety = safety
def update_post_notes(post: model.Post, notes: Any) -> None: assert post post.notes = [] for note in notes: for field in ('polygon', 'text'): if field not in note: raise InvalidPostNoteError('Note is missing %r field.' % field) if not note['text']: raise InvalidPostNoteError('A note\'s text cannot be empty.') if not isinstance(note['polygon'], (list, tuple)): raise InvalidPostNoteError( 'A note\'s polygon must be a list of points.') if len(note['polygon']) < 3: raise InvalidPostNoteError( 'A note\'s polygon must have at least 3 points.') for point in note['polygon']: if not isinstance(point, (list, tuple)): raise InvalidPostNoteError( 'A note\'s polygon point must be a list of length 2.') if len(point) != 2: raise InvalidPostNoteError( 'A point in note\'s polygon must have two coordinates.') try: pos_x = float(point[0]) pos_y = float(point[1]) if not 0 <= pos_x <= 1 or not 0 <= pos_y <= 1: raise InvalidPostNoteError( 'All points must fit in the image (0..1 range).') except ValueError: raise InvalidPostNoteError( 'A point in note\'s polygon must be numeric.') if util.value_exceeds_column_size(note['text'], model.PostNote.text): raise InvalidPostNoteError('Note text is too long.') post.notes.append( model.PostNote(polygon=note['polygon'], text=str(note['text'])))
def update_post_safety(post: model.Post, safety: str) -> None: assert post safety = util.flip(SAFETY_MAP).get(safety, None) if not safety: raise InvalidPostSafetyError('위험도는 다음중 하나여야 합니다: %r' % list(SAFETY_MAP.values())) post.safety = safety
def update_post_tags( post: model.Post, tag_names: List[str] ) -> List[model.Tag]: assert post existing_tags, new_tags = tags.get_or_create_tags_by_names(tag_names) post.tags = existing_tags + new_tags return new_tags
def update_post_notes(post: model.Post, notes: Any) -> None: assert post post.notes = [] for note in notes: for field in ('polygon', 'text'): if field not in note: raise InvalidPostNoteError('메모의 %r 필드가 누락되었습니다.' % field) if not note['text']: raise InvalidPostNoteError('메모의 텍스트가 반드시 필요합니다.') if not isinstance(note['polygon'], (list, tuple)): raise InvalidPostNoteError('메모의 폴리곤은 점의 리스트여야 합니다.') if len(note['polygon']) < 3: raise InvalidPostNoteError('메모의 폴리곤은 최소 3개의 점이 포함되어야 합니다.') for point in note['polygon']: if not isinstance(point, (list, tuple)): raise InvalidPostNoteError('메모의 폴리곤 점은 길이 2인 리스트여야 합니다.') if len(point) != 2: raise InvalidPostNoteError('메모의 폴리곤 점은 두 좌표축을 포함해야 합니다.') try: pos_x = float(point[0]) pos_y = float(point[1]) if not 0 <= pos_x <= 1 or not 0 <= pos_y <= 1: raise InvalidPostNoteError( '메모의 모든 폴리곤 점은 이미지 내부에 존재해야 합니다 (0 ~ 1 범위).') except ValueError: raise InvalidPostNoteError('메모의 폴리곤 점은 숫자여야 합니다.') if util.value_exceeds_column_size(note['text'], model.PostNote.text): raise InvalidPostNoteError('메모 텍스트가 너무 깁니다.') post.notes.append( model.PostNote(polygon=note['polygon'], text=str(note['text'])))
def update_post_notes(post: model.Post, notes: Any) -> None: assert post post.notes = [] for note in notes: for field in ('polygon', 'text'): if field not in note: raise InvalidPostNoteError('Note is missing %r field.' % field) if not note['text']: raise InvalidPostNoteError('A note\'s text cannot be empty.') if not isinstance(note['polygon'], (list, tuple)): raise InvalidPostNoteError( 'A note\'s polygon must be a list of points.') if len(note['polygon']) < 3: raise InvalidPostNoteError( 'A note\'s polygon must have at least 3 points.') for point in note['polygon']: if not isinstance(point, (list, tuple)): raise InvalidPostNoteError( 'A note\'s polygon point must be a list of length 2.') if len(point) != 2: raise InvalidPostNoteError( 'A point in note\'s polygon must have two coordinates.') try: pos_x = float(point[0]) pos_y = float(point[1]) if not 0 <= pos_x <= 1 or not 0 <= pos_y <= 1: raise InvalidPostNoteError( 'All points must fit in the image (0..1 range).') except ValueError: raise InvalidPostNoteError( 'A point in note\'s polygon must be numeric.') if util.value_exceeds_column_size(note['text'], model.PostNote.text): raise InvalidPostNoteError('Note text is too long.') post.notes.append( model.PostNote(polygon=note['polygon'], text=str(note['text'])))
def update_post_safety(post: model.Post, safety: str) -> None: assert post safety = util.flip(SAFETY_MAP).get(safety, None) if not safety: raise InvalidPostSafetyError( 'Safety can be either of %r.' % list(SAFETY_MAP.values())) post.safety = safety
def update_post_flags(post: model.Post, flags: List[str]) -> None: assert post target_flags = [] for flag in flags: flag = util.flip(FLAG_MAP).get(flag, None) if not flag: raise InvalidPostFlagError('Flag must be one of %r.' % list(FLAG_MAP.values())) target_flags.append(flag) post.flags = target_flags
def update_post_flags(post: model.Post, flags: List[str]) -> None: assert post target_flags = [] for flag in flags: flag = util.flip(FLAG_MAP).get(flag, None) if not flag: raise InvalidPostFlagError( 'Flag must be one of %r.' % list(FLAG_MAP.values())) target_flags.append(flag) post.flags = target_flags
def update_post_content(post: model.Post, content: Optional[bytes]) -> None: assert post if not content: raise InvalidPostContentError('Post content missing.') post.mime_type = mime.get_mime_type(content) if mime.is_flash(post.mime_type): post.type = model.Post.TYPE_FLASH elif mime.is_image(post.mime_type): if mime.is_animated_gif(content): post.type = model.Post.TYPE_ANIMATION else: post.type = model.Post.TYPE_IMAGE elif mime.is_video(post.mime_type): post.type = model.Post.TYPE_VIDEO else: raise InvalidPostContentError('Unhandled file type: %r' % post.mime_type) post.checksum = util.get_sha1(content) other_post = db.session \ .query(model.Post) \ .filter(model.Post.checksum == post.checksum) \ .filter(model.Post.post_id != post.post_id) \ .one_or_none() if other_post \ and other_post.post_id \ and other_post.post_id != post.post_id: raise PostAlreadyUploadedError(other_post) post.file_size = len(content) try: image = images.Image(content) post.canvas_width = image.width post.canvas_height = image.height except errors.ProcessingError: post.canvas_width = None post.canvas_height = None if (post.canvas_width is not None and post.canvas_width <= 0) \ or (post.canvas_height is not None and post.canvas_height <= 0): post.canvas_width = None post.canvas_height = None setattr(post, '__content', content)
def update_post_source(post: model.Post, source: Optional[str]) -> None: assert post if util.value_exceeds_column_size(source, model.Post.source): raise InvalidPostSourceError('Source is too long.') post.source = source or None
def update_post_content(post: model.Post, content: Optional[bytes]) -> None: assert post if not content: raise InvalidPostContentError("Post content missing.") update_signature = False post.mime_type = mime.get_mime_type(content) if mime.is_flash(post.mime_type): post.type = model.Post.TYPE_FLASH elif mime.is_image(post.mime_type): update_signature = True if mime.is_animated_gif(content): post.type = model.Post.TYPE_ANIMATION else: post.type = model.Post.TYPE_IMAGE elif mime.is_video(post.mime_type): post.type = model.Post.TYPE_VIDEO else: raise InvalidPostContentError( "Unhandled file type: %r" % post.mime_type ) post.checksum = util.get_sha1(content) other_post = ( db.session.query(model.Post) .filter(model.Post.checksum == post.checksum) .filter(model.Post.post_id != post.post_id) .one_or_none() ) if ( other_post and other_post.post_id and other_post.post_id != post.post_id ): raise PostAlreadyUploadedError(other_post) if update_signature: purge_post_signature(post) post.signature = generate_post_signature(post, content) post.file_size = len(content) try: image = images.Image(content) post.canvas_width = image.width post.canvas_height = image.height except errors.ProcessingError: if not config.config["allow_broken_uploads"]: raise InvalidPostContentError("Unable to process image metadata") else: post.canvas_width = None post.canvas_height = None if (post.canvas_width is not None and post.canvas_width <= 0) or ( post.canvas_height is not None and post.canvas_height <= 0 ): if not config.config["allow_broken_uploads"]: raise InvalidPostContentError( "Invalid image dimensions returned during processing" ) else: post.canvas_width = None post.canvas_height = None setattr(post, "__content", content)
def update_post_tags( post: model.Post, tag_names: List[str]) -> List[model.Tag]: assert post existing_tags, new_tags = tags.get_or_create_tags_by_names(tag_names) post.tags = existing_tags + new_tags return new_tags
def update_post_content(post: model.Post, content: Optional[bytes]) -> None: assert post if not content: raise InvalidPostContentError('Post content missing.') post.mime_type = mime.get_mime_type(content) if mime.is_flash(post.mime_type): post.type = model.Post.TYPE_FLASH elif mime.is_image(post.mime_type): if mime.is_animated_gif(content): post.type = model.Post.TYPE_ANIMATION else: post.type = model.Post.TYPE_IMAGE elif mime.is_video(post.mime_type): post.type = model.Post.TYPE_VIDEO else: raise InvalidPostContentError( 'Unhandled file type: %r' % post.mime_type) post.checksum = util.get_sha1(content) other_post = ( db.session .query(model.Post) .filter(model.Post.checksum == post.checksum) .filter(model.Post.post_id != post.post_id) .one_or_none()) if other_post \ and other_post.post_id \ and other_post.post_id != post.post_id: raise PostAlreadyUploadedError(other_post) post.file_size = len(content) try: image = images.Image(content) post.canvas_width = image.width post.canvas_height = image.height except errors.ProcessingError: post.canvas_width = None post.canvas_height = None if (post.canvas_width is not None and post.canvas_width <= 0) \ or (post.canvas_height is not None and post.canvas_height <= 0): post.canvas_width = None post.canvas_height = None setattr(post, '__content', content)
def update_post_source(post: model.Post, source: Optional[str]) -> None: assert post if util.value_exceeds_column_size(source, model.Post.source): raise InvalidPostSourceError('Source is too long.') post.source = source or None
def update_post_content(post: model.Post, content: Optional[bytes]) -> None: assert post if not content: raise InvalidPostContentError("Post content missing.") update_signature = False post.mime_type = mime.get_mime_type(content) if mime.is_flash(post.mime_type): raise InvalidPostContentError( "Flash animations are deprecated in this build and are slowly " + "being phased out.") elif mime.is_image(post.mime_type): update_signature = True if mime.is_animated_gif(content): post.type = model.Post.TYPE_ANIMATION else: post.type = model.Post.TYPE_IMAGE elif mime.is_video(post.mime_type): post.type = model.Post.TYPE_VIDEO else: raise InvalidPostContentError(f"Unhandled file type: {post.mime_type}") post.checksum = util.get_sha1(content) post.checksum_md5 = util.get_md5(content) other_post = (db.session.query( model.Post).filter(model.Post.checksum == post.checksum).filter( model.Post.post_id != post.post_id).one_or_none()) if (other_post and other_post.post_id and other_post.post_id != post.post_id): raise PostAlreadyUploadedError(other_post) if update_signature: purge_post_signature(post) post.signature = generate_post_signature(post, content) post.file_size = len(content) post.canvas_width = None post.canvas_height = None post.date_taken = None post.camera = None try: if post.type == model.Post.TYPE_IMAGE: media = images.Image(content) elif post.type in (model.Post.TYPE_ANIMATION, model.Post.TYPE_VIDEO): media = images.Video(content) except Exception as ex: logger.exception(ex) if not config.config["allow_broken_uploads"]: raise InvalidPostContentError("Unable to process image metadata") else: if not media.width or not media.height: if not config.config["allow_broken_uploads"]: raise InvalidPostContentError( "Invalid image dimensions returned during processing") post.canvas_width = media.width post.canvas_height = media.height post.date_taken = media.date_taken post.camera = media.camera setattr(post, "__content", content)