Exemple #1
0
 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
Exemple #2
0
 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
Exemple #3
0
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)})
Exemple #4
0
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
Exemple #5
0
 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
Exemple #6
0
    def add_to_search_history(cls, user_id, search_phrase):
        search_phrase = vacuum_whitespace(search_phrase)
        query = text(
            'SELECT search_history FROM authorized_users WHERE id = :user_id')
        result = db.session.execute(query, {'user_id': user_id}).first()
        if result:
            search_history = result['search_history'] or []
            if len(search_phrase) > cls.SEARCH_HISTORY_ITEM_MAX_LENGTH:
                if ' ' in search_phrase:
                    search_phrase = search_phrase[:cls.
                                                  SEARCH_HISTORY_ITEM_MAX_LENGTH
                                                  + 1]
                    search_phrase = search_phrase[:search_phrase.rindex(' ') +
                                                  1].strip()
                else:
                    search_phrase = search_phrase[:cls.
                                                  SEARCH_HISTORY_ITEM_MAX_LENGTH]
            phrase_lowered = search_phrase.lower()
            for idx, entry in enumerate(search_history):
                if phrase_lowered == entry.lower():
                    del search_history[idx]
            search_history.insert(0, search_phrase)

            max_size = app.config['USER_SEARCH_HISTORY_MAX_SIZE']
            if len(search_history) > max_size:
                del search_history[max_size:]

            sql_text = text(
                'UPDATE authorized_users SET search_history = :history WHERE id = :id'
            )
            db.session.execute(sql_text, {
                'history': search_history,
                'id': user_id
            })
            return cls.get_search_history(user_id)
        else:
            return None
Exemple #7
0
 def test_vacuum_whitespace(self):
     """Cleans up leading, trailing, and repeated whitespace."""
     assert util.vacuum_whitespace('  Firstname    Lastname   ') == 'Firstname Lastname'