Exemple #1
0
    def threshold_value(self):
        values = {}

        for n in self.siblings:

            tv = n['w:ThresholdValue/text()']

            if not tv:
                continue

            muid = n['w:MarineUnitID/text()'][0]

            # filter values according to region's marine unit ids

            if muid not in self.parent.muids:
                continue

            values[muid] = tv[0]

        rows = []
        count, mres = db.get_marine_unit_id_names(values.keys())
        muid_labels = dict(mres)

        for muid in sorted(values.keys()):
            name = u'{} = {}'.format(muid, values[muid])
            title = u'{} = {}'.format(muid_labels[muid], values[muid])

            label = ItemLabel(name, title)
            rows.append(label)

        return ItemList(rows=rows)
Exemple #2
0
    def _get_memberstate_reports(self, data, obligation):
        res = []

        for country_code, country_name in self.available_countries:
            values = []
            for row in data:
                if row.ReportingObligation != obligation:
                    continue

                if row.CountryCode != country_code:
                    continue

                date_received = row.DateReceived.date()
                envelope_url = row.LocationURL.replace(row.FileName, '')

                values.append((envelope_url, date_received))

            values = sorted(set(values), key=lambda i: i[1], reverse=True)
            unique_values = [
                self.rep_date_tpl.format(v[0], v[1]) for v in values
            ]

            res.append(ItemList(unique_values, sort=False))

        return res
Exemple #3
0
    def row_activity_types(self):
        x = 'parent::*/w:Activities/w:Activity/w:ActivityType/text()'

        v = set(self.node[x])
        labels = [ItemLabel(a, COMMON_LABELS.get(a, a)) for a in v]

        return ItemList(labels)
Exemple #4
0
def consolidate_singlevalue_to_list(proxies, fieldname, order=None):
    """ Given a list of proxies where one of the fields needs to be a list, but
    is spread across different similar proxies, consolidate the single values
    to a list and return only one object for that list of similar objects
    """

    map_ = defaultdict(list)

    for o in proxies:
        map_[o.hash(fieldname)].append(o)

    res = []

    for set_ in map_.values():
        o = set_[0]
        values = [getattr(xo, fieldname) for xo in set_]

        if any(values):
            l = ItemList(rows=values)
            setattr(o, fieldname, l)

        res.append(o)

    # consolidate_singlevalue_to_list is used in regional descriptor too
    # where we do not order the results
    if order:
        res = list(sorted(res, key=attrgetter(*order)))

    return res
Exemple #5
0
def _itemlist(values, sort):
    _sorted = values

    if sort:
        _sorted = sorted(set(values))

    return ItemList(_sorted)
Exemple #6
0
def csv_ges_labels_inverse_list_indicators(field, value, lang):
    vals = set(value.split(','))

    res = []

    for v in vals:
        i = get_indicators(field, v, lang)
        res.append(i)

    return ItemList(rows=res)
Exemple #7
0
def art11_indicators(field, value, lang):
    vals = re.split(comma_separator_re, value)

    res = []

    for v in vals:
        code = v.split(' - ')[0].strip()
        i = get_indicators(field, code, lang)
        res.append(i)

    return ItemList(rows=res)
Exemple #8
0
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)
Exemple #9
0
    def features(self):
        t = sql.t_MSFD9_Features
        count, res = db.get_all_records(
            t,
            t.c.MSFD9_Descriptor == self.descriptor_item.MSFD9_Descriptor_ID)
        vals = list(
            set([x.FeatureType
                 for x in res] + [x.FeaturesPressuresImpacts for x in res]))
        # return vals       # remove code below when properly integrated

        rows = [ItemLabel(v, get_label(v, 'features')) for v in vals]
        return ItemList(rows=rows)
Exemple #10
0
    def target_code(self):
        codes = []

        ti_code = self.ti.ReportingFeature
        codes.append(ti_code)

        dc_codes = [
            x.ReportingFeature for x in self.desc_crits
            if x.Topic == 'EnvironmentalTarget'
        ]
        codes.extend(dc_codes)

        return ItemList(set(codes))
