def _create_habitat_record(habitat_app, comment=False, role='assessment'): from art17 import models with habitat_app.app_context(): config = models.Config(id='CONSULTATION_DATASET', value='1') models.db.session.add(config) habitat = models.DataHabitat(id=1, code='1234') habitat.lu = models.LuHabitattypeCodes(objectid=1, code=1234) record = models.DataHabitattypeRegion( id=1, habitat=habitat, cons_role=role, region='ALP', cons_dataset_id=1, ) record.lu = models.LuBiogeoreg(objectid=1) models.db.session.add(record) if comment: comment = models.DataHabitattypeRegion( id=2, habitat_id=1, cons_role=DEFAULT_COMMENT_ROLE, cons_user_id='smith', region='ALP', range_surface_area=1337, cons_dataset_id=1, ) models.db.session.add(comment) models.db.session.commit() return comment or record
def test_flatten(): from art17.schemas import flatten_habitat_commentform from art17 import models obj = models.DataHabitattypeRegion() flatten_habitat_commentform(HABITAT_STRUCT_DATA, obj) for k, v in HABITAT_MODEL_DATA.items(): assert getattr(obj, k) == v
def test_save_all_form_fields(habitat_app): from art17 import forms from art17 import models from art17.common import flatten_dict from art17.schemas import flatten_habitat_commentform from werkzeug.datastructures import MultiDict form_data = MultiDict(flatten_dict(HABITAT_STRUCT_DATA)) with habitat_app.app_context(): form = forms.HabitatComment(form_data) assert form.validate() comment = models.DataHabitattypeRegion() flatten_habitat_commentform(form.data, comment) for k, v in HABITAT_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_habitat_commentform from art17 import models obj = models.DataHabitattypeRegion(**HABITAT_MODEL_DATA) data = parse_habitat_commentform(obj) assert data == HABITAT_STRUCT_DATA