diff options
| -rw-r--r-- | archaeological_context_records/templates/ishtar/sheet_contextrecord.html | 2 | ||||
| -rw-r--r-- | archaeological_operations/templates/ishtar/sheet_operation.html | 8 | ||||
| -rw-r--r-- | example_project/settings.py | 1 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/blocks/window_tables/static_documents.html | 11 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/sheet_person.html | 6 | ||||
| -rw-r--r-- | ishtar_common/templatetags/window_tables.py | 54 | ||||
| -rw-r--r-- | ishtar_common/views.py | 3 | ||||
| -rw-r--r-- | ishtar_common/widgets.py | 18 | 
8 files changed, 83 insertions, 20 deletions
| diff --git a/archaeological_context_records/templates/ishtar/sheet_contextrecord.html b/archaeological_context_records/templates/ishtar/sheet_contextrecord.html index 8a92a2f61..f7296848b 100644 --- a/archaeological_context_records/templates/ishtar/sheet_contextrecord.html +++ b/archaeological_context_records/templates/ishtar/sheet_contextrecord.html @@ -128,7 +128,7 @@  {% trans "Finds" as finds %}  {% if item.base_finds.count %} -{% dynamic_table_document finds 'finds_for_ope' 'base_finds__context_record' item.pk 'TABLE_COLS_FOR_OPE' %} +{% dynamic_table_document finds 'finds_for_ope' 'base_finds__context_record' item.pk 'TABLE_COLS_FOR_OPE' output %}  {% endif %}  {% trans "Documents from associated finds" as find_docs %} diff --git a/archaeological_operations/templates/ishtar/sheet_operation.html b/archaeological_operations/templates/ishtar/sheet_operation.html index c45d6eb73..4e6c85516 100644 --- a/archaeological_operations/templates/ishtar/sheet_operation.html +++ b/archaeological_operations/templates/ishtar/sheet_operation.html @@ -130,24 +130,24 @@  {% trans "Document from this operation" as operation_docs %}  {% if item.source.count %} -{% dynamic_table_document operation_docs 'operation_docs' 'operation' item.pk %} +{% dynamic_table_document operation_docs 'operation_docs' 'operation' item.pk '' output %}  {% endif %}  {% trans "Context records" as context_records %}  {% if item.context_record.count %} -{% dynamic_table_document context_records 'context_records_for_ope' 'operation' item.pk 'TABLE_COLS_FOR_OPE' %} +{% dynamic_table_document context_records 'context_records_for_ope' 'operation' item.pk 'TABLE_COLS_FOR_OPE' output %}  {% endif %}  {% comment %}  {% trans "Documents from associated context records" as cr_docs %}  {% if item.context_record_docs_q.count %} -{% dynamic_table_document cr_docs 'context_records_docs' 'context_record__operation' item.pk %} +{% dynamic_table_document cr_docs 'context_records_docs' 'context_record__operation' item.pk '' output %}  {% endif %}  {% endcomment %}  {% trans "Finds" as finds %}  {% if item.finds %} -{% dynamic_table_document finds 'finds_for_ope' 'base_finds__context_record__operation' item.pk 'TABLE_COLS_FOR_OPE' %} +{% dynamic_table_document finds 'finds_for_ope' 'base_finds__context_record__operation' item.pk 'TABLE_COLS_FOR_OPE' output %}  {% endif %}  {% comment %} diff --git a/example_project/settings.py b/example_project/settings.py index 4b91c554f..cb6e0ad3e 100644 --- a/example_project/settings.py +++ b/example_project/settings.py @@ -104,6 +104,7 @@ TEMPLATE_CONTEXT_PROCESSORS = (      "django.core.context_processors.i18n",      "django.core.context_processors.media",      "django.core.context_processors.static", +    "django.core.context_processors.request",  )  ROOT_URLCONF = 'example_project.urls' diff --git a/ishtar_common/templates/ishtar/blocks/window_tables/static_documents.html b/ishtar_common/templates/ishtar/blocks/window_tables/static_documents.html new file mode 100644 index 000000000..d6686b553 --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/window_tables/static_documents.html @@ -0,0 +1,11 @@ +{% load i18n %} + +<table class='simple'> +  <caption>{{caption}}</caption> +  <tr>{% for col in col_names %} +    <th>{% trans col %}</th>{% endfor %} +  </tr>{% for item in data %} +  <tr>{% for value in item %} +    <td>{{value}}</td>{%endfor%} +  </tr>{% endfor %} +</table> diff --git a/ishtar_common/templates/ishtar/sheet_person.html b/ishtar_common/templates/ishtar/sheet_person.html index 2dfc4bbea..796fe2c4a 100644 --- a/ishtar_common/templates/ishtar/sheet_person.html +++ b/ishtar_common/templates/ishtar/sheet_person.html @@ -35,17 +35,17 @@  {% trans "Associated operations as scientist" as ao %}  {% if item.operation_scientist_responsability.count %} -{% dynamic_table_document ao 'operations' 'scientist' item.pk %} +{% dynamic_table_document ao 'operations' 'scientist' item.pk '' output %}  {% endif %}  {% trans "Associated operations as responsible" as ao %}  {% if item.operation_responsability.count %} -{% dynamic_table_document ao 'operations' 'in_charge' item.pk %} +{% dynamic_table_document ao 'operations' 'in_charge' item.pk '' output %}  {% endif %}  {% trans "Associated archaelogical files" as af %}  {% if item.file_responsability.count %} -{% dynamic_table_document af 'files' 'in_charge' item.pk %} +{% dynamic_table_document af 'files' 'in_charge' item.pk '' output %}  {% endif %}  <table> diff --git a/ishtar_common/templatetags/window_tables.py b/ishtar_common/templatetags/window_tables.py index a1aa735a7..687b2cf49 100644 --- a/ishtar_common/templatetags/window_tables.py +++ b/ishtar_common/templatetags/window_tables.py @@ -1,8 +1,11 @@ +import json  import time  from django import template  from django.conf import settings +from django.core.urlresolvers import resolve  from django.template.defaultfilters import slugify +from django.template.loader import get_template  from django.utils.translation import ugettext_lazy as _  from ishtar_common.forms import reverse_lazy @@ -52,16 +55,21 @@ except:      pass -@register.inclusion_tag('ishtar/blocks/window_tables/dynamic_documents.html') -def dynamic_table_document(caption, associated_model, key, value, -                           table_cols='TABLE_COLS'): +@register.simple_tag(takes_context=True) +def dynamic_table_document(context, caption, associated_model, key, value, +                           table_cols='TABLE_COLS', output='html'): +    if not table_cols: +        table_cols = 'TABLE_COLS'      model, url, url_full = ASSOCIATED_MODELS[associated_model]      grid = JQueryJqGrid(None, None, model, table_cols=table_cols)      source = unicode(reverse_lazy(url))      source_full = unicode(reverse_lazy(url_full)) if url_full else ''      source_attrs = '?{}={}'.format(key, value) -    col_names, extra_cols = grid.get_cols() -    return {'caption': caption, +    if output == 'html': +        col_names, extra_cols = grid.get_cols() +        t = get_template('ishtar/blocks/window_tables/dynamic_documents.html') +        context = template.Context({ +            'caption': caption,              'name': slugify(caption) + '{}'.format(int(time.time())),              'source': source + source_attrs,              'source_full': source_full, @@ -72,4 +80,38 @@ def dynamic_table_document(caption, associated_model, key, value,              'no_result': unicode(_("No results")),              'loading': unicode(_("Loading...")),              'encoding': settings.ENCODING or 'utf-8', -            } +        }) +        return t.render(context) +    else: +        col_names, extra_cols = grid.get_cols(python=True) +        view, args, kwargs = resolve(source) +        request = context['request'] +        if source_attrs and source_attrs.startswith('?'): +            source_attrs = source_attrs[1:] +            dct = {} +            for attr in source_attrs.split('&'): +                if '=' in attr: +                    key, val = attr.split('=') +                    dct[key] = val +            request.GET = dct +        kwargs['request'] = request +        page = view(*args, **kwargs) +        data = [] +        if page.content: +            res = json.loads(page.content) +            if "rows" in res: +                for r in res["rows"]: +                    d = [] +                    for col in extra_cols: +                        if col in r: +                            d.append(r[col]) +                        else: +                            d.append('') +                    data.append(d) +        t = get_template('ishtar/blocks/window_tables/static_documents.html') +        context = template.Context({ +            'caption': caption, +            'col_names': col_names, +            'data': data +        }) +        return t.render(context) diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 1c944441f..5ea53374d 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -729,6 +729,7 @@ def show_item(model, name, extra_dct=None):              dct.update(extra_dct(request, item))          context_instance = RequestContext(request)          context_instance.update(dct) +        context_instance['output'] = 'html'          filename = ""          if hasattr(item, 'history_object'):              filename = item.history_object.associated_filename @@ -736,6 +737,7 @@ def show_item(model, name, extra_dct=None):              filename = item.associated_filename          if doc_type == "odt" and settings.ODT_TEMPLATE:              tpl = loader.get_template('ishtar/sheet_%s.html' % name) +            context_instance['output'] = 'ODT'              content = tpl.render(context_instance)              try:                  tidy_options = {'output-xhtml': 1, 'indent': 1, @@ -775,6 +777,7 @@ def show_item(model, name, extra_dct=None):              return response          elif doc_type == 'pdf':              tpl = loader.get_template('ishtar/sheet_%s_pdf.html' % name) +            context_instance['output'] = 'PDF'              content = tpl.render(context_instance)              result = StringIO.StringIO()              html = content.encode('utf-8') diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 688648e10..57aa8cf69 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -524,7 +524,7 @@ class JQueryJqGrid(forms.RadioSelect):          self.new, self.new_message = new, new_message          self.source_full = source_full -    def get_cols(self): +    def get_cols(self, python=False):          jq_col_names, extra_cols = [], []          col_labels = {}          if hasattr(self.associated_model, self.table_cols + '_LBL'): @@ -557,13 +557,19 @@ class JQueryJqGrid(forms.RadioSelect):                  field_name += f_name                  field_verbose_names.append(unicode(field_verbose_name))              if field_name in col_labels: -                jq_col_names.append(u'"%s"' % unicode(col_labels[field_name])) +                jq_col_names.append(unicode(col_labels[field_name]))              else: -                jq_col_names.append(u'"%s"' % settings.JOINT.join( +                jq_col_names.append(settings.JOINT.join(                      [f for f in field_verbose_names if f])) -            extra_cols.append(self.COL_TPL % {'idx': field_name}) -        jq_col_names = jq_col_names and ", ".join(jq_col_names) or "" -        extra_cols = extra_cols and ", ".join(extra_cols) or "" +            if not python: +                jq_col_names[-1] = u'"%s"' % jq_col_names[-1] +            if python: +                extra_cols.append(field_name) +            else: +                extra_cols.append(self.COL_TPL % {'idx': field_name}) +        if not python: +            jq_col_names = jq_col_names and ", ".join(jq_col_names) or "" +            extra_cols = extra_cols and ", ".join(extra_cols) or ""          return jq_col_names, extra_cols      def render(self, name, value=None, attrs=None): | 