Exemple #11
0
    def get_ges_components(self):
        ges_descriptors = set(x.GESDescriptorsCriteriaIndicators
                              for x in self.desc_crits)
        ges_needed = self.ok_ges_ids & ges_descriptors

        # TODO return raw values or labels?
        if ges_needed:
            # for ges in ges_needed:
            #     g = get_ges_component(ges)
            #     il = ItemLabel(ges, g.title)
            #     res.append(il)

            return ItemList(rows=ges_needed)

        return ()
Exemple #12
0
    def feature(self):
        # TODO: this needs more work, to aggregate with siblings
        res = set()

        all_nodes = [s.node for s in self.siblings]

        for n in all_nodes:
            fpi = n.xpath('w:Features/w:FeaturesPressuresImpacts/text()',
                          namespaces=NSMAP)

            for x in fpi:
                res.add(x)

        labels = [ItemLabel(k, COMMON_LABELS.get(k, k) or k) for k in res]

        return ItemList(labels)
Exemple #13
0
    def features(self):
        features = []
        for row in self.feat_pres:
            v = row.FeatureType
            if v:
                features.append(v)

            v = row.PhysicalChemicalHabitatsFunctionalPressures
            features.append(v)

            if 'Other' in v and row.Other:
                o = row.Other
                features.append(o)

        res = [ItemLabel(r, get_label(r, 'features')) for r in set(features)]

        return ItemList(res)
Exemple #14
0
    def threshold_value(self):
        values = {}

        for target in self.targets:
            muid = target.marine_unit_id
            values[muid] = target['w:ThresholdValue/text()'][0]

        rows = []
        count, mres = db.get_marine_unit_id_names(values.keys())
        muid_labels = dict(mres)

        for muid in sorted(values.keys()):
            name = u'{} = {}'.format(muid, values[muid])
            title = u'{} = {}'.format(muid_labels[muid], values[muid])

            label = ItemLabel(name, title)
            rows.append(label)

        return ItemList(rows=rows)
Exemple #15
0
    def get_values(self):
        rec, asses, indic, crit = self._args
        self.MarineUnitID = rec.MarineUnitID

        meta = self._metadata(rec)
        related_activities = self._related_activities(rec)

        return [
            ('MarineReportingUnit', rec.MarineUnitID),
            ('GEScomponent', getattr(self, 'ges_comp', 'D5')),
            ('Feature', getattr(self, 'feature', 'N/A')),

            ('GESAchieved', getattr(self, 'ges_achiev', 'N/A')),
            ('AssessmentPeriod',
             meta and meta.AssessmentDateStart or rec.RecentTimeStart),
            ('AssessmentPeriod2',
             meta and meta.AssessmentDateEnd or rec.RecentTimeEnd),

            ('MethodUsed', meta and meta.MethodUsed),
            ('MethodSources', meta and meta.Sources),

            ('RelatedActivities', ItemList(rows=related_activities)),
            ('Criteria',
                getattr(self, 'criteria', crit and crit.CriteriaType)),
            ('CriteriaStatus',
                getattr(self, 'crit_stat', asses and asses.Status)),
            ('DescriptionCriteria',
                getattr(self, 'desc_crit', asses and asses.StatusDescription)),
            ('Element', getattr(self, 'element', 'N/A')),
            ('ElementStatus', getattr(self, 'elem_status', 'N/A')),
            ('Parameter', getattr(self, 'parameter', self._param_topic(rec))),

            ('ThresholdQualitative', indic and indic.ThresholdValue),
            ('ValueUnit', indic and indic.ThresholdValueUnit),
            ('ProportionThresholdValue', indic and indic.ThresholdProportion),
            ('ProportionValueAchieved', rec.SumInfo1),
            ('Trend', getattr(self, 'trend', asses and asses.StatusTrend)),
            ('ParameterAchieved', 'N/A'),
            ('DescriptionParameter', rec.Description),
        ]
