def re_index(self, request, pk=None): """Re-index blob""" volume = get_object_for_user_or_404(request.user, 'p2_core.use_volume', pk=pk) signatures = [] for blob in volume.blob_set.all(): signatures.append( signal_marshall.s('p2.core.signals.BLOB_PAYLOAD_UPDATED', kwargs={ 'blob': { 'class': class_to_path(blob.__class__), 'pk': blob.uuid.hex, } })) signatures.append( signal_marshall.s('p2.core.signals.BLOB_POST_SAVE', kwargs={ 'blob': { 'class': class_to_path(blob.__class__), 'pk': blob.uuid.hex, } })) result = group(*signatures)().get() # Since each blob needs two calls we return the length divided by two return Response(len(result) / 2)
def save(self, *args, **kwargs): """Name file on storage after generated UUID and populate initial attributes""" # Save current time as `created` attribute. This can be changed by users, # but p2.log creates a log entry for new Blob being created if ATTR_BLOB_STAT_CTIME not in self.attributes: self.attributes[ATTR_BLOB_STAT_CTIME] = now() # Create/update `date_updated` attribute self.attributes[ATTR_BLOB_STAT_MTIME] = now() # Only save payload if it changed if self._writing_handle: self._writing_handle.seek(0) self.volume.storage.controller.commit(self, self._writing_handle) # Check if path exists already self.__failsafe_path() # Update prefix self.__update_prefix() self.volume.storage.controller.collect_attributes(self) super().save(*args, **kwargs) if self._writing_handle: signal_marshall.delay('p2.core.signals.BLOB_PAYLOAD_UPDATED', kwargs={ 'blob': { 'class': class_to_path(self.__class__), 'pk': self.uuid.hex, } })
def setUp(self): self.storage = get_test_storage() self.volume = Volume.objects.create(name='p2-unittest-public-access', storage=self.storage) self.component = Component.objects.create( volume=self.volume, controller_path=class_to_path(PublicAccessController))
def component(self, class_or_path): """Get component instance for class or class path. Return None if component not confugued.""" if not isinstance(class_or_path, str): class_or_path = class_to_path(class_or_path) component = self.component_set.filter(controller_path=class_or_path, enabled=True) if component.exists(): return component.first() return None
def get_test_storage(): """Get fully instantiated storage""" storage_name = 'p2-unittest' storage, _ = Storage.objects.get_or_create( name=storage_name, controller_path=class_to_path(LocalStorageController), defaults={'tags': { TAG_ROOT_PATH: mkdtemp() }}) return storage
def setUp(self): self.storage_path = './storage/local-unittest/' self.storage = Storage.objects.create( name='local-storage-1', controller_path=class_to_path(LocalStorageController), tags={ TAG_ROOT_PATH: self.storage_path }) self.volume = Volume.objects.create( name='local-volume-1', storage=self.storage)
def setUp(self): self.storage = get_test_storage() self.volume = Volume.objects.create( name='test-volume', storage=self.storage) self.image_component = Component.objects.create( volume=self.volume, controller_path=class_to_path(ImageController), tags={ TAG_IMAGE_EXIF_TAGS: [EXIF_MODEL] })
def create_default_storage(apps, schema_editor): from django.contrib.auth.models import User from django.conf import settings from p2.lib.reflection import class_to_path from p2.storage.local.constants import TAG_ROOT_PATH from guardian.conf import settings as guardian_settings from p2.storage.local.controller import LocalStorageController Storage = apps.get_model('p2_core', 'Storage') Volume = apps.get_model('p2_core', 'Volume') # Use local storage if we're in debug storage_path = '/storage/' if not settings.DEBUG else './storage/' # Only apply if no storage exists if Storage.objects.all().exists(): return storage = Storage.objects.create( name='local-storage', controller_path=class_to_path(LocalStorageController), tags={ # /storage/ is always mounted TAG_ROOT_PATH: storage_path }) Volume.objects.create(name='default-volume', storage=storage)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) components = [] existing_components = get_objects_for_user(self.request.user, 'p2_core.view_component') for controller in COMPONENT_MANAGER.list(): controller_path = class_to_path(controller) # Check if component for this volume is configure existing_component = existing_components.filter( controller_path=controller_path, volume=self.object) if existing_component.exists(): components.append(existing_component.first()) else: # Create an in-memory Object with the controller_path assigned _component = Component() _component.controller_path = controller_path _component.volume = self.object # Set an extra attribute so the template can reflect it _component.configured = False _component.enabled = False components.append(_component) context['components'] = components return context
def as_choices(self): """Get list of tuples of (path to class, class name)""" self.__actual_load() for cls in self.__class_list: yield (class_to_path(cls), cls.__name__)
def run_expire(self): """Remove blobs which have expired""" LOGGER.debug("Running expiry...") for component in Component.objects.filter( controller_path=class_to_path(ExpiryController), enabled=True): component.controller.expire_volume(component.volume)
def component_post_save(sender, instance, **kwargs): """Trigger initial sync after we've been saved""" if instance.controller_path == class_to_path(ReplicationController): initial_full_replication.delay(instance.volume.pk)