예제 #1
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')
예제 #2
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'}
예제 #3
0
def test_unrestricted_notice_form(session, categories, organizations, issues):
    users = UserCollection(session)
    user = users.add(
        username='******',
        realname='User',
        role='editor',
        password='******',
    )
    user.phone_number = '+41415554433'

    notice = GazetteNotice(title='Title', text='A <b>text</b>.')
    notice.organization_id = '200'
    notice.category_id = '13'
    notice.note = 'note'
    notice.issues = ['2017-43']
    notice.user = user

    # Test apply / update
    form = UnrestrictedNoticeForm()
    form.request = DummyRequest(session)

    form.apply_model(notice)
    assert form.title.data == 'Title'
    assert form.organization.data == '200'
    assert form.category.data == '13'
    assert form.text.data == 'A <b>text</b>.'
    assert form.issues.data == ['2017-43']
    assert form.phone_number.data == '+41415554433'
    assert form.note.data == 'note'

    form.title.data = 'Notice'
    form.organization.data = '300'
    form.category.data = '11'
    form.text.data = 'A <b>notice</b>.'
    form.issues.data = ['2017-44']
    form.phone_number.data = '796662211'
    form.note.data = 'A note'

    form.update_model(notice)
    assert notice.title == 'Notice'
    assert notice.organization == 'Municipality'
    assert notice.category == 'Education'
    assert notice.text == 'A <b>notice</b>.'
    assert notice.issues == {'2017-44': None}
    assert notice.first_issue == standardize_date(datetime(2017, 11, 3), 'UTC')
    assert notice.note == 'A note'
    assert user.phone_number == '+41796662211'

    notice.state = 'published'
    form.issues.data = ['2017-45']
    form.update_model(notice)
    assert notice.issues == {'2017-44': None}

    # Test on request
    with freeze_time("2019-11-01 14:00"):
        form = UnrestrictedNoticeForm()
        form.model = None
        form.request = DummyRequest(session)
        form.on_request()
        assert form.organization.choices == [('', 'Select one'),
                                             ('100', 'State Chancellery'),
                                             ('200', 'Civic Community'),
                                             ('300', 'Municipality'),
                                             ('410',
                                              'Evangelical Reformed Parish'),
                                             ('420', '(Sikh Community)'),
                                             ('430', 'Catholic Parish'),
                                             ('500', 'Corporation')]
        assert form.issues.choices == [
            ('2017-40', 'No. 40, Freitag 06.10.2017'),
            ('2017-41', 'No. 41, Freitag 13.10.2017'),
            ('2017-42', 'No. 42, Freitag 20.10.2017'),
            ('2017-43', 'No. 43, Freitag 27.10.2017'),
            ('2017-44', 'No. 44, Freitag 03.11.2017'),
            ('2017-45', 'No. 45, Freitag 10.11.2017'),
            ('2017-46', 'No. 46, Freitag 17.11.2017'),
            ('2017-47', 'No. 47, Freitag 24.11.2017'),
            ('2017-48', 'No. 48, Freitag 01.12.2017'),
            ('2017-49', 'No. 49, Freitag 08.12.2017'),
            ('2017-50', 'No. 50, Freitag 15.12.2017'),
            ('2017-51', 'No. 51, Freitag 22.12.2017'),
            ('2017-52', 'No. 52, Freitag 29.12.2017'),
            ('2018-1', 'No. 1, Freitag 05.01.2018')
        ]
        assert form.category.choices == [
            ('13', 'Commercial Register'),
            ('10', '(Complaints)'),
            ('11', 'Education'),
            ('14', 'Elections'),
            ('12', 'Submissions'),
        ]

    # Test disable issues
    form = UnrestrictedNoticeForm()
    form.model = None
    form.request = DummyRequest(session)
    form.on_request()
    form.disable_issues()
    assert form.issues.validators == []
    assert all([field.render_kw['disabled'] for field in form.issues])
