def validate_root_versions(node: SchemaNode, value: list): """Validate root versions.""" for root_version in value: if not IItemVersion.providedBy(root_version): msg = 'This resource is not a valid ' \ 'root version: {}'.format(resource_path(root_version)) raise Invalid(node, msg=msg)
def validate_root_versions(node: SchemaNode, value: list): # pragma: no cover """Validate root versions.""" for root_version in value: if not IItemVersion.providedBy(root_version): msg = 'This resource is not a valid ' \ 'root version: {}'.format(resource_path(root_version)) raise Invalid(node, msg=msg)
def _is_first_version(change: ChangelogMetadata, registry) -> bool: is_version = IItemVersion.providedBy(change.resource) if is_version: follows = registry.content.get_sheet_field(change.resource, IVersionable, 'follows') return follows == [] else: return False
def download_external_picture_for_created(event: IResourceCreatedAndAdded): """Download external_picture_url for new resources.""" if IItemVersion.providedBy(event.object): return # download_external_picture_for_version takes care for it registry = event.registry new_url = registry.content.get_sheet_field(event.object, IImageReference, 'external_picture_url') _download_picture_url(event.object, '', new_url, registry)
def _only_version_zero_exists(self, context: object, meta: ResourceMetadata) -> bool: only_first_version = False is_item_version = meta.iresource.isOrExtends(IItemVersion) has_item_parent = IItem.providedBy(context) if has_item_parent and is_item_version: children = context.values() versions = [x for x in children if IItemVersion.providedBy(x)] only_first_version = len(versions) == 1 return only_first_version
def test_create(self, context, registry, creator): from adhocracy_core.sheets.metadata import IMetadata from adhocracy_core.sheets.tags import ITags item = self.make_one(context, registry, creator=creator) version0 = item['VERSION_0000000'] assert IItemVersion.providedBy(version0) tags_sheet = registry.content.get_sheet(item, ITags) first = tags_sheet.get()['FIRST'] last = tags_sheet.get()['LAST'] assert first == version0 assert last == version0 meta_sheet = registry.content.get_sheet(version0, IMetadata) assert creator == meta_sheet.get()['creator']
def validate_post_root_versions(context, request: Request): """Check and transform the 'root_version' paths to resources.""" # TODO: make this a colander validator and move to schema.py # use the catalog to find IItemversions root_versions = request.validated.get("root_versions", []) valid_root_versions = [] for root in root_versions: if not IItemVersion.providedBy(root): error = "This resource is not a valid " "root version: {}".format(request.resource_url(root)) request.errors.append(error_entry("body", "root_versions", error)) continue valid_root_versions.append(root) request.validated["root_versions"] = valid_root_versions
def validate_post_root_versions(context, request: Request): """Check and transform the 'root_version' paths to resources.""" # TODO: make this a colander validator and move to schema.py root_versions = request.validated.get('root_versions', []) valid_root_versions = [] for root in root_versions: if not IItemVersion.providedBy(root): error = 'This resource is not a valid ' \ 'root version: {}'.format(request.resource_url(root)) request.errors.append(error_entry('body', 'root_versions', error)) continue valid_root_versions.append(root) request.validated['root_versions'] = valid_root_versions
def _get_entry_name(change: ChangelogMetadata) -> str: if change.created: if IItemVersion.providedBy(change.resource): return ActivityType.update else: return ActivityType.add elif change.visibility == VisibilityChange.concealed: return ActivityType.remove elif change.modified: if _is_workflow_state_change(change): return ActivityType.transition else: return ActivityType.update else: # pragma: no cover raise ValueError('Invalid change state', change)
def validate_post_root_versions(context, request: Request): """Check and transform the 'root_version' paths to resources.""" # TODO: make this a colander validator and move to schema.py # use the catalog to find IItemversions root_versions = request.validated.get('root_versions', []) valid_root_versions = [] for root in root_versions: if not IItemVersion.providedBy(root): error = 'This resource is not a valid ' \ 'root version: {}'.format(request.resource_url(root)) request.errors.append(error_entry('body', 'root_versions', error)) continue valid_root_versions.append(root) request.validated['root_versions'] = valid_root_versions
def test_create(self, context, registry): from adhocracy_core.sheets.tags import ITag as ITagS item = self.make_one(context, registry) version0 = item['VERSION_0000000'] assert IItemVersion.providedBy(version0) first_tag = item['FIRST'] assert ITag.providedBy(first_tag) last_tag = item['LAST'] assert ITag.providedBy(last_tag) first_targets = context.__graph__.get_references_for_isheet(first_tag, ITagS)['elements'] assert first_targets == [version0] last_targets = context.__graph__.get_references_for_isheet(last_tag, ITagS)['elements'] assert last_targets == [version0]
def _get_metadata(self, resource: IResource, creator: IResource, registry: Registry) -> dict: now = get_modification_date(registry) creator = creator if creator is not None else None metadata = {'creator': creator, 'creation_date': now, 'item_creation_date': now, 'modified_by': creator, 'modification_date': now, } item = find_interface(resource, IItem) if IItemVersion.providedBy(resource) and item is not None: item_sheet = get_sheet(item, IMetadata, registry=registry) item_creation_date = item_sheet.get()['creation_date'] metadata['item_creation_date'] = item_creation_date return metadata
def test_create(self, context, registry): from adhocracy_core.sheets.tags import ITag as ITagS item = self.make_one(context, registry) version0 = item['VERSION_0000000'] assert IItemVersion.providedBy(version0) first_tag = item['FIRST'] assert ITag.providedBy(first_tag) last_tag = item['LAST'] assert ITag.providedBy(last_tag) first_targets = context.__graph__.get_references_for_isheet( first_tag, ITagS)['elements'] assert first_targets == [version0] last_targets = context.__graph__.get_references_for_isheet( last_tag, ITagS)['elements'] assert last_targets == [version0]
def remove_tag_resources(root, registry): # pragma: no cover """Remove all ITag resources, create ITags sheet references instead.""" from adhocracy_core.sheets.tags import ITags from adhocracy_core.interfaces import IItem catalogs = find_service(root, 'catalogs') items = _search_for_interfaces(catalogs, IItem) items_with_tags = [x for x in items if 'FIRST' in x] count = len(items_with_tags) for index, item in enumerate(items_with_tags): logger.info('Migrate tag resource {0} of {1}'.format(index + 1, count)) del item['FIRST'] del item['LAST'] version_names = [x[0] for x in item.items() if IItemVersion.providedBy(x[1])] version_names.sort() # older version names are lower then younger ones first_version = version_names[0] last_version = version_names[-1] tags_sheet = registry.content.get_sheet(item, ITags) tags_sheet.set({'LAST': item[last_version], 'FIRST': item[first_version]})
def test_create(self, config, context): version_0 = self.make_one(config, context) assert IItemVersion.providedBy(version_0)
def _dispatch_created_event(self, resource: IResource): if IItemVersion.providedBy(resource): self._notify_new_version(resource.__parent__, resource) else: self._notify_new_child(resource.__parent__, resource)
def test_create(self, registry, item): version_0 = self.make_one(registry, item) assert IItemVersion.providedBy(version_0)
def _get_first_version(self, item: IItem) -> IItemVersion: for child in item.values(): if IItemVersion.providedBy(child): return child
def _creating_new_version(self) -> bool: iresource = self.request.validated['content_type'] return IItemVersion.isEqualOrExtendedBy(iresource)
def _get_object(change: ChangelogMetadata) -> IResource: if IItemVersion.providedBy(change.resource): item = find_interface(change.resource, IItem) return item else: return change.resource