Skip to content

Commit

Permalink
Add auto-tagging for Postmortems
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasha Radchenko committed Apr 7, 2021
1 parent 0114d30 commit 603494b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
15 changes: 10 additions & 5 deletions autopsy_app/funcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
import re
import string
import random
from flask import url_for
Expand All @@ -17,10 +17,6 @@ def choose_random_mortem(max_id):
return random.randrange(1, max_id+1) if max_id else None


def send_email(rec, message):
print(f'{rec} : {message}', file=sys.stderr)


def generate_password(data):
return flask_bcrypt.generate_password_hash(data).decode('utf-8')

Expand All @@ -42,3 +38,12 @@ def send_email(email, token):
If you didn't request this request - be beware of the Great Evil!
"""
mail.send(msg)


def auto_tag(content):
tag_pattern = re.compile(r"\b([A-Z]\S+)\b")
m = tag_pattern.findall(content)
return [tag.lower() for tag in m]

def get_tags(tags_data):
return tags_data.strip("{}").split(",")
1 change: 1 addition & 0 deletions autopsy_app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Mortem(db.Model):
mortem_url = db.Column(db.String(16), nullable=False)
mortem_content = db.Column(db.Text(), nullable=False)
mortem_resolution = db.Column(db.Text())
mortem_tags = db.Column(db.Text())
mortem_created = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
mortem_updated = db.Column(db.DateTime, nullable=False,
Expand Down
8 changes: 6 additions & 2 deletions autopsy_app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
PostmortemForm, SupportForm, RequestResetForm,
ResetForm)
from autopsy_app.funcs import (define_mortem_url, choose_random_mortem,
send_email, generate_password, verify_password)
auto_tag, send_email, generate_password,
verify_password, get_tags)


@app.route('/')
Expand Down Expand Up @@ -158,9 +159,10 @@ def add_postmortem():
url = define_mortem_url()
now = datetime.datetime.utcnow()
uid = current_user.id
tags = auto_tag(form.mortem.data)
mortem = Mortem(mortem_name=title, mortem_content=content,
mortem_url=url, mortem_created=now,
mortem_resolution=resolution,
mortem_resolution=resolution, mortem_tags=tags,
mortem_impact=impact, mortem_updated=now, user_id=uid)
db.session.add(mortem)
db.session.commit()
Expand All @@ -173,6 +175,7 @@ def add_postmortem():
@login_required
def get_postmortem(url):
mortem = Mortem.query.filter_by(mortem_url=url).first()
mortem.mortem_tags = get_tags(mortem.mortem_tags)
return render_template('get_postmortem.html', mortem=mortem)


Expand All @@ -190,6 +193,7 @@ def update_postmortem(url):
mortem.mortem_content = form.mortem.data
mortem.mortem_resolution = form.resolution.data
mortem.mortem_updated = datetime.datetime.utcnow()
mortem.mortem_tags = auto_tag(form.mortem.data)
db.session.commit()
flash('The mortem has been updated', 'success')
return redirect(url_for('get_postmortem', url=url))
Expand Down
5 changes: 5 additions & 0 deletions autopsy_app/static/autopsy.css
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,8 @@
font-weight: 900;
color: Tomato;
}

.mortem-tag span:hover {
font-weight: 900;
color: DodgerBlue;
}
10 changes: 10 additions & 0 deletions autopsy_app/templates/get_postmortem.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
<label class="form-control-label" for="resolution">Mortem resolution</label>
<textarea class="form-control form-control-md add-mortem-resolution" id="resolution" name="resolution" readonly>{{ mortem.mortem_resolution }}</textarea>
</div>
{% if mortem.mortem_tags %}
<div class="form-group py-2 my-2">
<label class="form-control-label">Mortem tags:</label>
<div class="mortem-tag">
{% for tag in mortem.mortem_tags %}
<span>#{{ tag }} </span>
{% endfor %}
</div>
</div>
{% endif %}

<div class="form-group py-2 my-2">
<a href="{{ url_for('postmortems') }}">
Expand Down
28 changes: 28 additions & 0 deletions migrations/versions/62af2f0ac0fc_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""empty message
Revision ID: 62af2f0ac0fc
Revises: 9a8483d12c24
Create Date: 2021-04-08 00:31:01.395854
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '62af2f0ac0fc'
down_revision = '9a8483d12c24'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('mortems', sa.Column('mortem_tags', sa.Text(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('mortems', 'mortem_tags')
# ### end Alembic commands ###

0 comments on commit 603494b

Please sign in to comment.