Beispiel #1
0
    def __init__(self, page=None, checklist_id=None, **kwargs):
        super(PreviewForm, self).__init__(**kwargs)

        if page == 'habitat':
            qs = list(
                get_habitat_checklist(dataset_id=checklist_id, distinct=True))
            qs_dict = dict(qs)
            qs.sort(key=lambda x: x[0])
        elif page == 'species':
            orig_qs = list(
                get_species_checklist(dataset_id=checklist_id, distinct=True))
            qs_dict = dict(orig_qs)

            orig_qs = {a[0]: a for a in orig_qs}
            qs = []
            for group in models.LuGrupSpecie.query.all():
                species = (models.LuHdSpecies.query.filter_by(
                    group_code=group.code).order_by(
                        models.LuHdSpecies.speciesname))
                species = [
                    orig_qs[str(s.code)] for s in species
                    if str(s.code) in orig_qs
                ]
                qs.append((group.description, species))
        else:
            raise NotImplementedError()
        self.subject.choices = qs
        self.qs_dict = qs_dict
Beispiel #2
0
    def __init__(self, page=None, checklist_id=None, **kwargs):
        super(PreviewForm, self).__init__(**kwargs)

        if page == 'habitat':
            qs = list(
                get_habitat_checklist(dataset_id=checklist_id, distinct=True))
            qs_dict = dict(qs)
            qs.sort(key=lambda x: x[0])
        elif page == 'species':
            orig_qs = list(
                get_species_checklist(dataset_id=checklist_id, distinct=True))
            qs_dict = dict(orig_qs)

            orig_qs = {a[0]: a for a in orig_qs}
            qs = []
            for group in models.LuGrupSpecie.query.all():
                species = (
                    models.LuHdSpecies.query
                    .filter_by(group_code=group.code)
                    .order_by(models.LuHdSpecies.speciesname)
                )
                species = [
                    orig_qs[str(s.code)]
                    for s in species if str(s.code) in orig_qs
                ]
                qs.append((group.description, species))
        else:
            raise NotImplementedError()
        self.subject.choices = qs
        self.qs_dict = qs_dict
def create_preview_aggregation(page, subject, comment, timestamp, user_id):
    """ Aggregate a single species/habitat
    """
    curr_report_id = get_reporting_id()
    curr_checklist = get_checklist(curr_report_id)
    dataset = models.Dataset(
        date=timestamp,
        user_id=user_id,
        preview=True,
        comment=comment,
        year_start=curr_checklist.year_start, year_end=curr_checklist.year_end,
    )
    models.db.session.add(dataset)

    if page == 'habitat':
        id_map = dict(
            models.db.session.query(
                models.DataHabitat.code,
                models.DataHabitat.id,
            )
        )
        rows = (
            get_habitat_checklist(dataset_id=curr_report_id)
            .filter_by(code=subject)
        )
        subgroup = 'n/a'
    elif page == 'species':
        id_map = dict(
            models.db.session.query(
                models.DataSpecies.code,
                models.DataSpecies.id,
            )
        )
        rows = (
            get_species_checklist(dataset_id=curr_report_id)
            .filter_by(code=subject)
        )
        subgroup = get_species_subgroup(subject)
    else:
        raise NotImplementedError()

    bioregions = {}
    refvals = get_subject_refvals_mixed(page, subject)
    prev = get_subject_prev(page, dataset)
    for row in rows:
        record = aggregate_object(row, dataset, refvals.get(row.bio_region),
                                  timestamp, user_id, prev)
        record.subject_id = id_map.get(row.code)
        models.db.session.add(record)
        bioregions[row.bio_region] = record.cons_role
    report = (
        ', '.join('{0}:{1}'.format(k, v) for k, v in
                  bioregions.items()) + ' [{0}]'.format(subgroup)
    )
    return report, dataset
