Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit ca137ea

Browse files
committed
Merge branch 'main' of github.com:entrepreneur-interet-general/opencollectivites into main
2 parents 1b4b427 + b2840cb commit ca137ea

File tree

12 files changed

+117
-31
lines changed

12 files changed

+117
-31
lines changed

.github/workflows/django.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Django-OpenCollectivites CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
strategy:
14+
max-parallel: 4
15+
matrix:
16+
python-version: [3.9]
17+
services:
18+
postgres:
19+
image: postgres:10.8
20+
env:
21+
POSTGRES_USER: sample_user_name
22+
POSTGRES_PASSWORD: sample_password
23+
POSTGRES_DB: sample_db_name
24+
ports:
25+
- 5432:5432
26+
# needed because the postgres container does not provide a healthcheck
27+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
28+
29+
steps:
30+
- uses: actions/checkout@v2
31+
- name: 🐍 Set up Python ${{ matrix.python-version }}
32+
uses: actions/setup-python@v2
33+
with:
34+
python-version: ${{ matrix.python-version }}
35+
- name: 🐘 psycopg2 prerequisites
36+
run: sudo apt install libpq-dev
37+
- name: 👷 Install Dependencies
38+
run: |
39+
python -m pip install --upgrade pip
40+
pip install poetry
41+
poetry install
42+
- name: 🧪 Run Tests
43+
run: |
44+
poetry run python manage.py test

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ postgres=# \c <db_name>
3232
- `git clone` ce répertoire quelque part et `cd` dedans.
3333

3434
### Régler les paramètres locaux
35-
- `cp settings_local.py.sample settings_local.py`
35+
- `cp settings_local_sample.py settings_local.py`
3636
- Remplir le fichier `settings_local.py`
3737

