def _create_user(context: IResource, registry: Registry, request: Request, user_data: dict) -> IUser: if _is_email_used(context, registry, request, user_data.get('email')): raise ValueError('User with ServiceKonto email already exists.') users = find_service(context, 'principals', 'users') groups = find_service(context, 'principals', 'groups') default_group = groups.get(DEFAULT_USER_GROUP_NAME, None) name = _generate_username(context, registry, request, user_data) appstruct = { IUserBasic.__identifier__: { 'name': name }, IUserExtended.__identifier__: { 'email': user_data.get('email') }, IPermissions.__identifier__: { 'groups': [default_group] }, IServiceKonto.__identifier__: { 'userid': int(user_data.get('userid')) }, IServiceKontoSettings.__identifier__: { 'enabled': True }, } user = registry.content.create(IUser.__identifier__, users, appstruct, registry=registry, send_event=False) user.activate() return user
def test_register_services(self, meta, registry, pool): from substanced.util import find_service from . import principal registry.content.create(meta.iresource.__identifier__, parent=pool) assert find_service(pool, 'principals', 'users') assert find_service(pool, 'principals', 'groups') assert find_service(pool, 'principals', 'resets')
def _import_users(context: IResource, registry: Registry, filename: str): users_info = _load_users_info(filename) users_info = [_normalize_user_info(u) for u in users_info] users = find_service(context, 'principals', 'users') groups = find_service(context, 'principals', 'groups') for user_info in users_info: user_by_name, user_by_email = _locate_user(user_info, context, registry) if user_by_name or user_by_email: logger.info('Updating user {} ({})'.format(user_info['name'], user_info['email'])) _update_user(user_by_name, user_by_email, user_info, groups, registry) else: logger.info('Creating user {}'.format(user_info['name'])) send_invitation = user_info.get('send_invitation_mail', False) activate = not send_invitation user = _create_user(user_info, users, registry, groups, activate=activate) if send_invitation: logger.info('Sending invitation mail to {}'.format(user.name)) _send_invitation_mail(user, user_info, registry) badge_names = user_info.get('badges', []) if badge_names: logger.info('Assign badge for user {}'.format(user.name)) badges = _create_badges(user, badge_names, registry) _assign_badges(user, badges, registry) transaction.commit()
def _add_initial_user_and_group(context, registry): settings = registry['config'] user_name = settings.adhocracy.initial_login user_password = settings.adhocracy.initial_password user_email = settings.adhocracy.initial_email group_name = settings.adhocracy.initial_group_name group_roles = ['god'] groups = find_service(context, 'principals', 'groups') appstructs = {adhocracy_core.sheets.principal.IGroup.__identifier__: {'roles': group_roles}, adhocracy_core.sheets.name.IName.__identifier__: {'name': group_name}, } group = registry.content.create(IGroup.__identifier__, groups, appstructs=appstructs, registry=registry) users = find_service(context, 'principals', 'users') password_sheet = adhocracy_core.sheets.principal.IPasswordAuthentication appstruct = {adhocracy_core.sheets.principal.IUserBasic.__identifier__: {'name': user_name}, adhocracy_core.sheets.principal.IUserExtended.__identifier__: {'email': user_email}, adhocracy_core.sheets.principal.IPermissions.__identifier__: {'groups': [group]}, password_sheet.__identifier__: {'password': user_password}, } user = registry.content.create(IUser.__identifier__, users, appstruct, run_after_creation=False, send_event=False, registry=registry) user.activate()
def _import_users(context: IResource, registry: Registry, filename: str): users_info = _load_users_info(filename) users_info = [_normalize_user_info(u) for u in users_info] users = find_service(context, 'principals', 'users') groups = find_service(context, 'principals', 'groups') for user_info in users_info: user_by_name, user_by_email = _locate_user(user_info, context, registry) if user_by_name or user_by_email: logger.info('Updating user {} ({})'.format(user_info['name'], user_info['email'])) _update_user(user_by_name, user_by_email, user_info, groups) else: logger.info('Creating user {}'.format(user_info['name'])) send_invitation = user_info.get('send_invitation_mail', False) activate = not send_invitation user = _create_user(user_info, users, registry, groups, activate=activate) if send_invitation: logger.info('Sending invitation mail to {}'.format(user.name)) _send_invitation_mail(user, user_info, registry) badge_names = user_info.get('badges', []) if badge_names: logger.info('Assign badge for user {}'.format(user.name)) badges = _create_badges(user, badge_names, registry) _assign_badges(user, badges, registry) transaction.commit()
def add_badge_assignments_services_to_proposal_items(root, registry): """Add badge assignments services to proposals.""" catalogs = find_service(root, 'catalogs') query = search_query._replace(interfaces=IMercatorProposal) proposals = catalogs.search(query).elements for proposal in proposals: if find_service(proposal, 'badge_assignments') is None: logger.info('add badge assignments to {0}'.format(proposal)) add_badge_assignments_service(proposal, registry, {})
def add_logbook_service_to_proposal_items(root): # pragma: no cover """Add logbook service to proposals.""" catalogs = find_service(root, 'catalogs') query = search_query._replace(interfaces=IMercatorProposal) proposals = catalogs.search(query).elements registry = get_current_registry(root) for proposal in proposals: if find_service(proposal, 'logbook') is None: logger.info('add logbook service to {0}'.format(proposal)) creator = get_sheet_field(proposal, IMetadata, 'creator') add_logbook_service(proposal, registry, {'creator': creator})
def lower_case_users_emails(root, registry): # pragma: no cover """Lower case users email, add 'private_user_email'/'user_name' index.""" _update_adhocracy_catalog(root) catalogs = find_service(root, 'catalogs') users = find_service(root, 'principals', 'users') for user in users.values(): if not IUserExtended.providedBy(user): return sheet = registry.content.get_sheet(user, IUserExtended) sheet.set({'email': user.email.lower()}) catalogs.reindex_index(user, 'private_user_email') catalogs.reindex_index(user, 'user_name')
def lower_case_users_emails(root): # pragma: no cover """Lower case users email, add 'private_user_email'/'user_name' index.""" _update_adhocracy_catalog(root) catalogs = find_service(root, "catalogs") users = find_service(root, "principals", "users") for user in users.values(): if not IUserExtended.providedBy(user): return sheet = get_sheet(user, IUserExtended) sheet.set({"email": user.email.lower()}) catalogs.reindex_index(user, "private_user_email") catalogs.reindex_index(user, "user_name")
def add_example_process(context: IPool, registry: Registry, options: dict): """Add meinberlin specific example content.""" registry = get_current_registry(context) # sample locations locations = find_service(context, 'locations') appstructs = {adhocracy_core.sheets.geo.IMultiPolygon.__identifier__: {'coordinates': POLYGON_ALT_TREPTOW, 'administrative_division': 'bezirksregion'}, adhocracy_core.sheets.name.IName.__identifier__: {'name': 'alt-treptow'}} kiezregion = registry.content.create(IMultiPolygon.__identifier__, parent=locations, appstructs=appstructs, registry=registry) # sample organisation appstructs = {adhocracy_core.sheets.name.IName.__identifier__: {'name': 'organisation'}} registry.content.create(IOrganisation.__identifier__, parent=context, appstructs=appstructs, registry=registry) # sample kiezkasse appstructs = {adhocracy_core.sheets.name.IName.__identifier__: {'name': 'kiezkasse'}, adhocracy_core.sheets.geo.ILocationReference.__identifier__: {'location': kiezregion}, adhocracy_core.sheets.title.ITitle.__identifier__: {'title': 'Sample Kiezkassen process'}} registry.content.create(resources.kiezkassen.IProcess.__identifier__, parent=context['organisation'], appstructs=appstructs, registry=registry) # sample bplan office_worker = None users = find_service(context, 'principals', 'users') if users is not None: # pragma: no cover # ease testing office_worker = users.values()[0] appstructs = {adhocracy_core.sheets.name.IName.__identifier__: {'name': 'bplan'}, adhocracy_core.sheets.title.ITitle.__identifier__: {'title': 'Sample BPlan process'}, adhocracy_meinberlin.sheets.bplan. IProcessSettings.__identifier__: {'office_worker': office_worker, 'plan_number': 112233, 'participation_kind': 'öffentliche Auslegung', 'participation_start_date': datetime.date(2015, 5, 5), 'participation_end_date': datetime.date(2015, 6, 11)}} registry.content.create(resources.bplan.IProcess.__identifier__, parent=context['organisation'], appstructs=appstructs, registry=registry)
def make_users_badgeable(root, registry): # pragma: no cover """Add badge services and make user badgeable.""" principals = find_service(root, 'principals') if not IHasBadgesPool.providedBy(principals): logger.info('Add badges service to {0}'.format(principals)) add_badges_service(principals, registry, {}) alsoProvides(principals, IHasBadgesPool) users = find_service(root, 'principals', 'users') assignments = find_service(users, 'badge_assignments') if assignments is None: logger.info('Add badge assignments service to {0}'.format(users)) add_badge_assignments_service(users, registry, {}) migrate_new_sheet(root, IUser, IBadgeable)
def get_assignable_badges(context: IBadgeable, request: Request) -> [IBadge]: """Get assignable badges for the IBadgeAssignment sheet.""" badges = find_service(context, 'badges') if badges is None: return [] catalogs = find_service(context, 'catalogs') query = search_query._replace( root=badges, interfaces=IBadge, allows=(request.effective_principals, 'assign_badge'), ) result = catalogs.search(query) return result.elements
def get_assignable_badges(context: IBadgeable, request: Request) -> [IBadge]: """Get assignable badges for the IBadgeAssignment sheet.""" badges = find_service(context, 'badges') if badges is None: return [] catalogs = find_service(context, 'catalogs') principals = request.effective_principals query = search_query._replace(root=badges, interfaces=IBadge, allows=(principals, 'assign_badge'), ) result = catalogs.search(query) return result.elements
def make_users_badgeable(root): # pragma: no cover """Add badge services and make user badgeable.""" registry = get_current_registry(root) principals = find_service(root, "principals") if not IHasBadgesPool.providedBy(principals): logger.info("Add badges service to {0}".format(principals)) add_badges_service(principals, registry, {}) alsoProvides(principals, IHasBadgesPool) users = find_service(root, "principals", "users") assignments = find_service(users, "badge_assignments") if assignments is None: logger.info("Add badge assignments service to {0}".format(users)) add_badge_assignments_service(users, registry, {}) migrate_new_sheet(root, IUser, IBadgeable)
def test_create_root_with_initial_content(self, registry): from adhocracy_core.resources.root import IRootPool from adhocracy_core.utils import find_graph from substanced.util import find_objectmap from substanced.util import find_catalog from substanced.util import find_service inst = registry.content.create(IRootPool.__identifier__) assert IRootPool.providedBy(inst) assert find_objectmap(inst) is not None assert find_graph(inst) is not None assert find_graph(inst)._objectmap is not None assert find_catalog(inst, 'system') is not None assert find_catalog(inst, 'adhocracy') is not None assert find_service(inst, 'principals', 'users') is not None assert find_service(inst, 'locations') is not None
def reindex_item_badge(event): """Reindex `item_badge` for all item versions of èvent.object.""" catalogs = find_service(event.object, 'catalogs') children = event.object.values() versionables = (c for c in children if IVersionable.providedBy(c)) for versionable in versionables: catalogs.reindex_index(versionable, 'item_badge')
def add_arbitrary_filter_nodes(cstruct: dict, schema: GETPoolRequestSchema, context: IResource, registry) -> GETPoolRequestSchema: """Add schema nodes for arbitrary/references filters to `schema`.""" extra_filters = [(k, v) for k, v in cstruct.items() if k not in schema] if extra_filters: schema = schema.clone() catalogs = find_service(context, 'catalogs') for filter_name, query in extra_filters: if _is_reference_filter(filter_name, registry): index_name = 'reference' elif filter_name == 'sheet': index_name = 'interfaces' elif filter_name == 'content_type': index_name = 'interfaces' elif _is_arbitrary_filter(filter_name, catalogs): index_name = filter_name else: continue # pragma: no cover index = catalogs.get_index(index_name) example_value = _get_index_example_value(index) node = create_arbitrary_filter_node(index, example_value, query) _add_node(schema, node, filter_name) return schema
def add_user(root, login: str = None, password: str = None, email: str = None, roles=None, registry=None) -> str: """Add user to :app:`Pyramid`.""" from substanced.util import find_service from adhocracy_core.resources.principal import IUser import adhocracy_core.sheets users = find_service(root, 'principals', 'users') roles = roles or [] passwd_sheet = adhocracy_core.sheets.principal.IPasswordAuthentication appstructs =\ {adhocracy_core.sheets.principal.IUserBasic.__identifier__: {'name': login}, adhocracy_core.sheets.principal.IUserExtended.__identifier__: {'email': email}, adhocracy_core.sheets.principal.IPermissions.__identifier__: {'roles': roles}, passwd_sheet.__identifier__: {'password': password}, } user = registry.content.create( IUser.__identifier__, parent=users, appstructs=appstructs, registry=registry, run_after_creation=False, ) user.activate() return user
def use_workflow_state_for_participation_time_range(root, registry): """use workflow state data for participation start and end.""" from adhocracy_core.sheets.workflow import IWorkflowAssignment from adhocracy_meinberlin.resources.bplan import IProcess from adhocracy_meinberlin.sheets.bplan import IProcessSettings catalogs = find_service(root, 'catalogs') bplaene = _search_for_interfaces(catalogs, IProcess) for bplan in bplaene: process_settings_sheet = bplan.\ _sheet_adhocracy_meinberlin_sheets_bplan_IProcessSettings if ('participation_start_date' in process_settings_sheet and 'participation_end_date' in process_settings_sheet): participation_start_date = \ process_settings_sheet['participation_start_date'] participation_end_date = \ process_settings_sheet['participation_end_date'] process_settings = registry.content.get_sheet(bplan, IProcessSettings) process_settings.delete_field_values(['participation_start_date']) process_settings.delete_field_values(['participation_end_date']) workflow_assignment = registry.content.get_sheet( bplan, IWorkflowAssignment) workflow_assignment.set( {'state_data': [{'name': 'participate', 'description': '', 'start_date': participation_start_date, 'end_date': participation_end_date}]})
def create_user(request, appstruct): if appstruct and 'user_data' in appstruct: source_data = appstruct.get('source_data', {}) data = appstruct.get('user_data', {}) root = getSite() locale = my_locale_negotiator(request) data['locale'] = locale person = Person(**data) person.set_source_data(source_data) principals = find_service(root, 'principals') name = person.first_name + ' ' + person.last_name users = principals['users'] name = name_chooser(users, name=name) users[name] = person grant_roles(person, roles=('Member',)) grant_roles(person, (('Owner', person),)) person.state.append('active') person.init_annotations() person.annotations.setdefault( PROCESS_HISTORY_KEY, PersistentList()) person.reindex() root.addtoproperty('news_letter_members', person) newsletters = root.get_newsletters_automatic_registration() email = getattr(person, 'email', '') if newsletters and email: for newsletter in newsletters: newsletter.subscribe( person.first_name, person.last_name, email) transaction.commit() return person return None
def move_autoname_last_counters_to_attributes(root): # pragma: no cover """Move autoname last counters of pools to attributes. Remove _autoname_lasts attribute. Instead add private attributes to store autoname last counter objects. Cleanup needless counter objects. """ registry = get_current_registry(root) prefixes = _get_autonaming_prefixes(registry) catalogs = find_service(root, 'catalogs') pools = _search_for_interfaces(catalogs, (IPool, IFolder)) count = len(pools) for index, pool in enumerate(pools): logger.info('Migrating resource {0} {1} of {2}' .format(pool, index + 1, count)) if hasattr(pool, '_autoname_last'): logger.info('Remove "_autoname_last" attribute') delattr(pool, '_autoname_last') if hasattr(pool, '_autoname_lasts'): used_prefixes = _get_used_autonaming_prefixes(pool, prefixes) for prefix in used_prefixes: is_badge_service = IBadgeAssignmentsService.providedBy(pool) if prefix == '' and not is_badge_service: continue logger.info('Move counter object for prefix {0} to attribute' .format(prefix)) counter = pool._autoname_lasts.get(prefix, Length()) setattr(pool, '_autoname_last_' + prefix, counter) logger.info('Remove "_autoname_lasts" attribute') delattr(pool, '_autoname_lasts')
def get_roles(user=None, obj=None, root=None, ignore_groups=False): if user is None: user = get_current() if isinstance(user, Anonymous): return [RoleAnonymous.name] if root is None: root = getSite() if obj is None: obj = root opts = {u'source_id': get_oid(user), u'target_id': get_oid(obj)} opts[u'reftype'] = 'Role' roles = [r.relation_id for r in find_relations(obj, opts).all()] principals = find_service(root, 'principals') sd_admin = principals['users']['admin'] if sd_admin is user and 'Admin' not in roles: roles.append('Admin') groups = [] if not ignore_groups: groups.extend(getattr(user, 'groups', [])) for group in groups: roles.extend(get_roles(group, obj, root)) return list(set(roles))
def use_workflow_state_for_participation_time_range(root, registry): """Use workflow state data for participation start and end.""" from adhocracy_core.sheets.workflow import IWorkflowAssignment from adhocracy_meinberlin.resources.bplan import IProcess from adhocracy_meinberlin.sheets.bplan import IProcessSettings catalogs = find_service(root, 'catalogs') bplaene = _search_for_interfaces(catalogs, IProcess) for bplan in bplaene: process_settings_sheet = bplan.\ _sheet_adhocracy_meinberlin_sheets_bplan_IProcessSettings if ('participation_start_date' in process_settings_sheet and 'participation_end_date' in process_settings_sheet): participation_start_date = \ process_settings_sheet['participation_start_date'] participation_end_date = \ process_settings_sheet['participation_end_date'] process_settings = registry.content.get_sheet( bplan, IProcessSettings) process_settings.delete_field_values(['participation_start_date']) process_settings.delete_field_values(['participation_end_date']) workflow_assignment = registry.content.get_sheet( bplan, IWorkflowAssignment) workflow_assignment.set({ 'state_data': [{ 'name': 'participate', 'description': '', 'start_date': participation_start_date, 'end_date': participation_end_date }] })
def reset_comment_count(root, registry): # pragma: no cover """Reset comment_count for all ICommentables - See #2194, #2188.""" from adhocracy_core.resources.comment import ICommentVersion from adhocracy_core.sheets.comment import ICommentable from adhocracy_core.resources.subscriber import update_comments_count catalogs = find_service(root, 'catalogs') query = search_query._replace(interfaces=ICommentable, only_visible=True, resolve=True) commentables = catalogs.search(query).elements count = len(commentables) for index, comment in enumerate(commentables): logger.info('Set comment_count to 0 for resource {0} of {1}' .format(index + 1, count)) commentable_sheet = registry.content.get_sheet(comment, ICommentable) commentable_sheet.set({'comments_count': 0}, omit_readonly=False) query = search_query._replace(interfaces=ICommentVersion, only_visible=True, resolve=True, indexes={'tag': 'FIRST'}) comment_versions = catalogs.search(query).elements count = len(comment_versions) for index, comment in enumerate(comment_versions): logger.info('Recalculate comment_count for resource {0} of {1}' .format(index + 1, count)) update_comments_count(comment, 1, registry)
def get_access_keys(user, root=None, to_exclude=[]): if isinstance(user, Anonymous): return ['anonymous'] principals = find_service(user, 'principals') sd_admin = principals['users']['admin'] pricipal_root = getSite() if root is None: root = pricipal_root root_oid = get_oid(root) principal_root_oid = get_oid(pricipal_root) if sd_admin is user: return list(set([('admin'+'_'+str(root_oid)).lower(), ('admin'+'_'+str(principal_root_oid)).lower()])) groups = list(getattr(user, 'groups', [])) groups.append(user) relations = [] for group in groups: opts = {u'source_id': get_oid(group)} opts[u'reftype'] = 'Role' relations.extend(list(find_relations(group, opts).all())) result = [(t.relation_id+'_'+str(t.target_id)).lower() \ for t in relations if t.target_id not in to_exclude] for relation in relations: if relation.relation_id == 'Admin': result.append(('admin'+'_'+str(principal_root_oid)).lower()) break return list(set(result))
def access_keys_evolve(root, registry): principals = find_service(root, 'principals') for user in principals['users'].values(): if hasattr(user, 'reindex'): user.reindex() log.info('Access keys evolved.')
def test_create_relationsservice(self, context, registry): from adhocracy_core.resources.relation import IRelationsService from substanced.util import find_service res = registry.content.create(IRelationsService.__identifier__, context) assert IRelationsService.providedBy(res) assert find_service(context, 'relations')
def test_import_groups_update(self, registry, log): from adhocracy_core.scripts.ad_import_groups import import_groups (self._tempfd, filename) = mkstemp() with open(filename, 'w') as f: f.write( json.dumps([{ "name": "moderators-xyz", "roles": ["reader"] }, { "name": "reviewers-xyz", "roles": ["annotator"] }])) root = registry.content.create(IRootPool.__identifier__) groups = find_service(root, 'principals', 'groups') import_groups(root, registry, filename) with open(filename, 'w') as f: f.write( json.dumps([{ "name": "moderators-xyz", "roles": ["annotator"] }])) import_groups(root, registry, filename) moderators = groups.get('moderators-xyz', None) assert moderators is not None assert moderators.roles == ['annotator'] reviewers = groups.get('reviewers-xyz', None) assert reviewers is not None assert reviewers.roles == ['annotator']
def evolve1_add_ititle_sheet_to_proposals(root): # pragma: no cover """Migrate title value from ole IIntroduction sheet to ITitle sheet.""" registry = get_current_registry() catalog = find_catalog(root, 'system') path = catalog['path'] interfaces = catalog['interfaces'] query = path.eq('/mercator') \ & interfaces.eq(IMercatorProposalVersion) \ & interfaces.noteq(ITitle) proposals = query.execute() catalogs = find_service(root, 'catalogs') for proposal in proposals: logger.info('updating {0}'.format(proposal)) introduction = get_sheet_field(proposal, IMercatorSubResources, 'introduction') if introduction == '' or introduction is None: continue alsoProvides(proposal, ITitle) catalogs.reindex_index(proposal, 'interfaces') sheet = registry.content.get_sheet(introduction, IIntroduction) if 'title' not in sheet.get().keys(): continue value = sheet.get()['title'] title = registry.content.get_sheet(proposal, ITitle) title.set({'title': value}) sheet.delete_field_values(['title'])
def customeraccount_evolve(root, registry): principals = find_service(root, 'principals') for user in principals['users'].values(): if hasattr(user, 'add_customeraccount'): user.add_customeraccount() log.info('Customer account evolved.')
def create_badge_assignment(user: IUser, badge: IBadge, badgeable: sheets.badge.IBadgeable, description: str, registry: Registry) -> IBadgeAssignment: """Create badge assignment.""" assignment_appstruct = { 'subject': user, 'badge': badge, 'object': badgeable } appstructs = { sheets.badge.IBadgeAssignment.__identifier__: assignment_appstruct } if description != '': # pragma: no branch appstructs[sheets.description.IDescription.__identifier__] =\ {'description': description} assignments = find_service(badgeable, 'badge_assignments') if _assignment_exists(assignment_appstruct, assignments, registry): logger.warn('Assignment already exists, skipping. {}'.format( assignment_appstruct)) return None assignment = registry.content.create(IBadgeAssignment.__identifier__, parent=assignments, appstructs=appstructs) return assignment
def add_user(root, login: str=None, password: str=None, email: str=None, roles=None, registry=None) -> str: """Add user to :app:`Pyramid`.""" from substanced.util import find_service from adhocracy_core.resources.principal import IUser import adhocracy_core.sheets users = find_service(root, 'principals', 'users') roles = roles or [] passwd_sheet = adhocracy_core.sheets.principal.IPasswordAuthentication appstructs =\ {adhocracy_core.sheets.principal.IUserBasic.__identifier__: {'name': login}, adhocracy_core.sheets.principal.IUserExtended.__identifier__: {'email': email}, adhocracy_core.sheets.principal.IPermissions.__identifier__: {'roles': roles}, passwd_sheet.__identifier__: {'password': password}, } user = registry.content.create(IUser.__identifier__, parent=users, appstructs=appstructs, registry=registry, run_after_creation=False, ) user.activate() return user
def change_pools_autonaming_scheme(root): # pragma: no cover """Change pool autonaming scheme.""" registry = get_current_registry(root) prefixes = _get_autonaming_prefixes(registry) catalogs = find_service(root, 'catalogs') pools = _search_for_interfaces(catalogs, (IPool, IFolder)) count = len(pools) for index, pool in enumerate(pools): logger.info('Migrating {0} of {1}: {2}'.format(index + 1, count, pool)) if hasattr(pool, '_autoname_last'): pool._autoname_lasts = PersistentMapping() for prefix in prefixes: pool._autoname_lasts[prefix] = Length(pool._autoname_last + 1) del pool._autoname_last elif not hasattr(pool, '_autoname_lasts'): pool._autoname_lasts = PersistentMapping() for prefix in prefixes: pool._autoname_lasts[prefix] = Length() if hasattr(pool, '_autoname_lasts'): # convert int to Length for prefix in pool._autoname_lasts.keys(): if isinstance(pool._autoname_lasts[prefix], int): pool._autoname_lasts[prefix] \ = Length(pool._autoname_lasts[prefix].value) elif isinstance(pool._autoname_lasts[prefix].value, Length): pool._autoname_lasts[prefix] = Length(1) # convert dict to PersistentMapping if not isinstance(pool._autoname_lasts, PersistentMapping): pool._autoname_lasts = PersistentMapping(pool._autoname_lasts)
def add_activity_service_to_root(root, registry): # pragma: no cover """Add activity service to root.""" from adhocracy_core.resources.activity import add_activiy_service activity_stream = find_service(root, 'activity_stream') if activity_stream is None: logger.info('Add activity service to {0}'.format(root)) add_activiy_service(root, registry, {})
def change_bplan_officeworker_email_representation(root, registry): """Change bplan officeworker email representation.""" from substanced.util import find_objectmap from adhocracy_core.utils import find_graph from adhocracy_meinberlin.resources.bplan import IProcess from adhocracy_meinberlin.sheets.bplan import IProcessSettings from adhocracy_meinberlin.sheets.bplan import IProcessPrivateSettings from adhocracy_meinberlin.sheets.bplan import OfficeWorkerUserReference migrate_new_sheet(root, IProcess, IProcessPrivateSettings) catalogs = find_service(root, 'catalogs') bplaene = _search_for_interfaces(catalogs, IProcess) objectmap = find_objectmap(root) graph = find_graph(root) for bplan in bplaene: process_settings_ref = graph.get_references_for_isheet( bplan, IProcessSettings) if 'office_worker' in process_settings_ref: office_worker = process_settings_ref['office_worker'][0] private_settings = registry.content.get_sheet( bplan, IProcessPrivateSettings) private_settings.set({'office_worker_email': office_worker.email}) objectmap.disconnect(bplan, office_worker, OfficeWorkerUserReference)
def add_activities_to_activity_stream(event: IActivitiesGenerated): """Add activity resources to activity_stream.""" request = event.request settings = request.registry['config'] activity_stream_enabled = settings.adhocracy.activity_stream.enabled if not activity_stream_enabled: return activities = event.activities service = find_service(request.root, 'activity_stream') translate = get_localizer(request).translate for activity in activities: description = generate_activity_description(activity, request) description_full = translate(description) appstructs = { adhocracy_core.sheets.activity.IActivity.__identifier__: { 'subject': activity.subject, 'type': activity.type.value, 'object': activity.object, 'target': activity.target, 'name': description_full, 'published': activity.published, } } request.registry.content.create(IActivity.__identifier__, appstructs=appstructs, parent=service, registry=request.registry)
def _reindex_resource_and_descendants(resource: IResource): catalogs = find_service(resource, 'catalogs') if catalogs is None: # ease testing return resource_and_descendants = list_resource_with_descendants(resource) for res in resource_and_descendants: catalogs.reindex_index(res, 'private_visibility')
def delete_password_resets(request: Request, age_in_days: int): """Delete password resets that are older than `age_in_days`.""" resets = find_service(request.root, 'principals', 'resets') expired = [u for u in resets.values() if is_older_than(u, age_in_days)] for reset in expired: logger.info('deleting reset {0}'.format(reset)) del resets[reset.__name__]
def test_update_badges(self, context, registry, log): from adhocracy_core import sheets self._tempfd, filename = mkstemp() with open(filename, 'w') as f: f.write(json.dumps([ {'name': 'Alice', 'email': '*****@*****.**', 'initial-password': '******', 'roles': ['contributor'], 'groups': ['gods'], 'badges': ['Moderator', 'Beginner']}, ])) locator = self._get_user_locator(context, registry) self.call_fut(context, registry, filename) with open(filename, 'w') as f: f.write(json.dumps([ {'name': 'Alice', 'email': '*****@*****.**', 'badges': ['Expert']}])) self.call_fut(context, registry, filename) alice = locator.get_user_by_login('Alice') assignments = find_service(alice, 'badge_assignments').values() assert len(assignments) == 1 assignment = assignments[0] assignment_sheet = registry.content.get_sheet( assignment, sheets.badge.IBadgeAssignment) badge = context['principals']['badges']['expert'] assert assignment_sheet.get() == {'object': alice, 'badge': badge, 'subject': alice}
def update_comments_count(resource: ICommentVersion, delta: int, registry: Registry): """Update all commentable resources related to `resource`. Traverse all commentable resources that have a IComment or ISubresource reference to `resource` and update the comment_count value with `delta`. Example reference structure that is traversed: comment <-IComment- comment <-IComment- comment """ catalogs = find_service(resource, 'catalogs') traverse = ReferenceComparator.traverse.value query = search_query._replace( only_visible=True, references=((traverse, (resource, sheets.comment.IComment, '', None)), ), resolve=True, ) commentables = catalogs.search(query).elements for commentable in commentables: commentable_sheet = registry.content.get_sheet(commentable, ICommentable) old_count = commentable_sheet.get()['comments_count'] commentable_sheet.set({'comments_count': old_count + delta}, omit_readonly=False)
def add_pages_service_to_root(root, registry): # pragma: no cover """Add pages service to root.""" from adhocracy_core.resources.page import add_page_service pages = find_service(root, 'pages') if pages is None: logger.info('Add pages service to {0}'.format(root)) add_page_service(root, registry, {})
def start(self, context, request, appstruct, **kw): login = appstruct['email'] adapter = request.registry.queryMultiAdapter( (context, request), IUserLocator ) if adapter is None: adapter = DefaultUserLocator(context, request) user = adapter.get_user_by_email(login) if user is not None: principals = find_service(user, 'principals') reset = principals.add_reset(user) reseturl = request.resource_url(reset) if not user.email: raise ValueError('User does not possess a valid email address.') subject = RESETPW_SUBJECT.format(novaideo_title=request.root.title) localizer = request.localizer message = RESETPW_MESSAGE.format( recipient_title=localizer.translate(_(getattr(user, 'user_title',''))), recipient_first_name=getattr(user, 'first_name', user.name), recipient_last_name=getattr(user, 'last_name',''), reseturl=reseturl, novaideo_title=request.root.title ) mailer_send(subject=subject, recipients=[user.email], body=message) return {}
def _search_user(self, index_name: str, value: str) -> IUser: catalogs = find_service(self.context, 'catalogs') query = search_query._replace(indexes={index_name: value}, resolve=True) users = catalogs.search(query).elements if len(users) == 1: return users[0]
def start(self, context, request, appstruct, **kw): login = appstruct['email'] adapter = request.registry.queryMultiAdapter( (context, request), IUserLocator ) if adapter is None: adapter = DefaultUserLocator(context, request) user = adapter.get_user_by_email(login) if user is not None: principals = find_service(user, 'principals') reset = principals.add_reset(user) reseturl = request.resource_url(reset) email = user.email site = get_site_folder(True) localizer = request.localizer mail_template = site.get_mail_template('reset_password') subject = mail_template['subject'] message = mail_template['template'].format( person=user, user_title=localizer.translate( _(getattr(user, 'user_title', ''))), reseturl=reseturl, lac_title=request.root.title) alert('email', [site.get_site_sender()], [email], {'subject': subject, 'body': message}) return {}
def reindex_comments(root, registry): # pragma: no cover """Update comments index.""" from adhocracy_core.sheets.comment import ICommentable catalogs = find_service(root, 'catalogs') resources = _search_for_interfaces(catalogs, ICommentable) for resource in resources: catalogs.reindex_index(resource, 'comments')
def evolve1_add_ititle_sheet_to_proposals(root, registry): # pragma: no cover """Migrate title value from ole IIntroduction sheet to ITitle sheet.""" catalog = find_catalog(root, 'system') path = catalog['path'] interfaces = catalog['interfaces'] query = path.eq('/mercator') \ & interfaces.eq(IMercatorProposalVersion) \ & interfaces.noteq(ITitle) proposals = query.execute() catalogs = find_service(root, 'catalogs') for proposal in proposals: logger.info('updating {0}'.format(proposal)) introduction = registry.content.get_sheet_field(proposal, IMercatorSubResources, 'introduction') if introduction == '' or introduction is None: continue alsoProvides(proposal, ITitle) catalogs.reindex_index(proposal, 'interfaces') sheet = registry.content.get_sheet(introduction, IIntroduction) if 'title' not in sheet.get().keys(): continue value = sheet.get()['title'] title = registry.content.get_sheet(proposal, ITitle) title.set({'title': value}) sheet.delete_field_values(['title'])
def remove_workflow_state_data_end_date(root, registry): """Remove end_date from state_data and add the next state.""" from adhocracy_core.sheets.workflow import IWorkflowAssignment from adhocracy_meinberlin.resources.bplan import IProcess catalogs = find_service(root, 'catalogs') bplaene = _search_for_interfaces(catalogs, IProcess) for bplan in bplaene: workflow_assignment = registry.content.get_sheet( bplan, IWorkflowAssignment) state_data = workflow_assignment.get()['state_data'] if len(state_data) > 0: state_data_participate = state_data[0] if 'end_date' in state_data_participate: start_date = state_data_participate['start_date'] end_date = state_data_participate['end_date'] workflow_assignment.set({ 'state_data': [{ 'name': 'participate', 'description': '', 'start_date': start_date }, { 'name': 'closed', 'description': '', 'start_date': end_date }] })