Beispiel #1
0
def test_issues_pdf_notice(gazette_app):
    session = gazette_app.session()

    with freeze_time("2017-01-01 12:00"):
        notice = GazetteNotice(title='title',
                               text='text',
                               author_place='place',
                               author_date=utcnow(),
                               author_name='author',
                               state='drafted')
        notice.files.append(pdf_attachment('attachment'))
        session.add(notice)
        session.flush()

    layout = Layout(notice, DummyRequest(session, gazette_app.principal))

    pdf = IssuePdf(BytesIO())
    pdf.init_a4_portrait()

    pdf.notice(notice, layout, '1')
    assert extract_pdf_story(pdf) == [
        '1', 'title', 'text', 'place, 1. Januar 2017<br/>author'
    ]

    notice.print_only = True
    pdf.notice(notice, layout, '2')
    assert extract_pdf_story(pdf) == [
        '1', 'title', 'text', 'place, 1. Januar 2017<br/>author', '2',
        '<i>This official notice is only available in the print version.</i>'
    ]
Beispiel #2
0
def test_issue(gazette_app, session):
    issue = Issue(
        id=0,
        name='2018-7',
        number=7,
        date=date(2017, 7, 1),
        deadline=standardize_date(datetime(2017, 6, 25, 12, 0), 'UTC'),
    )
    issue.pdf = 'PDF'.encode('utf-8')
    session.add(issue)
    session.flush()
    issue = session.query(Issue).one()

    assert issue.id == 0
    assert issue.name == '2018-7'
    assert issue.number == 7
    assert issue.date == date(2017, 7, 1)
    assert issue.deadline == standardize_date(datetime(2017, 6, 25, 12, 0),
                                              'UTC')
    assert issue.pdf.id
    assert issue.pdf.name == '2018-7.pdf'
    assert issue.pdf.type == 'gazette_issue'
    assert issue.pdf.reference.file.read().decode('utf-8') == 'PDF'

    # Test query etc
    assert len(issue.notices().all()) == 0
    assert issue.notices('accepted').all() == []
    assert issue.notices('submitted').all() == []
    assert issue.in_use is False

    issues = [issue.name]
    session.add(GazetteNotice(title='d', issues=issues))
    session.add(GazetteNotice(title='a', state='accepted', issues=issues))
    session.add(GazetteNotice(title='s', state='submitted', issues=issues))
    session.add(GazetteNotice(title='s', issues=['2018-1']))
    session.add(GazetteNotice(title='s', issues=['2018-1', issue.name]))
    session.flush()

    assert len(issue.notices().all()) == 4
    assert issue.notices('accepted').all()[0].title == 'a'
    assert issue.notices('submitted').one().title == 's'
    assert issue.in_use is True

    # Test date observer
    issue.date = date(2018, 7, 2)
    session.flush()
    dates = [i.first_issue for i in session.query(GazetteNotice)]
    dates = [d.date() for d in dates if d]
    assert set(dates) == set([issue.date])

    # Test publish
    issue.publish(DummyRequest(session))
    assert len(issue.notices().all()) == 4
    assert issue.notices('accepted').all() == []
    assert issue.notices('published').count() == 1
Beispiel #3
0
    def add(self, title, text, organization_id, category_id, user, issues,
            **kwargs):
        """ Add a new notice.

        A unique, URL-friendly name is created automatically for this notice
        using the title and optionally numbers for duplicate names.

        A entry is added automatically to the audit trail.

        Returns the created notice.
        """

        notice = GazetteNotice(id=uuid4(),
                               state='drafted',
                               title=title,
                               text=text,
                               name=self._get_unique_name(title),
                               issues=issues,
                               **kwargs)
        notice.user = user
        notice.group = user.group if user else None
        notice.organization_id = organization_id
        notice.category_id = category_id
        notice.apply_meta(self.session)
        self.session.add(notice)
        self.session.flush()

        audit_trail = MessageCollection(self.session, type='gazette_notice')
        audit_trail.add(channel_id=str(notice.id),
                        owner=str(user.id) if user else '',
                        meta={'event': _("created")})

        return notice
Beispiel #4
0
def test_layout_format(session, principal):
    request = DummyRequest(session, principal)
    layout = Layout(None, request)

    # Date
    assert layout.principal.time_zone == 'Europe/Zurich'
    assert layout.format_date(date(2019, 1, 2), 'date') == '02.01.2019'
    assert layout.format_date(datetime(2019, 1, 2, 12), 'date') == '02.01.2019'
    assert layout.format_date(datetime(2019, 1, 2, 12), 'date_long') == \
        '2. Januar 2019'
    assert layout.format_date(datetime(2019, 1, 2, 12), 'datetime') == \
        '02.01.2019 12:00'
    assert layout.format_date(
        standardize_date(datetime(2019, 1, 2, 12, 0), 'UTC'),
        'date') == '02.01.2019'
    assert layout.format_date(
        standardize_date(datetime(2019, 1, 2, 12, 0), 'UTC'),
        'datetime') == '02.01.2019 13:00'

    # Issue
    with raises(AssertionError):
        layout.format_issue(None)
    with raises(AssertionError):
        layout.format_issue('')

    assert layout.format_issue(Issue()) == 'No. , '

    assert layout.format_issue(Issue(number=1,
                                     date=date(2017, 1,
                                               2))) == 'No. 1, 02.01.2017'
    assert layout.format_issue(
        Issue(number=1, date=date(2017, 1, 2)),
        date_format='date_with_weekday') == 'No. 1, Montag 02.01.2017'
    assert layout.format_issue(Issue(name='2017-1',
                                     number=1,
                                     date=date(2017, 1, 2)),
                               notice=GazetteNotice()) == 'No. 1, 02.01.2017'
    assert layout.format_issue(
        Issue(name='2017-1', number=1, date=date(2017, 1, 2)),
        notice=GazetteNotice(issues=['2017-1'])) == 'No. 1, 02.01.2017'
    assert layout.format_issue(
        Issue(name='2017-1', number=1, date=date(2017, 1, 2)),
        notice=GazetteNotice(
            _issues={'2017-1': 10})) == 'No. 1, 02.01.2017 / 10'

    # Text
    assert layout.format_text(None) == ''
    assert layout.format_text('abc') == 'abc'
    assert layout.format_text('a\nb\r\nc') == 'a<br>b<br>c'
