Skip to content

Commit

Permalink
add org to timetable form
Browse files Browse the repository at this point in the history
  • Loading branch information
ashimali committed Dec 12, 2024
1 parent f7386be commit c788eab
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
11 changes: 11 additions & 0 deletions application/blueprints/timetable/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ class EventForm(FlaskForm):
local_plan_event = SelectField("Local plan event type", validators=[DataRequired()])
event_date = DatePartField("Event date", validators=[Optional()])
notes = TextAreaField("Notes")
organisation = SelectField("Organisation", validators=[Optional()])

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Always set choices when form is instantiated
self.local_plan_event.choices = self._get_event_choices()
self.organisation.choices = self._get_organisation_choices()

def process(self, formdata=None, obj=None, **kwargs):
# First set the choices
Expand All @@ -199,6 +201,15 @@ def _get_event_choices():
for evt in LocalPlanEventType.query.order_by(LocalPlanEventType.name).all()
]

@staticmethod
def _get_organisation_choices():
from application.models import Organisation

return [("", "")] + [
(org.organisation, org.name)
for org in Organisation.query.order_by(Organisation.name).all()
]

def get_error_summary(self):
"""Get summary of form errors for display"""
errors = []
Expand Down
3 changes: 3 additions & 0 deletions application/blueprints/timetable/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def edit(local_plan_reference, timetable_reference):
timetable.event_date = event_date
timetable.local_plan_event = local_plan_event_type.reference
timetable.notes = form.notes.data
timetable.organisation = (
form.organisation.data if form.organisation.data else None
)
db.session.add(timetable)
db.session.commit()
return redirect(url_for("local_plan.get_plan", reference=local_plan_reference))
Expand Down
7 changes: 4 additions & 3 deletions application/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ class LocalPlanTimetable(BaseModel):
local_plan: Mapped["LocalPlan"] = relationship(back_populates="timetable")
notes: Mapped[Optional[str]] = mapped_column(Text)

@property
def organisations(self):
return self.local_plan.organisations
organisation: Mapped[Optional[str]] = mapped_column(
ForeignKey("organisation.organisation"), nullable=True
)
organisation_obj: Mapped["Organisation"] = relationship()

def get_event_type_name(self, key):
if key not in self.event_data:
Expand Down
2 changes: 2 additions & 0 deletions application/templates/local_plan/plan.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ <h2 id="timetable" class="govuk-heading-m">Timetable</h2>
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header">Event</th>
<th scope="col" class="govuk-table__header" data-sort="none">Event date</th>
<th scope="col" class="govuk-table__header">Organisation</th>
<th scope="col" class="govuk-table__header">Notes</th>
<th scope="col" class="govuk-table__header app-cell--meta app-cell--actions"></th>
</tr>
Expand All @@ -110,6 +111,7 @@ <h2 id="timetable" class="govuk-heading-m">Timetable</h2>
<tr class="govuk-table__row" data-timetable-entry-reference="{{ event.reference }}" data-timetable-entry-plan="{{ event.reference }}">
<td scope="row" class="govuk-table__header">{{ event.event_type.name }}</td>
<td class="govuk-table__cell">{{ event.event_date }}</td>
<td class="govuk-table__cell">{% if event.organisation %}{{ event.organisation_obj.name }}{% endif %}</td>
<td class="govuk-table__cell">{% if event.notes %}{{ event.notes }}{% endif %}</td>
<td class="govuk-table__cell app-cell--meta app-cell--actions">
<ul class="govuk-list govuk-!-margin-bottom-0 govuk-!-font-size-16">
Expand Down
15 changes: 11 additions & 4 deletions application/templates/timetable/event-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ <h2 class="govuk-error-summary__title">

<form class="govuk-form" method="POST" action="{{ action }}">
{{ form.hidden_tag() }}

<div class="govuk-form-group">
{{ form.local_plan_event.label(class='govuk-label') }}
{{ form.local_plan_event(class='govuk-select', id='local_plan_event') }}
Expand Down Expand Up @@ -101,12 +100,14 @@ <h2 class="govuk-error-summary__title">
</div>
</fieldset>
</div>

<div class="govuk-form-group">
{{ form.organisation.label(class='govuk-label') }}
{{ form.organisation(class='govuk-select', id='organisation') }}
</div>
<div class="govuk-form-group">
{{ form.notes.label(class='govuk-label') }}
{{ form.notes(class='govuk-textarea') }}
</div>

<div class="govuk-button-group">
<button class="govuk-button" type="submit">Save</button>
<a class="govuk-link" href="{{ url_for('local_plan.get_plan', reference=local_plan.reference) }}">Cancel</a>
Expand All @@ -126,6 +127,12 @@ <h2 class="govuk-error-summary__title">
selectElement: $eventType
})
</script>
-->
<script src="{{ url_for('static', filename='javascripts/vendor/accessible-autocomplete.min.js') }}"></script>
<script>
const $eventType = document.getElementById('organisation')
accessibleAutocomplete.enhanceSelectElement({
selectElement: $eventType
})
</script>
{% endblock pageScripts %}
66 changes: 66 additions & 0 deletions migrations/versions/2d2ae3b7bc65_add_org_to_timetable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""add org to timetable
Revision ID: 2d2ae3b7bc65
Revises: 72095fc4eab8
Create Date: 2024-12-12 14:29:32.867950
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "2d2ae3b7bc65"
down_revision = "72095fc4eab8"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("local_plan_timetable", schema=None) as batch_op:
batch_op.add_column(sa.Column("organisation", sa.Text(), nullable=True))
batch_op.drop_index("ix_local_plan_event_end_date")
batch_op.create_index(
batch_op.f("ix_local_plan_timetable_end_date"), ["end_date"], unique=False
)
batch_op.create_foreign_key(
"fk_local_plan_timetable_organisation",
"organisation",
["organisation"],
["organisation"],
)

# Find plans with exactly one organisation and update their timetable entries
op.execute(
"""
WITH single_org_plans AS (
SELECT lpo.local_plan, MAX(lpo.organisation) AS organisation
FROM local_plan_organisation lpo
GROUP BY lpo.local_plan
HAVING COUNT(*) = 1
)
UPDATE local_plan_timetable lpt
SET organisation = sop.organisation
FROM single_org_plans sop
WHERE lpt.local_plan_reference = sop.local_plan
"""
)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("local_plan_timetable", schema=None) as batch_op:
batch_op.drop_constraint(
"fk_local_plan_timetable_organisation", type_="foreignkey"
)
batch_op.drop_index(batch_op.f("ix_local_plan_timetable_end_date"))
batch_op.create_index(
"ix_local_plan_event_end_date", ["end_date"], unique=False
)
batch_op.drop_column("organisation_id")

# ### end Alembic commands ###

0 comments on commit c788eab

Please sign in to comment.