summaryrefslogtreecommitdiff
path: root/archaeological_files
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-07-05 16:55:49 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-07-08 09:59:31 +0200
commitc02ea8056cde5f2c86af4787cadd64498c5ebf0b (patch)
tree79245aa6e7000c98c270b3d7110b7a3794638ea8 /archaeological_files
parentb564db3bb2dbbb71c0d1c1eb7f40ef44fa50e811 (diff)
downloadIshtar-c02ea8056cde5f2c86af4787cadd64498c5ebf0b.tar.bz2
Ishtar-c02ea8056cde5f2c86af4787cadd64498c5ebf0b.zip
Preventive file - admin: copy job and costs from price agreement
Diffstat (limited to 'archaeological_files')
-rw-r--r--archaeological_files/admin.py101
-rw-r--r--archaeological_files/migrations/0107_auto_20220705_1612.py (renamed from archaeological_files/migrations/0107_auto_20220630_1519.py)12
-rw-r--r--archaeological_files/models.py6
-rw-r--r--archaeological_files/templates/admin/copy_price.html14
-rw-r--r--archaeological_files/templates/admin/copy_price_change_list.html14
5 files changed, 145 insertions, 2 deletions
diff --git a/archaeological_files/admin.py b/archaeological_files/admin.py
index edc6be716..df2afd677 100644
--- a/archaeological_files/admin.py
+++ b/archaeological_files/admin.py
@@ -19,7 +19,13 @@
from ajax_select import make_ajax_form
+from django import forms
from django.conf import settings
+from django.conf.urls import url
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
+from django.shortcuts import get_object_or_404, render
+from django.utils.translation import ugettext_lazy as _
from ishtar_common.apps import admin_site
from ishtar_common.admin import (
@@ -87,6 +93,7 @@ class JobAdmin(GeneralTypeAdmin):
list_filter = ("available", "price_agreement")
LIST_DISPLAY = [
"label",
+ "price_agreement",
"permanent_contract",
"order",
"ground_daily_cost",
@@ -116,6 +123,7 @@ class EquipmentServiceCostAdmin(ImportActionAdmin):
list_filter = ("available", "price_agreement")
list_display = [
"equipment_service_type",
+ "price_agreement",
"specificity",
"parent",
"unitary_cost",
@@ -130,7 +138,98 @@ class EquipmentServiceCostAdmin(ImportActionAdmin):
admin_site.register(models.EquipmentServiceCost, EquipmentServiceCostAdmin)
-class PriceAgreementAdmin(GeneralTypeAdmin):
+class CopyPriceForm(forms.Form):
+ source = forms.ChoiceField(
+ label=_("Copy from"),
+ choices=[],
+ )
+ destination = forms.ChoiceField(
+ label=_("To"),
+ choices=[],
+ )
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ choices = [
+ (idx, label)
+ for idx, label in models.PriceAgreement.objects.values_list("id", "label")
+ ]
+ self.fields["source"].choices = choices
+ self.fields["destination"].choices = choices
+
+ def clean(self):
+ if self.cleaned_data.get("source", None) \
+ == self.cleaned_data.get("destination", None):
+ raise forms.ValidationError(
+ _("Source and destination must be different.")
+ )
+ return self.cleaned_data
+
+
+class CopyPriceAgreementAdmin(GeneralTypeAdmin):
+ change_list_template = "admin/copy_price_change_list.html"
+
+ def get_urls(self):
+ urls = super(CopyPriceAgreementAdmin, self).get_urls()
+ my_urls = [
+ url(r"^copy-price-agreement/$", self.copy_price_agreement),
+ ]
+ return my_urls + urls
+
+ def copy_price_agreement(self, request):
+ form = None
+
+ if "apply" in request.POST:
+ form = CopyPriceForm(request.POST)
+ if form.is_valid():
+ created, already_here = 0, 0
+ source = get_object_or_404(
+ models.PriceAgreement, pk=form.cleaned_data["source"]
+ )
+ destination = get_object_or_404(
+ models.PriceAgreement, pk=form.cleaned_data["destination"]
+ )
+ for model, slug_name in ((models.Job, "txt_idx"),
+ (models.EquipmentServiceCost, "slug")):
+ items = model.objects.filter(price_agreement_id=source.pk).all()
+ for item in items:
+ slug = getattr(item, slug_name).split("--")[0] + "--" + \
+ str(source.pk)
+ q = model.objects.filter(**{slug_name: slug})
+ if q.count():
+ already_here += 1
+ continue
+ new_item = item
+ new_item.pk = None
+ setattr(new_item, slug_name, slug)
+ new_item.price_agreement_id = destination.pk
+ new_item.save()
+ created += 1
+ if created:
+ self.message_user(
+ request, str(_("{} item(s) created.")).format(created)
+ )
+ if already_here:
+ self.message_user(
+ request, str(_("{} item(s) already in database.")).format(
+ already_here)
+ )
+ c_url = reverse(
+ "admin:{}_{}_changelist".format(
+ self.model._meta.app_label, self.model._meta.model_name
+ )
+ )
+ return HttpResponseRedirect(c_url)
+ if not form:
+ form = CopyPriceForm()
+ return render(
+ request,
+ "admin/copy_price.html",
+ {"file_form": form, "current_action": "import_generic"},
+ )
+
+
+class PriceAgreementAdmin(CopyPriceAgreementAdmin):
list_filter = ("available",)
extra_list_display = [
"start_date",
diff --git a/archaeological_files/migrations/0107_auto_20220630_1519.py b/archaeological_files/migrations/0107_auto_20220705_1612.py
index 4d0331f0c..2589f0a15 100644
--- a/archaeological_files/migrations/0107_auto_20220630_1519.py
+++ b/archaeological_files/migrations/0107_auto_20220705_1612.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-# Generated by Django 1.11.28 on 2022-06-30 15:19
+# Generated by Django 1.11.28 on 2022-07-05 16:12
from __future__ import unicode_literals
import datetime
@@ -52,6 +52,16 @@ class Migration(migrations.Migration):
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='archaeological_files.PriceAgreement', verbose_name='Price agreement'),
),
migrations.AddField(
+ model_name='file',
+ name='price_agreement',
+ field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='archaeological_files.PriceAgreement', verbose_name='Price agreement'),
+ ),
+ migrations.AddField(
+ model_name='historicalfile',
+ name='price_agreement',
+ field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='archaeological_files.PriceAgreement', verbose_name='Price agreement'),
+ ),
+ migrations.AddField(
model_name='job',
name='price_agreement',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='archaeological_files.PriceAgreement', verbose_name='Price agreement'),
diff --git a/archaeological_files/models.py b/archaeological_files/models.py
index 8d3f014da..60f9882fe 100644
--- a/archaeological_files/models.py
+++ b/archaeological_files/models.py
@@ -80,6 +80,8 @@ class PriceAgreement(GeneralType):
verbose_name_plural = _("Price agreement")
ordering = (
"order",
+ "start_date",
+ "end_date",
"label",
)
@@ -669,6 +671,10 @@ class File(
# <-- research archaeology
# --> preventive detail
+ price_agreement = models.ForeignKey(
+ PriceAgreement, verbose_name=_("Price agreement"), blank=True, null=True,
+ on_delete=models.SET_NULL
+ )
study_period = models.CharField(
_("Study period"), max_length=200, default="", blank=True
)
diff --git a/archaeological_files/templates/admin/copy_price.html b/archaeological_files/templates/admin/copy_price.html
new file mode 100644
index 000000000..f66f9f7a1
--- /dev/null
+++ b/archaeological_files/templates/admin/copy_price.html
@@ -0,0 +1,14 @@
+{% extends "admin/base_site.html" %}
+
+{% block content %}
+
+<form action="." method="post">
+{% csrf_token %}
+<table>
+{{ file_form }}
+</table>
+<input type="hidden" name="action" value="{{current_action}}" />
+<input type="submit" name="apply" value="Copy" />
+</form>
+
+{% endblock %}
diff --git a/archaeological_files/templates/admin/copy_price_change_list.html b/archaeological_files/templates/admin/copy_price_change_list.html
new file mode 100644
index 000000000..1e3ae8585
--- /dev/null
+++ b/archaeological_files/templates/admin/copy_price_change_list.html
@@ -0,0 +1,14 @@
+{% extends "admin/gen_change_list.html" %}
+{% load i18n admin_urls static admin_list %}
+
+ {% block object-tools-items %}
+ {{ block.super }}
+ {% if has_add_permission %}
+ <li>
+ <a href="copy-price-agreement/" class="addlink">
+ {% trans "Copy price agreement" %}
+ </a>
+ </li>
+ {% endif %}
+ {% endblock %}
+