Example #1
0
    def _create_pdf(request, app):
        session = app.session()
        agencies = ExtendedAgencyCollection(session)
        orga = session.query(Organisation).one()

        if root:
            app.root_pdf = app.pdf_class.from_agencies(
                agencies=agencies.roots,
                title=app.org.name,
                toc=True,
                exclude=app.org.hidden_people_fields,
                page_break_on_level=int(
                    orga.meta.get('page_break_on_level_root_pdf', 1)))
            click.secho("Root PDF created", fg='green')

        if recursive:
            for agency in agencies.query():
                agency.pdf_file = app.pdf_class.from_agencies(
                    agencies=[agency],
                    title=agency.title,
                    toc=False,
                    exclude=app.org.hidden_people_fields,
                    page_break_on_level=int(
                        orga.meta.get('page_break_on_level_orga_pdf', 1)))
                click.secho(f"Created PDF of '{agency.title}'", fg='green')
Example #2
0
def test_membership_form_choices(session):
    people = ExtendedPersonCollection(session)
    people.add(first_name="Nick", last_name="Rivera")
    people.add(first_name="Nick", last_name="Rivera", phone="1234")
    people.add(first_name="Nick", last_name="Rivera", phone="5555", email="x")
    people.add(first_name="Nick", last_name="Rivera", phone_direct="4")
    people.add(first_name="Nick", last_name="Rivera", address="Street")
    people.add(first_name="Nick", last_name="Rivera", email="*****@*****.**")

    agencies = ExtendedAgencyCollection(session)
    agency = agencies.add_root(title="Hospital")
    doc = people.add(first_name="Nick", last_name="Rivera")
    agency.add_person(doc.id, "Doc")

    request = DummyRequest(session)
    form = MembershipForm()
    form.request = request
    form.on_request()

    assert sorted([x[1] for x in form.person_id.choices]) == [
        'Rivera Nick',
        'Rivera Nick (1234)',
        'Rivera Nick (4)',
        'Rivera Nick (5555)',
        'Rivera Nick (Hospital)',
        'Rivera Nick (Street)',
        'Rivera Nick ([email protected])'
    ]
Example #3
0
    def update_model(self, model):
        session = self.request.session
        agencies = ExtendedAgencyCollection(session)

        parent_id = None
        parent = None
        if self.parent_id.data.isdigit():
            parent_id = int(self.parent_id.data)
            parent = agencies.by_id(parent_id)
        model.name = agencies.get_unique_child_name(model.title, parent)
        model.parent_id = parent_id
Example #4
0
def add_agency(self, request, form):

    if form.submitted(request):
        collection = ExtendedAgencyCollection(request.session)
        agency = collection.add(self, **form.get_useful_data())
        request.success(_("Added a new agency"))
        return redirect(request.link(agency))

    layout = AgencyLayout(self, request)
    layout.breadcrumbs.append(Link(_("New"), '#'))

    return {'layout': layout, 'title': _("New agency"), 'form': form}
Example #5
0
def test_extended_people_filter(session):
    people = ExtendedPersonCollection(session)
    people.add(first_name="Hans", last_name="Maulwurf")
    people.add(first_name="Waylon", last_name="Śmithers")
    people.add(first_name="Lenny", last_name="leonard")
    people.add(first_name="Carl", last_name="Çarlson")
    ned = people.add(first_name="Ned", last_name="Flanders")

    agencies = ExtendedAgencyCollection(session)
    agencies.add_root(title="Police").add_person(ned.id, "Snitch")
    agencies.add_root(title="Ħospital").add_person(ned.id, "Volunteer")
    agencies.add_root(title="Moe's Tavern")

    assert [p.last_name for p in people.query()
            ] == ["Çarlson", "Flanders", "leonard", "Maulwurf", "Śmithers"]

    people = people.for_filter(letter="C")
    assert [p.last_name for p in people.query()] == ['Çarlson']

    people = people.for_filter(letter="c")
    assert [p.last_name for p in people.query()] == ['Çarlson']

    people = people.for_filter(letter="L")
    assert [p.last_name for p in people.query()] == ['leonard']

    people = people.for_filter(agency="Police")
    assert [p.last_name for p in people.query()] == []

    people = people.for_filter(letter=None)
    assert [p.last_name for p in people.query()] == ['Flanders']