예제 #4
0
def test_notice_form(session, categories, organizations, issues):
    users = UserCollection(session)
    user = users.add(
        username='******',
        realname='User',
        role='editor',
        password='******',
    )
    user.phone_number = '+41415554433'

    notice = GazetteNotice(
        title='Title',
        text='A <b>text</b>.',
        author_place='Govikon',
        author_name='State Chancellerist',
        author_date=standardize_date(datetime(2018, 1, 1), 'UTC'),
    )
    notice.organization_id = '200'
    notice.category_id = '13'
    notice.issues = ['2017-43']
    notice.user = user

    # Test apply / update
    form = NoticeForm()
    form.request = DummyRequest(session)

    form.apply_model(notice)
    assert form.title.data == 'Title'
    assert form.organization.data == '200'
    assert form.category.data == '13'
    assert form.print_only.data is False
    assert form.at_cost.data == 'no'
    assert form.billing_address.data == ''
    assert form.text.data == 'A <b>text</b>.'
    assert form.author_place.data == 'Govikon'
    assert form.author_name.data == 'State Chancellerist'
    assert form.author_date.data == standardize_date(datetime(2018, 1, 1),
                                                     'UTC')
    assert form.issues.data == ['2017-43']
    assert form.phone_number.data == '+41415554433'

    form.title.data = 'Notice'
    form.organization.data = '300'
    form.category.data = '11'
    form.print_only.data = True
    form.at_cost.data = 'yes'
    form.billing_address.data = 'someone\nsomewhere'
    form.text.data = 'A <b>notice</b>.'
    form.author_place.data = 'Govtown'
    form.author_name.data = 'Bureau of Public Affairs'
    form.author_date.data = standardize_date(datetime(2019, 1, 1), 'UTC')
    form.issues.data = ['2017-44']
    form.phone_number.data = '796662211'

    form.update_model(notice)
    assert notice.title == 'Notice'
    assert notice.organization == 'Municipality'
    assert notice.category == 'Education'
    assert notice.print_only is True
    assert notice.at_cost is True
    assert notice.billing_address == 'someone\nsomewhere'
    assert notice.text == 'A <b>notice</b>.'
    assert notice.author_place == 'Govtown'
    assert notice.author_name == 'Bureau of Public Affairs'
    assert notice.author_date == standardize_date(datetime(2019, 1, 1), 'UTC')
    assert notice.issues == {'2017-44': None}
    assert notice.first_issue == standardize_date(datetime(2017, 11, 3), 'UTC')
    assert user.phone_number == '+41796662211'

    # Test validation
    form = NoticeForm()
    form.request = DummyRequest(session)
    assert not form.validate()

    form = NoticeForm()
    form.request = DummyRequest(session)
    form.issues.choices = [('2017-5', '2017-5')]
    form.organization.choices = [('onegov', 'onegov')]
    form.category.choices = [('important', 'important')]
    form.process(
        DummyPostData({
            'title': 'Title',
            'organization': 'onegov',
            'category': 'important',
            'issues': ['2017-5'],
            'text': 'Text',
            'author_place': 'Govtown',
            'author_name': 'Bureau of Public Affairs',
            'author_date': '2019-01-01'
        }))
    assert form.validate()

    # Test UTC conversion
    assert form.author_date.data == date(2019, 1, 1)
    assert form.author_date_utc == standardize_date(datetime(2019, 1, 1),
                                                    'UTC')
    assert NoticeForm().author_date_utc is None

    # Test on request
    with freeze_time("2017-11-01 14:00"):
        form = NoticeForm()
        form.model = None
        form.request = DummyRequest(session)
        form.on_request()
        assert form.organization.choices == [('', 'Select one'),
                                             ('100', 'State Chancellery'),
                                             ('200', 'Civic Community'),
                                             ('300', 'Municipality'),
                                             ('410',
                                              'Evangelical Reformed Parish'),
                                             ('430', 'Catholic Parish'),
                                             ('500', 'Corporation')]
        assert form.issues.choices == [
            ('2017-45', 'No. 45, Freitag 10.11.2017'),
            ('2017-46', 'No. 46, Freitag 17.11.2017'),
            ('2017-47', 'No. 47, Freitag 24.11.2017'),
            ('2017-48', 'No. 48, Freitag 01.12.2017'),
            ('2017-49', 'No. 49, Freitag 08.12.2017'),
            ('2017-50', 'No. 50, Freitag 15.12.2017'),
            ('2017-51', 'No. 51, Freitag 22.12.2017'),
            ('2017-52', 'No. 52, Freitag 29.12.2017'),
            ('2018-1', 'No. 1, Freitag 05.01.2018'),
        ]
        assert form.category.choices == [
            ('13', 'Commercial Register'),
            ('11', 'Education'),
            ('14', 'Elections'),
            ('12', 'Submissions'),
        ]
        assert form.print_only is None

        form = NoticeForm()
        form.model = None
        form.request = DummyRequest(session, private=True)
        form.on_request()
        assert form.organization.choices == [('', 'Select one'),
                                             ('100', 'State Chancellery'),
                                             ('200', 'Civic Community'),
                                             ('300', 'Municipality'),
                                             ('410',
                                              'Evangelical Reformed Parish'),
                                             ('430', 'Catholic Parish'),
                                             ('500', 'Corporation')]
        assert form.issues.choices == [
            ('2017-44', 'No. 44, Freitag 03.11.2017'),
            ('2017-45', 'No. 45, Freitag 10.11.2017'),
            ('2017-46', 'No. 46, Freitag 17.11.2017'),
            ('2017-47', 'No. 47, Freitag 24.11.2017'),
            ('2017-48', 'No. 48, Freitag 01.12.2017'),
            ('2017-49', 'No. 49, Freitag 08.12.2017'),
            ('2017-50', 'No. 50, Freitag 15.12.2017'),
            ('2017-51', 'No. 51, Freitag 22.12.2017'),
            ('2017-52', 'No. 52, Freitag 29.12.2017'),
            ('2018-1', 'No. 1, Freitag 05.01.2018'),
        ]
        assert form.category.choices == [
            ('13', 'Commercial Register'),
            ('11', 'Education'),
            ('14', 'Elections'),
            ('12', 'Submissions'),
        ]
        assert form.issues.render_kw['data-hot-issue'] == '2017-44'
        assert form.print_only is not None