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_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')
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")})