Example #6
0
def view_hidden_agencies(self, request):
    session = request.session

    agencies = ExtendedAgencyCollection(session).query()
    agencies = agencies.filter(
        ExtendedAgency.meta['is_hidden_from_public'] == True
    )
    agencies = agencies.order_by(None).order_by(ExtendedAgency.title)
    agencies = agencies.all()

    memberships = AgencyMembershipCollection(session).query(
        order_by='order_within_agency'
    )
    memberships = memberships.filter(
        AgencyMembership.meta['is_hidden_from_public'] == True
    )
    memberships = memberships.order_by(None).order_by(AgencyMembership.title)
    memberships = memberships.all()

    people = ExtendedPersonCollection(session).query()
    people = people.filter(
        ExtendedPerson.meta['is_hidden_from_public'] == True
    )
    people = people.order_by(None).order_by(
        ExtendedPerson.last_name,
        ExtendedPerson.first_name
    )
    people = people.all()

    layout = DefaultLayout(self, request)
    layout.breadcrumbs.append(Link(_("Hidden contents"), '#'))

    return {
        'title': _("Hidden contents"),
        'agencies': agencies,
        'memberships': memberships,
        'people': people,
        'layout': layout
    }
Example #7
0
def test_agency_collection_layout():
    request = DummyRequest()
    model = ExtendedAgencyCollection(None)

    layout = AgencyCollectionLayout(model, request)
    assert layout.editbar_links is None
    assert path(layout.breadcrumbs) == 'DummyOrg/ExtendedAgencyCollection'
    assert layout.move_agency_url_template == 'AgencyMove/?csrf-token=x'

    # Log in as manager
    request.is_manager = True
    layout = AgencyCollectionLayout(model, request)
    assert list(hrefs(layout.editbar_links)) == [
        'ExtendedAgencyCollection/create-pdf', 'ExtendedAgencyCollection/new'
    ]
Example #8
0
    def execute(self):
        from onegov.agency.collections import ExtendedAgencyCollection

        agencies = ExtendedAgencyCollection(self.session)
        subject = agencies.by_id(self.subject_id)
        target = agencies.by_id(self.target_id)
        if subject and target and subject != target:
            if subject.parent_id == target.parent_id:
                agencies.move(
                    subject=subject,
                    target=target,
                    direction=getattr(MoveDirection, self.direction)
                )
Example #9
0
def test_extended_people_used_agencies(session):
    assert ExtendedPersonCollection(session).used_agencies == []

    agencies = ExtendedAgencyCollection(session)
    police = agencies.add_root(title="Police")
    hospital = agencies.add_root(title="Ħospital")
    agencies.add_root(title="Moe's Tavern")

    assert ExtendedPersonCollection(session).used_agencies == []

    people = ExtendedPersonCollection(session)
    ned = people.add(first_name="Ned", last_name="Flanders")
    police.add_person(ned.id, "Snitch")
    hospital.add_person(ned.id, "Volunteer")

    assert people.used_agencies == ['Ħospital', 'Police']
