Skip to content

Commit

Permalink
OrderedTrekChild OtM working - #5
Browse files Browse the repository at this point in the history
  • Loading branch information
IdrissaD committed May 2, 2022
1 parent f6dabf7 commit 25648aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
28 changes: 27 additions & 1 deletion gag_app/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand All @@ -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))
6 changes: 3 additions & 3 deletions gag_app/config/config.py
Original file line number Diff line number Diff line change
@@ -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".
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion gag_app/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 25648aa

Please sign in to comment.