Exemple #16
0
    def get_article_rows(self):
        # Group the data by envelope, report due, report date and report delay
        data = self.data
        rows = []

        groups = defaultdict(list)

        for row in data:
            filename = row.get('ReportedFileLink').split('/')[-1]
            envelope = self.location_url(row.get('ReportedFileLink'), filename)

            # Article 18 files not relevant for this report, exclude them
            if 'art18' in envelope:
                continue

            report_due = datetime(year=2018, month=10, day=15).date()
            report_date = row.get('ReportingDate')
            report_delay = report_due - report_date
            k = (envelope, report_due, report_date, report_delay.days)

            groups[k].append(filename)

        for _k, filenames in groups.items():
            values = [
                ItemList(rows=filenames),  # Filenames
                _k[0],  # Envelope url
                _k[1],  # Report due
                _k[2],  # Report date
                _k[3]  # Report delay
            ]
            rows.append(values)

        text_files = self.get_text_and_spacial_files()
        rows.extend(text_files)

        sorted_rows = sorted(rows,
                             key=lambda _row: (_row[3], _row[1]),
                             reverse=True)

        return sorted_rows
Exemple #17
0
    def get_text_reports(self):
        res = []

        for country_code, country_name in self.available_countries:
            text_reports = get_text_reports_2018(country_code)
            values = []

            for row in text_reports:
                file_url = row[0]
                release_date = row[1].date()
                envelope_url = '/'.join(file_url.split('/')[:-1])

                values.append((envelope_url, release_date))

            values = sorted(set(values), key=lambda i: i[1], reverse=True)
            unique_values = [
                self.rep_date_tpl.format(v[0], v[1]) for v in values
            ]

            res.append(ItemList(unique_values, sort=False))

        return res
Exemple #18
0
    def get_data_by_obligations(self, obligations):
        # Group the data by envelope, report due, report date
        # and report delay
        data = [
            row for row in self.data
            if row.get('ReportingObligation') in obligations
        ]
        rows = []

        groups = defaultdict(list)

        for row in data:
            filename = row.get('FileName')
            envelope = self.location_url(row.get('LocationURL'), filename)
            report_due = self.format_date(row.get('DateDue'))
            report_date = self.format_date(row.get('DateReleased'))

            report_delay = calculate_reporting_delay(row.get('ReportingDelay'),
                                                     report_due, report_date)
            k = (envelope, report_due, report_date, report_delay)

            groups[k].append(filename)

        for _k, filenames in groups.items():
            values = [
                ItemList(rows=set(filenames)),  # Filenames
                _k[0],  # Envelope url
                _k[1],  # Report due
                _k[2],  # Report date
                _k[3]  # Report delay
            ]
            rows.append(values)

        sorted_rows = sorted(rows,
                             key=lambda _row: (_row[3], _row[2]),
                             reverse=True)

        return sorted_rows
Exemple #19
0
def __link_to_nat_desc_art11(field, value, self):
    url = self.request['URL0']

    reg_main = self._countryregion_folder.id.upper()
    subregions = [
        r.subregions for r in REGIONAL_DESCRIPTORS_REGIONS
        if reg_main in r.code
    ]

    res = []
    template = u'<a style="cursor: pointer;" target="_blank" href="{}">{}</a>'

    c_code = value.split('/')[3].upper()
    regions = [
        r.code for r in REGIONAL_DESCRIPTORS_REGIONS if len(r.subregions) == 1
        and c_code in r.countries and r.code in subregions[0]
    ]

    for r in regions:
        report_url = get_nat_desc_country_url(url, reg_main, c_code, r)
        res.append(ItemLabel(r, template.format(report_url, r)))

    return ItemList(res)