Example #10
0
def test_agency_move(session):
    # test URL template
    move = AgencyMove(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 = ExtendedAgencyCollection(session)
    collection.add_root(title='2', id=2, order=2)
    collection.add_root(title='1', id=1, order=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']]]

    AgencyMove(session, 1, 2, 'below').execute()
    assert tree() == [['2', []], ['1', []], ['3', ['4', '5']]]

    AgencyMove(session, 3, 1, 'above').execute()
    assert tree() == [['2', []], ['3', ['4', '5']], ['1', []]]

    AgencyMove(session, 5, 4, 'above').execute()
    session.flush()
    session.expire_all()
    assert tree() == [['2', []], ['3', ['5', '4']], ['1', []]]

    # invalid
    AgencyMove(session, 8, 9, 'above').execute()
    assert tree() == [['2', []], ['3', ['5', '4']], ['1', []]]

    AgencyMove(session, 5, 2, 'above').execute()
    session.expire_all()
    assert tree() == [['2', []], ['3', ['5', '4']], ['1', []]]
Example #11
0
def delete_agency(self, request):

    request.assert_valid_csrf_token()
    ExtendedAgencyCollection(request.session).delete(self)
Example #12
0
def test_move_agency_form(session):
    agencies = ExtendedAgencyCollection(session)
    agency_a = agencies.add_root(title="a")
    agencies.add(title="a.1", parent=agency_a)
    agency_a_2 = agencies.add(title="a.2", parent=agency_a)
    agency_a_2_1 = agencies.add(title="a.2.1", parent=agency_a_2)
    agency_f = agencies.add_root(title="f")
    agencies.add_root(title="ç")

    # request
    form = MoveAgencyForm()
    form.request = DummyRequest(session)
    form.on_request()
    assert form.parent_id.choices == [
        ('root', '- Root -'),
        ('1', 'a'), ('2', 'a.1'), ('3', 'a.2'), ('4', 'a.2.1'),
        ('6', 'ç'), ('5', 'f')
    ]

    # apply
    form.apply_model(agency_a)
    assert form.parent_id.choices == [
        ('6', 'ç'), ('5', 'f')
    ]

    form.on_request()
    form.apply_model(agency_a_2)
    assert form.parent_id.choices == [
        ('root', '- Root -'),
        ('2', 'a.1'),
        ('6', 'ç'), ('5', 'f')
    ]

    form.on_request()
    form.apply_model(agency_a_2_1)
    assert form.parent_id.choices == [
        ('root', '- Root -'),
        ('1', 'a'), ('2', 'a.1'),
        ('6', 'ç'), ('5', 'f')
    ]

    form.on_request()
    form.apply_model(agency_f)
    assert form.parent_id.choices == [
        ('1', 'a'), ('2', 'a.1'), ('3', 'a.2'), ('4', 'a.2.1'),
        ('6', 'ç')
    ]

    # update
    model = ExtendedAgency(title="Agency")
    form = MoveAgencyForm()
    form.request = DummyRequest(session)
    form.update_model(model)
    assert model.parent_id == None

    form = MoveAgencyForm(DummyPostData({'parent_id': 'root'}))
    form.request = DummyRequest(session)
    form.update_model(model)
    assert model.parent_id == None

    form = MoveAgencyForm(DummyPostData({'parent_id': '10'}))
    form.request = DummyRequest(session)
    form.update_model(model)
    assert model.parent_id == 10

    # update with rename
    agency_a_2_2 = agencies.add(title="a.2", parent=agency_a_2)
    form = MoveAgencyForm(DummyPostData({'parent_id': agency_a_2.parent_id}))
    form.request = DummyRequest(session)
    form.update_model(agency_a_2_2)
    session.flush()
Example #13
0
 def collection(self):
     return ExtendedAgencyCollection(self.session)
Example #14
0
    def _import(request, app):
        EXPORT_FIELDS = {
            'academic_title': 'person.academic_title',
            'address': 'person.address',
            'direct_number': 'person.phone_direct',
            'firstname': 'person.first_name',
            'lastname': 'person.last_name',
            'occupation': 'person.profession',
            'phone': 'person.phone',
            'political_party': 'person.political_party',
            'postfix': 'membership.addition',
            'role': 'membership.title',
            'start': 'membership.since',
            'title': 'person.title',
            'year': 'person.born',
        }

        class LinkFilter(Filter):
            """ Uses the href rather than the content of an a-tag. """
            def __iter__(self):
                in_link = False
                for token in Filter.__iter__(self):
                    if token.get('name') == 'a':
                        if token['type'] == 'StartTag':
                            in_link = True
                            data = token['data'][(None, 'href')]
                            data = data.replace('mailto:', '')
                            yield {'type': 'Characters', 'data': data}
                        elif token['type'] == 'EndTag':
                            in_link = False
                    elif token['type'] == 'Characters':
                        if not in_link:
                            yield token
                    else:
                        yield token

        cleaner = Cleaner(tags=['a', 'p', 'br'],
                          attributes={'a': 'href'},
                          strip=True,
                          filters=[LinkFilter, whitespace_filter])

        session = app.session()

        if clear:
            click.secho("Deleting all agencies", fg='yellow')
            for root in AgencyCollection(session).roots:
                session.delete(root)
            click.secho("Deleting all people", fg='yellow')
            for person in PersonCollection(session).query():
                session.delete(person)

        workbook = open_workbook(file)

        click.secho("Importing agencies", fg='green')
        agencies = ExtendedAgencyCollection(session)
        people = ExtendedPersonCollection(session)
        sheet = workbook.sheet_by_name('Organisationen')
        ids = {}
        parents = {}
        alphabetical = []
        for row in range(1, sheet.nrows):
            if skip_root and row == 1:
                continue

            if row and (row % 50 == 0):
                app.es_indexer.process()

            # We use our own, internal IDs which are auto-incremented
            external_id = int(sheet.cell_value(row, 0))

            # Remove the HTML code from the portrait, prepend the description
            portrait = '\n'.join(
                (sheet.cell_value(row, 3).strip(),
                 html_to_text(cleaner.clean(sheet.cell_value(row, 4)))))
            portrait = portrait.replace('\n\n', '\n').strip()

            # Re-map the export fields
            export_fields = sheet.cell_value(row, 7) or 'role,title'
            export_fields = export_fields.split(',')
            export_fields = [EXPORT_FIELDS[field] for field in export_fields]

            agency = agencies.add(
                parent=parents.get(external_id),
                title=sheet.cell_value(row, 2).strip(),
                portrait=portrait,
                export_fields=export_fields,
                is_hidden_from_public=sheet.cell_value(row, 8) == 'private',
                order=external_id,
            )
            ids[external_id] = agency.id

            # Download and add the organigram
            if not skip_download:
                organigram_url = sheet.cell_value(row, 6)
                if organigram_url:
                    response = get(organigram_url)
                    response.raise_for_status()
                    agency.organigram_file = BytesIO(response.content)

            if sheet.cell_value(row, 5):
                alphabetical.append(agency.id)

            for child in sheet.cell_value(row, 1).split(','):
                if child:
                    child = int(child)
                    parents[child] = agency

        # Let's make sure, the order have nice, cohere values
        def defrag_ordering(agency):
            for order, child in enumerate(agency.children):
                child.order = order
                defrag_ordering(child)

        for order, root in enumerate(agencies.roots):
            root.order = order
            defrag_ordering(root)

        click.secho("Importing people and memberships", fg='green')
        sheet = workbook.sheet_by_name('Personen')
        for row in range(1, sheet.nrows):
            if row and (row % 50 == 0):
                app.es_indexer.process()

            notes = '\n'.join((sheet.cell_value(row, 13).strip(),
                               sheet.cell_value(row, 14).strip())).strip()

            person = people.add(
                academic_title=sheet.cell_value(row, 0).strip(),
                profession=sheet.cell_value(row, 1).strip(),
                function=(sheet.cell_value(row, 17).strip()
                          if sheet.ncols > 17 else ''),
                first_name=sheet.cell_value(row, 2).strip(),
                last_name=sheet.cell_value(row, 3).strip(),
                political_party=sheet.cell_value(row, 4).strip(),
                born=sheet.cell_value(row, 5).strip(),
                email=sheet.cell_value(row, 6).strip(),
                address=sheet.cell_value(row, 7).strip(),
                phone=sheet.cell_value(row, 8).strip(),
                phone_direct=sheet.cell_value(row, 9).strip(),
                salutation=sheet.cell_value(row, 10).strip(),
                website=sheet.cell_value(row, 12).strip(),
                is_hidden_from_public=sheet.cell_value(row, 15) == 'private',
                notes=notes,
            )
            memberships = sheet.cell_value(row, 16).split('//')
            for membership in memberships:
                if membership:
                    matched = re.match(
                        r'^\((\d*)\)\((.*)\)\((.*)\)\((.*)\)'
                        r'\((.*)\)\((.*)\)\((\d*)\)\((\d*)\)$', membership)
                    if matched:
                        values = matched.groups()
                    else:
                        # old version before order_within_person existed
                        matched = re.match(
                            r'^\((\d*)\)\((.*)\)\((.*)\)\((.*)\)'
                            r'\((.*)\)\((.*)\)\((\d*)\)$', membership)
                        values = list(matched.groups())
                        values.append('0')
                    person.memberships.append(
                        ExtendedAgencyMembership(
                            agency_id=ids[int(values[0])],
                            title=values[1] or "",
                            since=values[2] or None,
                            prefix=values[3],
                            addition=values[4],
                            note=values[5],
                            order_within_agency=int(values[6]),
                            order_within_person=int(values[7]),
                        ))

        # Order the memberships alphabetically, if desired
        for id_ in alphabetical:
            agencies.by_id(id_).sort_relationships()

        # Show a tree view of what we imported
        if visualize:
            click.secho("Imported data:", fg='green')

            def show(agency, level):
                text = f'{agency.title}\n'
                for membership in agency.memberships:
                    person = membership.person
                    text += f'* {membership.title}: {person.title}\n'
                click.echo(indent(text.strip(), level * '  '))

                for child in agency.children:
                    show(child, level + 1)

            for root in agencies.roots:
                show(root, 1)

        # Abort the transaction if requested
        if dry_run:
            transaction.abort()
            click.secho("Aborting transaction", fg='yellow')
Example #15
0
def test_extended_agencies(session):
    agencies = ExtendedAgencyCollection(session)
    root = agencies.add_root(title="Agency")
    assert isinstance(root, ExtendedAgency)
    assert isinstance(agencies.query().one(), ExtendedAgency)