def csv_ges_labels_inverse_list(field, value, lang): vals = set(value.split(',')) res = [] for v in vals: title = GES_LABELS.get(field.label_collection, v) i = ItemLabel(title, v) res.append(i) return ItemList(rows=res)
def get_indicators(field, value, lang): value_orig = value title = GES_LABELS.get('indicators', value) url = GES_LABELS.get('indicators_url', value) tr = get_translated(title, lang) if tr: value = u"{} ({})".format(value, title) title = tr if url != value_orig: template = u'<a style="cursor: help;" target="_blank" href="{}">{}</a>' return ItemLabel(value, template.format(url, title)) # if tr: # value = u"{} ({})".format(value, title) # # return ItemLabel(value, tr) else: return ItemLabel(value, title)
def group_by_mru(data): """ Group data by mru It returns a dict where the keys are ItemLabels of the MRU and values are list of rows for that mru """ if not data: return {} res = defaultdict(list) mrus = {} # rows with empty MRU field are added to all MRUs # This case applies for Art9, when justification for delay is reported rows_extra = [] for row in data: if not row.MarineReportingUnit: # skip rows without muid rows_extra.append(row) continue mru = row.MarineReportingUnit if mru not in mrus: mru_label = GES_LABELS.get('mrus', mru) if mru_label != mru: mru_label = u"{} ({})".format(mru_label, unicode(mru)) mru_item = ItemLabel(mru, mru_label) mrus[mru] = mru_item else: mru_item = mrus[mru] res[mru_item].append(row) for mru in res.keys(): for row in rows_extra: res[mru].append(row) # Special case for Art9, when no real data is reported besides # justification for delay if not res and rows_extra: _mru = ItemLabel('No MRUs', 'No MRUs') res[_mru].extend(rows_extra) return res
def set_value(self, fieldname, value): """ Set the field to a specific value, according to set policies """ field = None for f in self.fields: if f.name == fieldname: field = f break if not field: # field definition was not found, fallback setattr(self, fieldname, value) return label_collection = field.label_collection converter_name = field.converter filter_values = field.filter_values # assert (label_name or converter), 'Field should be dropped' if filter_values: ok_values = getattr(self.report_class, filter_values) if ok_values: values = set(value.split(',')) filtered = values.intersection(ok_values) value = u','.join(filtered) if converter_name: assert '.' not in converter_name converter = getattr(convert, converter_name) # special convert method, needs the report_class instance to get # additional info like url, article, descriptor, region etc. if converter_name.startswith('__'): value = converter(field, value, self.report_class) else: value = converter(field, value, self.report_class.country_code) elif label_collection: title = GES_LABELS.get(label_collection, value) value = ItemLabel(value, title) setattr(self, fieldname, value)
def set_value(self, fieldname, value): """ Set the field to a specific value, according to set policies """ field = None for f in self.fields: if f.name == fieldname: field = f break if not field: # field definition was not found, fallback setattr(self, fieldname, value) return label_collection = field.label_collection converter = field.converter filter_values = field.filter_values # assert (label_name or converter), 'Field should be dropped' if filter_values: ok_values = getattr(self.report_class, filter_values) if ok_values: values = set(value.split(',')) filtered = values.intersection(ok_values) value = u','.join(filtered) if converter: assert '.' not in converter converter = getattr(convert, converter) value = converter(field, value, self.report_class.country_code) elif label_collection: title = GES_LABELS.get(label_collection, value) value = ItemLabel(value, title) setattr(self, fieldname, value)
def consolidate_date_by_mru(data): """ Takes data (proxies of data) organized by mru and groups them according to similarity of data (while ignoring the mru of that proxied row) This is used by the A9 2018 report. """ groups = [] # Rows without MRU reported # This case applies for Art9, when justification for delay is reported rows_without_mru = [] for obj in chain(*data.values()): found = False for group in groups: # compare only with the first object from a group because # all objects from a group should contain the same data first_from_group = group[0] if proxy_cmp(obj, first_from_group): group.append(obj) found = True if not found: groups.append([obj]) # regroup the data by mru, now that we found identical rows regroup = defaultdict(list) for batch in groups: # TODO: get a proper MarineUnitID object mrus = tuple(sorted(set([r.MarineReportingUnit for r in batch]))) if mrus[0] is None: rows_without_mru.append(batch[0]) continue regroup[mrus].append(batch[0]) out = {} # rewrite the result keys to list of MRUs for mrus, rows in regroup.items(): mrus_labeled = tuple([ ItemLabel(row, u'{} ({})'.format(GES_LABELS.get('mrus', row), row)) for row in mrus ]) label = LabeledItemList(rows=mrus_labeled) # TODO how to explain better? # Skip rows from rows_without_mru if the GESComponent exists # in rows (we do not insert justification delay/non-use # if the GESComponent has reported data) # example: ges component D6C3, D6C4 # .../fi/bal/d6/art9/@@view-report-data-2018 ges_comps_with_data = set(x.GESComponent.id for x in rows) for row_extra in rows_without_mru: ges_comp = row_extra.GESComponent.id if ges_comp in ges_comps_with_data: continue rows.append(row_extra) # rows.extend(rows_without_mru) out[label] = rows if not regroup and rows_without_mru: rows = ItemLabel('No Marine unit ID reported', 'No Marine unit ID reported') label = LabeledItemList(rows=(rows, )) out[label] = rows_without_mru return out
def inverse_label(field, value, lang): title = GES_LABELS.get(field.label_collection, value) item = ItemLabel(title, value) return item