Exemple #20
0
    def get_values(self):
        rec, pres, ast, indic, crit = self._args
        self.MarineUnitID = rec.MarineUnitID
        meta = self._get_metadata(rec)

        return [
            ('MarineReportingUnit', rec.MarineUnitID),

            ('GEScomponent', self.get_conditional_value(
                'ges_comp', 'D1')
             ),
            ('Feature', self.get_conditional_value(
                'feature', getattr(rec, 'ReportingFeature', None))
             ),
            ('GESachieved', self.get_conditional_value(
                'ges_achiev', getattr(rec, 'Summary1', None))
             ),
            ('AssessmentPeriod', meta and meta.AssessmentDateStart
                or getattr(rec, 'RecentTimeStart', None)),
            ('AssessmentPeriod2', meta and meta.AssessmentDateEnd
                or getattr(rec, 'RecentTimeEnd', None)),
            ('MethodUsed', meta and meta.MethodUsed),
            ('MethodSources', meta and meta.Sources),

            ('RelatedPressures', ItemList(rows=self.get_pressures(pres))),
            # ('RelatedActivities', ItemList(rows=related_activities)),
            #
            ('Criteria', self.get_conditional_value(
                'criteria', getattr(crit, 'CriteriaType', None))
             ),
            # TODO? ????
            ('CriteriaStatus', self.get_conditional_value(
                'crit_status', getattr(ast, 'Status', None))
             ),
            ('DescriptionCriteria', self.get_conditional_value(
                'desc_crit', getattr(ast, 'StatusDescription', None))
             ),
            ('Element', self.get_conditional_value(
                'element', getattr(rec, 'ReportingFeature', None))
             ),
            ('ElementSource', self.get_conditional_value(
                'elem_source',
                getattr(rec, 'SourceClassificationListAuthority', None))
             ),
            ('DescriptionElement', self.get_conditional_value(
                'descr_elem', getattr(ast, 'StatusDescription', None))
             ),
            ('ElementStatus', self.get_conditional_value(
                'elem_status', getattr(ast, 'Status', None))
             ),
            ('Parameter', self.get_conditional_value('param', rec.Topic)),
            ('ThresholdQualitative', indic and indic.ThresholdValue),
            ('ValueUnit', indic and indic.ThresholdValueUnit),
            ('ProportionThresholdValue', indic and indic.ThresholdProportion),

            # ('ProportionValueAchieved', rec.SumInfo1),
            ('Trend', self.get_conditional_value(
                'trend', getattr(ast, 'TrendStatus', None))
             ),
            ('ParameterAchieved', 'N/A'),
            ('DescriptionParameter', self.get_conditional_value(
                'desc_param', None)
             ),
        ]
Exemple #21
0
def simple_itemlist_art11_measures(field, value, lang):
    vals = re.split(art11_measure_separator, value)

    return ItemList(rows=set(vals))
Exemple #22
0
def simple_itemlist(field, value, lang):
    vals = set(value.split(','))

    return ItemList(rows=vals)
Exemple #23
0
    def setup_data(self, filename=None):
        if not filename:
            filename = self.context.get_report_filename()

        if not isinstance(filename, tuple):
            filename = [filename]

        def filter_descriptors(nodes, descriptor):
            res = []

            for d in nodes:
                rf = x('w:ReportingFeature/text()', d)[0]
                rf = criteria_from_gescomponent(rf)

                if rf in all_ids:
                    res.append(d)

            return res

        descriptor_class = get_descriptor(self.descriptor)
        all_ids = descriptor_class.all_ids()
        self.descriptor_label = descriptor_class.title

        if self.descriptor.startswith('D1.'):
            all_ids.add('D1')

        _cols = []
        _muids = []

        for fname in filename:
            text = get_xml_report_data(fname)

            if not text:
                self.rows = []

                return self.template()

            root = fromstring(text)

            def x(xpath, node=root):
                return node.xpath(xpath, namespaces=NSMAP)

            # these are the records we are interested in
            descriptors = filter_descriptors(x('//w:Descriptors'),
                                             self.descriptor)

            # the xml file is a collection of records that need to be agregated
            # in order to "simplify" their information. For example, the
            # ThresholdValues need to be shown for all MarineUnitIds, but the
            # grouping criteria is the "GES Component"

            cols = []
            seen = []

            muids = root.xpath('//w:MarineUnitID/text()', namespaces=NSMAP)

            count, res = db.get_marine_unit_id_names(list(set(muids)))
            muid_ids = [y.id for y in self.muids]

            labels = [ItemLabel(m, t or m) for m, t in res if m in muid_ids]

            # special case for PL where marine_unit_ids are not imported into DB
            # therefore we cannot get the labels for them
            if muids and not labels:
                labels = [ItemLabel(m, m) for m in set(muids)]

            self.muids_labeled = sorted(labels,
                                        key=lambda l: natural_sort_key(l.name))
            _muids.extend(labels)
            muids = ItemList(labels)

            for node in descriptors:
                col_id = node.find('w:ReportingFeature', namespaces=NSMAP).text

                if col_id in seen:
                    continue

                item = A9Item(self, node, descriptors, muids)
                cols.append(item)
                seen.append(item.id)

            _cols.extend(cols)

        _muids = ItemList(_muids)
        # insert missing criterions
        self.insert_missing_criterions(descriptor_class, _cols, _muids)

        self.rows = []
        sorted_ges_c = sorted_by_criterion([c.ges_component() for c in _cols])

        def sort_func(col):
            return sorted_ges_c.index(col.ges_component())

        sorted_cols = sorted(_cols, key=sort_func)
        self.cols = list(sorted_cols)

        for col in sorted_cols:
            for name in col.keys():
                values = []

                for inner in sorted_cols:
                    values.append(inner[name])

                raw_values = []
                vals = []
                for v in values:
                    raw_values.append(v)
                    vals.append(
                        self.context.translate_value(name, v,
                                                     self.country_code))

                # values = [self.context.translate_value(name, value=v)
                #           for v in values]

                row = RawRow(name, vals, raw_values)
                self.rows.append(row)

            break  # only need the "first" row
