Skip to content

Commit

Permalink
Adds support to enter a geo_map_url for an event #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Apr 2, 2023
1 parent d5c320e commit 54395c4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
10 changes: 10 additions & 0 deletions api/factory/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def get_hash_runs(params: HashParams) -> List[Hash]:
"geo_lat": post_attr.get("geolocation_lat"),
"geo_long": post_attr.get("geolocation_long"),
"geo_location_name": post_attr.get("geolocation_formatted_address"),
"geo_map_url": post_attr.get("_hash_geo_map_url"),
"location_name": post_attr.get("_event_location"),
"location_additional_info": post_attr.get("_hash_location_specifics"),
"facebook_group_id": config.app_settings.default_facebook_group_id,
Expand Down Expand Up @@ -246,6 +247,15 @@ def get_hash_runs(params: HashParams) -> List[Hash]:
if event_attributes is not None and isinstance(event_attributes, list):
hash_data["event_attributes"] = event_attributes

# handle geo_map_url
if post_attr.get("geo_map_url") is None and \
post_attr.get("geo_lat") is not None and post_attr.get("geo_long") is not None:

hash_data["geo_map_url"] = config.app_settings.maps_url_template.format(
lat=post_attr.get("geo_lat"),
long=post_attr.get("geo_long")
)

# parse event data
try:
run = Hash(**hash_data)
Expand Down
20 changes: 19 additions & 1 deletion api/models/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
from pytz import utc
from enum import Enum

from pydantic import BaseModel, AnyHttpUrl, Field, validator, root_validator
from pydantic import BaseModel, AnyHttpUrl, Field, validator, root_validator, ValidationError
from pydantic.dataclasses import dataclass
from fastapi import Query
from fastapi.exceptions import RequestValidationError

from config.hash import hash_attributes, hash_scope
from common.misc import format_slug
from common.log import get_logger
from api.models.exceptions import RequestValidationError

log = get_logger()


# generate from config.hash lists
HashAttributes = Enum('HashAttributes', {x: format_slug(x) for x in hash_attributes}, type=str)
Expand Down Expand Up @@ -175,4 +178,19 @@ def set_empty_strings_to_none(cls, value):
return None
return value

@validator("geo_map_url", always=True, pre=True)
def loose_type_geo_map_url(cls, value):
class SelfValidate(BaseModel):
url: AnyHttpUrl

if value is None:
return
try:
SelfValidate(url=value)
except ValidationError as e:
log.warning(f"Issues while validating 'geo_map_url' value '{value}': {e.errors()[0].get('msg')}")
return

return value

# EOF
5 changes: 5 additions & 0 deletions config-example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ hash_kennels = Nerd H3, Nerd Full Moon H3
# add the FaceBook group ID here. This ID will be added to each run.
#default_facebook_group_id =

# Define a maps url (google, apple, open street map) template,
# params "lat" and "long" will be substituted if present for a run
# and field "Maps Location URL" has not been defined.
#maps_url_template = "https://www.google.com/maps/search/?api=1&query={lat},{long}"

###
### [listmonk]
###
Expand Down
19 changes: 18 additions & 1 deletion config/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@

from typing import Union, List
from config.models import EnvOverridesBaseSettings
from pydantic import validator
from pydantic import validator, AnyHttpUrl
import pytz

from common.misc import split_quoted_string
from common.log import get_logger

log = get_logger()

maps_url_template = "https://www.google.com/maps/search/?api=1&query={lat},{long}"


# noinspection PyMethodParameters
Expand All @@ -24,6 +29,7 @@ class AppSettings(EnvOverridesBaseSettings):
default_currency: str = None
default_facebook_group_id: int = None
timezone_string: str = None
maps_url_template: AnyHttpUrl = maps_url_template

# currently not implemented in WP Event manager
# default_kennel: str = None
Expand Down Expand Up @@ -55,6 +61,17 @@ def split_hash_kennels(cls, value):
value = split_quoted_string(value, strip=True)
return value

@validator("maps_url_template")
def check_maps_url_formatting(cls, value):

try:
value.format(lat=123, long=456)
except KeyError as e:
log.error(f"Unable to parse 'maps_url_template' formatting, KeyError: {e}. Using default value.")
return maps_url_template

return value

"""
# currently not implemented in WP Event manager
@validator("default_run_attributes")
Expand Down
6 changes: 6 additions & 0 deletions source/manage_event_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def __init__(self):
"description": "Hide event from exposure on consumer site (Harrier Central)",
"required": False,
"type": "checkbox"
},
"hash_geo_map_url": {
"label": "Maps Location URL",
"description": "Add an maps URL with a specific location",
"required": False,
"type": "text"
}
}

Expand Down

0 comments on commit 54395c4

Please sign in to comment.