Beispiel #4
0
    def get_context(self, **kwargs):
        checklist_id = get_reporting_id()
        current_checklist = get_checklist(checklist_id)
        checklist_id = current_checklist.id

        species_refvals = load_species_refval()
        species_checklist = get_species_checklist(dataset_id=checklist_id)
        species_data = parse_checklist_ref(species_checklist)

        species_list = get_species_checklist(groupped=True,
                                             dataset_id=checklist_id)

        habitat_refvals = load_habitat_refval()
        habitat_checklist = get_habitat_checklist(dataset_id=checklist_id)
        habitat_data = parse_checklist_ref(habitat_checklist)
        habitat_list = get_habitat_checklist(distinct=True,
                                             dataset_id=checklist_id,
                                             groupped=True)
        relevant_regions = (
            {s.bio_region for s in species_checklist}.union(
                {h.bio_region for h in habitat_checklist}
            ))
        bioreg_list = dal.get_biogeo_region_list(relevant_regions)

        groups = dict(
            LuGrupSpecie.query
            .with_entities(LuGrupSpecie.code, LuGrupSpecie.description)
        )

        return dict(
            species_refvals=species_refvals,
            species_data=species_data,
            species_list=species_list,
            habitat_refvals=habitat_refvals,
            habitat_data=habitat_data,
            habitat_list=habitat_list,
            bioreg_list=bioreg_list,
            GROUPS=groups,
            current_checklist=current_checklist,
            page='refvalues',
        )
Beispiel #5
0
def checklist(dataset_id=None):
    species = get_species_checklist(dataset_id=dataset_id)
    habitats = get_habitat_checklist(dataset_id=dataset_id)
    species_dict = parse_checklist(species)
    habitats_dict = parse_checklist(habitats)

    return render_template(
        'aggregation/admin/checklist.html',
        species_dict=species_dict,
        habitats_dict=habitats_dict,
        page='checklist',
    )
Beispiel #6
0
def create_preview_aggregation(page, subject, comment, timestamp, user_id):
    """ Aggregate a single species/habitat
    """
    curr_report_id = get_reporting_id()
    curr_checklist = get_checklist(curr_report_id)
    dataset = models.Dataset(
        date=timestamp,
        user_id=user_id,
        preview=True,
        comment=comment,
        year_start=curr_checklist.year_start,
        year_end=curr_checklist.year_end,
    )
    models.db.session.add(dataset)

    if page == 'habitat':
        id_map = dict(
            models.db.session.query(
                models.DataHabitat.code,
                models.DataHabitat.id,
            ))
        rows = (get_habitat_checklist(dataset_id=curr_report_id).filter_by(
            code=subject))
        subgroup = 'n/a'
    elif page == 'species':
        id_map = dict(
            models.db.session.query(
                models.DataSpecies.code,
                models.DataSpecies.id,
            ))
        rows = (get_species_checklist(dataset_id=curr_report_id).filter_by(
            code=subject))
        subgroup = get_species_subgroup(subject)
    else:
        raise NotImplementedError()

    bioregions = {}
    refvals = get_subject_refvals_mixed(page, subject)
    prev = get_subject_prev(page, dataset)
    for row in rows:
        record = aggregate_object(row, dataset, refvals.get(row.bio_region),
                                  timestamp, user_id, prev)
        record.subject_id = id_map.get(row.code)
        models.db.session.add(record)
        bioregions[row.bio_region] = record.cons_role
    report = (', '.join('{0}:{1}'.format(k, v)
                        for k, v in bioregions.items()) +
              ' [{0}]'.format(subgroup))
    return report, dataset
Beispiel #7
0
def checklists():
    checklists = get_checklists()

    default_checklist = {
        'species_checklist': get_species_checklist().all(),
        'habitat_checklist': get_habitat_checklist().all(),
    }
    years = (db.session.query(Dataset.year_start)
             .filter(or_(Dataset.preview == False, Dataset.preview == None))
             .order_by(Dataset.year_start.desc())
             .distinct())
    periods = [(year, get_year_end(year)) for year, in years]

    return render_template(
        'aggregation/admin/checklists.html',
        page='checklist',
        default_list=default_checklist,
        checklists=checklists,
        periods=periods,
    )