Exemple #24
0
def simple_itemlist_re(field, value, lang):
    vals = re.split(comma_separator_re, value)

    return ItemList(rows=set(vals))
Exemple #25
0
    def get_adequacy_assessment_data_art10(self):
        def get_assessed_elements(self, question, country_code):
            muids = []

            _region = self.country_region_code
            _subregions = [
                _r.subregions for _r in REGIONAL_DESCRIPTORS_REGIONS
                if _region in _r.code
            ][0]

            _country_regions = [
                r.code for r in REGIONAL_DESCRIPTORS_REGIONS
                if len(r.subregions) == 1 and country_code in r.countries
                and r.code in _subregions
            ]
            for _subregion in _country_regions:
                _muids = self.muids(country_code.upper(), _subregion.upper(),
                                    self.year)

                muids.append((_subregion.upper(), _muids))

            elements = {}

            for _subreg, _muids in muids:
                targets = question.get_assessed_elements(self.descriptor_obj,
                                                         muids=_muids)

                elements[_subreg] = targets

            return elements

        answer_tpl = u"<span class='empty-colorbox as-value-{}'/>" \
                     u"<span><b>{}:</b> {}</span>"
        Field = namedtuple('Field', ['name', 'title'])

        countries = self.available_countries
        questions = NAT_DESC_QUESTIONS.get(self.article, [])
        region = self.country_region_code
        subregions = [
            r.subregions for r in REGIONAL_DESCRIPTORS_REGIONS
            if region in r.code
        ][0]

        descriptor = self.descriptor
        article = self.article

        res = [
            RegionalCompoundRow(self, self.request,
                                Field('Country', 'Country'),
                                self.make_countries_row())
        ]

        self.raw_adeq_assess_data = [
            tuple(['Country', ''] + [c[1] for c in self.available_countries])
        ]

        for question in questions:
            q_id = question.id
            q_def = question.definition
            q_answ = question.answers
            field = Field(q_id, q_def)

            rows = []
            rows_raw = []
            conclusion_values = []
            summary_values = []
            conclusion_values_raw = []
            summary_values_raw = []

            # Init the result with empty values
            answer_values = {
                answer: {country_code: []
                         for country_code, _ in countries}
                for answer in q_answ
            }
            answer_values_raw = {
                answer: {country_code: []
                         for country_code, _ in countries}
                for answer in q_answ
            }

            for country_code, country_name in countries:
                country_concl = []
                country_sums = []
                country_concl_raw = []
                country_sums_raw = []
                country_targets = get_assessed_elements(
                    self, question, country_code)

                country_regions = [
                    r.code for r in REGIONAL_DESCRIPTORS_REGIONS
                    if len(r.subregions) == 1 and country_code in r.countries
                    and r.code in subregions
                ]

                for subregion in country_regions:
                    assess_data = self.get_nat_desc_assessment_data(
                        country_code, subregion, descriptor, article)

                    # Setup the conclusion and the summary
                    score = assess_data.get('{}_{}_Score'.format(
                        article, q_id))
                    summary = assess_data.get('{}_{}_Summary'.format(
                        article, q_id))
                    summary_raw = summary or "-"
                    summary = u"<b>{}:</b> {}".format(subregion, summary_raw)

                    conclusion_raw = score and score.conclusion or "-"
                    conclusion = \
                        u"<span class='empty-colorbox as-value-{}'/>" \
                        u"<span><b>{}:</b> {}</span>" \
                        .format(self._conclusion_color(score), subregion,
                                conclusion_raw)

                    country_concl.append(conclusion)
                    country_sums.append(summary)
                    country_concl_raw.append(u"{}: {}".format(
                        subregion, conclusion_raw))
                    country_sums_raw.append(u"{}: {}".format(
                        subregion, summary_raw))

                    # Setup the targets
                    subregion_targets = country_targets[subregion]

                    for target in subregion_targets:
                        option = assess_data.get('{}_{}_{}'.format(
                            article, q_id, target.id))
                        if option is None:
                            continue

                        option_txt = q_answ[option]
                        option_score = question.scores[option]

                        _val = answer_tpl.format(
                            self._answer_color(option_score), subregion,
                            target.title)
                        _val_raw = u"{}: {}".format(subregion, target.title)

                        answer_values[option_txt][country_code].append(_val)
                        answer_values_raw[option_txt][country_code]\
                            .append(_val_raw)

                conclusion_values.append(ItemList(country_concl))
                summary_values.append(ItemList(country_sums))
                conclusion_values_raw.append(ItemList(country_concl_raw))
                summary_values_raw.append(ItemList(country_sums_raw))

            # Add rows with the answers and values into the table
            for _answer in q_answ:
                _countries = answer_values[_answer]
                values = [
                    ItemList(_countries[country_code])
                    for country_code, _ in countries
                ]

                _countries_raw = answer_values_raw[_answer]
                values_raw = [
                    ItemList(_countries_raw[country_code])
                    for country_code, _ in countries
                ]

                rows.append((_answer, values))
                rows_raw.append(tuple([q_def, _answer] + values_raw))

            rows.append((u'Conclusion', conclusion_values))
            rows.append((u'Summary', summary_values))
            rows_raw.append(
                tuple([q_def, u'Conclusion'] + conclusion_values_raw))
            rows_raw.append(tuple([q_def, u'Summary'] + summary_values_raw))

            res.append(RegionalCompoundRow(self, self.request, field, rows))
            self.raw_adeq_assess_data.extend(rows_raw)

        return res
