def auto_transition_process_workflow(root: IRoot, registry: Registry): """Automatically transition workflow states of processes.""" date_now = now() processes = _get_processes_with_auto_transition(root, registry) for process in processes: is_outdated = _state_is_outdated(process, registry, date_now) if is_outdated: _do_auto_transition(process, registry)
def activity(self, activity): from adhocracy_core.interfaces import ActivityType from adhocracy_core.utils import now return activity._replace(subject=testing.DummyResource(), type=ActivityType.add, object=testing.DummyResource(), target=testing.DummyResource(), published=now())
def activity(self, activity): from adhocracy_core.interfaces import ActivityType from adhocracy_core.utils import now return activity._replace( subject=testing.DummyResource(), type=ActivityType.add, object=testing.DummyResource(), target=testing.DummyResource(), published=now() )
def test_path_is_not_reset_password(self, node, request_, context, registry, mock_sheet): from adhocracy_core.utils import now user = testing.DummyResource() mock_sheet.get.return_value = {'creator': user, 'creation_date': now()} registry.content.get_sheet.return_value = mock_sheet validator = self.call_fut(node, {'request': request_, 'context': context}) context['reset'] = testing.DummyResource() with raises(colander.Invalid): validator(node, context['reset'])
def test_path_is_reset_password(self, node, request_, context, registry, mock_sheet): from adhocracy_core.resources.principal import IPasswordReset from adhocracy_core.utils import now user = testing.DummyResource() mock_sheet.get.return_value = {'creator': user, 'creation_date': now()} registry.content.get_sheet.return_value = mock_sheet validator = self.call_fut(node, {'request': request_, 'context': context}) context['reset'] = testing.DummyResource(__provides__=IPasswordReset) validator(node, context['reset']) assert request_.validated['user'] is user
def test_path_is_reset_password_but_8_days_old( self, node, request_, context, registry, mock_sheet): import datetime from adhocracy_core.resources.principal import IPasswordReset from adhocracy_core.utils import now user = testing.DummyResource() creation_date = now() - datetime.timedelta(days=7) mock_sheet.get.return_value = {'creator': user, 'creation_date': creation_date} registry.content.get_sheet.return_value = mock_sheet validator = self.call_fut(node, {'request': request_, 'context': context}) context['reset'] = testing.DummyResource(__provides__=IPasswordReset) with raises(colander.Invalid): validator(node, context['reset'])
def next_name(self, subobject, prefix='') -> str: """Generate name to add subobject to the folder. This method does: - increment the last generated name associated to the prefix. - zero-filling the left hand side of the result with 7 zeros. - add prefix to the left hand side if any If the generated Name exists add timestamp to the right side. """ number = self._get_next_number(prefix) name = prefix + self._zfill(number) if name in self.data: timestamp = now().isoformat() name += '_' + timestamp return name
def _create_activities(changes: [ChangelogMetadata], request: Request) -> [Activity]: activities = [] for change in changes: activity_type = _get_entry_name(change) sheets = _get_content_sheets(change, request) sheet_data = _get_sheet_data(sheets) object = _get_object(change) target = _get_target(object) activity = Activity()._replace(subject=request.user, type=activity_type, object=object, sheet_data=sheet_data, target=target, published=now(), ) activities.append(activity) return activities
def delete_not_referenced_images( root, max_age: int, ): """Delete images older than `max_age` that are not referenced.""" catalogs = find_service(root, 'catalogs') max_date = now() - timedelta(days=max_age) query = search_query._replace( interfaces=IImage, resolve=True, indexes={'item_creation_date': (FieldComparator.lt.value, max_date)}) images = catalogs.search(query).elements msg = 'Found {0} images older then {1} days'.format(len(images), max_age) logger.info(msg) for image in images: picture_reference = Reference(None, IImageReference, '', image) query = search_query._replace(references=(picture_reference, )) referencing = catalogs.search(query) if referencing.count > 0: msg = 'Deleting image {0} that is not referenced'.format(image) logger.info(msg) del image.__parent__[image.__name__]
def _delete_not_referenced_images(root, max_age: int, ): catalogs = find_service(root, 'catalogs') max_date = now() - timedelta(days=max_age) query = search_query._replace(interfaces=IImage, resolve=True, indexes={'item_creation_date': (FieldComparator.lt.value, max_date) } ) images = catalogs.search(query).elements msg = 'Found {0} images older then {1} days'.format(len(images), max_age) logger.info(msg) for image in images: picture_reference = Reference(None, IImageReference, '', image) query = search_query._replace(references=(picture_reference,)) referencing = catalogs.search(query) if referencing.count > 0: msg = 'Deleting image {0} that is not referenced'.format(image) logger.info(msg) del image.__parent__[image.__name__]
def is_older_than(resource: IMetadata, days: int) -> bool: """Check if the creation date of `context` is older than `days`.""" timedelta = now() - resource.creation_date return timedelta.days > days
def mock_now(self, monkeypatch): from adhocracy_core.utils import now date = now() monkeypatch.setattr('adhocracy_core.schema.now', lambda: date) return date
def mock_now(monkeypatch): from adhocracy_core.utils import now date = now() monkeypatch.setattr('adhocracy_core.schema.now', lambda: date) return date
def mock_mod_date(self, monkeypatch): from adhocracy_core.utils import now date = now() monkeypatch.setattr('adhocracy_core.resources.get_modification_date', lambda x: date) return date
def _raise_if_outdated(node: SchemaNode, value: IPasswordReset, creation_date: datetime): if (now() - creation_date).days >= 7: value.__parent__ = None # commit_suicide msg = 'This password reset is older than 7 days.' raise colander.Invalid(node, msg)
def deferred_date_default(node: colander.MappingSchema, kw: dict) -> datetime: """Return current date.""" return now()
def _raise_if_outdated(node: SchemaNode, value: IPasswordReset, creation_date: datetime): if (now() - creation_date).days >= 7: value.__parent__ = None # commit_suicide msg = 'This password reset is older than 7 days.' raise Invalid(node, msg)