def _create_species_record(species_app, comment=False, role='assessment'): from art17 import models with species_app.app_context(): config = models.Config(id='CONSULTATION_DATASET', value='1') models.db.session.add(config) species = models.DataSpecies(id=1, code='1234') group = models.LuGrupSpecie(oid=1, code='M') species.lu = models.LuHdSpecies(objectid=1, code=1234, group_code='M') record = models.DataSpeciesRegion( id=1, species=species, cons_role=role, region='ALP', cons_dataset_id=1, ) record.lu = models.LuBiogeoreg(objectid=1) models.db.session.add(group) models.db.session.add(record) if comment: comment = models.DataSpeciesRegion( id=2, species_id=1, cons_role=DEFAULT_COMMENT_ROLE, cons_user_id='smith', cons_dataset_id=1, region='ALP', range_surface_area=1337, ) models.db.session.add(comment) models.db.session.commit() return comment or record
def test_flatten(): from art17.schemas import flatten_species_commentform from art17 import models obj = models.DataSpeciesRegion() flatten_species_commentform(SPECIES_STRUCT_DATA, obj) for k, v in SPECIES_MODEL_DATA.items(): assert getattr(obj, k) == v
def test_save_all_form_fields(species_app): from art17 import forms from art17 import models from art17.common import flatten_dict from art17.schemas import flatten_species_commentform from werkzeug.datastructures import MultiDict form_data = MultiDict(flatten_dict(SPECIES_STRUCT_DATA)) with species_app.app_context(): form = forms.SpeciesComment(form_data) form.validate() assert form.validate() comment = models.DataSpeciesRegion() flatten_species_commentform(form.data, comment) for k, v in SPECIES_MODEL_DATA.items(): assert getattr(comment, k) == v
def aggregate_object(obj, dataset, refvals, timestamp, user_id, prev): """ Aggregate a habitat or a species. Returns a new row to be inserted into database. """ if isinstance(obj, models.DataHabitatsCheckList): region_code = obj.bio_region result = models.DataHabitattypeRegion( dataset=dataset, region=region_code, ) record_type = 'habitat' elif isinstance(obj, models.DataSpeciesCheckList): region_code = obj.bio_region result = models.DataSpeciesRegion( dataset=dataset, region=region_code, ) record_type = 'species' else: raise NotImplementedError('Unknown check list obj') result.cons_date = timestamp result.cons_user_id = user_id result.cons_generalstatus = obj.presence if result.cons_generalstatus != '1': result.cons_role = ROLE_AGGREGATED return result if not refvalue_ok(refvals, record_type): result.cons_role = ROLE_MISSING return result # Agregation starts here result.cons_role = ROLE_AGGREGATED if isinstance(obj, models.DataHabitatsCheckList): result = aggregate_habitat(obj, result, refvals, prev) else: result = aggregate_species(obj, result, refvals, prev) return result
def test_parse(): from art17.schemas import parse_species_commentform from art17 import models obj = models.DataSpeciesRegion(**SPECIES_MODEL_DATA) data = parse_species_commentform(obj) assert data == SPECIES_STRUCT_DATA