def test_titleize(self, app): """Converts a sentence to title case, excepting articles and abbreviations.""" app.config['ABBREVIATED_WORDS'] = None assert util.titleize('head like a hole, black as your soul') == 'Head Like a Hole, Black as Your Soul' assert util.titleize('I\'D RATHER DIE THAN GIVE YOU CONTROL') == 'I\'d Rather Die Than Give You Control' app.config['ABBREVIATED_WORDS'] = ['BOW', 'YOU'] assert util.titleize('bOw dOwn bEfOrE thE OnE yOu sErvE') == 'BOW Down Before the One YOU Serve' assert util.titleize('YOU\'RE GOING To GET WHAT (you) DESERVE') == 'You\'re Going to Get What (YOU) Deserve'
def create(cls, creator_id, subject, title, attachments=(), body='', is_private=False, topics=()): creator = AuthorizedUser.find_by_id(creator_id) if creator: note_template = cls(body=body, creator_id=creator_id, is_private=is_private, subject=subject, title=title) for topic in topics: note_template.topics.append( NoteTemplateTopic.create( note_template.id, titleize(vacuum_whitespace(topic))), ) for byte_stream_bundle in attachments: note_template.attachments.append( NoteTemplateAttachment.create( note_template_id=note_template.id, name=byte_stream_bundle['name'], byte_stream=byte_stream_bundle['byte_stream'], uploaded_by=creator.uid, ), ) db.session.add(note_template) std_commit() return note_template
def create(cls, author_uid, author_name, author_role, author_dept_codes, sid, subject, body, topics=(), attachments=()): note = cls(author_uid, author_name, author_role, author_dept_codes, sid, subject, body) for topic in topics: note.topics.append( NoteTopic.create_note_topic(note, titleize(vacuum_whitespace(topic)), author_uid), ) for byte_stream_bundle in attachments: note.attachments.append( NoteAttachment.create_attachment( note=note, name=byte_stream_bundle['name'], byte_stream=byte_stream_bundle['byte_stream'], uploaded_by=author_uid, ), ) db.session.add(note) std_commit() cls.refresh_search_index() return note
def _add_topics_to_notes(author_uid, note_ids, topics): for prepared_topic in [ titleize(vacuum_whitespace(topic)) for topic in topics ]: count_per_chunk = 10000 for chunk in range(0, len(note_ids), count_per_chunk): query = """ INSERT INTO note_topics (author_uid, note_id, topic) SELECT author_uid, note_id, topic FROM json_populate_recordset(null::note_topics, :json_dumps); """ note_ids_subset = note_ids[chunk:chunk + count_per_chunk] data = [{ 'author_uid': author_uid, 'note_id': note_id, 'topic': prepared_topic, } for note_id in note_ids_subset] db.session.execute(query, {'json_dumps': json.dumps(data)})
def _update_appointment_topics(appointment, topics, updated_by): modified = False now = utc_now() topics = set([titleize(vacuum_whitespace(topic)) for topic in topics]) existing_topics = set(appointment_topic.topic for appointment_topic in AppointmentTopic.find_by_appointment_id(appointment.id)) topics_to_delete = existing_topics - topics topics_to_add = topics - existing_topics for topic in topics_to_delete: topic_to_delete = next((t for t in appointment.topics if t.topic == topic), None) if topic_to_delete: topic_to_delete.deleted_at = now modified = True for topic in topics_to_add: appointment.topics.append( AppointmentTopic.create(appointment, topic), ) modified = True if modified: appointment.updated_at = now appointment.updated_by = updated_by
def _update_note_topics(cls, note, topics): modified = False now = utc_now() topics = set([titleize(vacuum_whitespace(topic)) for topic in topics]) existing_topics = set( note_topic.topic for note_topic in NoteTopic.find_by_note_id(note.id)) topics_to_delete = existing_topics - topics topics_to_add = topics - existing_topics for topic in topics_to_delete: topic_to_delete = next( (t for t in note.topics if t.topic == topic), None) if topic_to_delete: topic_to_delete.deleted_at = now modified = True for topic in topics_to_add: note.topics.append(NoteTopic.create(note, topic, note.author_uid), ) modified = True if modified: note.updated_at = now