Beispiel #5
0
def test_notices_pdf_from_notices(gazette_app):
    session = gazette_app.session()

    with freeze_time("2017-01-01 12:00"):
        notice = GazetteNotice(title='first title',
                               text='first text',
                               author_place='first place',
                               author_date=utcnow(),
                               author_name='first author',
                               state='submitted')
        notice.files.append(pdf_attachment('first attachment'))
        session.add(notice)
        session.flush()

    with freeze_time("2017-01-02 12:00"):
        notice = GazetteNotice(title='second title',
                               text='second text',
                               author_place='second place',
                               author_date=utcnow(),
                               author_name='second author',
                               state='submitted')
        session.add(notice)
        session.flush()

    with freeze_time("2018-01-01 12:00"):
        request = DummyRequest(session, gazette_app.principal)
        notices = GazetteNoticeCollection(session)
        file = NoticesPdf.from_notices(notices, request)
        reader = PdfFileReader(file)
        assert [page.extractText() for page in reader.pages
                ] == [('© 2018 Govikon\n1\n'
                       'xxx\nfirst title\nfirst text\n'
                       'first place, 1. Januar 2017\nfirst author\n'),
                      '© 2018 Govikon\n2\n',
                      ('© 2018 Govikon\n3\n'
                       'xxx\nsecond title\nsecond text\n'
                       'second place, 2. Januar 2017\nsecond author\n')]

        file = NoticesPdf.from_notices(notices.for_order('title', 'desc'),
                                       request)
        reader = PdfFileReader(file)
        assert [page.extractText() for page in reader.pages
                ] == [('© 2018 Govikon\n1\n'
                       'xxx\nsecond title\nsecond text\n'
                       'second place, 2. Januar 2017\nsecond author\n'
                       'xxx\nfirst title\nfirst text\n'
                       'first place, 1. Januar 2017\nfirst author\n'),
                      '© 2018 Govikon\n2\n']
Beispiel #6
0
def test_issues_po_pdf_query_used(session, issues, organizations, categories):
    issues = {issue.name: issue for issue in issues}
    for _issues, organization_id, category_id, meta in (
        ({
            '2017-40': '1',
            '2017-41': '4'
        }, '100', '14', None),
        ({
            '2017-40': '2',
            '2017-41': '3'
        }, '200', '13', {}),
        ({
            '2017-40': '3',
            '2017-41': '2'
        }, '300', '12', {
            'print_only': None
        }),
        ({
            '2017-40': '4',
            '2017-41': '1'
        }, '410', '11', {
            'print_only': True
        }),
        ({
            '2017-41': '5',
            '2017-42': '1'
        }, '420', '10', {
            'print_only': False
        }),
    ):
        session.add(
            GazetteNotice(title='title',
                          text=', '.join([
                              '{}-{}'.format(issue, _issues[issue])
                              for issue in sorted(_issues)
                          ]),
                          _issues=_issues,
                          organization_id=organization_id,
                          category_id=category_id,
                          state='published',
                          meta=meta))
    session.flush()

    assert IssuePrintOnlyPdf.query_used_categories(
        session, issues['2017-40']) == {'11'}
    assert IssuePrintOnlyPdf.query_used_categories(
        session, issues['2017-41']) == {'11'}
    assert IssuePrintOnlyPdf.query_used_categories(session,
                                                   issues['2017-42']) == set()
    assert IssuePrintOnlyPdf.query_used_categories(session,
                                                   issues['2017-43']) == set()

    assert IssuePrintOnlyPdf.query_used_organizations(
        session, issues['2017-40']) == {'410'}
    assert IssuePrintOnlyPdf.query_used_organizations(
        session, issues['2017-41']) == {'410'}
    assert IssuePrintOnlyPdf.query_used_organizations(
        session, issues['2017-42']) == set()
    assert IssuePrintOnlyPdf.query_used_organizations(
        session, issues['2017-43']) == set()
