def handle(self, *args, **options): if len(args) == 0: medias = Media.objects.filter(is_local=False) logger.info("Atualizando todos as copias locais dos medias") else: since = int(args[0]) some_day_ago = timezone.now().date() - timedelta(days=since) logger.info("Atualizando os medias desde o dia: " + str(some_day_ago)) medias = Media.objects.filter(date__gte=some_day_ago) for media in medias: try: # Synchronize/update tags. # # 1) Add all tags found in the git-annex metadata and not # already present on the media. # 2) If tags from other mucuas have been deleted (are missing in # the git_annex metadata tags), remove them from this media. tags_on_media = set(git_annex_list_tags(media)) existing_tags = set( (t.namespace, t.name) for t in media.tags.all()) # Add new tags to media for t in tags_on_media - existing_tags: # Add tag - search for existing, if none found create new tag. namespace, name = t try: tag = Tag.objects.get(name=unicode(name), namespace=unicode(namespace)) except Tag.DoesNotExist: tag = Tag(name=name, namespace=namespace) tag.save() media.tags.add(tag) # Remove tags that were removed on remote media for t in existing_tags - tags_on_media: namespace, name = t tag = Tag.objects.get(name=name, namespace=namespace) media.tags.remove(tag) media.save(is_syncing=True) except OSError, e: logger.debug('Requested media not found: ' + media.name)
def handle(self, *args, **options): if len(args) == 0: medias = Media.objects.filter(is_local=False) logger.info("Atualizando todos as copias locais dos medias") else: since = int(args[0]) some_day_ago = timezone.now().date() - timedelta(days=since) logger.info("Atualizando os medias desde o dia: " + str(some_day_ago)) medias = Media.objects.filter(date__gte=some_day_ago) for media in medias: try: # Synchronize/update tags. # # 1) Add all tags found in the git-annex metadata and not # already present on the media. # 2) If tags from other mucuas have been deleted (are missing in # the git_annex metadata tags), remove them from this media. tags_on_media = set(git_annex_list_tags(media)) existing_tags = set((t.namespace, t.name) for t in media.tags.all()) # Add new tags to media for t in tags_on_media - existing_tags: # Add tag - search for existing, if none found create new tag. namespace, name = t try: tag = Tag.objects.get(name=unicode(name), namespace=unicode(namespace)) except Tag.DoesNotExist: tag = Tag(name=name, namespace=namespace) tag.save() media.tags.add(tag) # Remove tags that were removed on remote media for t in existing_tags - tags_on_media: namespace, name = t tag = Tag.objects.get(name=name, namespace=namespace) media.tags.remove(tag) media.save(is_syncing=True) except OSError, e: logger.debug('Requested media not found: ' + media.name)
def add_and_synchronize_tags(media, tags, mucua): """ Add tags to media, synchronize with git-annex repository. """ # Remove tags that originate in this mucua for tag in media.tags.filter(namespace__contains=mucua.uuid): media.tags.remove(tag) # Now add each tag in list. for tag in tags: if not tag or tag.isspace(): continue try: tag = tag.strip() tag = Tag.objects.filter(name=tag)[0] except (Tag.DoesNotExist, IndexError): tag = Tag(name=tag, namespace=mucua.uuid + "-tag") # TODO: Handle namespaces! tag.save() media.tags.add(tag) # Synchronize tags # First, add new ones as metadata on files. tags = media.tags.all() existing_tags = git_annex_list_tags(media) for t in media.tags.all(): if (t.namespace, t.name) not in existing_tags: git_annex_add_tag(media, t.namespace, t.name) # Then, *remove* tags that are no longer present. # Only remove tags set with present namespace! for namespace, name in existing_tags: if namespace.startswith(mucua.uuid) and not (namespace, name) in [ (t.namespace, t.name) for t in tags ]: git_annex_remove_tag(media, namespace, name)
def create_objects_from_files(repository=get_default_repository().name): """Recria os midias no Django a partir dos medias serializados em JSON.""" try: repository = Repository.objects.get( name=repository) except Repository.DoesNotExist: return None logger.info(u">>> %s" % _('DESERIALIZING')) logger.info(u"%s: %s" % (_('Repository'), repository)) #logger.debug(u"%s \n %s" % (_('List of media found in repository..'), get_latest_media(repository))) try: for serialized_media in get_latest_media(repository): logger.info(u"%s: %s" % (_('Serialized Media'), serialized_media)) media_json_file_path = os.path.join(REPOSITORY_DIR, repository.name, serialized_media) if os.path.isfile(media_json_file_path): with open(media_json_file_path) as media_json_file: try: data = JSONParser().parse(media_json_file) except: print u"Problem parsing JSON: " + media_json_file.read() continue try: media = Media.objects.get(uuid=data["uuid"]) serializer = MediaSerializer(media, data=data, partial=True) print serializer.is_valid() print serializer.errors serializer.object.save(is_syncing=True) logger.info(u"%s" % _('This media already exist. Updated.')) except Media.DoesNotExist: serializer = MediaSerializer(data=data) print serializer.is_valid() print serializer.errors serializer.object.save(is_syncing=True) media = serializer.object logger.info(u"%s" % _('New media created')) # Synchronize/update tags. # # 1) Add all tags found in the git-annex metadata and not # already present on the media. # 2) If tags from other mucuas have been deleted (are missing in # the git_annex metadata tags), remove them from this media. tags_on_media = set(git_annex_list_tags(media)) existing_tags = set((t.namespace, t.name) for t in media.tags.all()) # Add new tags to media for t in tags_on_media - existing_tags: # Add tag - search for existing, if none found create new tag. namespace, name = t try: tag = Tag.objects.get(name=unicode(name), namespace=unicode(namespace)) except Tag.DoesNotExist: tag = Tag(name=name, namespace=namespace) tag.save() media.tags.add(tag) # Remove tags that were removed on remote media for t in existing_tags - tags_on_media: namespace, name = t tag = Tag.objects.get(name=name, namespace=namespace) media.tags.remove(tag) except CommandError: pass
def create_objects_from_files(repository=get_default_repository().name): """Recria os midias no Django a partir dos medias serializados em JSON.""" try: repository = Repository.objects.get(name=repository) except Repository.DoesNotExist: return None logger.info(u">>> %s" % _('DESERIALIZING')) logger.info(u"%s: %s" % (_('Repository'), repository)) #logger.debug(u"%s \n %s" % (_('List of media found in repository..'), get_latest_media(repository))) try: for serialized_media in get_latest_media(repository): logger.info(u"%s: %s" % (_('Serialized Media'), serialized_media)) media_json_file_path = os.path.join(REPOSITORY_DIR, repository.name, serialized_media) if os.path.isfile(media_json_file_path): with open(media_json_file_path) as media_json_file: try: data = JSONParser().parse(media_json_file) except: print u"Problem parsing JSON: " + media_json_file.read( ) continue try: media = Media.objects.get(uuid=data["uuid"]) serializer = MediaSerializer(media, data=data, partial=True) print serializer.is_valid() print serializer.errors serializer.object.save(is_syncing=True) logger.info(u"%s" % _('This media already exist. Updated.')) except Media.DoesNotExist: serializer = MediaSerializer(data=data) print serializer.is_valid() print serializer.errors serializer.object.save(is_syncing=True) media = serializer.object logger.info(u"%s" % _('New media created')) # Synchronize/update tags. # # 1) Add all tags found in the git-annex metadata and not # already present on the media. # 2) If tags from other mucuas have been deleted (are missing in # the git_annex metadata tags), remove them from this media. tags_on_media = set(git_annex_list_tags(media)) existing_tags = set( (t.namespace, t.name) for t in media.tags.all()) # Add new tags to media for t in tags_on_media - existing_tags: # Add tag - search for existing, if none found create new tag. namespace, name = t try: tag = Tag.objects.get(name=unicode(name), namespace=unicode(namespace)) except Tag.DoesNotExist: tag = Tag(name=name, namespace=namespace) tag.save() media.tags.add(tag) # Remove tags that were removed on remote media for t in existing_tags - tags_on_media: namespace, name = t tag = Tag.objects.get(name=name, namespace=namespace) media.tags.remove(tag) except CommandError: pass