Skip to content

Commit ae6ee87

Browse files
authored
Merge pull request #31 from wmo-raf/dev
Refactor getting weather icons as a function
2 parents 60b883e + 1c195f0 commit ae6ee87

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

forecastmanager/constants.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.templatetags.static import static
21
from django.utils.translation import gettext_lazy as _
32

43
# extracted from https://nrkno.github.io/yr-weather-symbols/
@@ -198,12 +197,6 @@
198197

199198
WEATHER_CONDITIONS_AS_DICT = {condition['id']: condition for condition in WEATHER_CONDITIONS}
200199

201-
WEATHER_CONDITION_ICONS = [
202-
{"value": condition["id"], **condition,
203-
'icon_url': static("forecastmanager/weathericons/{0}.png".format(condition["id"]))} for condition in
204-
WEATHER_CONDITIONS
205-
]
206-
207200
WEATHER_PARAMETERS_ICON_LIST = [
208201
"direction",
209202
"droplet-degree",

forecastmanager/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.templatetags.static import static
2+
3+
from .constants import WEATHER_CONDITIONS
4+
5+
6+
def get_weather_condition_icons():
7+
icons = [
8+
{"value": condition["id"], **condition,
9+
'icon_url': static("forecastmanager/weathericons/{0}.png".format(condition["id"]))} for condition in
10+
WEATHER_CONDITIONS
11+
]
12+
13+
return icons

forecastmanager/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from wagtail.api.v2.utils import get_full_url
1313

1414
from forecastmanager.models import City, Forecast
15-
from .constants import WEATHER_CONDITION_ICONS
1615
from .forecast_settings import ForecastSetting
1716
from .forms import CityLoaderForm
1817
from .serializers import CitySerializer, ForecastSerializer
18+
from .utils import get_weather_condition_icons
1919

2020

2121
class ReadOnly(BasePermission):
@@ -132,7 +132,7 @@ def forecast_settings(request):
132132

133133

134134
def weather_icons(request):
135-
options = WEATHER_CONDITION_ICONS
135+
options = get_weather_condition_icons()
136136
icons = [{"id": icon["id"], "name": icon["name"], "url": get_full_url(request, icon["icon_url"])} for icon in
137137
options]
138138
return JsonResponse(icons, safe=False)

forecastmanager/widgets.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from wagtail.utils.widgets import WidgetWithScript
77
from wagtail.widget_adapters import WidgetAdapter
88

9-
from forecastmanager.constants import WEATHER_CONDITION_ICONS, WEATHER_PARAMETER_CHOICES
9+
from .constants import WEATHER_PARAMETER_CHOICES
10+
from .utils import get_weather_condition_icons
1011

1112

1213
class WeatherSymbolChooserWidget(WidgetWithScript, widgets.TextInput):
@@ -27,7 +28,7 @@ def get_context(self, name, value, attrs):
2728
return context
2829

2930
def render_js_init(self, id_, name, value):
30-
options = WEATHER_CONDITION_ICONS
31+
options = get_weather_condition_icons()
3132
return "$(document).ready(() => new WeatherSymbolChooserWidget({0},{1}));".format(json.dumps(id_),
3233
json.dumps(options))
3334

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = forecastmanager
3-
version = 0.5.3
3+
version = 0.5.4
44
description = Integration of Weather City Forecasts Manager in Wagtail Projects.
55
long_description = file:README.md
66
long_description_content_type = text/markdown

0 commit comments

Comments
 (0)