Exemple #26
0
    def get_adequacy_assessment_data(self):
        if self.article in ['Art10']:
            return self.get_adequacy_assessment_data_art10()

        def get_assessed_elements(self, question):
            muids = []

            elements = {
                x.id: []
                for x in question.get_assessed_elements(self.descriptor_obj,
                                                        muids=muids)
            }

            if not elements:
                elements['All criteria'] = []

            return elements

        answer_tpl = u"<span class='empty-colorbox as-value-{}'/>" \
                     u"<span><b>{}:</b> {}</span>"
        Field = namedtuple('Field', ['name', 'title'])

        countries = self.available_countries
        questions = NAT_DESC_QUESTIONS.get(self.article, [])
        region = self.country_region_code
        subregions = [
            r.subregions for r in REGIONAL_DESCRIPTORS_REGIONS
            if region in r.code
        ][0]

        descriptor = self.descriptor
        article = self.article

        res = [
            RegionalCompoundRow(self, self.request,
                                Field('Country', 'Country'),
                                self.make_countries_row())
        ]

        self.raw_adeq_assess_data = [
            tuple(['Country', ''] + [c[1] for c in self.available_countries])
        ]

        for question in questions:
            q_id = question.id
            q_def = question.definition
            field = Field(q_id, q_def)

            rows = []
            rows_raw = []
            criteria_values = get_assessed_elements(self, question)
            criteria_values_raw = defaultdict(list)
            conclusion_values = []
            summary_values = []
            conclusion_values_raw = []
            summary_values_raw = []

            for country_code, country_name in countries:
                country_concl = []
                country_concl_raw = []
                country_sums = []
                country_sums_raw = []
                country_crits = get_assessed_elements(self, question)
                country_crits_raw = defaultdict(list)
                country_regions = [
                    r.code for r in REGIONAL_DESCRIPTORS_REGIONS
                    if len(r.subregions) == 1 and country_code in r.countries
                    and r.code in subregions
                ]

                for subregion in country_regions:
                    assess_data = self.get_nat_desc_assessment_data(
                        country_code, subregion, descriptor, article)

                    score = assess_data.get('{}_{}_Score'.format(
                        article, q_id))
                    summary = assess_data.get('{}_{}_Summary'.format(
                        article, q_id))
                    summary_raw = summary or "-"
                    summary = u"<b>{}:</b> {}".format(subregion, summary_raw)

                    conclusion_raw = score and score.conclusion or "-"
                    conclusion = \
                        u"<span class='empty-colorbox as-value-{}'/>" \
                        u"<span><b>{}:</b> {}</span>" \
                        .format(self._conclusion_color(score), subregion,
                                conclusion_raw)

                    country_concl.append(conclusion)
                    country_concl_raw.append(u"{}: {}".format(
                        subregion, conclusion_raw))
                    country_sums.append(summary)
                    country_sums_raw.append(u"{}: {}".format(
                        subregion, summary_raw))

                    for crit_id in country_crits.keys():
                        option_txt = '-'
                        option_score = 0
                        if question.use_criteria == 'none':
                            option = assess_data.get('{}_{}'.format(
                                article, q_id))
                        else:
                            option = assess_data.get('{}_{}_{}'.format(
                                article, q_id, crit_id))
                        if option is not None:
                            option_txt = question.answers[option]
                            option_score = question.scores[option]
                        _val = answer_tpl.format(
                            self._answer_color(option_score), subregion,
                            option_txt)
                        _val_raw = u"{}: {}".format(subregion, option_txt)

                        country_crits[crit_id].append(_val)
                        country_crits_raw[crit_id].append(_val_raw)

                for crit_id in criteria_values.keys():
                    criteria_values[crit_id].append(
                        ItemList(country_crits[crit_id]))
                    criteria_values_raw[crit_id].append(
                        ItemList(country_crits_raw[crit_id]))

                conclusion_values.append(ItemList(country_concl))
                summary_values.append(ItemList(country_sums))
                conclusion_values_raw.append(ItemList(country_concl_raw))
                summary_values_raw.append(ItemList(country_sums_raw))

            for crit_id, crit_values in criteria_values.items():
                rows.append((crit_id, crit_values))

            for crit_id, crit_values in criteria_values_raw.items():
                rows_raw.append(tuple([q_def, crit_id] + crit_values))

            rows = sorted(rows, key=lambda i: i[0])
            rows_raw = sorted(rows_raw, key=lambda i: i[1])

            rows.append((u'Conclusion', conclusion_values))
            rows.append((u'Summary', summary_values))
            rows_raw.append(
                tuple([q_def, u'Conclusion'] + conclusion_values_raw))
            rows_raw.append(tuple([q_def, u'Summary'] + summary_values_raw))

            res.append(RegionalCompoundRow(self, self.request, field, rows))
            self.raw_adeq_assess_data.extend(rows_raw)

        return res
