Skip to content

Commit

Permalink
(PC-30235)[BO] feat: show ineligibility reason in Titelive search result
Browse files Browse the repository at this point in the history
  • Loading branch information
prouzet-pass authored and prouzet committed Jun 11, 2024
1 parent d96eab9 commit 23ebe07
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ <h5 class="card-title">{{ json.oeuvre.titre|unescape }}</h5>
{% endfor %}
</ul>
<ul class="list-group list-group-flush">
{% if ineligibility_reason %}
<li class="list-group-item">
<b>Inéligible pass Culture :</b> <span class="badge text-bg-danger shadow-sm">{{ ineligibility_reason }}</span>
</li>
{% endif %}
<li class="list-group-item">
<b>EAN white listé :</b> {{ 'Oui' if product_whitelist else 'Non' }}
</li>
Expand Down
11 changes: 11 additions & 0 deletions api/src/pcapi/routes/backoffice/titelive/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from flask import url_for
from flask_login import current_user
from markupsafe import Markup
import pydantic.v1 as pydantic_v1
import sqlalchemy as sa
from sqlalchemy import orm

from pcapi.connectors.serialization import titelive_serializers
from pcapi.connectors.titelive import GtlIdError
from pcapi.connectors.titelive import get_by_ean13
from pcapi.core import search
Expand All @@ -18,6 +20,7 @@
from pcapi.core.offers import exceptions as offers_exceptions
from pcapi.core.offers import models as offers_models
from pcapi.core.permissions import models as perm_models
from pcapi.core.providers.titelive_book_search import get_ineligibility_reason
from pcapi.core.users import models as users_models
from pcapi.models import db
from pcapi.models.offer_mixin import OfferValidationType
Expand Down Expand Up @@ -62,6 +65,13 @@ def search_titelive() -> utils.BackofficeResponse:
form.ean.errors.append(f"Erreur API Tite Live: {status_code}")
return render_template("titelive/search_result.html", form=form, dst=url_for(".search_titelive")), 400

try:
data = pydantic_v1.parse_obj_as(titelive_serializers.TiteLiveBookOeuvre, json["oeuvre"])
except Exception: # pylint: disable=broad-exception-caught
ineligibility_reason = None
else:
ineligibility_reason = get_ineligibility_reason(data.article[0], data.titre)