Beispiel #8
0
def manage_refvals_form(page, subject):
    data = get_subject_refvals(page, subject)

    if request.method == "POST":
        set_subject_refvals_wip(page, subject, request.form)
        flask.flash(u"Valori actualizate", 'success')

    extra = get_subject_refvals_wip(page, subject)
    full = get_subject_refvals_mixed(page, subject)
    checklist_id = get_reporting_id()
    current_checklist = get_checklist(checklist_id)
    checklist_id = current_checklist.id
    if page == 'habitat':
        names = get_habitat_checklist(distinct=True, dataset_id=checklist_id)
    elif page == 'species':
        names = get_species_checklist(distinct=True, dataset_id=checklist_id)
    name_and_code = names.filter_by(code=subject).first()[1]
    _, _, name = name_and_code.partition(subject)

    return flask.render_template(
        'aggregation/manage/refvals_form.html', page=page, subject=subject,
        data=data, extra=extra, full=full, name=name, REGIONS=REGIONS,
    )
Beispiel #9
0
def download_refvals(page, subject):
    checklist_id = get_reporting_id()
    current_checklist = get_checklist(checklist_id)
    checklist_id = current_checklist.id
    if page == 'habitat':
        names = get_habitat_checklist(distinct=True, dataset_id=checklist_id)
    elif page == 'species':
        names = get_species_checklist(distinct=True, dataset_id=checklist_id)
    else:
        raise NotImplementedError()
    name_and_code = names.filter_by(code=subject).first()[1]
    _, _, name = name_and_code.partition(subject)
    full = get_subject_refvals_mixed(page, subject)

    changed_dict = {}
    for (region, d1) in full.items():
        for (sheet_name, d2) in d1.items():
            if sheet_name not in changed_dict.keys():
                changed_dict[sheet_name] = {}
            changed_dict[sheet_name][region] = d2

    wb = Workbook()
    wb.remove_sheet(wb.get_sheet_by_name('Sheet'))

    for (sheet_name, d1) in changed_dict.items():
        ws = wb.create_sheet()
        ws.title = REFGROUPS.get(sheet_name, sheet_name.title())
        operators = d1.values()[0].keys()
        ws.append(['COD SPECIE', 'NUME', 'BIOREGIUNE'] + operators)
        for (bioregion, d2) in d1.items():
            ws.append([subject, name, bioregion] +
                      [d2.get(operator) for operator in operators])

    response = Response(save_virtual_workbook(wb), mimetype=MIMETYPE)
    response.headers.add('Content-Disposition',
                         'attachment; filename={}.xlsx'.format(subject))
    return response