Exemple #27
0
    def __init__(self, context, criterion, targets_indicators, country_code,
                 region_code, muids):
        super(A10Item, self).__init__([])

        self.context = context
        self.criterion = criterion
        self.targets_indicators = targets_indicators
        self.country_code = country_code
        self.region_code = region_code

        self.targets = []

        # Note: the handling of D1.x indicators is not ideal

        if self.is_descriptor:
            for ti in targets_indicators:
                targets = ti.targets_for_descriptor(self.criterion)
                self.targets.extend(targets)
            print self.targets
        else:
            for ti in targets_indicators:
                targets = ti.targets_for_criterion(self.criterion)
                self.targets.extend(targets)

        pick = self.pick

        attrs = [
            ('Feature [Target or Indicator code]', self.criterion),
            ('GES descriptor, criterion or indicator [GEScomponent]',
             self.ges_component()),
            ('MarineUnitID', muids),
            ('Method used', self.method_used()),
            # ('Feature [Target code]', self.target_code()),
            ('Description [Targets]', self.description()),
            ('Threshold value [TargetValue]', self.threshold_value_a9()),
            ('Reference point type', pick('w:ReferencePointType/text()')),
            ('Baseline', pick('w:Baseline/text()')),
            ('Proportion', pick('w:Proportion/text()')),
            ('Assessment method', pick('w:AssessmentMethod/text()')),
            ('Development status', pick('w:DevelopmentStatus/text()')),
            ('Type of target/indicator', pick('w:Type/text()')),
            ('Timescale', pick('w:TimeScale/text()')),
            ('Interim or GES target', pick('w:InterimGESTarget/text()')),
            ('Compatibility with existing targets/indicators',
             pick('w:CompatibilityExistingTargets/text()')),
        ]

        feature_pressures = self.get_feature_pressures()

        for name in [
                'Physical/chemical features', 'Predominant habitats',
                'Functional group', 'Pressures'
        ]:
            labels = [
                ItemLabel(k, COMMON_LABELS.get(k, k))
                for k in feature_pressures[name]
            ]
            attrs.append((name, ItemList(labels)))

        for title, value in attrs:
            self[title] = value
