diff options
Diffstat (limited to 'ishtar_common')
| -rw-r--r-- | ishtar_common/forms_common.py | 22 | ||||
| -rw-r--r-- | ishtar_common/models.py | 95 | ||||
| -rw-r--r-- | ishtar_common/static/js/ishtar.js | 14 | ||||
| -rw-r--r-- | ishtar_common/templates/blocks/JQueryAutocomplete.js | 2 | ||||
| -rw-r--r-- | ishtar_common/templates/blocks/bs_field_snippet.html | 1 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/forms/qa_new_item.html | 3 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/wizard/default_wizard.html | 2 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/wizard/parcels_wizard.html | 7 | ||||
| -rw-r--r-- | ishtar_common/urls.py | 8 | ||||
| -rw-r--r-- | ishtar_common/views.py | 7 | ||||
| -rw-r--r-- | ishtar_common/views_item.py | 65 | 
11 files changed, 211 insertions, 15 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 2b52f0919..2df825594 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -315,7 +315,7 @@ class TargetKeyFormset(BaseModelFormSet):  class OrganizationForm(ManageOldType, NewItemForm):      prefix = 'organization' -    form_label = _(u"Organization") +    form_label = _("Organization")      associated_models = {'organization_type': models.OrganizationType,                           "precise_town": models.Town}      name = forms.CharField( @@ -348,7 +348,7 @@ class OrganizationForm(ManageOldType, NewItemForm):              models.OrganizationType.get_help()          self.limit_fields() -    def save(self, user): +    def save(self, user, item=None):          dct = self.cleaned_data          dct['history_modifier'] = user          dct['organization_type'] = models.OrganizationType.objects.get( @@ -359,7 +359,12 @@ class OrganizationForm(ManageOldType, NewItemForm):                      pk=dct["precise_town"])              except models.Town.DoesNotExist:                  dct.pop("precise_town") -        new_item = models.Organization(**dct) +        if not item: +            new_item = models.Organization(**dct) +        else: +            new_item = item +            for k in dct: +                setattr(new_item, k, dct[k])          new_item.save()          return new_item @@ -792,7 +797,7 @@ class PersonForm(SimplePersonForm):          self.fields['person_types'].help_text = models.PersonType.get_help()          self.limit_fields() -    def save(self, user): +    def save(self, user, item=None):          dct = self.cleaned_data          dct['history_modifier'] = user          for key in self.associated_models.keys(): @@ -806,7 +811,14 @@ class PersonForm(SimplePersonForm):                      except model.DoesNotExist:                          dct.pop(key)          person_types = dct.pop('person_types') -        new_item = models.Person.objects.create(**dct) +        if not item: +            new_item = models.Person.objects.create(**dct) +        else: +            for k in dct: +                setattr(item, k, dct[k]) +            item.save() +            item.person_types.clear() +            new_item = item          for pt in person_types:              new_item.person_types.add(pt)          return new_item diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 5fa668faf..9b446ff9e 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1046,6 +1046,13 @@ class GeneralType(Cached, models.Model):              item.generate_key() +def get_general_type_label(model, slug): +    obj = model.get_cache(slug) +    if not obj: +        return "" +    return str(obj) + +  class HierarchicalType(GeneralType):      parent = models.ForeignKey('self', blank=True, null=True,                                 on_delete=models.SET_NULL, @@ -3971,6 +3978,46 @@ class Address(BaseHistorizedItem):      class Meta:          abstract = True +    def get_short_html_items(self): +        items = [] +        if self.address: +            items.append( +                """<span class="subadress">{}</span>""".format(self.address)) +        if self.address_complement: +            items.append( +                """<span class="subadress-complement">{}</span>""".format( +                    self.address_complement)) +        if self.postal_code: +            items.append( +                """<span class="postal-code">{}</span>""".format( +                    self.postal_code)) +        if self.precise_town: +            items.append( +                """<span class="town">{}</span>""".format( +                    self.precise_town.name)) +        elif self.town: +            items.append( +                """<span class="town">{}</span>""".format( +                    self.town)) +        if self.country: +            items.append( +                """<span class="country">{}</span>""".format( +                    self.country)) +        return items + +    def get_short_html_detail(self): +        html = """<div class="address">""" +        items = self.get_short_html_items() +        if not items: +            items = [ +                "<span class='no-address'>{}</span>".format( +                    _("No associated address") +                ) +            ] +        html += "".join(items) +        html += """</div>""" +        return html +      def get_town_centroid(self):          if self.precise_town:              return self.precise_town.center, self._meta.verbose_name @@ -4138,6 +4185,20 @@ class OrganizationType(GeneralType):          ordering = ('label',) +def get_orga_planning_service_label(): +    lbl = get_general_type_label(OrganizationType, "planning_service") +    if lbl: +        return lbl +    return _("Error: planning_service type is missing") + + +def get_orga_general_contractor_label(): +    lbl = get_general_type_label(OrganizationType, "general_contractor") +    if lbl: +        return lbl +    return _("Error: general_contractor type is missing") + +  post_save.connect(post_save_cache, sender=OrganizationType)  post_delete.connect(post_save_cache, sender=OrganizationType) @@ -4259,6 +4320,27 @@ person_type_pk_lazy = lazy(PersonType.get_or_create_pk, str)  person_type_pks_lazy = lazy(PersonType.get_or_create_pks, str) +def get_sra_agent_label(): +    lbl = get_general_type_label(PersonType, "sra_agent") +    if lbl: +        return lbl +    return _("Error: sra_agent type is missing") + + +def get_general_contractor_label(): +    lbl = get_general_type_label(PersonType, "general_contractor") +    if lbl: +        return lbl +    return _("Error: general_contractor type is missing") + + +def get_responsible_planning_service_label(): +    lbl = get_general_type_label(PersonType, "responsible_planning_service") +    if lbl: +        return lbl +    return _("Error: responsible_planning_service type is missing") + +  class TitleType(GeneralType):      class Meta:          verbose_name = _("Title type") @@ -4407,6 +4489,19 @@ class Person(Address, Merge, OwnPerms, ValueGetter, MainItem):          profile.save()          return profile +    def get_short_html_items(self): +        items = super(Person, self).get_short_html_items() +        if items or not self.attached_to: +            return items +        orga_address = self.attached_to.get_short_html_items() +        if not orga_address: +            return [] +        items.append( +            """<span class="organization">{}</span>""".format( +                self.attached_to.name)) +        items += orga_address +        return items +      def simple_lbl(self):          values = [str(getattr(self, attr)) for attr in ('surname', 'name')                    if getattr(self, attr)] diff --git a/ishtar_common/static/js/ishtar.js b/ishtar_common/static/js/ishtar.js index b8a9bf9ce..60ebb0bc9 100644 --- a/ishtar_common/static/js/ishtar.js +++ b/ishtar_common/static/js/ishtar.js @@ -706,15 +706,21 @@ function save_and_close_window_data(main_page, name_label,                                      name_pk, item_name, item_pk){      var cur_item;      var cur_item_pk; +    var id_item;      if (main_page){ -        cur_item = $(main_page).find("#"+name_label); -        cur_item_pk = $(main_page).find("#"+name_pk); +        cur_item = $(main_page).find("#" + name_label); +        cur_item_pk = $(main_page).find("#" + name_pk); +        id_item = $(main_page).find("#id_" + name_label);      } else { -        cur_item = $("#"+name_label); -        cur_item_pk = $("#"+name_pk); +        cur_item = $("#" + name_label); +        cur_item_pk = $("#" + name_pk); +        id_item = $("#id_" + name_label);      }      cur_item.val(item_name); +    cur_item.change();      cur_item_pk.val(item_pk); +    cur_item_pk.change(); +    id_item.change();  }  function save_and_close_window_many(name_label, name_pk, item_name, item_pk){ diff --git a/ishtar_common/templates/blocks/JQueryAutocomplete.js b/ishtar_common/templates/blocks/JQueryAutocomplete.js index 7cd0e2a60..aaf493db9 100644 --- a/ishtar_common/templates/blocks/JQueryAutocomplete.js +++ b/ishtar_common/templates/blocks/JQueryAutocomplete.js @@ -52,7 +52,7 @@ $(function() {  	{{safe_field_id}}_modify = function(){          var current_val = $('#id_{{field_id}}').val();          if (current_val){ -			dt_qa_open('/modify-{{model_name}}/' + current_val + "/", +			dt_qa_open('/modify-{{model_name}}/{{field_id}}/' + current_val + "/",  					   'modal-dynamic-form-{{model_name}}');  	    }  	} diff --git a/ishtar_common/templates/blocks/bs_field_snippet.html b/ishtar_common/templates/blocks/bs_field_snippet.html index 644f7f433..dcee87f7f 100644 --- a/ishtar_common/templates/blocks/bs_field_snippet.html +++ b/ishtar_common/templates/blocks/bs_field_snippet.html @@ -2,6 +2,7 @@      <div id="main_div-{{field.auto_id}}" class="form-group{% if not field.label %} no-label{% endif %} {% if field.field.widget.attrs.cols or force_large_col %}col-lg-12{% else %}col-lg-6{% endif %}{% if field.errors %} is-invalid{% endif %}{% if field.field.required %} required{% endif %}{% if force_large_col %} full-width{% endif %}"           data-alt-name="{{field.field.alt_name}}">          {% if field.label %}{{ field.label_tag }}{% endif %} +        {% if extra_field_label %}<label><em>{{extra_field_label}}</em></label>{% endif %}          {% if show_field_number or form.show_field_number %}          {% if field.field.order_number %}<span class="badge badge-pill badge-success field-tip">              {{field.field.order_number}} diff --git a/ishtar_common/templates/ishtar/forms/qa_new_item.html b/ishtar_common/templates/ishtar/forms/qa_new_item.html index ae57adfc2..435531594 100644 --- a/ishtar_common/templates/ishtar/forms/qa_new_item.html +++ b/ishtar_common/templates/ishtar/forms/qa_new_item.html @@ -3,7 +3,8 @@  {% block main_form %}  {% if new_item_pk %} -<p>{% if new_item_label %}{{new_item_label}}{% else %}{% trans "Item" %}{% endif %} {% trans "created." %}</p> +<p>{% if new_item_label %}{{new_item_label}}{% else %}{% trans "Item" %}{% endif %} +    {% if modify %}{% trans "modified." %}{% else %}{% trans "created." %}{% endif %}</p>  {% else %}      {% bs_compact_form form %}  {% endif %} diff --git a/ishtar_common/templates/ishtar/wizard/default_wizard.html b/ishtar_common/templates/ishtar/wizard/default_wizard.html index b5ea4207c..cfd17e58f 100644 --- a/ishtar_common/templates/ishtar/wizard/default_wizard.html +++ b/ishtar_common/templates/ishtar/wizard/default_wizard.html @@ -62,6 +62,8 @@ $(document).ready(function(){          return false;      });      {% if open_url %}load_window("{{open_url}}");{% endif %} +    {% block "js_extra_ready" %} +    {% endblock %}  });  </script>  {% endblock %} diff --git a/ishtar_common/templates/ishtar/wizard/parcels_wizard.html b/ishtar_common/templates/ishtar/wizard/parcels_wizard.html index 2028d8792..d8c902c72 100644 --- a/ishtar_common/templates/ishtar/wizard/parcels_wizard.html +++ b/ishtar_common/templates/ishtar/wizard/parcels_wizard.html @@ -22,13 +22,16 @@    {% bs_form wizard.form.selection_form %}  <table class='inline-table' id='parcel-table'> -  <tr>{% for field in wizard.form.forms.0 %}<th{% if not forloop.last %} rowspan='2'{% endif %}>{{ field.label_tag }}</th>{% endfor %}</tr> +  <tr>{% for field in wizard.form.forms.0 %} +    {% if field.required %}<th{%else%}<td{% endif %}{% if not forloop.last %} rowspan='2'{% endif %}> +    {{ field.label_tag }}{% if field.required %}</th>{%else%}</td>{% endif %}{% endfor %} +  </tr>    <tr><td>({% trans "all"%} <input type='checkbox' name='check-all' class='check-all'/>)</td></tr>    {% inline_formset 'Parcels' wizard.form.forms False %}  </table>  {% if add_all %}<p><input type='checkbox' name='add_all_parcels' id='add_all_parcels'> <label for='add_all_parcels'>{% trans "Add all parcels from the archaeological file" %}</label></p>{% endif %} -  <div class="text-center"> +  <div class="mt-3 text-center">        <button class="btn btn-success" name="formset_modify" value="{{wizard.steps.current}}">          {% trans "Add/Modify" %}        </button> diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index cc8d3eed4..8b506c55c 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -206,6 +206,14 @@ urlpatterns += [          views.get_by_importer, name='get-by-importer'),      url(r'new-person/(?:(?P<parent_name>[^/]+)/)?(?:(?P<limits>[^/]+)/)?$',          views.new_person, name='new-person'), +    url(r'modify-person/(?:(?P<parent_name>[^/]+)/)?(?P<pk>[\d+]+)/$', +        views.modify_person, name='modify-person'), +    url(r'detail-person/(?P<pk>[\d+]+)/$', +        views.detail_person, name='detail-person'), +    url(r'modify-organization/(?:(?P<parent_name>[^/]+)/)?(?P<pk>[\d+]+)/$', +        views.modify_organization, name='modify-organization'), +    url(r'detail-organization/(?P<pk>[\d+]+)/$', +        views.detail_organization, name='detail-organization'),      url(r'new-person-noorga/'          r'(?:(?P<parent_name>[^/]+)/)?(?:(?P<limits>[^/]+)/)?$',          views.new_person_noorga, name='new-person-noorga'), diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 4724d61b7..b662c1a4d 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -70,7 +70,8 @@ from ishtar_common.widgets import JQueryAutoComplete  from ishtar_common import tasks  from .views_item import CURRENT_ITEM_KEYS, CURRENT_ITEM_KEYS_DICT, \ -    check_permission, display_item, get_item, show_item, new_qa_item +    check_permission, display_item, get_item, show_item, new_qa_item, \ +    modify_qa_item, get_short_html_detail  logger = logging.getLogger(__name__) @@ -791,11 +792,15 @@ def autocomplete_author(request):  new_person = new_qa_item(models.Person, forms.PersonForm) +modify_person = modify_qa_item(models.Person, forms.PersonForm) +detail_person = get_short_html_detail(models.Person)  new_person_noorga = new_qa_item(models.Person, forms.NoOrgaPersonForm)  new_organization = new_qa_item(models.Organization, forms.OrganizationForm)  show_organization = show_item(models.Organization, 'organization')  get_organization = get_item(models.Organization, 'get_organization',                              'organization') +modify_organization = modify_qa_item(models.Organization, forms.OrganizationForm) +detail_organization = get_short_html_detail(models.Organization)  new_author = new_qa_item(models.Author, forms.AuthorForm)  show_person = show_item(models.Person, 'person') diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 8cd903a00..aa9a39965 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -22,6 +22,7 @@ from django.core.urlresolvers import reverse, NoReverseMatch  from django.db.models import Q, Count, Sum, ImageField, Func, \      ExpressionWrapper, FloatField, FileField  from django.db.models.fields import FieldDoesNotExist +from django.forms.models import model_to_dict  from django.http import HttpResponse  from django.shortcuts import render  from django.template import loader @@ -146,7 +147,7 @@ def new_qa_item(model, frm, many=False):          template = "ishtar/forms/qa_new_item.html"          model_name = model._meta.object_name          if not check_permission(request, 'add_' + model_name.lower()): -            not_permitted_msg = ugettext(u"Operation not permitted.") +            not_permitted_msg = ugettext("Operation not permitted.")              return HttpResponse(not_permitted_msg)          slug = model.SLUG          if model.SLUG == "site": @@ -177,6 +178,68 @@ def new_qa_item(model, frm, many=False):      return func +def get_short_html_detail(model): +    def func(request, pk): +        model_name = model._meta.object_name +        not_permitted_msg = ugettext("Operation not permitted.") +        if not check_permission(request, 'view_' + model_name.lower(), +                                pk): +            return HttpResponse(not_permitted_msg) +        try: +            item = model.objects.get(pk=pk) +        except model.DoesNotExist: +            return HttpResponse(not_permitted_msg) +        html = item.get_short_html_detail() +        return HttpResponse(html) +    return func + + +def modify_qa_item(model, frm): +    def func(request, parent_name="", pk=None): +        template = "ishtar/forms/qa_new_item.html" +        model_name = model._meta.object_name +        not_permitted_msg = ugettext("Operation not permitted.") +        if not check_permission(request, 'change_' + model_name.lower(), +                                pk): +            return HttpResponse(not_permitted_msg) +        slug = model.SLUG +        if model.SLUG == "site": +            slug = "archaeologicalsite" +        try: +            item = model.objects.get(pk=pk) +        except model.DoesNotExist: +            return HttpResponse(not_permitted_msg) +        url_slug = "modify-" + slug +        dct = {'page_name': str(_('Modify a %s' % model_name.lower())), +               'url': reverse(url_slug, args=[parent_name, pk]), +               'slug': slug, +               "modify": True, +               'parent_name': parent_name} +        if request.method == 'POST': +            dct['form'] = frm(request.POST) +            if dct['form'].is_valid(): +                new_item = dct['form'].save(request.user, item) +                lbl = str(new_item) +                if not lbl and hasattr(new_item, "_generate_cached_label"): +                    lbl = new_item._generate_cached_label() +                dct['new_item_label'] = lbl +                dct['new_item_pk'] = new_item.pk +                dct['parent_pk'] = parent_name +                if dct['parent_pk'] and '_select_' in dct['parent_pk']: +                    parents = dct['parent_pk'].split('_') +                    dct['parent_pk'] = "_".join([parents[0]] + parents[2:]) +                return render(request, template, dct) +        else: +            data = model_to_dict(item) +            for k in list(data.keys()): +                if data[k] and isinstance(data[k], list) and hasattr( +                        data[k][0], "pk"): +                    data[k] = [i.pk for i in data[k]] +            dct['form'] = frm(initial=data) +        return render(request, template, dct) +    return func + +  def display_item(model, extra_dct=None, show_url=None):      def func(request, pk, **dct):          if show_url:  | 
