Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Preview of add first report on add location, refs #680
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 23, 2021
1 parent 83c6e2f commit 9dd3ece
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
32 changes: 30 additions & 2 deletions vaccinate/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.admin.models import LogEntry
from django.db.models import Count, Exists, Max, Min, OuterRef, Q, TextField
from django.db.models.query import QuerySet
from django.db import transaction
from django.forms import Textarea
from django.http import HttpRequest, HttpResponseNotAllowed, HttpResponseRedirect
from django.template.loader import render_to_string
Expand Down Expand Up @@ -490,6 +491,15 @@ def has_change_permission(self, request, obj=None):
return False


class FirstReportInline(admin.StackedInline):
model = Report
min_num = 0
max_num = 1
raw_id_fields = ("claimed_by", "reported_by", "call_request", "location")
fields = ("appointment_tag", "availability_tags", "vaccines_offered")
autocomplete_fields = ("availability_tags",)


@admin.register(Location)
class LocationAdmin(DynamicListDisplayMixin, CompareVersionAdmin):
change_form_template = "admin/change_location.html"
Expand Down Expand Up @@ -618,6 +628,14 @@ class LocationAdmin(DynamicListDisplayMixin, CompareVersionAdmin):
)
deliberately_omitted_from_fieldsets = ("point",)

def get_inlines(self, request, obj):
inlines = self.inlines
if obj is None and request.GET.get("_preview"):
# Add form - include first-report inline
return self.inlines + [FirstReportInline]
else:
return self.inlines

def matched_source_locations(self, obj):
return mark_safe(
"".join(
Expand Down Expand Up @@ -772,8 +790,18 @@ def save_formset(self, request, form, formset, change):
for obj in formset.deleted_objects:
obj.delete()
for instance in instances:
instance.author = request.user
instance.save()
if isinstance(instance, Report):
instance.reported_by = Reporter.for_user(request.user)
instance.save()
# Run after the commit to ensure availability_tags are there:
transaction.on_commit(
lambda: instance.location.derive_availability_and_inventory(
save=True
)
)
else:
instance.author = request.user
instance.save()
formset.save_m2m()

def create_approved_review_note(self, obj, author):
Expand Down
4 changes: 4 additions & 0 deletions vaccinate/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ def get_user(self):
self.save()
return self.user

@classmethod
def for_user(cls, user):
return user.reporters.first()


class AvailabilityTag(models.Model):
"""
Expand Down
2 changes: 1 addition & 1 deletion vaccinate/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ <h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('D
}

function checkboxEditorForVaccinesOffered() {
var ta = document.getElementById('id_vaccines_offered');
var ta = document.getElementById('id_vaccines_offered') || document.getElementById('id_reports-0-vaccines_offered');
if (!ta) {
return;
}
Expand Down

0 comments on commit 9dd3ece

Please sign in to comment.