Skip to content

Commit 54395c4

Browse files
committed
Adds support to enter a geo_map_url for an event #8
1 parent d5c320e commit 54395c4

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

api/factory/runs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def get_hash_runs(params: HashParams) -> List[Hash]:
176176
"geo_lat": post_attr.get("geolocation_lat"),
177177
"geo_long": post_attr.get("geolocation_long"),
178178
"geo_location_name": post_attr.get("geolocation_formatted_address"),
179+
"geo_map_url": post_attr.get("_hash_geo_map_url"),
179180
"location_name": post_attr.get("_event_location"),
180181
"location_additional_info": post_attr.get("_hash_location_specifics"),
181182
"facebook_group_id": config.app_settings.default_facebook_group_id,
@@ -246,6 +247,15 @@ def get_hash_runs(params: HashParams) -> List[Hash]:
246247
if event_attributes is not None and isinstance(event_attributes, list):
247248
hash_data["event_attributes"] = event_attributes
248249

250+
# handle geo_map_url
251+
if post_attr.get("geo_map_url") is None and \
252+
post_attr.get("geo_lat") is not None and post_attr.get("geo_long") is not None:
253+
254+
hash_data["geo_map_url"] = config.app_settings.maps_url_template.format(
255+
lat=post_attr.get("geo_lat"),
256+
long=post_attr.get("geo_long")
257+
)
258+
249259
# parse event data
250260
try:
251261
run = Hash(**hash_data)

api/models/run.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
from pytz import utc
1313
from enum import Enum
1414

15-
from pydantic import BaseModel, AnyHttpUrl, Field, validator, root_validator
15+
from pydantic import BaseModel, AnyHttpUrl, Field, validator, root_validator, ValidationError
1616
from pydantic.dataclasses import dataclass
1717
from fastapi import Query
1818
from fastapi.exceptions import RequestValidationError
1919

2020
from config.hash import hash_attributes, hash_scope
2121
from common.misc import format_slug
22+
from common.log import get_logger
2223
from api.models.exceptions import RequestValidationError
2324

25+
log = get_logger()
26+
2427

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

181+
@validator("geo_map_url", always=True, pre=True)
182+
def loose_type_geo_map_url(cls, value):
183+
class SelfValidate(BaseModel):
184+
url: AnyHttpUrl
185+
186+
if value is None:
187+
return
188+
try:
189+
SelfValidate(url=value)
190+
except ValidationError as e:
191+
log.warning(f"Issues while validating 'geo_map_url' value '{value}': {e.errors()[0].get('msg')}")
192+
return
193+
194+
return value
195+
178196
# EOF

config-example.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ hash_kennels = Nerd H3, Nerd Full Moon H3
9494
# add the FaceBook group ID here. This ID will be added to each run.
9595
#default_facebook_group_id =
9696

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

config/models/app.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99

1010
from typing import Union, List
1111
from config.models import EnvOverridesBaseSettings
12-
from pydantic import validator
12+
from pydantic import validator, AnyHttpUrl
1313
import pytz
1414

1515
from common.misc import split_quoted_string
16+
from common.log import get_logger
17+
18+
log = get_logger()
19+
20+
maps_url_template = "https://www.google.com/maps/search/?api=1&query={lat},{long}"
1621

1722

1823
# noinspection PyMethodParameters
@@ -24,6 +29,7 @@ class AppSettings(EnvOverridesBaseSettings):
2429
default_currency: str = None
2530
default_facebook_group_id: int = None
2631
timezone_string: str = None
32+
maps_url_template: AnyHttpUrl = maps_url_template
2733

2834
# currently not implemented in WP Event manager
2935
# default_kennel: str = None
@@ -55,6 +61,17 @@ def split_hash_kennels(cls, value):
5561
value = split_quoted_string(value, strip=True)
5662
return value
5763

64+
@validator("maps_url_template")
65+
def check_maps_url_formatting(cls, value):
66+
67+
try:
68+
value.format(lat=123, long=456)
69+
except KeyError as e:
70+
log.error(f"Unable to parse 'maps_url_template' formatting, KeyError: {e}. Using default value.")
71+
return maps_url_template
72+
73+
return value
74+
5875
"""
5976
# currently not implemented in WP Event manager
6077
@validator("default_run_attributes")

source/manage_event_fields.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ def __init__(self):
107107
"description": "Hide event from exposure on consumer site (Harrier Central)",
108108
"required": False,
109109
"type": "checkbox"
110+
},
111+
"hash_geo_map_url": {
112+
"label": "Maps Location URL",
113+
"description": "Add an maps URL with a specific location",
114+
"required": False,
115+
"type": "text"
110116
}
111117
}
112118

0 commit comments

Comments
 (0)