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
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])
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