def retrieve_playlist_tmp_files(playlist): """ Retrieve all files for a given playlist into the temporary folder. """ preview_file_ids = [] for shot in playlist["shots"]: preview_file = PreviewFile.get(shot["preview_file_id"]) if preview_file is not None and preview_file.extension == "mp4": preview_file_ids.append(preview_file.id) file_paths = [] for preview_file_id in preview_file_ids: if config.FS_BACKEND == "local": file_path = file_store.get_local_movie_path( "previews", preview_file_id) else: file_path = os.path.join( config.TMP_DIR, "cache-previews-%s.%s" % (preview_file_id, "mp4")) if not os.path.exists(file_path): with open(file_path, 'wb') as tmp_file: for chunk in file_store.open_file("previews", preview_file_id): tmp_file.write(chunk) file_name = names_service.get_preview_file_name(preview_file_id) tmp_file_path = os.path.join(config.TMP_DIR, file_name) copyfile(file_path, tmp_file_path) file_paths.append((tmp_file_path, file_name)) return file_paths
def remove_comment(comment_id): comment = Comment.get(comment_id) if comment is not None: notifications = Notification.query.filter_by(comment_id=comment.id) for notification in notifications: notification.delete() news_list = News.query.filter_by(comment_id=comment.id) for news in news_list: news.delete() if comment.preview_file_id is not None: preview_file = PreviewFile.get(comment.preview_file_id) comment.preview_file_id = None comment.save() remove_preview_file(preview_file) previews = [preview for preview in comment.previews] comment.delete() for preview in previews: remove_preview_file(preview) reset_task_data(comment.object_id) events.emit("comment:delete", {"comment_id": comment.id}) return comment.serialize() else: raise CommentNotFoundException
def run_last_events_files(minutes=0, page_size=50): """ Retrieve last events from target instance and import related data and action. """ path = "events/last?only_files=true&page_size=%s" % page_size if minutes > 0: now = datetime.datetime.now() min_before = now - datetime.timedelta(minutes=minutes) after = min_before.strftime("%Y-%m-%dT%H:%M:%S") path += "&before=%s" % now.strftime("%Y-%m-%dT%H:%M:%S") path += "&after=%s" % after events = gazu.client.fetch_all(path) events.reverse() for event in events: event_name = event["name"].split(":")[0] if event_name == "preview-file": preview_file = PreviewFile.get(event["data"]["preview_file_id"]) if preview_file is not None: download_preview_from_another_instance(preview_file) else: download_thumbnail_from_another_instance( event_name, event["data"]["%s_id" % event_name] )
def update_entity_preview(entity_id, preview_file_id): """ Update given entity main preview. If entity or preview is not found, it raises an exception. """ entity = Entity.get(entity_id) if entity is None: raise EntityNotFoundException preview_file = PreviewFile.get(preview_file_id) if preview_file is None: raise PreviewFileNotFoundException entity.update({"preview_file_id": preview_file.id}) events.emit( "preview-file:set-main", { "entity_id": entity_id, "preview_file_id": preview_file_id }, ) entity_type = EntityType.get(entity.entity_type_id) entity_type_name = "asset" if entity_type.name in ["Shot", "Scene", "Sequence", "Episode"]: entity_type_name = entity_type.name.lower() events.emit( "%s:update" % entity_type_name, {"%s_id" % entity_type_name: str(entity.id)}, ) return entity.serialize()
def get_preview_files_for_task(task_id): """ Get all preview files for given task. """ previews = PreviewFile.filter_by(task_id=task_id).order_by( PreviewFile.revision.desc()) return fields.serialize_models(previews)
def sanitize_import_data(self, data): from zou.app.models.preview_file import PreviewFile from zou.app.models.entity_type import EntityType entity_ids = [] model_type = data.get("type", "Shot") if "entities_out" in data: entity_ids = data["entities_out"] del data["entities_out"] if "asset_type_id" in data: data["entity_type_id"] = data["asset_type_id"] del data["asset_type_id"] del data["asset_type_name"] if "sequence_id" in data: data["parent_id"] = data["sequence_id"] del data["sequence_id"] if ( "preview_file_id" in data and data["preview_file_id"] is not None and len(data["preview_file_id"]) > 0 ): preview_file = PreviewFile.get(data["preview_file_id"]) if preview_file is None: del data["preview_file_id"] elif "preview_file_id" in data: del data["preview_file_id"] if "frame_in" in data: data["data"]["frame_in"] = data["frame_in"] del data["frame_in"] if "frame_out" in data: data["data"]["frame_out"] = data["frame_out"] del data["frame_out"] if "fps" in data: data["data"]["fps"] = data["fps"] del data["fps"] for field in [ "entities_in", "episode_id", "episode_name", "project_name", "sequence_name", "tasks", "type", ]: if field in data: del data[field] if model_type in ["Shot", "Sequence", "Episode"]: entity_type = EntityType.get_by(name=model_type) data["entity_type_id"] = entity_type.id return (data, entity_ids)
def generate_fixture_preview_file(self, revision=1): self.preview_file = PreviewFile.create(name="main", revision=revision, description="test description", source="pytest", task_id=self.task.id, person_id=self.person.id) return self.preview_file
def set_preview_files(self, preview_file_ids): from zou.app.models.preview_file import PreviewFile self.preview_files = [] for preview_file_id in preview_file_ids: preview_file = PreviewFile.get(preview_file_id) if preview_file is not None: self.previews.append(preview_file) self.save()
def get_comments(task_id): """ Return all comments related to given task. """ comments = [] query = Comment.query.order_by(Comment.created_at.desc()) \ .filter_by(object_id=task_id) \ .join(Person, TaskStatus) \ .add_columns( TaskStatus.name, TaskStatus.short_name, TaskStatus.color, TaskStatus.is_reviewable, Person.first_name, Person.last_name, Person.has_avatar ) for result in query.all(): ( comment, task_status_name, task_status_short_name, task_status_color, task_status_is_reviewable, person_first_name, person_last_name, person_has_avatar ) = result comment_dict = comment.serialize() comment_dict["person"] = { "first_name": person_first_name, "last_name": person_last_name, "has_avatar": person_has_avatar, "id": str(comment.person_id) } comment_dict["task_status"] = { "name": task_status_name, "short_name": task_status_short_name, "color": task_status_color, "is_reviewable": task_status_is_reviewable, "id": str(comment.task_status_id) } if comment.preview_file_id is not None: preview = PreviewFile.get(comment.preview_file_id) comment_dict["preview"] = { "id": str(preview.id), "revision": preview.revision, "is_movie": preview.is_movie, "extension": preview.extension } comments.append(comment_dict) return comments
def create_preview_file_raw( name, revision, task_id, person_id, source="webgui", extension="mp4" ): return PreviewFile.create( name=name, revision=revision, source=source, task_id=task_id, person_id=person_id, extension=extension, )
def test_preview_file(self): preview_file_dict = { "id": "2b65b36d-a394-41f9-8415-9e92abeb0d49", "created_at": "2019-08-25T16:48:34", "updated_at": "2019-08-25T18:48:34", "name": "d7c1d6ec-b3e6", "original_name": "picture.png", "revision": 1, "source": "webgui", "extension": "png", "annotations": [], "task_id": str(self.task.id), "person_id": str(self.person.id), "source_file_id": None, "type": "PreviewFile", "comments": [self.comment["id"]], } PreviewFile.create_from_import(preview_file_dict) preview_file = PreviewFile.get(preview_file_dict["id"]) self.assertEqual(preview_file.name, preview_file_dict["name"])
def create_preview_file(name, revision, task_id, person_id, is_movie, source="webgui"): return PreviewFile.create(name=name, revision=revision, source=source, task_id=task_id, person_id=person_id, is_movie=is_movie).serialize()
def import_entry(self, data): preview_file = PreviewFile.get_by(shotgun_id=data["shotgun_id"]) if preview_file is None: preview_file = PreviewFile.get_by(name=data["name"], task_id=data["task_id"]) if preview_file is None: preview_file = PreviewFile(**data) preview_file.save() current_app.logger.info("PreviewFile created: %s" % preview_file) else: preview_file.update(data) current_app.logger.info("PreviewFile updated: %s" % preview_file) return preview_file
def get_preview_file_raw(preview_file_id): """ Get preview file as active record. """ try: preview_file = PreviewFile.get(preview_file_id) except StatementError: raise PreviewFileNotFoundException() if preview_file is None: raise PreviewFileNotFoundException() return preview_file
def retrieve_file(data): if data.get("sync", False): return try: preview_file_id = data["preview_file_id"] preview_file = PreviewFile.get(preview_file_id) download_preview_from_another_instance(preview_file) forward_event({"name": "preview-file:add-file", "data": data}) logger.info("Preview file and related downloaded: %s" % preview_file_id) except gazu.exception.RouteNotFoundException as e: logger.error("Route not found: %s" % e) logger.error("Fail to dowonload preview file: %s" % (preview_file_id))
def update_entity_preview(entity_id, preview_file_id): entity = Entity.get(entity_id) if entity is None: raise EntityNotFoundException preview_file = PreviewFile.get(preview_file_id) if preview_file is None: raise PreviewFileNotFoundException entity.update({"preview_file_id": preview_file.id}) events.emit("preview-file:set-main", { "entity_id": entity_id, "preview_file_id": preview_file_id }) return entity.serialize()
def create_preview_file_raw(name, revision, task_id, person_id, source="webgui", extension="mp4", position=1): return PreviewFile.create(name=name, revision=revision, source=source, task_id=task_id, person_id=person_id, extension=extension, position=position, status="processing")
def generate_fixture_preview_file( self, revision=1, name="main", position=1, status="ready" ): self.preview_file = PreviewFile.create( name=name, revision=revision, description="test description", source="pytest", task_id=self.task.id, extension="mp4", person_id=self.person.id, position=position, status=status, ) return self.preview_file
def update_preview_file_position(preview_file_id, position): """ Change positions for preview files of given task and revision. Given position is the new position for given preview file. :q """ preview_file = files_service.get_preview_file_raw(preview_file_id) task_id = preview_file.task_id revision = preview_file.revision preview_files = (PreviewFile.query.filter_by( task_id=task_id, revision=revision).order_by(PreviewFile.position, PreviewFile.created_at).all()) if position > 0 and position <= len(preview_files): tmp_list = [p for p in preview_files if str(p.id) != preview_file_id] tmp_list.insert(position - 1, preview_file) for (i, preview) in enumerate(tmp_list): preview.update({"position": i + 1}) return PreviewFile.serialize_list(preview_files)
def get_playlist_with_preview_file_revisions(playlist_id): """ Return given playlist. Shot list is augmented with all previews available for a given shot. """ playlist = Playlist.get(playlist_id) if playlist is None: raise PlaylistNotFoundException() playlist_dict = playlist.serialize() if playlist_dict["shots"] is None: playlist_dict["shots"] = [] for shot in playlist_dict["shots"]: try: preview_file = PreviewFile.get(shot["preview_file_id"]) if preview_file is None or preview_file.extension != 'mp4': preview_file = PreviewFile.query \ .filter_by(task_id=preview_file.task_id) \ .filter_by(extension="mp4") \ .join(Task) \ .join(TaskType) \ .order_by(TaskType.priority.desc()) \ .order_by(TaskType.name) \ .order_by(PreviewFile.revision.desc()) \ .first() if preview_file is not None: shot["preview_file_id"] = str(preview_file.id) shot["extension"] = preview_file.extension shot["annotations"] = preview_file.annotations shot["task_id"] = fields.serialize_value(preview_file.task_id) else: del shot["preview_file_id"] except Exception as e: print(e) try: shot["preview_files"] = get_preview_files_for_shot(shot["shot_id"]) except ShotNotFoundException: playlist_dict["shots"].remove(shot) return playlist_dict
def update_entity_preview(entity_id, preview_file_id): """ Update given entity main preview. If entity or preview is not found, it raises an exception. """ entity = Entity.get(entity_id) if entity is None: raise EntityNotFoundException preview_file = PreviewFile.get(preview_file_id) if preview_file is None: raise PreviewFileNotFoundException entity.update({"preview_file_id": preview_file.id}) events.emit("preview-file:set-main", { "entity_id": entity_id, "preview_file_id": preview_file_id }) return entity.serialize()
def remove_comment(comment_id): """ Remove a comment from database and everything related (notifs, news, and preview files) """ comment = Comment.get(comment_id) if comment is not None: task = Task.get(comment.object_id) notifications = Notification.query.filter_by(comment_id=comment.id) for notification in notifications: notification.delete() news_list = News.query.filter_by(comment_id=comment.id) for news in news_list: news.delete() if comment.preview_file_id is not None: preview_file = PreviewFile.get(comment.preview_file_id) comment.preview_file_id = None comment.save() remove_preview_file(preview_file) previews = [preview for preview in comment.previews] comment.delete() for preview in previews: remove_preview_file(preview) if task is not None: events.emit( "comment:delete", {"comment_id": comment.id}, project_id=str(task.project_id), ) return comment.serialize() else: raise CommentNotFoundException
def remove_preview_file_by_id(preview_file_id): preview_file = PreviewFile.get(preview_file_id) return remove_preview_file(preview_file)