-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_pages.py
204 lines (174 loc) · 6.81 KB
/
generate_pages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
from jinja2 import Environment, FileSystemLoader
import json
import utils.helpers
import utils.zone_helpers
import utils.MadBoulderDatabase
from slugify import slugify
from flask_babel import Babel, _
LINK_FIELD = 'link'
NAME_FIELD = 'name'
ZONE_CODE_FIELD = 'zone_code'
LATITUDE_FIELD = 'latitude'
LONGITUDE_FIELD = 'longitude'
ALTITUDE_FIELD = 'altitude'
GUIDES_FIELD = 'guides'
SECTORS_FIELD = 'sectors'
LINKS_FIELD = 'links'
COUNTRY_CODE_FIELD = 'country'
STATE_CODE_FIELD = 'state'
ROCK_TYPE_FIELD = 'rock_type'
OVERVIEW_FIELD = 'overview'
def main():
"""
Generate html map templates for all the areas located inside the data folder
as well as a general map that contains all the areas
"""
areas = utils.MadBoulderDatabase.getAreasData()
countriesData = utils.MadBoulderDatabase.getCountriesData()
playlistsData = utils.MadBoulderDatabase.getPlaylistsData()
videoData = utils.MadBoulderDatabase.getAllVideoData()
template_loader = FileSystemLoader(searchpath='.')
template_env = Environment(loader=template_loader)
generateFilterBoxElement(template_env)
generateAreasListPage(template_env, areas, playlistsData)
templatePage = template_env.get_template('templates/templates/area_page_template.html')
templatePage_es = template_env.get_template('templates/templates/es/area_page_template.html')
playlists = {}
for areaCode, area in areas.items():
print(f"Creating area page of {area[NAME_FIELD]}")
# get external links
links = [link for link in area.get(LINKS_FIELD, []) if link.get(LINK_FIELD)]
# get external guides
guides = [
{
"name": guide["name"],
"link": guide["link"][0] if isinstance(guide["link"], list) else guide["link"],
"thumbnail": guide["thumbnail"]
}
for guide in area.get(GUIDES_FIELD, [])
if guide.get(LINK_FIELD)
]
guides = [guide for guide in guides if guide.get(LINK_FIELD)]
# problems
if areaCode in videoData:
problems_dict = videoData[areaCode]
problems_list = sorted(problems_dict.values(), key=lambda x: x['name'])
problems_list.sort(key= lambda x: x['name'])
# sectors
sectors = utils.zone_helpers.getSectors(problems_dict)
sectors.sort(key= lambda x: x)
#playlists
if areaCode in playlistsData:
playlists = playlistsData[areaCode]
#country
areaInfo = utils.zone_helpers.getStateAndCountryInfoFromData(areas, countriesData, areaCode)
#statistics
statistics = generateAreaStatistics(problems_list, playlists)
#overview
overview = area.get("overview", [""])[0]
output = templatePage.render(
problems=problems_list,
sectors=sectors,
area_code=areaCode,
name=area[NAME_FIELD],
rock_type=utils.zone_helpers.getRockTypeStr(area.get(ROCK_TYPE_FIELD, "")),
overview=overview,
tag_name=area[NAME_FIELD].replace("'", r"\'"),
links_list=links,
guides_list=guides,
map_url='maps/'+areaCode,
playlists=playlists,
lat=area[LATITUDE_FIELD],
lng=area[LONGITUDE_FIELD],
alt=int(area.get(ALTITUDE_FIELD,'0')),
zone=area[NAME_FIELD],
areaInfo=areaInfo,
stats_list=statistics
)
with open('templates/zones/'+areaCode+'.html', 'w', encoding='utf-8') as template:
template.write(output)
guides_es = [
{
"name": guide["name"],
"link": guide["link"][1] if isinstance(guide["link"], list) else guide["link"],
"thumbnail": guide["thumbnail"]
}
for guide in area.get(GUIDES_FIELD, [])
if guide.get(LINK_FIELD)
]
guides_es = [guide for guide in guides_es if guide.get(LINK_FIELD)]
overview_es = area.get("overview", [""])[1]
output = templatePage_es.render(
problems=problems_list,
sectors=sectors,
area_code=areaCode,
name=area[NAME_FIELD],
rock_type=utils.zone_helpers.getRockTypeStr(area.get(ROCK_TYPE_FIELD, "")),
overview=overview_es,
tag_name=area[NAME_FIELD].replace("'", r"\'"),
links_list=links,
guides_list=guides_es,
map_url='maps/'+areaCode,
playlists=playlists,
lat=area[LATITUDE_FIELD],
lng=area[LONGITUDE_FIELD],
alt=int(area.get(ALTITUDE_FIELD,'0')),
zone=area[NAME_FIELD],
areaInfo=areaInfo,
stats_list=statistics
)
with open('templates/zones/es/'+areaCode+'.html', 'w', encoding='utf-8') as template_es:
template_es.write(output)
def generateFilterBoxElement(template_env):
country_data = utils.MadBoulderDatabase.getCountriesData()
rockTypes_data = utils.zone_helpers.getRockTypeList()
templatePageList = template_env.get_template('templates/templates/search-sort-filter-box-template.html')
output = templatePageList.render(
country_data=country_data,
rockTypes_data=rockTypes_data
)
with open('templates/elements/search-sort-filter-box.html', 'w', encoding='utf-8') as template:
template.write(output)
def generateAreasListPage(template_env, areas, playlists):
country_data = utils.MadBoulderDatabase.getCountriesData()
rockTypes_data = utils.zone_helpers.getRockTypeList()
templatePageList = template_env.get_template('templates/templates/areas_list_page_template.html')
output = templatePageList.render(
zones=areas,
playlists=playlists,
country_data=country_data,
rockTypes_data=rockTypes_data
)
with open('templates/bouldering-areas-list.html', 'w', encoding='utf-8') as template:
template.write(output)
def generateAreaStatistics(problems, playlists):
views_count = utils.zone_helpers.get_view_count_from_problems(problems)
contributor_count = utils.zone_helpers.get_contributor_count_from_problems(problems)
video_count = len(problems)
sector_count = len(playlists.get('sectors', []))
data = [
{
'logo': 'fas fa-video-camera',
'text': _('Videos'),
'data': video_count
},
{
'logo': 'fas fa-eye',
'text': _('Views'),
'data': views_count
},
{
'logo': 'fas fa-user',
'text': _('Contributors'),
'data': contributor_count
},
{
'logo': 'fa fa-map-marked',
'text': _('Sectors'),
'data': sector_count
}
]
return data
if __name__ == '__main__':
main()