product_whitelist = (
fraud_models.ProductWhitelist.query.filter(fraud_models.ProductWhitelist.ean == ean)
.options(
Expand All @@ -82,6 +92,7 @@ def search_titelive() -> utils.BackofficeResponse:
form=form,
dst=url_for(".search_titelive"),
json=json,
ineligibility_reason=ineligibility_reason,
product_whitelist=product_whitelist,
)

Expand Down
127 changes: 127 additions & 0 deletions api/tests/connectors/titelive/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,133 @@ def build_titelive_one_book_response(
"extraparams": {"ws": 1},
}

INELIGIBLE_BOOK_BY_EAN_FIXTURE = {
"type": 1,
"magid": "6505",
"compatibility": [],
"oeuvre": {
"id": "3869601",
"titre": "Annales ABC du bac ; sujets & corrig\u00e9s : math\u00e9matiques ; terminale (\u00e9dition 2024)",
"auteurs": "Julien Besson, Isabelle Lericque, Luis Mateus, Jo\u00ebl Ternoy",
"auteurs_multi": {
"356832": "Julien Besson",
"356833": "Luis Mateus",
"414043": "Isabelle Lericque",
"414044": "Jo\u00ebl Ternoy",
},
"auteurs_id": ["356832", "414043", "356833", "414044"],
"auteurs_fonctions": [],
"article": [
{
"nombre": 0,
"gencod": "9782095023584",
"id_catalogue": ["0"],
"collection": "Annales Abc Du Bac ; Sujets & Corriges",
"collection_no": "0",
"editeur": "Nathan",
"distributeur": "Interforum",
"code_distributeur": "371",
"prix": 8.9,
"taux_tva": "5.50",
"code_tva": 2,
"prixpays": {
"fr": {"value": 8.9, "devise": "&euro;"},
"be": {"value": 8.9, "devise": "&euro;"},
"ch": {"value": 14.6, "devise": "CHF"},
},
"dateparution": "24/08/2023",
"dateparutionentier": 1692828000,
"datecreation": "19/04/2023",
"datecreationentier": 1681855200,
"datemodification": "30/05/2024",
"dispo": 1,
"dispopays": {"fr": 1, "be": 1, "ch": 1},
"libelledispo": "Disponible",
"resume": "Les Annales ABC du BAC pour r\u00e9viser et pr\u00e9parer l'\u00e9preuve de Math\u00e9matiques Terminale du Bac 2024.\r\n- 30 sujets corrig\u00e9s pour pr\u00e9parer l'\u00e9preuve et le Grand oral.\r\n- Des fiches de r\u00e9visions pour retenir l'essentiel.\r\n- Des exercices pour contr\u00f4ler ses connaissances.\r\n- Des aides pas \u00e0 pas et la m\u00e9thode en contexte + R\u00e9dig\u00e9 par des enseignants ! + Annales ABC du BAC 2024 Math\u00e9matiques Terminale - Enseignement de sp\u00e9cialit\u00e9 Conforme aux programmes du Bac Une nouvelle formule pour pr\u00e9parer avec succ\u00e8s l'\u00e9preuve finale du nouveau Bac !\r\nLes \u00e9preuves du nouveau Bac expliqu\u00e9es.\r\nLes bonnes m\u00e9thodes \u00e0 acqu\u00e9rir pour r\u00e9ussir.\r\nDes rappels de cours, des QCM et des exercices pour faire le point.\r\nDes sujets pas \u00e0 pas avec des corrig\u00e9s expliqu\u00e9s.\r\nDes sujets de Bac comme \u00e0 l'examen pour s'exercer.\r\nUn cahier sp\u00e9cial Grand oral.",
"image": 1,
"image_spe": 1,
"image_4": 0,
"imagesUrl": {
"recto": "https://images.epagine.fr/584/9782095023584_1_75.jpg",
"vign": "https://images.epagine.fr/584/9782095023584_1_v.jpg",
"moyen": "https://images.epagine.fr/584/9782095023584_1_m.jpg",
"verso": "https://images.epagine.fr/is/6505/9782095023584_4_75.jpg",
},
"detailUrl": "/livre/9782095023584-annales-abc-du-bac-sujets-corriges-mathematiques-terminale-edition-2024-julien-besson-isabelle-lericque-luis-mateus-joel-ternoy/",
"langue": "Fran\u00e7ais",
"langueiso": "fra",
"langueflag": "https://static.epagine.fr/commonfiles/flags/fr.png",
"nbmag": 183,
"codesupport": "T",
"libellesupport": "Grand format",
"drm": 0,
"biblio": 0,
"extrait": 0,
"extrait_oeuvre": 1,
"pages": 384,
"longueur": 21.2,
"largeur": 14.6,
"epaisseur": 2.2,
"poids": 478,
"biographie": 0,
"biographies": [],
"websites": [],
"serie": "Non pr\u00e9cis\u00e9e",
"idserie": "0",
"scolaire": 0,
"livre_etranger": 0,
"racine_editeur": "209",
"code_clil": "3006",
"type_prix": "0",
"typeproduit": 0,
"iad": "0",
"id_code_langue": "10000",
"stock": 0,
"gtl": {
"first": {
"1": {"code": "12000000", "libelle": "Parascolaire"},
"2": {"code": "12090000", "libelle": "Baccalaur&eacute;at g&eacute;n&eacute;ral (annales)"},
"3": {"code": "12090400", "libelle": "Math&eacute;matiques"},
"4": {"code": "12090418", "libelle": "Terminale Enseignement de Sp&eacute;cialit&eacute;"},
}
},
"public_averti": 0,
"replace": ["9782091572758"],
"precommande": 0,
"ispromotion": 0,
"isnew": 0,
"drmlcp": 0,
"bonus_extraits": {
"gencod": "9782095023584",
"pagesinterieures": 0,
"pdfextrait": 0,
"pdfchapitre": 0,
"pdfparagraphe": 0,
"mp3_count": 0,
},
"id_lectorat": 0,
"livre_lu": 0,
"grands_caracteres": 0,
"multilingue": 0,
"illustre": 0,
"luxe": 0,
"relie": 0,
"broche": 1,
"NombreMagasins": 0,
"collector": 0,
"code_editeur": "77",
}
],
"biographies": [],
"websites": [],
"typeproduit": 0,
},
"ean": "9782095023584",
"changeEncodingTo": "UTF-8",
"extraparams": {"ws": 1},
}


NO_RESULT_BY_EAN_FIXTURE = {
"type": 1,
"magid": "7",
Expand Down
14 changes: 14 additions & 0 deletions api/tests/routes/backoffice/titelive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_search_ean_not_whitelisted(self, mock_get_by_ean13, authenticated_clien
assert "Code support : " + article["libellesupport"] + " (" + article["codesupport"] + ")" in card_text[0]
assert "Code GTL : Littérature (01000000) Rayon (CSR): Littérature française (0100)" in card_text[0]
assert "Récit (01050000) Rayon (CSR): Littérature française Récits, Aventures, Voyages (0105)" in card_text[0]
assert "Inéligible pass Culture :" not in card_text[0]
assert "EAN white listé : Non" in card_text[0]

@patch("pcapi.routes.backoffice.titelive.blueprint.get_by_ean13")
Expand All @@ -99,11 +100,24 @@ def test_search_ean_whitelisted(self, mock_get_by_ean13, authenticated_client):
assert response.status_code == 200

card_text = html_parser.extract_cards_text(response.data)
assert "Inéligible pass Culture :" not in card_text[0]
assert "EAN white listé : Oui" in card_text[0]
assert f"Date d'ajout : {datetime.datetime.utcnow().strftime('%d/%m/%Y')}" in card_text[0]
assert "Auteur : Frank Columbo" in card_text[0]
assert "Commentaire : Superbe livre !" in card_text[0]

@patch(
"pcapi.routes.backoffice.titelive.blueprint.get_by_ean13", return_value=fixtures.INELIGIBLE_BOOK_BY_EAN_FIXTURE
)
def test_search_ean_with_ineligibility_reason(self, mock_get_by_ean13, authenticated_client):
with assert_num_queries(self.expected_num_queries):
response = authenticated_client.get(url_for(self.endpoint, **self.endpoint_kwargs))
assert response.status_code == 200

card_text = html_parser.extract_cards_text(response.data)
assert "Inéligible pass Culture : extracurricular" in card_text[0]
assert "EAN white listé : Non" in card_text[0]


class WhitelistButtonTest(button_helpers.ButtonHelper):
needed_permission = perm_models.Permissions.PRO_FRAUD_ACTIONS
Expand Down

0 comments on commit 23ebe07

Please sign in to comment.