Skip to content

base_notifications

Push Notifications Template

BaseNotifications #

Bases: ABC

Base Notifications

Source code in camply/notifications/base_notifications.py
class BaseNotifications(ABC):
    """
    Base Notifications
    """

    last_gasp: bool = True

    ignored_notification_fields = [
        CampsiteContainerFields.LOCATION,
        CampsiteContainerFields.CAMPSITE_ATTRIBUTES,
    ]

    def __init__(self) -> None:
        """
        Instantiate with a Requests Session
        """
        self.session = requests.Session()

    def __repr__(self) -> str:
        """
        String Representation
        """
        return f"<{self.__class__.__name__}>"

    @abstractmethod
    def send_message(self, message: str, **kwargs):
        """
        Send a message

        Parameters
        ----------
        message: str
            Message Text
        **kwargs
            All kwargs passed to underlying notification method
        """
        pass

    @abstractmethod
    def send_campsites(self, campsites: List[AvailableCampsite], **kwargs):
        """
        Send a message with a campsite object

        Parameters
        ----------
        campsites: List[AvailableCampsite]
        """
        pass

    @classmethod
    def format_standard_campsites(
        cls, campsite: AvailableCampsite
    ) -> Tuple[str, Dict[str, str]]:
        """
        Format Standard Message
        """
        fields = {}
        message_title = " | ".join(
            [
                campsite.recreation_area,
                campsite.facility_name,
                campsite.booking_date.strftime("%Y-%m-%d"),
            ]
        )
        for key, value in campsite.dict().items():
            if key in [
                CampsiteContainerFields.BOOKING_DATE,
                CampsiteContainerFields.BOOKING_END_DATE,
            ]:
                value: datetime.date
                value: str = value.strftime("%Y-%m-%d")
            elif key == CampsiteContainerFields.BOOKING_URL:
                key = "booking_link"
            elif key == CampsiteContainerFields.PERMITTED_EQUIPMENT:
                equipment = (
                    []
                    if campsite.permitted_equipment is None
                    else campsite.permitted_equipment
                )
                value = "\n  - " + "\n  - ".join(
                    {item.equipment_name for item in equipment}
                )
            if key not in cls.ignored_notification_fields:
                formatted_key = key.replace("_", " ").title()
                fields[formatted_key] = value
        return message_title, fields

__init__() #

Instantiate with a Requests Session

Source code in camply/notifications/base_notifications.py
def __init__(self) -> None:
    """
    Instantiate with a Requests Session
    """
    self.session = requests.Session()

__repr__() #

String Representation

Source code in camply/notifications/base_notifications.py
def __repr__(self) -> str:
    """
    String Representation
    """
    return f"<{self.__class__.__name__}>"

format_standard_campsites(campsite) classmethod #

Format Standard Message

Source code in camply/notifications/base_notifications.py
@classmethod
def format_standard_campsites(
    cls, campsite: AvailableCampsite
) -> Tuple[str, Dict[str, str]]:
    """
    Format Standard Message
    """
    fields = {}
    message_title = " | ".join(
        [
            campsite.recreation_area,
            campsite.facility_name,
            campsite.booking_date.strftime("%Y-%m-%d"),
        ]
    )
    for key, value in campsite.dict().items():
        if key in [
            CampsiteContainerFields.BOOKING_DATE,
            CampsiteContainerFields.BOOKING_END_DATE,
        ]:
            value: datetime.date
            value: str = value.strftime("%Y-%m-%d")
        elif key == CampsiteContainerFields.BOOKING_URL:
            key = "booking_link"
        elif key == CampsiteContainerFields.PERMITTED_EQUIPMENT:
            equipment = (
                []
                if campsite.permitted_equipment is None
                else campsite.permitted_equipment
            )
            value = "\n  - " + "\n  - ".join(
                {item.equipment_name for item in equipment}
            )
        if key not in cls.ignored_notification_fields:
            formatted_key = key.replace("_", " ").title()
            fields[formatted_key] = value
    return message_title, fields

send_campsites(campsites, **kwargs) abstractmethod #

Send a message with a campsite object

Parameters:

Name Type Description Default
campsites List[AvailableCampsite]
required
Source code in camply/notifications/base_notifications.py
@abstractmethod
def send_campsites(self, campsites: List[AvailableCampsite], **kwargs):
    """
    Send a message with a campsite object

    Parameters
    ----------
    campsites: List[AvailableCampsite]
    """
    pass

send_message(message, **kwargs) abstractmethod #

Send a message

Parameters:

Name Type Description Default
message str

Message Text

required
**kwargs

All kwargs passed to underlying notification method

{}
Source code in camply/notifications/base_notifications.py
@abstractmethod
def send_message(self, message: str, **kwargs):
    """
    Send a message

    Parameters
    ----------
    message: str
        Message Text
    **kwargs
        All kwargs passed to underlying notification method
    """
    pass

NotificationError #

Bases: Exception

Notification Exceptions

Source code in camply/notifications/base_notifications.py
class NotificationError(Exception):
    """
    Notification Exceptions
    """