Beispiel #7
0
def test_index_pdf_from_notices(gazette_app):
    session = gazette_app.session()

    with freeze_time("2017-01-01 12:00"):
        notice = GazetteNotice(title='first title',
                               text='first text',
                               organization_id='100',
                               category_id='10',
                               _issues={
                                   '2017-40': '1',
                                   '2017-41': '3',
                               },
                               state='accepted')
        session.add(notice)
        session.flush()

    with freeze_time("2017-01-02 12:00"):
        notice = GazetteNotice(title='second title',
                               text='second text',
                               organization_id='200',
                               category_id='11',
                               _issues={
                                   '2017-40': '2',
                                   '2017-42': '4',
                               },
                               state='published')
        session.add(notice)
        session.flush()

    with freeze_time("2018-01-01 12:00"):
        request = DummyRequest(session, gazette_app.principal)
        notices = GazetteNoticeCollection(session)
        file = IndexPdf.from_notices(notices, request)
        reader = PdfFileReader(file)
        assert [page.extractText() for page in reader.pages
                ] == [('© 2018 Govikon\n1\nGazette\nIndex\n'
                       'Organizations\n'
                       'C\n'
                       'Civic Community  2017-40-2, 2017-42-4\n'
                       'S\n'
                       'State Chancellery  2017-40-1, 2017-41-3\n'),
                      ('Gazette\n© 2018 Govikon\n2\n'
                       'Categories\n'
                       'C\n'
                       'Complaints  2017-40-1, 2017-41-3\n'
                       'E\n'
                       'Education  2017-40-2, 2017-42-4\n')]