Exemple #28
0
    def setup_data(self):
        self.article9_cols = self.get_article9_columns()
        filename = self.context.get_report_filename()

        if not isinstance(filename, tuple):
            filename = [filename]

        _cols = []
        _muids = []

        for fname in filename:
            text = get_xml_report_data(fname)

            if not text:
                self.rows = []

                return self.template()

            root = fromstring(text)

            def xp(xpath, node=root):
                return node.xpath(xpath, namespaces=NSMAP)

            muids = xp('//w:MarineUnitID/text()')
            count, res = db.get_marine_unit_id_names(list(set(muids)))

            labels = [ItemLabel(m, u'{} ({})'.format(t, m)) for m, t in res]

            # special case for PL where marine_unit_ids are not imported into DB
            # therefore we cannot get the labels for them
            if muids and not labels:
                labels = [ItemLabel(m, m) for m in set(muids)]

            _muids.extend(labels)
            muids = ItemList(labels)

            descriptor = get_descriptor(self.descriptor)
            self.descriptor_label = descriptor.title

            reported = xp("//w:DesriptorCriterionIndicator/text()")
            gcs = self.filtered_ges_components(reported)

            self.rows = []

            # wrap the target per MarineUnitID
            all_target_indicators = [
                TargetsIndicators(node) for node in xp('w:TargetsIndicators')
            ]
            cols = [
                A10Item(self, gc, all_target_indicators, self.country_code,
                        self.region_code, muids) for gc in gcs
            ]

            _cols.extend(cols)

        self.muids_labeled = sorted(_muids,
                                    key=lambda l: natural_sort_key(l.name))
        self.cols = _cols
        # unwrap the columns into rows

        for col in _cols:
            for name in col.keys():
                values = []

                for inner in _cols:
                    values.append(inner[name])

                raw_values = []
                vals = []
                for v in values:
                    raw_values.append(v)
                    vals.append(
                        self.context.translate_value(name, v,
                                                     self.country_code))

                row = RawRow(name, vals, raw_values)
                self.rows.append(row)

            break  # only need the "first" row
Exemple #29
0
def ges_component_list(field, value, lang):
    values = value.split(',')
    rows = [ges_component(None, v, lang) for v in values]

    return ItemList(rows=rows)