Skip to content

Commit

Permalink
add, edi and delete note
Browse files Browse the repository at this point in the history
  • Loading branch information
ashimali committed May 20, 2024
1 parent c38900a commit c05b919
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 3 deletions.
4 changes: 4 additions & 0 deletions application/blueprints/planning_consideration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ class ConsiderationForm(FlaskForm):
choices=[("public", "Public"), ("private", "Private")],
default="public",
)


class NoteForm(FlaskForm):
text = TextAreaField("Note", validators=[DataRequired()])
72 changes: 70 additions & 2 deletions application/blueprints/planning_consideration/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import datetime

from flask import Blueprint, flash, redirect, render_template, request, session, url_for
from flask import (
Blueprint,
abort,
flash,
redirect,
render_template,
request,
session,
url_for,
)
from markupsafe import Markup
from slugify import slugify

Expand All @@ -10,14 +19,15 @@
FrequencyForm,
LinkForm,
LLCForm,
NoteForm,
PriorityForm,
PublicForm,
StageForm,
SynonymForm,
)
from application.extensions import db
from application.forms import DeleteForm
from application.models import Consideration, FrequencyOfUpdates, Stage
from application.models import Consideration, FrequencyOfUpdates, Note, Stage
from application.utils import login_required, true_false_to_bool

planning_consideration = Blueprint(
Expand Down Expand Up @@ -513,3 +523,61 @@ def edit_legislation(slug):
return render_template(
"questiontypes/input.html", consideration=consideration, form=form, page=page
)


@planning_consideration.route("/<slug>/note", methods=["GET", "POST"])
@login_required
def add_note(slug):
consideration = Consideration.query.filter(Consideration.slug == slug).first()
form = NoteForm()

if form.validate_on_submit():
note = Note(text=form.text.data, author=session["user"]["name"])
if consideration.notes is None:
consideration.notes = []
consideration.notes.append(note)

db.session.add(consideration)
db.session.commit()
return redirect(url_for("planning_consideration.consideration", slug=slug))

page = {"title": "Add note", "submit_text": "Save note"}

return render_template(
"questiontypes/input.html", consideration=consideration, form=form, page=page
)


@planning_consideration.route("/<slug>/note/<note_id>", methods=["GET", "POST"])
@login_required
def edit_note(slug, note_id):
consideration = Consideration.query.filter(Consideration.slug == slug).first()
note = Note.query.get(note_id)
if note is None or note.deleted_date is not None:
abort(404)

form = NoteForm(obj=note)

if form.validate_on_submit():
note.text = form.text.data
note.author = session["user"]["name"]

db.session.add(note)
db.session.commit()
return redirect(url_for("planning_consideration.consideration", slug=slug))

page = {"title": "Edit note", "submit_text": "Update note"}

return render_template(
"questiontypes/input.html", consideration=consideration, form=form, page=page
)


@planning_consideration.get("/<slug>/note/<note_id>/delete")
@login_required
def delete_note(slug, note_id):
note = Note.query.get(note_id)
note.deleted_date = datetime.date.today()
db.session.add(note)
db.session.commit()
return redirect(url_for("planning_consideration.consideration", slug=slug))
19 changes: 18 additions & 1 deletion application/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Consideration(DateModel):
changes: Mapped[Optional[list]] = mapped_column(MutableList.as_mutable(JSONB))

is_local_land_charge: Mapped[bool] = mapped_column(Boolean, default=False)
notes: Mapped[List["Note"]] = relationship(back_populates="consideration")

def delete(self):
self.deleted_date = datetime.date.today()
Expand Down Expand Up @@ -123,7 +124,7 @@ def receive_before_update(mapper, connection, target):
modifications = {}
state = db.inspect(target)
for attr in state.attrs:
if attr.key not in ["changes", "udpated", "deleted_date"]:
if attr.key not in ["changes", "udpated", "deleted_date", "notes"]:
history = attr.load_history()
if history.has_changes():
c = {}
Expand Down Expand Up @@ -219,6 +220,22 @@ def __repr__(self):
return f"<Question {self.text}> <Stage {self.stage}>"


class Note(DateModel):

id: Mapped[uuid.uuid4] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
text: Mapped[str] = mapped_column(Text)
consideration_id: Mapped[uuid.uuid4] = mapped_column(
UUID(as_uuid=True), ForeignKey("consideration.id")
)
consideration: Mapped[Consideration] = relationship(back_populates="notes")
author: Mapped[str] = mapped_column(Text)

def __repr__(self):
return f"<Note {self.text}>"


# pydantic models


Expand Down
28 changes: 28 additions & 0 deletions application/templates/consideration.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,34 @@ <h2 class="govuk-heading-l">Design process stages</h2>
</ul>
</section>

<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible">

{% if not config.AUTHENTICATION_ON or session["user"] %}
<section id="notes">
<h2 class="govuk-heading-l">Notes</h2>
<ul class="govuk-list govuk-list--bullet govuk-list--spaced">
{% for note in consideration.notes %}
{% if not note.deleted_date %}
<li>
<p>{{ note.text }}</p>
<span class="govuk-!-font-size-14">author: {{ note.author }}</span>
<span class="govuk-!-font-size-14">added: {{ note.created }}</span>
<br>
<span class="govuk-!-font-size-14">
<a href="{{ url_for('planning_consideration.edit_note', slug=consideration.slug, note_id=note.id )}}" class="govuk-link" >Edit<span class="govuk-visually-hidden"> note</span></a>
</span>
<span class="govuk-!-font-size-14">
<a href="{{ url_for('planning_consideration.delete_note', slug=consideration.slug, note_id=note.id )}}" class="govuk-link
app-destructive-link" >Remove<span class="govuk-visually-hidden"> note</span></a>
</span>
</li>
{% endif %}
{% endfor %}
</ul>
<a href="{{ url_for('planning_consideration.add_note', slug=consideration.slug)}}" class="govuk-button govuk-button--secondary"><i class="app-icon">+</i>Add note</a>
</section>
{% endif %}

{% endblock content_primary %}

{% block content_secondary %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""add notes to considerations app
Revision ID: a37de03e6a59
Revises: 376575e0c068
Create Date: 2024-05-20 10:27:29.764107
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = 'a37de03e6a59'
down_revision = '376575e0c068'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('note',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('text', sa.Text(), nullable=False),
sa.Column('consideration_id', sa.UUID(), nullable=False),
sa.Column('author', sa.Text(), nullable=False),
sa.Column('created', sa.Date(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=True),
sa.Column('deleted_date', sa.Date(), nullable=True),
sa.ForeignKeyConstraint(['consideration_id'], ['consideration.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('note')
# ### end Alembic commands ###

0 comments on commit c05b919

Please sign in to comment.