diff --git a/gag_app/classes.py b/gag_app/classes.py index 8dd5e23..ecb1c47 100644 --- a/gag_app/classes.py +++ b/gag_app/classes.py @@ -2,10 +2,11 @@ import os import urllib.request from mimetypes import guess_type +from warnings import warn from geotrek.common.models import FileType, Attachment from geotrek.authent.models import User +from geotrek.trekking.models import Trek, OrderedTrekChild from django.conf import settings -from warnings import warn from django.apps import apps from django.contrib.contenttypes.models import ContentType from gag_app.env import model_to_import, source_cat_to_gag_cat, core_topology, common, list_label_field @@ -367,6 +368,25 @@ def import_attachments(self): attachment_to_add, created = Attachment.objects.update_or_create(uuid=attachment_dict['uuid'], defaults={**attachment_dict}) self.obj_to_insert.attachments.add(attachment_to_add, bulk=False) + def get_treks_relationships(self): + signal = False + + if self.api_data[self.index]['children']: + print('children: ', self.api_data[self.index]['children']) + + for old_id in self.api_data[self.index]['children']: + uuid = [ad['uuid'] for ad in self.api_data if ad['id'] == old_id] + if uuid: + child = Trek.objects.get(uuid=uuid[0]) + parent = Trek.objects.get(uuid=self.api_data[self.index]['uuid']) + to_add = OrderedTrekChild(child=child, parent=parent) + parent.trek_children.add(to_add, bulk=False) + signal = True + else: + print(f"{old_id} trek may be not published, therefore isn't in API results") + + return signal + def run(self): for self.index in range(len(self.api_data)): #print('self.api_data[self.index]: ', self.api_data[self.index]) @@ -386,3 +406,9 @@ def run(self): self.import_attachments() print("\n{} OBJECT N°{} INSERTED!\n".format(self.model_lowercase.upper(), self.index+1)) + + for self.index in range(len(self.api_data)): + if self.model_to_import_name == 'Trek': + signal = self.get_treks_relationships() + if signal: + print("\nRELATIONSHIPS OF {} OBJECT N°{} CREATED!\n".format(self.model_lowercase.upper(), self.index+1)) diff --git a/gag_app/config/config.py b/gag_app/config/config.py index 601ff08..0187e09 100644 --- a/gag_app/config/config.py +++ b/gag_app/config/config.py @@ -1,9 +1,9 @@ # URL du Geotrek-admin source : -GADMIN_BASE_URL = 'geotrek-admin.cevennes-parcnational.net' +GADMIN_BASE_URL = 'geotrekdemo.ecrins-parcnational.fr' # Si nécessaire, nom du (ou des) portail(s) de la base source # dont on veut récupérer les données : -PORTALS = ['DEP_48'] +PORTALS = None # Attribution des données à une structure. # Doit correspondre à une entrée dans la table "authent_structure". @@ -12,7 +12,7 @@ # Il s'agit uniquement d'attribuer les données importées à une structure # dans la base aggregator. # Ne dépend donc pas des structures enregistrées dans la BDD source -AUTHENT_STRUCTURE = 'PNC' +AUTHENT_STRUCTURE = 'PNE' # Nom de l'user auquel sera attribuée la création des médias # Par exemple un compte d'administration. diff --git a/gag_app/env.py b/gag_app/env.py index f6677f9..8fc7515 100644 --- a/gag_app/env.py +++ b/gag_app/env.py @@ -101,8 +101,8 @@ "Route": "trek_route", "ReservationSystem": "reservationsystem", "Theme": "theme", - # "Rating": "trek_rating", "TrekNetwork": "trek_network", + # "Rating": "trek_rating", # "Accessibility": "trek_accessibility", # "InformationDesk": "informationdesk", # "RecordSource": "source",