Beispiel #10
0
def create_aggregation(timestamp, user_id):
    """ Run the full aggregation on all items in the checklist.
    """
    curr_report_id = get_reporting_id()
    curr_checklist = get_checklist(curr_report_id)
    dataset = models.Dataset(
        date=timestamp,
        user_id=user_id,
        checklist_id=curr_report_id,
        year_start=curr_checklist.year_start,
        year_end=curr_checklist.year_end,
    )
    models.db.session.add(dataset)

    species_refvals = load_species_refval()
    habitat_refvals = load_habitat_refval()

    species_prev = load_species_prev(dataset)
    habitat_prev = load_habitat_prev(dataset)

    habitat_id_map = dict(
        models.db.session.query(
            models.DataHabitat.code,
            models.DataHabitat.id,
        ))

    habitat_checklist_query = get_habitat_checklist(dataset_id=curr_report_id)

    habitat_report = defaultdict(set)
    for row in habitat_checklist_query:
        refval_key = row.code + "-" + row.bio_region
        refvals = habitat_refvals.get(refval_key)
        habitat_row = aggregate_object(row, dataset, refvals, timestamp,
                                       user_id, habitat_prev)
        habitat_code = row.natura_2000_code
        habitat_id = habitat_id_map.get(habitat_code)

        habitat_row.subject_id = habitat_id
        models.db.session.add(habitat_row)

        habitat_report[habitat_code].add(habitat_row.region)

    species_id_map = dict(
        models.db.session.query(
            models.DataSpecies.code,
            models.DataSpecies.id,
        ))

    species_checklist_query = get_species_checklist(dataset_id=curr_report_id)

    species_report = defaultdict(set)
    for row in species_checklist_query:
        refval_key = row.code + "-" + row.bio_region
        refvals = species_refvals.get(refval_key)
        species_row = aggregate_object(row, dataset, refvals, timestamp,
                                       user_id, species_prev)
        species_code = row.natura_2000_code
        species_id = species_id_map.get(species_code)
        species_row.subject_id = species_id
        models.db.session.add(species_row)

        species_report[species_code].add(species_row.region)

    report = StringIO()
    print >> report, "Habitate:"
    for habitat_code, regions in sorted(habitat_report.items()):
        print >> report, "  %s: %s" % (habitat_code, ', '.join(
            sorted(regions)))

    print >> report, "\n\n"
    print >> report, "Specii:"
    for species_code, regions in sorted(species_report.items()):
        print >> report, "  %s: %s" % (species_code, ', '.join(
            sorted(regions)))

    return report.getvalue(), dataset
Beispiel #11
0
def create_aggregation(timestamp, user_id):
    """ Run the full aggregation on all items in the checklist.
    """
    curr_report_id = get_reporting_id()
    curr_checklist = get_checklist(curr_report_id)
    dataset = models.Dataset(
        date=timestamp,
        user_id=user_id,
        checklist_id=curr_report_id,
        year_start=curr_checklist.year_start, year_end=curr_checklist.year_end,
    )
    models.db.session.add(dataset)

    species_refvals = load_species_refval()
    habitat_refvals = load_habitat_refval()

    species_prev = load_species_prev(dataset)
    habitat_prev = load_habitat_prev(dataset)

    habitat_id_map = dict(
        models.db.session.query(
            models.DataHabitat.code,
            models.DataHabitat.id,
        )
    )

    habitat_checklist_query = get_habitat_checklist(dataset_id=curr_report_id)

    habitat_report = defaultdict(set)
    for row in habitat_checklist_query:
        refval_key = row.code + "-" + row.bio_region
        refvals = habitat_refvals.get(refval_key)
        habitat_row = aggregate_object(row, dataset, refvals,
                                       timestamp, user_id, habitat_prev)
        habitat_code = row.natura_2000_code
        habitat_id = habitat_id_map.get(habitat_code)

        habitat_row.subject_id = habitat_id
        models.db.session.add(habitat_row)

        habitat_report[habitat_code].add(habitat_row.region)

    species_id_map = dict(
        models.db.session.query(
            models.DataSpecies.code,
            models.DataSpecies.id,
        )
    )

    species_checklist_query = get_species_checklist(dataset_id=curr_report_id)

    species_report = defaultdict(set)
    for row in species_checklist_query:
        refval_key = row.code + "-" + row.bio_region
        refvals = species_refvals.get(refval_key)
        species_row = aggregate_object(row, dataset, refvals,
                                       timestamp, user_id, species_prev)
        species_code = row.natura_2000_code
        species_id = species_id_map.get(species_code)
        species_row.subject_id = species_id
        models.db.session.add(species_row)

        species_report[species_code].add(species_row.region)

    report = StringIO()
    print >> report, "Habitate:"
    for habitat_code, regions in sorted(habitat_report.items()):
        print >> report, "  %s: %s" % (
            habitat_code, ', '.join(sorted(regions)))

    print >> report, "\n\n"
    print >> report, "Specii:"
    for species_code, regions in sorted(species_report.items()):
        print >> report, "  %s: %s" % (
            species_code, ', '.join(sorted(regions)))

    return report.getvalue(), dataset