Beispiel #8
0
def test_notice_change(session):
    users = UserCollection(session)

    session.add(GazetteNoticeChange(text='text', channel_id='channel'))
    session.flush()

    change = session.query(GazetteNoticeChange).one()
    assert change.text == 'text'
    assert change.channel_id == 'channel'
    assert change.user is None
    assert change.user_name is None
    assert change._user_name is None
    assert change.notice is None
    assert change.event is None

    # Add user
    change.event = 'event'
    user = users.add('*****@*****.**', 'test', 'editor')
    change.user = user
    session.flush()
    session.refresh(change)

    assert change.text == 'text'
    assert change.channel_id == 'channel'
    assert change.user == user
    assert change.user_name == '*****@*****.**'
    assert change._user_name == '*****@*****.**'
    assert change.notice is None
    assert change.event == 'event'
    assert user.changes == [change]

    # Add to notice
    session.add(GazetteNotice(state='drafted', title='title', name='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()
    change.notice = notice

    assert notice.changes.one() == change

    # Rename user
    user.realname = 'Peter'
    session.flush()
    session.refresh(change)

    assert change.user == user
    assert change.user_name == 'Peter'
    assert change._user_name == 'Peter'
    assert user.changes == [change]

    # Delete user
    users.delete(user.username)
    session.flush()
    session.refresh(change)

    assert change.user is None
    assert change.user_name == '(Peter)'
    assert change._user_name == 'Peter'
Beispiel #9
0
 def notice(title, text, number=None):
     notice = GazetteNotice(title=title,
                            text=text,
                            _issues={'2017-40': '1'},
                            organization_id='100',
                            category_id='10',
                            state='published')
     session.add(notice)
     session.flush()
     return notice
Beispiel #10
0
def test_issues_po_pdf_query_notices(session, issues, organizations,
                                     categories):
    for _issues, meta in (
        ({
            '2017-40': '1',
            '2017-41': '4'
        }, None),
        ({
            '2017-40': '2',
            '2017-41': '3'
        }, {}),
        ({
            '2017-40': '3',
            '2017-41': '2'
        }, {
            'print_only': None
        }),
        ({
            '2017-40': '4',
            '2017-41': '1'
        }, {
            'print_only': True
        }),
        ({
            '2017-41': '5',
            '2017-42': '1'
        }, {
            'print_only': False
        }),
    ):
        session.add(
            GazetteNotice(title='title',
                          text=', '.join([
                              '{}-{}'.format(issue, _issues[issue])
                              for issue in sorted(_issues)
                          ]),
                          _issues=_issues,
                          organization_id='100',
                          category_id='10',
                          state='published',
                          meta=meta))
    session.flush()

    query = session.query(GazetteNotice)
    assert IssuePrintOnlyPdf.query_notices(
        session, '2017-40', '100',
        '10') == [query.filter_by(text='2017-40-4, 2017-41-1').one().id]

    assert IssuePrintOnlyPdf.query_notices(
        session, '2017-41', '100',
        '10') == [query.filter_by(text='2017-40-4, 2017-41-1').one().id]

    assert IssuePrintOnlyPdf.query_notices(session, '2017-42', '100',
                                           '10') == []
Beispiel #11
0
def test_issues_pdf_query_notices(session, issues, organizations, categories):
    for _issues in (
        {
            '2017-40': '1',
            '2017-41': '4'
        },
        {
            '2017-40': '2',
            '2017-41': '3'
        },
        {
            '2017-40': '3',
            '2017-41': '2'
        },
        {
            '2017-40': '4',
            '2017-41': '1'
        },
        {
            '2017-41': '5',
            '2017-42': '1'
        },
    ):
        session.add(
            GazetteNotice(title='title',
                          text=', '.join([
                              '{}-{}'.format(issue, _issues[issue])
                              for issue in sorted(_issues)
                          ]),
                          _issues=_issues,
                          organization_id='100',
                          category_id='10',
                          state='published'))
    session.flush()

    query = session.query(GazetteNotice)
    assert IssuePdf.query_notices(session, '2017-40', '100', '10') == [
        query.filter_by(text='2017-40-1, 2017-41-4').one().id,
        query.filter_by(text='2017-40-2, 2017-41-3').one().id,
        query.filter_by(text='2017-40-3, 2017-41-2').one().id,
        query.filter_by(text='2017-40-4, 2017-41-1').one().id
    ]
    assert IssuePdf.query_notices(session, '2017-41', '100', '10') == [
        query.filter_by(text='2017-40-4, 2017-41-1').one().id,
        query.filter_by(text='2017-40-3, 2017-41-2').one().id,
        query.filter_by(text='2017-40-2, 2017-41-3').one().id,
        query.filter_by(text='2017-40-1, 2017-41-4').one().id,
        query.filter_by(text='2017-41-5, 2017-42-1').one().id
    ]
    assert IssuePdf.query_notices(session, '2017-42', '100', '10') == [
        query.filter_by(text='2017-41-5, 2017-42-1').one().id
    ]
Beispiel #12
0
    def get_publication(self, identifier):
        """ Fetches a single publication and adds it as an official notice.

        """
        session = self.session

        response = get(f'{self.endpoint}/publications/{identifier}/xml')
        response.raise_for_status()
        response.encoding = 'utf-8'

        root = etree.fromstring(response.text.encode('utf-8'))
        subrubric = root.find('meta/subRubric').text
        converter = self.converters[subrubric](root)

        name = get_unique_notice_name(converter.title, session, GazetteNotice)
        author_date = converter.publication_date or None
        if author_date:
            author_date = standardize_date(author_date, 'UTC')
        expiry_date = converter.expiration_date or None
        if expiry_date:
            expiry_date = standardize_date(expiry_date, 'UTC')

        notice = GazetteNotice(id=uuid4(),
                               name=name,
                               state='imported',
                               source=converter.source,
                               title=converter.title,
                               text=converter.text,
                               organization_id=self.organization,
                               category_id=self.category,
                               issues=converter.issues(session),
                               author_date=author_date,
                               expiry_date=expiry_date)
        notice.apply_meta(session)
        session.add(notice)
        session.flush()

        MessageCollection(session, type='gazette_notice').add(
            channel_id=str(notice.id), meta={'event': _("imported")})
Beispiel #13
0
def test_issues_pdf_query_excluded_notices_count(session, issues):
    issues = {issue.name: issue for issue in issues}
    for _issues, meta in (
        ({
            '2017-40': '1',
            '2017-41': '4'
        }, None),
        ({
            '2017-40': '2',
            '2017-41': '3'
        }, {}),
        ({
            '2017-40': '3',
            '2017-41': '2'
        }, {
            'print_only': None
        }),
        ({
            '2017-40': '4',
            '2017-41': '1'
        }, {
            'print_only': True
        }),
        ({
            '2017-41': '5',
            '2017-42': '1'
        }, {
            'print_only': False
        }),
    ):
        session.add(
            GazetteNotice(title='title',
                          text=', '.join([
                              '{}-{}'.format(issue, _issues[issue])
                              for issue in sorted(_issues)
                          ]),
                          _issues=_issues,
                          organization_id='100',
                          category_id='10',
                          state='published',
                          meta=meta))
    session.flush()

    assert IssuePdf.query_excluded_notices_count(session,
                                                 issues['2017-40']) == 1
    assert IssuePdf.query_excluded_notices_count(session,
                                                 issues['2017-41']) == 1
    assert IssuePdf.query_excluded_notices_count(session,
                                                 issues['2017-42']) == 0
    assert IssuePdf.query_excluded_notices_count(session,
                                                 issues['2017-43']) == 0
Beispiel #14
0
def test_notice_invalid_category(session, categories):
    session.add(GazetteNotice(title='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()
    assert notice.invalid_category

    notice.category_id = '0'
    assert notice.invalid_category

    notice.category_id = '10'
    assert notice.invalid_category

    notice.category_id = '13'
    assert not notice.invalid_category
Beispiel #15
0
def test_notice_invalid_organization(session, organizations):
    session.add(GazetteNotice(title='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()
    assert notice.invalid_organization

    notice.organization_id = '0'
    assert notice.invalid_organization

    notice.organization_id = '420'
    assert notice.invalid_organization

    notice.organization_id = '410'
    assert not notice.invalid_organization
Beispiel #16
0
def test_notice_organization(session):
    session.add(GazetteNotice(title='notice', organization_id='xxx'))
    session.flush()

    notice = session.query(GazetteNotice).one()
    assert notice.organization_id == 'xxx'
    assert notice.organization_object is None
    assert notice.organization is None

    session.add(Organization(name='xxx', title='Organization', active=True))
    organization = session.query(Organization).one()
    session.flush()
    assert notice.organization_id == 'xxx'
    assert notice.organization_object == organization
    assert notice.organization == 'Organization'  # through title observer
Beispiel #17
0
def test_notice_category(session):
    session.add(GazetteNotice(title='notice', category_id='xxx'))
    session.flush()

    notice = session.query(GazetteNotice).one()
    assert notice.category_id == 'xxx'
    assert notice.category_object is None
    assert notice.category is None

    session.add(Category(name='xxx', title='Category', active=True))
    category = session.query(Category).one()
    session.flush()
    assert notice.category_id == 'xxx'
    assert notice.category_object == category
    assert notice.category == 'Category'  # through title observer
Beispiel #18
0
def test_notice_expired_issues(session, issues):
    session.add(GazetteNotice(title='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()
    assert not notice.expired_issues

    notice.issues = ['2017-40']
    with freeze_time("2017-01-01 12:00"):
        assert not notice.expired_issues
    with freeze_time("2017-10-05 23:59"):
        assert not notice.expired_issues

    with freeze_time("2017-10-06 00:01"):
        assert notice.expired_issues
    with freeze_time("2018-01-01 12:00"):
        assert notice.expired_issues
Beispiel #19
0
def test_bool_is(session):
    session.add(GazetteNotice(title='F.1'))
    session.add(GazetteNotice(title='F.2', meta={'y': 1}))
    session.add(GazetteNotice(title='F.3', meta={'x': None}))
    session.add(GazetteNotice(title='F.4', meta={'x': None, 'y': 1}))
    session.add(GazetteNotice(title='F.5', meta={'x': False}))
    session.add(GazetteNotice(title='F.6', meta={'x': False, 'y': 1}))
    session.add(GazetteNotice(title='T.1', meta={'x': True}))
    session.add(GazetteNotice(title='T.2', meta={'x': True, 'y': 1}))
    session.flush()

    query = session.query(GazetteNotice)
    assert sorted([
        r.title for r in query.filter(bool_is(GazetteNotice.meta['x'], True))
    ]) == ['T.1', 'T.2']
    assert sorted([
        r.title for r in query.filter(bool_is(GazetteNotice.meta['x'], False))
    ]) == ['F.1', 'F.2', 'F.3', 'F.4', 'F.5', 'F.6']
Beispiel #20
0
def test_notice_file(gazette_app, session):
    session.add(GazetteNotice(title='notice'))
    session.flush()

    notice = session.query(GazetteNotice).one()
    notice.files.append(
        GazetteNoticeFile(id='abcd',
                          name='test.txt',
                          reference=as_fileintent('Test text.'.encode('utf-8'),
                                                  'test.txt')))
    session.flush()

    file = session.query(GazetteNoticeFile).one()
    assert file.id == 'abcd'
    assert file.name == 'test.txt'
    assert file.type == 'gazette_notice'
    assert file.reference.file.read().decode('utf-8') == 'Test text.'

    assert notice.files[0] == file
    assert file.linked_official_notices[0] == notice
Beispiel #21
0
def test_notices_pdf_from_notice(gazette_app):
    session = gazette_app.session()

    with freeze_time("2017-01-01 12:00"):
        notice = GazetteNotice(title='title',
                               text='text',
                               author_place='place',
                               author_date=utcnow(),
                               author_name='author',
                               state='drafted')
        notice.files.append(pdf_attachment('attachment'))
        session.add(notice)
        session.flush()

    with freeze_time("2018-01-01 12:00"):
        request = DummyRequest(session, gazette_app.principal)
        file = NoticesPdf.from_notice(notice, request)
        reader = PdfFileReader(file)
        assert [page.extractText() for page in reader.pages] == [
            '© 2018 Govikon\n1\n'
            'xxx\ntitle\ntext\nplace, 1. Januar 2017\nauthor\n',
            '© 2018 Govikon\n2\n'
        ]
Beispiel #22
0
def test_category(session):
    session.add(Category(name='100', title='Election', active=True))
    session.flush()
    category = session.query(Category).one()

    assert category.name == '100'
    assert category.title == 'Election'
    assert category.active is True

    # Test helpers
    assert len(category.notices().all()) == 0
    assert category.in_use is False

    session.add(GazetteNotice(title='notice', category_id=category.name))
    session.flush()

    assert len(category.notices().all()) == 1
    assert category.in_use is True

    # Test title observer
    category.title = 'Vote'
    session.flush()
    assert session.query(GazetteNotice).one().category == 'Vote'
Beispiel #23
0
def test_sogc_importer_get_publication_ids(session):

    importer = create_importer(session)
    with patch('onegov.gazette.utils.sogc_importer.get') as get:
        get.return_value = DummyResponse([BULK_EMPTY])

        result = importer.get_publication_ids()

        assert result == []
        assert get.called
        assert get.call_args == call('https://localhost/publications/xml',
                                     params={
                                         'publicationStates':
                                         'PUBLISHED',
                                         'cantons':
                                         'GV',
                                         'subRubrics': [
                                             'KK01', 'KK02', 'KK03', 'KK04',
                                             'KK05', 'KK06', 'KK07', 'KK08',
                                             'KK09', 'KK10'
                                         ],
                                         'pageRequest.page':
                                         0,
                                         'pageRequest.size':
                                         2000
                                     })

    session.add(GazetteNotice(title='title', text='text', source='YYY2'))

    with patch('onegov.gazette.utils.sogc_importer.get') as get:
        get.return_value = DummyResponse(
            [BULK_NOT_EMPTY, BULK_NOT_EMPTY, BULK_EMPTY])

        result = importer.get_publication_ids()

        assert result == ['XXX1', 'XXX3']
        assert get.called
Beispiel #24
0
def test_organization(session):
    session.add(
        Organization(name='100', title='State Chancellery', active=True))
    session.flush()
    parent = session.query(Organization).one()

    assert parent.name == '100'
    assert parent.title == 'State Chancellery'
    assert parent.active is True
    assert parent.external_name is None

    # Test in use
    session.add(
        Organization(parent=parent,
                     name='101',
                     title='Administration',
                     active=True))
    session.flush()
    child = session.query(Organization).filter_by(name='101').one()

    assert len(parent.notices().all()) == 0
    assert len(child.notices().all()) == 0
    assert parent.in_use is False
    assert child.in_use is False

    session.add(GazetteNotice(title='notice', organization_id='101'))
    session.flush()

    assert len(parent.notices().all()) == 0
    assert len(child.notices().all()) == 1
    assert parent.in_use is False
    assert child.in_use is True

    # Test title observer
    child.title = 'Administrations'
    session.flush()
    assert session.query(GazetteNotice).one().organization == 'Administrations'
Beispiel #25
0
def test_issue_publication_numbers(session):
    session.add(Issue(name='2016-1', number=1, date=date(2016, 1, 1)))
    session.add(Issue(name='2017-1', number=1, date=date(2017, 1, 1)))
    session.add(Issue(name='2017-2', number=2, date=date(2017, 2, 2)))
    session.add(Issue(name='2018-1', number=1, date=date(2018, 1, 1)))
    session.add(Issue(name='2018-2', number=2, date=date(2018, 2, 2)))
    session.add(Issue(name='2018-3', number=2, date=date(2018, 3, 3)))
    session.flush()

    def issue(name):
        return session.query(Issue).filter_by(name=name).one()

    assert issue('2016-1').first_publication_number == 1
    assert issue('2017-1').first_publication_number == 1
    assert issue('2017-2').first_publication_number == 1
    assert issue('2018-1').first_publication_number == 1
    assert issue('2018-2').first_publication_number == 1
    assert issue('2018-3').first_publication_number == 1

    assert issue('2016-1').publication_numbers() == {}
    assert issue('2017-1').publication_numbers() == {}
    assert issue('2017-2').publication_numbers() == {}
    assert issue('2018-1').publication_numbers() == {}
    assert issue('2018-2').publication_numbers() == {}
    assert issue('2018-3').publication_numbers() == {}

    assert {1} == set(
        [issue.first_publication_number for issue in session.query(Issue)])

    session.add(GazetteNotice(title='1', _issues={'2016-1': '5'}))
    session.add(GazetteNotice(title='2', _issues={'2017-1': None}))
    session.add(
        GazetteNotice(title='3', _issues={
            '2017-1': '5',
            '2017-2': '6'
        }))
    session.add(
        GazetteNotice(title='4', _issues={
            '2017-1': None,
            '2018-3': '4'
        }))
    session.add(
        GazetteNotice(title='4',
                      _issues={
                          '2017-2': '9',
                          '2018-1': '1',
                          '2018-2': '2'
                      }))
    session.flush()

    assert issue('2016-1').first_publication_number == 1
    assert issue('2017-1').first_publication_number == 1
    assert issue('2017-2').first_publication_number == 6
    assert issue('2018-1').first_publication_number == 1
    assert issue('2018-2').first_publication_number == 2
    assert issue('2018-3').first_publication_number == 3

    assert set(issue('2016-1').publication_numbers().values()) == {'5'}
    assert set(issue('2017-1').publication_numbers().values()) == {'5', None}
    assert set(issue('2017-2').publication_numbers().values()) == {'6', '9'}
    assert set(issue('2018-1').publication_numbers().values()) == {'1'}
    assert set(issue('2018-2').publication_numbers().values()) == {'2'}
    assert set(issue('2018-3').publication_numbers().values()) == {'4'}

    session.add(Issue(name='2016-2', number=2, date=date(2016, 2, 1)))
    session.add(Issue(name='2017-3', number=3, date=date(2017, 3, 1)))
    session.add(Issue(name='2018-4', number=4, date=date(2018, 4, 1)))
    session.flush()

    assert issue('2016-2').first_publication_number == 6
    assert issue('2017-3').first_publication_number == 10
    assert issue('2018-4').first_publication_number == 5

    session.add(Issue(name='2016-12', number=12, date=date(2016, 12, 1)))
    session.add(Issue(name='2017-12', number=12, date=date(2017, 12, 1)))
    session.add(Issue(name='2018-12', number=12, date=date(2018, 12, 1)))
    session.flush()

    assert issue(name='2016-12').first_publication_number == 6
    assert issue(name='2017-12').first_publication_number == 10
    assert issue(name='2018-12').first_publication_number == 5
Beispiel #26
0
def test_issue_publish(gazette_app, session):
    session.add(Category(name='a', title='a', active=True))
    session.add(Category(name='b', title='b', active=True))

    session.add(Organization(name='1', title='1', active=True))
    session.add(Organization(name='2', title='2', active=True))

    session.add(Issue(name='2016-1', number=1, date=date(2016, 1, 1)))
    session.add(Issue(name='2016-2', number=1, date=date(2016, 2, 1)))
    session.add(Issue(name='2016-4', number=1, date=date(2016, 4, 1)))
    session.add(Issue(name='2017-1', number=1, date=date(2017, 1, 1)))
    session.flush()

    session.add(
        GazetteNotice(title='x',
                      organization_id='1',
                      category_id='b',
                      _issues={
                          '2016-1': None,
                          '2016-4': None,
                          '2017-1': None
                      },
                      state='accepted',
                      text='XXX'))
    session.add(
        GazetteNotice(title='y',
                      organization_id='1',
                      category_id='a',
                      _issues={
                          '2016-1': None,
                          '2016-2': None
                      },
                      state='accepted',
                      text='YYY'))
    session.add(
        GazetteNotice(title='z',
                      organization_id='2',
                      category_id='b',
                      _issues={
                          '2016-4': None,
                          '2017-1': None
                      },
                      state='accepted',
                      text='ZZZ'))
    session.flush()

    request = DummyRequest(session)
    session.query(Issue).filter_by(name='2016-2').one().publish(request)

    def notice(title):
        return session.query(GazetteNotice).filter_by(title=title).one()

    assert notice('x').issues == {
        '2016-1': None,
        '2016-4': None,
        '2017-1': None
    }
    assert notice('y').issues == {'2016-1': None, '2016-2': '1'}
    assert notice('z').issues == {'2016-4': None, '2017-1': None}
    assert notice('x').state == 'accepted'
    assert notice('y').state == 'published'
    assert notice('z').state == 'accepted'

    session.query(Issue).filter_by(name='2016-4').one().publish(request)
    assert notice('x').issues == {
        '2016-1': None,
        '2016-4': '2',
        '2017-1': None
    }
    assert notice('y').issues == {'2016-1': None, '2016-2': '1'}
    assert notice('z').issues == {'2016-4': '3', '2017-1': None}
    assert notice('x').state == 'published'
    assert notice('y').state == 'published'
    assert notice('z').state == 'published'

    session.query(Issue).filter_by(name='2016-1').one().publish(request)
    assert notice('x').issues == {'2016-1': '2', '2016-4': '2', '2017-1': None}
    assert notice('y').issues == {'2016-1': '1', '2016-2': '1'}
    assert notice('z').issues == {'2016-4': '3', '2017-1': None}

    session.query(Issue).filter_by(name='2016-2').one().publish(request)
    assert notice('x').issues == {'2016-1': '2', '2016-4': '2', '2017-1': None}
    assert notice('y').issues == {'2016-1': '1', '2016-2': '3'}
    assert notice('z').issues == {'2016-4': '3', '2017-1': None}

    session.query(Issue).filter_by(name='2016-4').one().publish(request)
    assert notice('x').issues == {'2016-1': '2', '2016-4': '4', '2017-1': None}
    assert notice('y').issues == {'2016-1': '1', '2016-2': '3'}
    assert notice('z').issues == {'2016-4': '5', '2017-1': None}

    session.query(Issue).filter_by(name='2017-1').one().publish(request)
    assert notice('x').issues == {'2016-1': '2', '2016-4': '4', '2017-1': '1'}
    assert notice('y').issues == {'2016-1': '1', '2016-2': '3'}
    assert notice('z').issues == {'2016-4': '5', '2017-1': '2'}
Beispiel #27
0
def test_notice_apply_meta(session, categories, organizations, issues):
    notice = GazetteNotice()

    notice.apply_meta(session)
    assert notice.organization is None
    assert notice.category is None
    assert notice.first_issue is None

    notice.organization_id = 'invalid'
    notice.category_id = 'invalid'
    notice.issues = [str(IssueName(2020, 1))]
    notice.apply_meta(session)
    assert notice.organization is None
    assert notice.category is None
    assert notice.first_issue is None

    notice.organization_id = '100'
    notice.category_id = '12'
    notice.issues = [str(IssueName(2017, 46))]
    notice.apply_meta(session)
    assert notice.organization == 'State Chancellery'
    assert notice.category == 'Submissions'
    assert notice.first_issue == standardize_date(datetime(2017, 11, 17),
                                                  'UTC')

    notice.issues = [str(IssueName(2017, 46)), str(IssueName(2017, 40))]
    notice.apply_meta(session)
    assert notice.first_issue == standardize_date(datetime(2017, 10, 6), 'UTC')
Beispiel #28
0
def test_notice_states(session):
    class DummyIdentity():
        userid = None

    user = UserCollection(session).add('*****@*****.**', 'test', 'publisher')

    session.add(GazetteNotice(state='drafted', title='title', name='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()

    request = DummyRequest(session)
    with raises(AssertionError):
        notice.accept(request)
    with raises(AssertionError):
        notice.reject(request, 'XXX')
    with raises(AssertionError):
        notice.publish(request)
    notice.submit(request)

    with raises(AssertionError):
        notice.submit(request)
    with raises(AssertionError):
        notice.publish(request)
    notice.reject(request, 'Some reason')
    notice.submit(request)
    notice.reject(request, 'Some other reason')
    notice.submit(request)
    notice.accept(request)

    with raises(AssertionError):
        notice.submit(request)
    with raises(AssertionError):
        notice.accept(request)
    with raises(AssertionError):
        notice.reject(request, 'Some reason')

    notice.publish(request)

    with raises(AssertionError):
        notice.submit(request)
    with raises(AssertionError):
        notice.accept(request)
    with raises(AssertionError):
        notice.reject(request, 'Some reason')
    with raises(AssertionError):
        notice.publish(request)

    notice.add_change(request, 'printed')

    request.identity = DummyIdentity()
    request.identity.userid = user.username
    notice.add_change(request, 'finished', text='all went well')
    session.flush()
    session.refresh(user)

    # the test might be to fast for the implicit ordering by id, we sort it
    # ourselves
    changes = notice.changes.order_by(None)
    changes = changes.order_by(GazetteNoticeChange.edited.desc())
    assert [(change.event, change.user, change.text)
            for change in changes] == [('submitted', None, ''),
                                       ('rejected', None, 'Some reason'),
                                       ('submitted', None, ''),
                                       ('rejected', None, 'Some other reason'),
                                       ('submitted', None, ''),
                                       ('accepted', None, ''),
                                       ('published', None, ''),
                                       ('printed', None, ''),
                                       ('finished', user, 'all went well')]
    assert notice.rejected_comment == 'Some other reason'

    assert [(change.event, change.user, change.text) for change in user.changes
            ] == [('finished', user, 'all went well')]
Beispiel #29
0
def test_notice_issues(session):
    # Test connection to model
    session.add(GazetteNotice(title='notice', issues=['2017-1']))
    session.flush()

    notice = session.query(GazetteNotice).one()
    assert list(notice.issues.keys()) == ['2017-1']
    assert notice.issue_objects == []
    assert notice.first_issue is None

    session.add(Issue(name='2017-1', number=1, date=date(2017, 7, 1)))
    issue = session.query(Issue).one()
    session.flush()
    assert list(notice.issues.keys()) == ['2017-1']
    assert notice.issue_objects == [issue]
    assert notice.first_issue.date() == date(2017, 7, 1)  # through observer

    # Test HSTORE
    notice = GazetteNotice()
    assert notice.issues == {}

    notice.issues = ['2010-1', '2011-4', '2008-7']
    assert list(notice.issues.keys()) == ['2008-7', '2010-1', '2011-4']
    assert notice.issues == {'2008-7': None, '2010-1': None, '2011-4': None}

    notice.issues = {'2010-1', '2010-2', '2010-11'}
    assert list(notice.issues.keys()) == ['2010-1', '2010-2', '2010-11']
    assert notice.issues == {'2010-1': None, '2010-2': None, '2010-11': None}

    notice.issues = {'2010-1': 'a', '2009-2': 'b', '2010-11': 'c'}
    assert list(notice.issues.keys()) == ['2009-2', '2010-1', '2010-11']
    assert notice.issues == {'2009-2': 'b', '2010-1': 'a', '2010-11': 'c'}

    notice.issues = {
        str(IssueName(2017, 10)): 1004,
        str(IssueName(2017, 11)): 1022
    }
    assert notice.issues == {'2017-10': 1004, '2017-11': 1022}

    # Test settting the publication number
    notice = GazetteNotice()

    with raises(AssertionError):
        notice.set_publication_number('2017-10', 1)

    notice.issues = ['2010-1', '2010-2', '2010-3']
    notice.set_publication_number('2010-1', 1)
    notice.set_publication_number('2010-2', '2')
    notice.set_publication_number('2010-3', 3)
    assert notice.issues == {'2010-1': '1', '2010-2': '2', '2010-3': '3'}
Beispiel #30
0
def test_notice_user_and_group(session):
    users = UserCollection(session)
    groups = UserGroupCollection(session)

    session.add(GazetteNotice(title='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()

    assert notice.user is None
    assert notice.user_id is None
    assert notice.user_name is None
    assert notice._user_name is None
    assert notice.group is None
    assert notice.group_id is None
    assert notice.group_name is None
    assert notice._group_name is None

    # add user and group
    user = users.add('[email protected]', 'p', 'editor', realname='user')
    group = groups.add(name='group')
    notice.user = user
    notice.group = group
    session.flush()
    session.refresh(notice)

    assert notice.user == user
    assert notice.user_id == user.id
    assert notice.user_name == 'user'
    assert notice._user_name == 'user'
    assert notice.group == group
    assert notice.group_id == group.id
    assert notice.group_name == 'group'
    assert notice._group_name == 'group'

    # rename user and group
    user.realname = 'xxx'
    group.name = 'yyy'
    session.flush()
    session.refresh(notice)

    assert notice.user == user
    assert notice.user_id == user.id
    assert notice.user_name == 'xxx'
    assert notice._user_name == 'xxx'
    assert notice.group == group
    assert notice.group_id == group.id
    assert notice.group_name == 'yyy'
    assert notice._group_name == 'yyy'

    # delete user and group
    users.delete(user.username)
    groups.delete(group)
    session.flush()
    session.refresh(notice)

    assert notice.user is None
    assert notice.user_id is None
    assert notice.user_name == '(xxx)'
    assert notice._user_name == 'xxx'
    assert notice.group is None
    assert notice.group_id is None
    assert notice.group_name == '(yyy)'
    assert notice._group_name == 'yyy'