def create_from_airtable(airtable_json): category = Category( external_id=airtable_json.get('id'), airtable_json=airtable_json, airtable_url='https://airtable.com/tblJ8W5fjnatj4hki/' + airtable_json.get('id') + '/', title=airtable_json['fields'].get('Title').strip(), description=airtable_json['fields'].get('Description'), practice_external_ids=airtable_json['fields'].get('Practices'), ) image_name = get_airtable_media_name(airtable_json, 'Image') image_content_file = get_airtable_media_content_file(airtable_json, 'Image') if image_name and image_content_file: category.image.save(image_name, image_content_file, save=True) return category
def create_from_airtable(airtable_json, airtable_resource_images): url = airtable_json['fields'].get('Url') resource = Resource( external_id=airtable_json.get('id'), airtable_json=airtable_json, airtable_url='https://airtable.com/tblVb2GDuCPGUrt35/' + airtable_json.get('id') + '/', name=airtable_json['fields'].get('Nom'), description=airtable_json['fields'].get('Description'), resource_type=Resource._get_resource_type_from_airtable(airtable_json), url=url, ) resource_image = next((x for x in airtable_resource_images if x['fields'].get('URL_principal') in url), None) if resource_image: image_name = get_airtable_media_name(resource_image, 'logo') image_content_file = get_airtable_media_content_file(resource_image, 'logo') if image_name and image_content_file: resource.image.save(image_name, image_content_file, save=True) return resource
def create_from_airtable(airtable_json, json_culture_practices, json_departments_practices, json_departments, json_glyphosate, json_glyphosate_practices, mechanisms, resources, json_practice_types, json_weeds, json_weed_practices, json_pests, json_pest_practices): fields = airtable_json['fields'] mechanism_external_ids = fields.get('Marges de manoeuvre', []) resource_external_ids = fields.get('CTA lien', []) practice_types = [ x for x in json_practice_types if x['id'] in fields.get('Types') ] needs_shallow_tillage = any([ x for x in practice_types if x['fields'].get('Enum code') == PracticeTypeCategory.TRAVAIL_DU_SOL.name ]) needs_deep_tillage = any([ x for x in practice_types if x['fields'].get('Enum code') == PracticeTypeCategory.TRAVAIL_PROFOND.name ]) practice = Practice( external_id=airtable_json.get('id'), mechanism=next((x for x in mechanisms if x.external_id in mechanism_external_ids), None), main_resource=next( (x for x in resources if x.external_id in resource_external_ids), None), main_resource_label=fields.get('CTA title'), airtable_json=airtable_json, airtable_url='https://airtable.com/tblobpdQDxkzcllWo/' + airtable_json.get('id') + '/', title=fields.get('Nom').strip(), short_title=fields.get('Nom court').strip(), description=fields.get('Description'), equipment=fields.get('Matériel'), schedule=fields.get('Période de travail'), impact=fields.get('Impact'), additional_benefits=fields.get('Bénéfices supplémentaires'), success_factors=fields.get('Facteur clé de succès'), needs_tillage=fields.get('Nécessite travail du sol', False), livestock_multiplier=fields.get('Élevage multiplicateur'), needs_livestock=fields.get('Élevage nécessaire', False), balances_sowing_period=fields.get('Équilibre période semis', False), direct_sale_multiplier=fields.get('Vente directe multiplicateur'), precision=fields.get('Précision'), difficulty=fields.get('Difficulté'), added_cultures=fields.get('Ajout dans la rotation cultures'), culture_whitelist=fields.get('Cultures whitelist'), problems_addressed=Practice._get_problems_addressed(airtable_json), department_multipliers=Practice._get_department_multipliers( airtable_json, json_departments_practices, json_departments), glyphosate_multipliers=Practice._get_glyphosate_multipliers( airtable_json, json_glyphosate, json_glyphosate_practices), culture_multipliers=Practice._get_culture_multipliers( airtable_json, json_culture_practices), needs_shallow_tillage=needs_shallow_tillage, needs_deep_tillage=needs_deep_tillage, weed_multipliers=Practice._get_weed_multipliers( airtable_json, json_weeds, json_weed_practices), pest_multipliers=Practice._get_pest_multipliers( airtable_json, json_pests, json_pest_practices), weed_whitelist_external_ids=fields.get('Adventices whitelist', []), pest_whitelist_external_ids=fields.get('Ravageurs whitelist', []), ) image_name = get_airtable_media_name(airtable_json, 'Image principale') image_content_file = get_airtable_media_content_file( airtable_json, 'Image principale') if image_name and image_content_file: practice.image.save(image_name, image_content_file, save=True) return practice