Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #264 from Qwant/block-delivery
Browse files Browse the repository at this point in the history
add block `delivery`
  • Loading branch information
remi-dupre authored Jul 22, 2021
2 parents 426d970 + 6784dc4 commit 3584de7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 13 deletions.
3 changes: 3 additions & 0 deletions idunn/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TransactionalBlock,
SocialBlock,
DescriptionBlock,
DeliveryBlock,
)
from idunn.utils.settings import _load_yaml_file
from idunn.datasources.mimirsbrunn import MimirPoiFilter
Expand Down Expand Up @@ -136,6 +137,7 @@ def default_list(cls):
TransactionalBlock,
SocialBlock,
DescriptionBlock,
DeliveryBlock,
],
Verbosity.LIST: [
OpeningDayEvent,
Expand All @@ -149,6 +151,7 @@ def default_list(cls):
RecyclingBlock,
TransactionalBlock,
SocialBlock,
DeliveryBlock,
],
Verbosity.SHORT: [OpeningHourBlock, Covid19Block],
}
Expand Down
2 changes: 2 additions & 0 deletions idunn/blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .transactional import TransactionalBlock
from .social import SocialBlock
from .description import DescriptionBlock
from .delivery import DeliveryBlock

AnyBlock = Union[
OpeningHourBlock,
Expand All @@ -46,4 +47,5 @@
TransactionalBlock,
SocialBlock,
DescriptionBlock,
DeliveryBlock,
]
33 changes: 33 additions & 0 deletions idunn/blocks/delivery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from enum import Enum
from typing import Literal

from .base import BaseBlock


class DeliveryState(Enum):
YES = "yes"
UNKNOWN = "unknown"

@classmethod
def from_bool(cls, is_yes: bool):
return cls.YES if is_yes else cls.UNKNOWN


class DeliveryBlock(BaseBlock):
type: Literal["delivery"] = "delivery"
click_and_collect: DeliveryState
delivery: DeliveryState
takeaway: DeliveryState

@classmethod
def from_es(cls, place, lang):
states = {
"click_and_collect": DeliveryState.from_bool(place.has_click_and_collect()),
"delivery": DeliveryState.from_bool(place.has_delivery()),
"takeaway": DeliveryState.from_bool(place.has_takeaway()),
}

if all(state == DeliveryState.UNKNOWN for state in states.values()):
return None

return cls(**states)
9 changes: 9 additions & 0 deletions idunn/places/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ def get_description(self, lang):
def get_description_url(self, _lang):
return None

def has_click_and_collect(self):
return False

def has_delivery(self):
return self.properties.get("delivery") == "yes"

def has_takeaway(self):
return self.properties.get("takeaway") in ("yes", "only")

def get_bbox(self):
return None

Expand Down
37 changes: 27 additions & 10 deletions idunn/places/pj_poi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
from ..api.constants import PoiSource
from ..api.urlsolver import resolve_url

WHEELCHAIN_ACCESSIBLE = re.compile(
"|".join(
[
"accès handicapés?",
"accès aux personnes à mobilité réduite",
"accès pour personne à mobilité réduite",
]
)
)

CLICK_AND_COLLECT = re.compile(r"retrait .*")
DELIVERY = re.compile(r"commande en ligne|livraison.*")
TAKEAWAY = re.compile(r".* à emporter")
WHEELCHAIR_ACCESSIBLE = re.compile("accès (handicapés?|(aux|pour) personnes? à mobilité réduite)")

DOCTORS = (
"Chiropracteur",
Expand Down Expand Up @@ -362,7 +358,7 @@ def get_raw_opening_hours(self):
def get_raw_wheelchair(self):
return (
any(
WHEELCHAIN_ACCESSIBLE.match(label)
WHEELCHAIR_ACCESSIBLE.match(label)
for desc in self.data.business_descriptions
for label in desc.values
)
Expand Down Expand Up @@ -518,3 +514,24 @@ def get_description_url(self, lang):
return None

return self.get_source_url()

def has_click_and_collect(self):
return any(
CLICK_AND_COLLECT.match(label.lower())
for desc in self.data.business_descriptions
for label in desc.values
)

def has_delivery(self):
return any(
DELIVERY.match(label.lower())
for desc in self.data.business_descriptions
for label in desc.values
)

def has_takeaway(self):
return any(
TAKEAWAY.match(label.lower())
for desc in self.data.business_descriptions
for label in desc.values
)
10 changes: 10 additions & 0 deletions tests/fixtures/pj/api_musee_picasso.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
"boutique",
"accès aux personnes à mobilité réduite"
]
},
{
"label": "Prestations",
"values": [
"RETRAIT 1H",
"ÉCO-COLLECTE",
"LIVRAISON 2H CHRONO",
"LIVRAISON EN 1 JOUR",
"LIVRAISON"
]
}
],
"categories": [
Expand Down
14 changes: 14 additions & 0 deletions tests/test_delivery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from idunn.blocks.delivery import DeliveryBlock
from idunn.places import POI


def test_delivery_block():
delivery_block = DeliveryBlock.from_es(
POI({"properties": {"delivery": "yes", "takeaway": "yes"}}), lang="en"
)

assert delivery_block == DeliveryBlock(
click_and_collect="unknown",
delivery="yes",
takeaway="yes",
)
11 changes: 8 additions & 3 deletions tests/test_pj_poi.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ def test_pj_api_place(enable_pj_source):

assert blocks[8]["type"] == "description"
assert blocks[8]["source"] == "pagesjaunes"
assert (
blocks[8]["description"]
== "Le musée Picasso est le musée national français consacré à la vie et à l'œuvre de Pablo Picasso ainsi qu'aux artistes qui lui furent liés. "
assert blocks[8]["description"] == (
"Le musée Picasso est le musée national français consacré à la vie et à l'œuvre de Pablo "
"Picasso ainsi qu'aux artistes qui lui furent liés. "
)

assert blocks[9]["type"] == "delivery"
assert blocks[9]["click_and_collect"] == "yes"
assert blocks[9]["delivery"] == "yes"
assert blocks[9]["takeaway"] == "unknown"


@pytest.mark.parametrize(
"enable_pj_source",
Expand Down

0 comments on commit 3584de7

Please sign in to comment.