3838
### Créer et activer l'environnement virtuel et installer les dépendances

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
[![Made with Wagtail](https://img.shields.io/badge/Made%20with-Wagtail-0F7676.svg)](https://wagtail.io/)
33
[![GitHub license](https://img.shields.io/github/license/entrepreneur-interet-general/opencollectivites.svg)](https://github.com/entrepreneur-interet-general/opencollectivites/blob/master/LICENSE)
44
[![Website open-collectivites.fr](https://img.shields.io/website.svg?down_color=red&down_message=down&up_color=green&up_message=up&url=https%3A%2F%2Fwww.open-collectivites.fr)](https://www.open-collectivites.fr)
5-
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=entrepreneur-interet-general_opencollectivites&metric=alert_status)](https://sonarcloud.io/dashboard?id=entrepreneur-interet-general_opencollectivites)
5+
[![OpenCollectivites CI](https://github.com/entrepreneur-interet-general/opencollectivites/actions/workflows/django.yml/badge.svg)](https://github.com/entrepreneur-interet-general/opencollectivites/actions/workflows/django.yml)
66
[![CodeQL](https://github.com/entrepreneur-interet-general/opencollectivites/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/entrepreneur-interet-general/opencollectivites/actions/workflows/codeql-analysis.yml)
7+
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=entrepreneur-interet-general_opencollectivites&metric=alert_status)](https://sonarcloud.io/dashboard?id=entrepreneur-interet-general_opencollectivites)
78

89
# Open Collectivités
910
<img align="right" width="250" src="open-collectivites.png?raw=true" title="Open Collectivités" alt="" />

config/settings.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
https://docs.djangoproject.com/en/3.1/ref/settings/
1111
"""
1212

13-
# Keep secrets and environment_specific variables in a separate file not using version control
14-
from settings_local import *
1513
from os import path
1614
import sys
1715
import logging
1816

17+
# Keep secrets and environment_specific variables in a separate file not using version control
18+
try:
19+
from settings_local import *
20+
except ImportError:
21+
from settings_local_sample import *
22+
23+
1924
# Build paths inside the project like this: BASE_DIR / 'subdir'.
2025
BASE_DIR = LOCAL_BASE_DIR
2126

external_apis/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class OpenDataSoftQueryAdmin(TimeStampModelAdmin):
6868
"identify_regions",
6969
"identify_departements",
7070
"identify_metropoles",
71+
"identify_years",
7172
]
7273
},
7374
),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.2.8 on 2021-10-18 09:16
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('external_apis', '0005_alter_bnspquery_options'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='opendatasoftquery',
15+
name='identify_years',
16+
field=models.BooleanField(default=False, verbose_name='Identifier les années de publication'),
17+
),
18+
]

external_apis/models.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class OpenDataSoftQuery(ExternalApiQuery):
6363
identify_metropoles = models.BooleanField(
6464
verbose_name="Identifier les noms de métropoles", default=False
6565
)
66+
identify_years = models.BooleanField(
67+
verbose_name="Identifier les années de publication", default=False
68+
)
6669

6770
class Meta:
6871
verbose_name = "OpenDataSoft — requête"
@@ -151,6 +154,9 @@ def create_or_update_document(self, result: dict) -> None:
151154
if self.identify_metropoles:
152155
new_doc.identify_metropoles(full_text)
153156

157+
if self.identify_years:
158+
self.get_years_from_keywords(new_doc, metadata["keyword"])
159+
154160
def apply_mappings(self, document: Document, metadata: dict) -> None:
155161
"""
156162
Applies the selected mappings to the document
@@ -171,27 +177,37 @@ def add_metadata_with_mapping(
171177
metadata_values = metadata[field]
172178
for mapping_value, properties in mapping_values.items():
173179
if mapping_value in metadata_values:
174-
if isinstance(properties, list):
175-
for property in properties:
176-
self.add_metadata_properties(document, property)
177-
else:
178-
self.add_metadata_properties(document, properties)
180+
self.add_metadata_properties(document, properties)
179181

180182
def add_metadata_properties(self, document: Document, properties: dict) -> None:
181183
"""
182184
Add metadata from a properties dict
183185
"""
184-
if properties["type"] == "topic":
185-
document.topics.add(properties["value"])
186-
elif properties["type"] == "scope":
187-
document.scope.add(properties["value"])
188-
elif properties["type"] == "publication_pages":
189-
document.publication_pages.add(properties["value"])
190-
elif properties["type"] == "region":
191-
document.regions.add(properties["value"])
192-
elif properties["type"] == "departement":
193-
document.departements.add(properties["value"])
186+
for key, values in properties.items():
187+
if not isinstance(values, list):
188+
values = [values]
189+
190+
if key == "topic":
191+
document.topics.add(*values)
192+
elif key == "scope":
193+
document.scope.add(*values)
194+
elif key == "publication_pages":
195+
document.publication_pages.add(*values)
196+
elif key == "region":
197+
document.regions.add(*values)
198+
elif key == "departement":
199+
document.departements.add(*values)
200+
201+
document.save()
194202

203+
def get_years_from_keywords(self, document: Document, keywords: dict) -> None:
204+
"""
205+
Tries to identify publications years from the keywords dict
206+
"""
207+
for keyword in keywords:
208+
if keyword.isnumeric() and int(keyword) >= 2000 and int(keyword) <= 2100:
209+
year, year_is_new = DataYear.objects.get_or_create(year=keyword)
210+
document.years.add(year)
195211
document.save()
196212

197213

external_apis/services/opendatasoft_api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def api_call(self, endpoint: str, payload: dict) -> dict:
4242
while retries:
4343
logging.warning(f"An error happened, retrying (retries: {retries})")
4444
logging.debug(f"Parameters used:")
45-
logging.debug(f"endpoint:", endpoint)
46-
logging.debug(f"payload:", payload)
47-
logging.debug(f"response:", response.text)
45+
logging.debug(f"endpoint: {endpoint}")
46+
logging.debug(f"payload: {payload}")
47+
logging.debug(f"response: {response.text}")
4848
time.sleep(5)
4949
response = requests.get(endpoint, params=payload)
5050
if response.status_code == 200:
@@ -81,7 +81,9 @@ def api_call_loop(self, endpoint: str, payload: dict, results_key: str) -> list:
8181
logging.info("The research returned no (new) results.")
8282
return []
8383

84-
def catalog_datasets(self, where: str = "", sort: str = "", exclude: str = "") -> list:
84+
def catalog_datasets(
85+
self, where: str = "", sort: str = "", exclude: str = ""
86+
) -> list:
8587
"""
8688
Perform a query on the Search endpoint
8789
"""

external_apis/tests/test_services_gallica_search_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def setUp(self) -> None:
1515
"?operation=searchRetrieve&version=1.2&maximumRecords=15"
1616
"&startRecord=1&query=dc.title+all+%22jaune+bleu%22"
1717
)
18-
with open("bnsp/tests/testdata/SRU_jaune_bleu_p1.xml", "r") as f:
18+
with open("external_apis/tests/testdata/SRU_jaune_bleu_p1.xml", "r") as f:
1919
self.test_response_p1 = f.read()
2020

21-
with open("bnsp/tests/testdata/SRU_jaune_bleu_p2.xml", "r") as f:
21+
with open("external_apis/tests/testdata/SRU_jaune_bleu_p2.xml", "r") as f:
2222
self.test_response_p2 = f.read()
2323

2424
def test_search_instance_is_created(self) -> None:

feeds

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)