Skip to content

Commit

Permalink
Merge pull request #1989 from laws-africa/ammendments-made-tasks
Browse files Browse the repository at this point in the history
create Amendment tasks on approve when a work amends other works
  • Loading branch information
goose-life authored Feb 21, 2024
2 parents b7b496f + 2f1e3ba commit e7c3fb6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
31 changes: 18 additions & 13 deletions indigo_app/forms/works.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from datetime import date
from functools import cached_property
from itertools import chain

from django import forms
from django.conf import settings
Expand Down Expand Up @@ -818,21 +819,26 @@ class WorkBulkApproveForm(forms.Form):
gazette_task_works = forms.ModelMultipleChoiceField(queryset=Work.objects, required=False)
update_gazette_tasks = forms.ChoiceField(choices=TASK_CHOICES, widget=RadioSelect, required=False)
gazette_task_description = forms.CharField(required=False)
amendment_task_works = forms.ModelMultipleChoiceField(queryset=Work.objects, required=False)
amendments = forms.ModelMultipleChoiceField(queryset=Amendment.objects, required=False)
update_amendment_tasks = forms.ChoiceField(choices=TASK_CHOICES, widget=RadioSelect, required=False)
# TODO: add multichoice label dropdown per task type too
approve = forms.BooleanField(required=False)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.is_valid():
self.add_amendment_task_description_fields(self.cleaned_data.get('works_in_progress', []))
self.add_amendment_task_description_fields(self.get_amendments())
self.full_clean()

def add_amendment_task_description_fields(self, works_in_progress):
for work in works_in_progress:
for amendment in work.amendments.all():
self.fields[f'amendment_task_description_{amendment.pk}'] = forms.CharField()
def get_amendments(self):
works_in_progress = self.cleaned_data.get('works_in_progress', [])
all_amendments = [w.amendments.all() for w in works_in_progress if w.amendments.exists()] + \
[w.amendments_made.all() for w in works_in_progress if w.amendments_made.exists()]
return set(chain(*all_amendments))

def add_amendment_task_description_fields(self, amendments):
for amendment in amendments:
self.fields[f'amendment_task_description_{amendment.pk}'] = forms.CharField()

def save_changes(self, request):
for work in self.cleaned_data["works_in_progress"]:
Expand All @@ -852,14 +858,13 @@ def save_changes(self, request):
self.block_or_cancel_tasks(gazette_tasks, self.cleaned_data['update_gazette_tasks'], request.user)

# amendment tasks
if self.cleaned_data.get('amendment_task_works'):
if self.cleaned_data.get('amendments'):
amendment_tasks = []
for work in self.cleaned_data['amendment_task_works']:
for amendment in work.amendments.all():
amendment_tasks.append(self.get_or_create_task(
work=work, task_type='apply-amendment',
description=self.cleaned_data[f'amendment_task_description_{amendment.pk}'],
user=request.user, timeline_date=amendment.date))
for amendment in self.cleaned_data['amendments']:
amendment_tasks.append(self.get_or_create_task(
work=amendment.amended_work, task_type='apply-amendment',
description=self.cleaned_data[f'amendment_task_description_{amendment.pk}'],
user=request.user, timeline_date=amendment.date))
if self.cleaned_data.get('update_amendment_tasks'):
self.block_or_cancel_tasks(amendment_tasks, self.cleaned_data['update_amendment_tasks'], request.user)

Expand Down
26 changes: 9 additions & 17 deletions indigo_app/templates/indigo_app/place/_bulk_approve_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ <h5 class="modal-title">
{% for work in gazette_task_works %}
<input type="hidden" name="gazette_task_works" value="{{ work.pk }}">
{% endfor %}
{% for work in amendments_per_work %}
<input type="hidden" name="amendment_task_works" value="{{ work.pk }}">
{% for amendment in amendments %}
<input type="hidden" name="amendments" value="{{ amendment.pk }}">
{% endfor %}
<div class="alert alert-warning">
<ul>
Expand Down Expand Up @@ -106,23 +106,15 @@ <h5 class="card-header">
{% trans "Amendment tasks will be created" %}
</h5>
<div class="card-body">
<ul>
{% for work in amendments_per_work %}
<li>
<a target="_blank" href="{% url 'work' work.frbr_uri %}">{{ work.title }}</a>
</li>
{% endfor %}
</ul>
<div>
{% for work, amendments in amendments_per_work.items %}
<div class="mt-4">
<hr>
{% for work, amendments in amendments_per_work.items %}
<div class="mb-3 list-group">
<div class="list-group-item">
<a target="_blank" href="{% url 'work' work.frbr_uri %}">{{ work.title }}</a>
{% for amendment in amendments %}
<div class="mt-3">
<label for="amendment_task_description_id_{{ amendment.pk }}">
{% blocktrans with title=amendment.amending_work.title numbered_title=amendment.amending_work.numbered_title date=amendment.date|date:"Y-m-d" %}
Task description: {{ title }} ({{ numbered_title }}) – {{ date }}
{% blocktrans with amended_title=amendment.amended_work.title amended_numbered_title=amendment.amended_work.numbered_title amending_title=amendment.amending_work.title amending_numbered_title=amendment.amending_work.numbered_title date=amendment.date|date:"Y-m-d" %}
Task description: {{ amended_title }} ({{ amended_numbered_title }}) amended by {{ amending_title }} ({{ amending_numbered_title }}) – {{ date }}
{% endblocktrans %}
</label>
{% blocktrans asvar description_default with title=amendment.amending_work.title numbered_title=amendment.amending_work.numbered_title date=amendment.date|date:"Y-m-d" %}
Expand All @@ -134,8 +126,8 @@ <h5 class="card-header">
</div>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
<div class="mt-3">
{% for opt in form.update_amendment_tasks %}
<div>
Expand Down
12 changes: 5 additions & 7 deletions indigo_app/views/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,8 +1279,9 @@ def get_context_data(self, form, **kwargs):
context["works_in_progress"] = works_in_progress = form.cleaned_data.get("works_in_progress").order_by("-created_at")
context["import_task_works"] = works_in_progress.filter(principal=True)
context["gazette_task_works"] = [w for w in works_in_progress if not w.has_publication_document()]
amendment_task_works = [w for w in works_in_progress if w.amendments.exists()]
context["amendments_per_work"] = {w: w.amendments.all() for w in amendment_task_works}
context["amendments"] = all_amendments = form.get_amendments()
amended_works = set(a.amended_work for a in all_amendments)
context["amendments_per_work"] = {w: w.amendments.filter(pk__in=[a.pk for a in all_amendments]) for w in amended_works}
return context

def form_valid(self, form):
Expand All @@ -1291,11 +1292,8 @@ def form_valid(self, form):
messages.success(self.request, f"Created {form.cleaned_data['import_task_works'].count()} Import tasks.")
if form.cleaned_data.get('gazette_task_works'):
messages.success(self.request, f"Created {form.cleaned_data['gazette_task_works'].count()} Gazette tasks.")
if form.cleaned_data.get('amendment_task_works'):
amendments_count = 0
for work in form.cleaned_data['amendment_task_works']:
amendments_count += work.amendments.count()
messages.success(self.request, f"Created {amendments_count} Amendment tasks.")
if form.cleaned_data.get('amendments'):
messages.success(self.request, f"Created {form.cleaned_data['amendments'].count()} Amendment tasks.")
return redirect(self.request.headers["Referer"])
return self.form_invalid(form)

Expand Down

0 comments on commit e7c3fb6

Please sign in to comment.