diff options
Diffstat (limited to 'ishtar_common/views.py')
-rw-r--r-- | ishtar_common/views.py | 145 |
1 files changed, 133 insertions, 12 deletions
diff --git a/ishtar_common/views.py b/ishtar_common/views.py index a45a9f87c..1fa968a95 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -71,12 +71,14 @@ from archaeological_finds.forms import DashboardTreatmentForm, \ from ishtar_common.forms import FinalForm, FinalDeleteForm from ishtar_common.widgets import JQueryAutoComplete from ishtar_common.utils import get_random_item_image_link, shortify, \ - get_all_field_names + get_all_field_names, get_field_labels_from_path from ishtar_common import forms_common as forms from ishtar_common import wizards from ishtar_common.models import HistoryError, PRIVATE_FIELDS, \ get_current_profile +from ishtar_common.templatetags.link_to_window import link_to_window + import models CSV_OPTIONS = {'delimiter': ',', 'quotechar': '"', 'quoting': csv.QUOTE_ALL} @@ -1882,8 +1884,8 @@ class ImportStepByStepView(IshtarMixin, LoginRequiredMixin, TemplateView): headers = [f.label for f in self.imprt.get_formaters()] dct['values'] = zip(headers, self.imprt.current_csv_line) return dct - headers, interpreted_values = [], [] - for formater in self.imprt.get_formaters(): + headers, self.path_to_column, interpreted_values = [], {}, [] + for idx, formater in enumerate(self.imprt.get_formaters()): lbl = formater.label if formater.comment: lbl += u' <i data-toggle="tooltip" class="fa ' @@ -1896,26 +1898,145 @@ class ImportStepByStepView(IshtarMixin, LoginRequiredMixin, TemplateView): if formater.export_field_name: field_name = formater.export_field_name[0] value = self.new_data[0].copy() - for key in field_name.split('__'): - if value and key in value: + + field_name_tuple = field_name.split(u'__') + # associate each path level to this column + while field_name_tuple: + current_field_name = u'__'.join(field_name_tuple) + if current_field_name not in self.path_to_column: + self.path_to_column[current_field_name] = [] + self.path_to_column[current_field_name].append(idx) + field_name_tuple.pop() + + for idx, key in enumerate(field_name.split('__')): + if isinstance(value, dict) and key in value: value = value[key] + elif not idx: + # no key reach + value = None else: - value = u"-" - if value in (None, [], [None]): - value = _(u"* empty *") - if isinstance(value, list): - value = u" ; ".join(value) + break + value = self.get_value(value) interpreted_values.append(value) dct['values'] = zip( range(1, len(headers) + 1), headers, self.imprt.current_csv_line, interpreted_values ) - dct['new_objects'] = self.imprt.new_objects - dct['updated_objects'] = self.imprt.updated_objects + + new_objects = {} + for path, cls, new_dct in self.imprt.new_objects: + # transform path to explicit label + label = self.transform_path_to_label(self.imprt.OBJECT_CLS, path) + + created_dict = {} + for k, val in new_dct.items(): + if val in ('', None, [], [None]): + continue + created_dict[k] = val + # check if it is not previously created + key = (cls, tuple(sorted(created_dict.items()))) + if key in new_objects: + # regroup it + new_objects[key][0].append(label) + continue + + # values - transform key to explicit label + value_dct = self.transform_keys_to_label(path, cls, created_dict) + + for k in value_dct.keys(): + value_dct[k] = self.get_value(value_dct[k]) + + new_objects[key] = ([label], cls, value_dct) + + dct['new_objects'] = [ + [u" – ".join(lbls), cls, new_dct] + for lbls, cls, new_dct in new_objects.values() + ] + + updated_objects = [] + for path, obj, values, updated_values in self.imprt.updated_objects: + + # transform path to explicit label + label = self.transform_path_to_label(obj.__class__, path) + + # transform key into explicit label + values = self.transform_keys_to_label(path, obj.__class__, values) + + # add current values and changed bool + old_and_updated = {} + for k in updated_values.keys(): + current_value = getattr(obj, k) + updated_value = updated_values[k] + if hasattr(current_value, 'all'): + current_value = list(current_value.all()) + changed = False + for v in updated_value: + if v not in current_value: + changed = True + current_value = self.list_to_html(current_value) + updated_value = self.list_to_html(updated_value) + else: + changed = current_value != updated_value + current_value = self.get_value(current_value) + updated_value = self.get_value(updated_value) + old_and_updated[k] = [current_value, updated_value, + changed] + + # transform key into explicit label + old_and_updated = self.transform_keys_to_label(path, obj.__class__, + old_and_updated) + updated_objects.append((label, obj, values, old_and_updated)) + + dct['updated_objects'] = updated_objects dct['ambiguous_objects'] = self.imprt.ambiguous_objects dct['not_find_objects'] = self.imprt.not_find_objects return dct + def transform_path_to_label(self, cls, path): + label = u" > ".join( + unicode(l) + for l in get_field_labels_from_path(cls, path) + ) + if not label: + label = unicode(cls._meta.verbose_name) + return label + + def transform_keys_to_label(self, path, cls, dct): + value_dct = {} + for k in dct: + label = unicode( + get_field_labels_from_path(cls, [k])[0] + ) + + concat_path = u"__".join(list(path) + [k]) + if concat_path in self.path_to_column: + for col in self.path_to_column[concat_path]: + col += 1 + label += u" <a href=\"#col-{}\">"\ + u"<span class=\"badge badge-info\"> {} {} </span>"\ + u"</a>".format(col, _(u"Col. "), col) + value_dct[label] = dct[k] + return value_dct + + def list_to_html(self, lst): + if not lst: + return _(u"* empty *") + return u"<ul class='list-group'><li class='list-group-item'>" + \ + u"</li><li class='list-group-item'>".join([ + self.get_value(item) for item in lst + ]) + u"</li></ul>" + + def get_value(self, item): + if hasattr(item, 'SHOW_URL'): + return u"{}{}".format(unicode(item), link_to_window(item)) + if hasattr(item, 'explicit_label'): + return item.explicit_label + if item in (None, [], [None]): + return _(u"* empty *") + if isinstance(item, list): + return self.list_to_html(item) + return unicode(item) + class ImportListTableView(ImportListView): template_name = 'ishtar/import_table.html' |