Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Structures] Réparer la synchro avec les emplois #993

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@
# API Marché APProch
MARCHE_APPROCH_TOKEN_RECETTE = env.str("MARCHE_APPROCH_TOKEN_RECETTE", "set-it")

# API Emplois de l'inclusion
API_EMPLOIS_INCLUSION_URL = "https://emplois.inclusion.beta.gouv.fr/api/v1"
API_EMPLOIS_INCLUSION_TOKEN = env.str("API_EMPLOIS_INCLUSION_TOKEN", "set-it")


# Django REST Framework (DRF)
# https://www.django-rest-framework.org/
Expand Down
65 changes: 3 additions & 62 deletions lemarche/siaes/management/commands/sync_c1_c4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import re
from datetime import timedelta

import psycopg2
import psycopg2.extras
from django.conf import settings
from django.contrib.gis.geos import GEOSGeometry
from django.core.management.base import CommandError
Expand All @@ -12,7 +10,7 @@

from lemarche.siaes import constants as siae_constants
from lemarche.siaes.models import Siae
from lemarche.utils.apis import api_mailjet, api_slack
from lemarche.utils.apis import api_emplois_inclusion, api_mailjet, api_slack
from lemarche.utils.commands import BaseCommand
from lemarche.utils.constants import DEPARTMENT_TO_REGION
from lemarche.utils.data import rename_dict_key
Expand Down Expand Up @@ -45,46 +43,6 @@

C1_EXTRA_KEYS = ["convention_is_active", "convention_asp_id"]

REQUEST_SQL = """
SELECT
siae.id as id,
siae.siret,
siae.naf,
siae.kind,
siae.name,
siae.brand,
siae.phone,
siae.email,
siae.website,
siae.description,
siae.address_line_1,
siae.address_line_2,
siae.post_code,
siae.city,
siae.department,
siae.source,
ST_X(siae.coords::geometry) AS longitude,
ST_Y(siae.coords::geometry) AS latitude,
convention.is_active as convention_is_active,
convention.asp_id as convention_asp_id,
ad.admin_name as admin_name,
ad.admin_email as admin_email
FROM siaes_siae AS siae
LEFT OUTER JOIN siaes_siaeconvention AS convention
ON convention.id = siae.convention_id
LEFT OUTER JOIN (
SELECT
m.siae_id as siae_id,
u.username as admin_name,
u.email as admin_email
FROM
siaes_siaemembership m
JOIN users_user u
ON m.user_id = u.id
WHERE m.is_admin = True
) ad ON ad.siae_id = siae.id
"""


def map_siae_presta_type(siae_kind):
if siae_kind:
Expand Down Expand Up @@ -191,16 +149,9 @@ def handle(self, dry_run=False, **options):
self.stdout_messages_success(msg_success)
api_slack.send_message_to_channel("\n".join(msg_success))

def c1_export(self):
def c1_export(self): # noqa C901
try:
conn = self.get_c1_connection()
c1_list_temp = list()

with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute(REQUEST_SQL)
response = cur.fetchall()
for row in response:
c1_list_temp.append(dict(row))
c1_list_temp = api_emplois_inclusion.get_siae_list()

# clean fields
c1_list_cleaned = list()
Expand Down Expand Up @@ -253,21 +204,11 @@ def c1_export(self):

self.stdout_info(f"Found {len(c1_list_cleaned)} Siae in C1")
return c1_list_cleaned
except psycopg2.OperationalError as e:
raise psycopg2.OperationalError(e)
except Exception as e:
api_slack.send_message_to_channel("Erreur lors de la synchronisation C1 <-> C4")
self.stdout_error(e)
raise Exception(e)

def get_c1_connection(self):
try:
return psycopg2.connect(os.environ.get("C1_DSN"))
except psycopg2.OperationalError as e:
self.stdout_error(e)
api_slack.send_message_to_channel("Erreur de connexion à la db du C1 lors de la synchronisation C1 <-> C4")
raise psycopg2.OperationalError(e)

def filter_c1_export(self, c1_list):
"""
Some rules to filter out the siae that we don't want:
Expand Down
25 changes: 25 additions & 0 deletions lemarche/utils/apis/api_emplois_inclusion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import requests
from django.conf import settings


API_ENDPOINT = f"{settings.API_EMPLOIS_INCLUSION_URL}/marche"
API_HEADERS = {"Authorization": f"Token {settings.API_EMPLOIS_INCLUSION_TOKEN}"}


def get_siae_list():
siae_list = list()
pagination = 0

# loop on API to fetch all the data
while True:
response = requests.get(f"{API_ENDPOINT}?page_size={pagination}", headers=API_HEADERS)
data = response.json()
if data["results"]:
for siae in data["results"]:
siae_list.append(siae)
if data["next"]:
pagination += 1000
else:
break

return siae_list