def _import_organizations(request, app): request.locale = locale headers = { 'id': request.translate(_("ID")), 'name': request.translate(_("Name")), 'title': request.translate(_("Title")), 'active': request.translate(_("Active")), 'external_name': request.translate(_("External ID")), 'parent': request.translate(_("Parent Organization")) } session = app.session() organizations = OrganizationCollection(session) if clear: click.secho("Deleting organizations", fg='yellow') for organization in organizations.query(): session.delete(organization) csvfile = convert_xls_to_csv( file, sheet_name=request.translate(_("Organizations")) ) csv = CSVFile(csvfile, expected_headers=headers.values()) lines = list(csv.lines) columns = { key: csv.as_valid_identifier(value) for key, value in headers.items() } count = 0 for line in lines: count += 1 id_ = int(getattr(line, columns['id'])) name = getattr(line, columns['name']) parent = getattr(line, columns['parent']) parent = int(parent) if parent else None title = getattr(line, columns['title']) active = bool(int(getattr(line, columns['active']))) external_name = getattr(line, columns['external_name']) organization = organizations.add_root( id=id_, name=name, title=title, active=active, external_name=external_name, order=count ) organization.parent_id = parent click.secho(f"{count} organization(s) imported", fg='green') if dry_run: transaction.abort() click.secho("Aborting transaction", fg='yellow')
def execute(self): from onegov.gazette.collections import OrganizationCollection organizations = OrganizationCollection(self.session) subject = organizations.by_id(self.subject_id) target = organizations.by_id(self.target_id) if subject and target and subject != target: if subject.parent_id == target.parent_id: OrganizationCollection(self.session).move( subject=subject, target=target, direction=getattr(MoveDirection, self.direction) )
def test_notice_collection_used_organizations(session): collection = GazetteNoticeCollection(session) organizations = OrganizationCollection(session) a = organizations.add_root(name='1', title='A') b = organizations.add_root(name='2', title='B') c = organizations.add_root(name='3', title='C') for organization, count in (('1', 2), ('2', 4), ('3', 10)): for x in range(count): collection.add(title='', text='', organization_id=organization, category_id='', issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.used_organizations == [a, b, c]
def delete_organization(self, request, form): """ Delete a organization. Only unused organizations may be deleted. """ layout = Layout(self, request) session = request.session if self.children or self.in_use: request.message( _("Only unused organizations with no sub-organisations may be " "deleted."), 'alert') return { 'layout': layout, 'title': self.title, 'subtitle': _("Delete Organization"), 'show_form': False } if form.submitted(request): collection = OrganizationCollection(session) collection.delete(self) request.message(_("Organization deleted."), 'success') return redirect(layout.manage_organizations_link) return { 'message': _('Do you really want to delete "${item}"?', mapping={'item': self.title}), 'layout': layout, 'form': form, 'title': self.title, 'subtitle': _("Delete Organization"), 'button_text': _("Delete Organization"), 'button_class': 'alert', 'cancel': layout.manage_organizations_link }
def test_notice_collection_count_by_organization(session): collection = GazetteNoticeCollection(session) assert collection.count_by_organization() == [] organizations = OrganizationCollection(session) organizations.add_root(name='1', title='A') organizations.add_root(name='2', title='B') organizations.add_root(name='3', title='C') for organization, count in (('1', 2), ('2', 4), ('3', 10)): for x in range(count): collection.add(title='', text='', organization_id=organization, category_id='', issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.count_by_organization() == [ ('A', 1), ('B', 6), ('C', 45), ] assert collection.count_by_organization() == \ collection.for_state('drafted').count_by_organization() collection.issues = ['2017-1', '2017-4'] assert collection.count_by_organization() == [('B', 2), ('C', 13)]
def test_organization_move(session): # test URL template move = OrganizationMove(None, None, None, None).for_url_template() assert move.direction == '{direction}' assert move.subject_id == '{subject_id}' assert move.target_id == '{target_id}' # test execute collection = OrganizationCollection(session) collection.add_root(title='2', id=2, order=2) collection.add_root(title='1', id=1, oder=1) parent = collection.add_root(title='3', id=3, order=3) collection.add(parent=parent, title='5', id=5, order=2) collection.add(parent=parent, title='4', id=4, order=1) def tree(): return [[o.title, [c.title for c in o.children]] for o in collection.query().filter_by(parent_id=None)] assert tree() == [['1', []], ['2', []], ['3', ['4', '5']]] OrganizationMove(session, 1, 2, 'below').execute() assert tree() == [['2', []], ['1', []], ['3', ['4', '5']]] OrganizationMove(session, 3, 1, 'above').execute() assert tree() == [['2', []], ['3', ['4', '5']], ['1', []]] OrganizationMove(session, 5, 4, 'above').execute() session.flush() session.expire_all() assert tree() == [['2', []], ['3', ['5', '4']], ['1', []]] # invalid OrganizationMove(session, 8, 9, 'above').execute() assert tree() == [['2', []], ['3', ['5', '4']], ['1', []]] OrganizationMove(session, 5, 2, 'above').execute() session.expire_all() assert tree() == [['2', []], ['3', ['5', '4']], ['1', []]]
def test_notice_collection_for_organizations(session): collection = GazetteNoticeCollection(session) organizations = OrganizationCollection(session) organizations.add_root(name='1', title='A') organizations.add_root(name='2', title='B') organizations.add_root(name='3', title='C') for organization, count in (('1', 2), ('2', 4), ('3', 10)): for x in range(count): collection.add(title='', text='', organization_id=organization, category_id='', issues=['2017-{}'.format(y) for y in range(x)], user=None) assert collection.for_organizations(['1']).query().count() == 2 assert collection.for_organizations(['2']).query().count() == 4 assert collection.for_organizations(['3']).query().count() == 10 assert collection.for_organizations(['1', '3']).query().count() == 12
def get_organization(app, id): return OrganizationCollection(app.session()).by_id(id)
def get_organizations(app): return OrganizationCollection(app.session())
def test_organization_collection(session): collection = OrganizationCollection(session) collection.add_root(title='First', active=True) collection.add_root(title='Second', active=False) collection.add_root(title='Third') collection.add_root(title='Fourth', active=True) categories = collection.query().all() assert categories[0].title == 'First' assert categories[0].name == '1' assert categories[1].title == 'Fourth' assert categories[1].name == '4' assert categories[2].title == 'Second' assert categories[2].name == '2' assert categories[3].title == 'Third' assert categories[3].name == '3'
def manage_organizations_link(self): return self.request.link(OrganizationCollection(self.session))
def test_organization_form(session): request = DummyRequest(session) # Test on request organizations = OrganizationCollection(session) parent = organizations.add_root(title='parent', active=True, order=1) child = organizations.add(parent=parent, title='child', active=True) other = organizations.add_root(title='other', active=True, order=2) other.external_name = 'xxx' form = OrganizationForm() form.request = request form.on_request() assert form.parent.choices == [('', '- none -'), ('1', 'parent'), ('3', 'other')] # Test apply / update # ... unused form.apply_model(parent) assert form.title.data == 'parent' assert form.active.data is True assert form.parent.data == '' assert form.name.data == '1' assert form.external_name.data is None form.apply_model(child) assert form.title.data == 'child' assert form.active.data is True assert form.parent.data == '1' assert form.name.data == '2' assert form.external_name.data is None form.apply_model(other) assert form.title.data == 'other' assert form.active.data is True assert form.parent.data == '' assert form.name.data == '3' assert form.external_name.data == 'xxx' form.title.data = 'DEF' form.active.data = False form.parent.data = '1' form.external_name.data = 'yyy' form.update_model(other) session.flush() session.expire(other) assert other.title == 'DEF' assert other.active is False assert other.parent == parent assert other.siblings.filter_by(id='3') assert other.name == '3' assert form.external_name.data == 'yyy' form.name.data = '4' form.update_model(other) assert other.name == '4' # ... used GazetteNoticeCollection(session).add('title', 'text', '4', '', None, []) form.apply_model(other) assert form.name.render_kw == {'readonly': True} # Test validation # ... empty values form = OrganizationForm() form.request = request assert not form.validate() # ... new model form = OrganizationForm( DummyPostData({ 'title': 'title', 'parent': '', 'name': '2' })) form.request = request assert not form.validate() assert 'This value already exists.' in form.errors['name'] form = OrganizationForm( DummyPostData({ 'title': 'title', 'parent': '', 'name': '5' })) form.request = request assert form.validate() # ... existing model form = OrganizationForm( DummyPostData({ 'title': 'title', 'parent': '', 'name': '2' })) form.model = organizations.query().filter_by(name='4').one() form.request = request assert not form.validate() assert 'This value already exists.' in form.errors['name'] assert 'This value is in use.' in form.errors['name'] form = OrganizationForm( DummyPostData({ 'title': 'title', 'parent': '', 'name': '5' })) form.model = organizations.query().filter_by(name='1').one() form.request = request assert form.validate()
def create_organizations(session): organizations = OrganizationCollection(session) organizations.add_root(name='100', order=1, title='State Chancellery', active=True) organizations.add_root(name='200', order=2, title='Civic Community', active=True) organizations.add_root(name='300', order=3, title='Municipality', active=True) churches = organizations.add_root(name='400', order=4, title='Churches', active=True) organizations.add(parent=churches, name='410', order=1, title='Evangelical Reformed Parish', active=True) organizations.add(parent=churches, name='420', order=2, title='Sikh Community', active=False, external_name='4') organizations.add(parent=churches, name='430', order=3, title='Catholic Parish', active=True) organizations.add_root(name='500', order=5, title='Corporation', active=True) return organizations.query().all()