diff options
Diffstat (limited to 'archaeological_context_records')
| -rw-r--r-- | archaeological_context_records/forms.py | 69 | ||||
| -rw-r--r-- | archaeological_context_records/models.py | 2 | ||||
| -rw-r--r-- | archaeological_context_records/templates/ishtar/sheet_contextrecord.html | 11 | ||||
| -rw-r--r-- | archaeological_context_records/urls.py | 8 | ||||
| -rw-r--r-- | archaeological_context_records/views.py | 41 |
5 files changed, 101 insertions, 30 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index df074c768..9f7dfa0b4 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -43,7 +43,6 @@ from archaeological_context_records import models from ishtar_common.forms import ( FinalForm, - FormSet, reverse_lazy, get_form_selection, ManageOldType, @@ -512,28 +511,6 @@ class RecordFormGeneral(CustomForm, ManageOldType): return cleaned_data -class DatingForm(ManageOldType, forms.Form): - form_label = _("Dating") - base_model = "dating" - associated_models = { - "dating_type": models.DatingType, - "quality": models.DatingQuality, - "period": models.Period, - } - period = forms.ChoiceField(label=_("Chronological period"), choices=[]) - start_date = forms.IntegerField(label=_("Start date"), required=False) - end_date = forms.IntegerField(label=_("End date"), required=False) - quality = forms.ChoiceField(label=_("Quality"), required=False, choices=[]) - dating_type = forms.ChoiceField(label=_("Dating type"), required=False, choices=[]) - precise_dating = forms.CharField(label=_("Precise on this dating"), required=False) - - TYPES = [ - FieldType("dating_type", models.DatingType), - FieldType("quality", models.DatingQuality), - FieldType("period", models.Period), - ] - - class RecordFormInterpretation(CustomForm, ManageOldType): HEADERS = {} form_label = _("Interpretation") @@ -921,3 +898,49 @@ class QAContextRecordFormMulti(QAForm): def _set_qa_relation_type(self, item, __): pass + + +class QADating(ManageOldType, forms.Form): + form_label = _("Dating") + associated_models = { + "dating_type_id": models.DatingType, + "quality_id": models.DatingQuality, + "period_id": models.Period, + } + pk = forms.IntegerField(required=False, widget=forms.HiddenInput) + reference = forms.CharField( + label=_("Reference"), validators=[validators.MaxLengthValidator(400)], + required=False + ) + period_id = forms.ChoiceField(label=_("Chronological period"), choices=[], + required=False) + start_date = forms.IntegerField(label=_("Start date"), required=False) + end_date = forms.IntegerField(label=_("End date"), required=False) + quality_id = forms.ChoiceField(label=_("Quality"), required=False, choices=[]) + dating_type_id = forms.ChoiceField(label=_("Dating type"), required=False, choices=[]) + precise_dating = forms.CharField(label=_("Precise on this dating"), required=False, + widget=forms.Textarea) + + TYPES = [ + FieldType("dating_type_id", models.DatingType), + FieldType("quality_id", models.DatingQuality), + FieldType("period_id", models.Period), + ] + + def clean(self): + cleaned_data = self.cleaned_data + if any(1 for k in self.cleaned_data if self.cleaned_data[k]): + return cleaned_data + raise forms.ValidationError(_("No data provided.")) + + def save(self, model, item): + data = copy(self.cleaned_data) + data[model.CURRENT_MODEL_ATTR + "_id"] = item.pk + for attr in ['period_id', 'quality_id', 'dating_type_id']: + if not data.get(attr, None): + data[attr] = None + if data.get("pk", None): + pk = data.pop("pk") + model.objects.filter(pk=pk).update(**data) + return + model.objects.create(**data) diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 0c8a18f43..63e6a773d 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -1376,7 +1376,7 @@ class ContextRecord( return self.full_label() def _generate_cached_periods(self): - return " & ".join(dating.period.label for dating in self.datings.all()) + return " & ".join(period.label for period in self.periods.all()) def _generate_cached_related_context_records(self): return self.detailed_related_context_records() diff --git a/archaeological_context_records/templates/ishtar/sheet_contextrecord.html b/archaeological_context_records/templates/ishtar/sheet_contextrecord.html index 869d6edd5..bd766f9f6 100644 --- a/archaeological_context_records/templates/ishtar/sheet_contextrecord.html +++ b/archaeological_context_records/templates/ishtar/sheet_contextrecord.html @@ -28,6 +28,7 @@ {% with display_interpretation=item|safe_or:"identifications.count"|or_:item.interpretation|or_:item.activity %} {% with display_datations=dating_list|or_:item.taq|or_:item.taq_estimated|or_:item.tpq|or_:item.tpq_estimated|or_:has_cultural_attributions %} {% with can_view_finds=permission_view_own_find|or_:permission_view_find %} +{% with can_change=permission_change_own_find|or_:permission_change_find %} {% with has_finds=item|safe_or:"base_finds.count"|safe_and_not:"base_finds_not_available" %} {% with display_finds=has_finds|and_:can_view_finds %} {% with display_data=item.data %} @@ -50,7 +51,7 @@ </a> </li> {% endif %} - {% if display_datations %} + {% if display_datations or can_change %} <li class="nav-item"> <a class="nav-link" id="{{window_id}}-datations-tab" data-toggle="tab" href="#{{window_id}}-datations" role="tab" @@ -211,14 +212,14 @@ </div> {% endif %} - {% if display_datations %} + {% if display_datations or can_change %} <div class="tab-pane fade" id="{{window_id}}-datations" role="tabpanel" aria-labelledby="{{window_id}}-datations-tab"> - {% if dating_list or item.periods_count %} <h3>{% trans "Periods / Datings" %}</h3> - {% endif %} {% field_flex_multiple_obj _("Periods") item 'periods' %} + {% with url_dating="context-record-dating" %} {% include "ishtar/blocks/sheet_dating_list.html" %} + {% endwith %} {% if item.cultural_attributions_count or item.taq or item.taq_estimated or item.tpq or item.tpq_estimated or datings_comment %} <h3>{% trans "Dating complements" %}</h3> <div class='row'> @@ -370,6 +371,6 @@ {% endif %} </div> -{% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} +{% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endwith %} {% endblock %} diff --git a/archaeological_context_records/urls.py b/archaeological_context_records/urls.py index bb06e9945..ec529deeb 100644 --- a/archaeological_context_records/urls.py +++ b/archaeological_context_records/urls.py @@ -162,6 +162,14 @@ urlpatterns = [ name="context-record-relation-modify", ), re_path( + r"^context-record-dating/(?P<pk>.+)/$", + check_permissions(["archaeological_context_records.change_contextrecord", + "archaeological_context_records.change_own_contextrecord"])( + views.context_record_dating_add + ), + name="context-record-dating-add", + ), + re_path( r"^operation-qa-contextrecord/(?P<pks>[0-9]+)/$", check_permissions(["archaeological_context_records.add_contextrecord"])( views.QAOperationContextRecordView.as_view() diff --git a/archaeological_context_records/views.py b/archaeological_context_records/views.py index 69f995932..8c222f395 100644 --- a/archaeological_context_records/views.py +++ b/archaeological_context_records/views.py @@ -19,9 +19,10 @@ import json +from django.core.exceptions import PermissionDenied from django.db.models import Q from django.http import HttpResponse, HttpResponseRedirect, Http404 -from django.shortcuts import redirect +from django.shortcuts import redirect, render from django.urls import reverse from ishtar_common.utils import gettext_lazy as _ from django.views.generic import RedirectView @@ -194,6 +195,44 @@ context_record_modify_relations = get_relation_modify( ) +def get_dating_form(model, dating_model, url_name, action='add'): + def _dating_add(request, pk, current_right=None): + try: + item = model.objects.get(pk=pk) + except model.DoesNotExist: + raise Http404() + if "_own_" in current_right: + if not request.user.has_perm(current_right, item): + raise PermissionDenied() + elif current_right: + if not request.user.has_perm(current_right): + raise PermissionDenied() + if request.method == 'POST': + form = forms.QADating(request.POST) + if form.is_valid(): + form.save(dating_model, item) + return redirect(reverse(url_name, args=[pk])) + else: + form = forms.QADating() + button_name = _("Add") if action == "add" else _("Modify") + icon = "fa fa-plus" if action == "add" else "fa fa-pencil" + return render( + request, + "ishtar/forms/qa_form.html", { + "page_name": _("Dating"), + "icon": icon, + "action_name": button_name, + "form": form, + "url": reverse(url_name, args=[pk]) + }) + return _dating_add + + +context_record_dating_add = get_dating_form( + models.ContextRecord, models.ContextRecordDating, "context-record-dating-add" +) + + class GenerateRelationImage(IshtarMixin, LoginRequiredMixin, RedirectView): upper_model = models.Operation model = models.ContextRecord |
