def testZipInit(self): latLonZipName = ['42.377651', '-72.50323', '01002', 'Amherst, MA'] location = Location(latLonZipName[2]) self.assertEqual(latLonZipName[0], location.latitude) self.assertEqual(latLonZipName[1], location.longitude) self.assertEqual(latLonZipName[2], location.zipcode) self.assertEqual(latLonZipName[3], location.name)
def embedForecast(zipOrLatLon): """ :param zipOrLatLon: URL query parameters: same as showForecast(), but not list """ try: # make the rangeDict rangeDictFromQuery = rangesDictFromRequestArgs( request.args) if request.args.get( 'p') else None # check for at least one rangeDict = rangeDictFromQuery or Forecast.PARAM_RANGE_STEPS_DEFAULT # make the Forecast zipOrLatLonList = zipOrLatLon.split( '|') if '|' in zipOrLatLon else zipOrLatLon forecast = Forecast(Location(zipOrLatLonList), rangeDict) # render the forecast queryParamsDict = queryParamsDictFromRangeDict(rangeDict) fullUrl = fullUrlForEndpoint('showForecast', zipOrLatLon, queryParamsDict) return render_template("embedded-forecast.html", forecast=forecast, fullUrl=fullUrl) except Exception as ex: return render_template("embedded-forecast.html", error=ex.args[0])
def generateStickerImage(zipOrLatLon): """ :param zipOrLatLon: URL query parameters: same as showForecast(), but not list """ # make the rangeDict rangesDictJson = request.cookies.get(RANGES_COOKIE_NAME) rangeDictFromCookie = json.loads( rangesDictJson) if rangesDictJson else None rangeDictFromQuery = rangesDictFromRequestArgs( request.args) if request.args.get( 'p') else None # check for at least one rangeDict = rangeDictFromQuery or rangeDictFromCookie or Forecast.PARAM_RANGE_STEPS_DEFAULT # make the Forecast zipOrLatLonList = zipOrLatLon.split( '|') if '|' in zipOrLatLon else zipOrLatLon forecast = Forecast(Location(zipOrLatLonList), rangeDict) # construct the sticker image and return it as a png image = Sticker(forecast).image bytesIO = BytesIO() image.save(bytesIO, format="png") response = make_response(bytesIO.getvalue()) response.mimetype = 'image/png' return response
def showStickersEditor(zipOrLatLon): """ :param zipOrLatLon: same as showForecast() URL query parameters: same as showForecast(), but not list """ try: # make the rangeDict rangesDictJson = request.cookies.get(RANGES_COOKIE_NAME) rangeDictFromCookie = json.loads( rangesDictJson) if rangesDictJson else None rangeDictFromQuery = rangesDictFromRequestArgs( request.args) if request.args.get( 'p') else None # check for at least one rangeDict = rangeDictFromQuery or rangeDictFromCookie or Forecast.PARAM_RANGE_STEPS_DEFAULT # make the Forecast zipOrLatLonList = zipOrLatLon.split( '|') if '|' in zipOrLatLon else zipOrLatLon forecast = Forecast(Location(zipOrLatLonList), rangeDict) # construct the sticker image queryParamsDict = queryParamsDictFromRangeDict(rangeDict) stickerImageUrl = fullUrlForEndpoint('generateStickerImage', zipOrLatLon, queryParamsDict) image = Sticker(forecast).image imageWidth = image.size[0] # render the forecast forecastUrl = fullUrlForEndpoint('showForecast', zipOrLatLon, queryParamsDict) stickerCode = render_template("sticker-code.html", forecast=forecast, forecastUrl=forecastUrl, stickerImageUrl=stickerImageUrl, imageWidth=imageWidth) return render_template("stickers.html", forecast=forecast, forecastUrl=forecastUrl, stickerImageUrl=stickerImageUrl, imageWidth=imageWidth, stickerCode=stickerCode) except Exception as ex: return render_template("message.html", title="Error getting forecast", message=ex.args[0], isError=True)
def showForecast(zipOrLatLon): """ :param zipOrLatLon: location to get the forecast for. either a zip code string or a comma-separated list of latitude and longitude strings. ex: '01002' or '42.375370,-72.519249'. URL query parameters: o list=true: shows list format for debugging o four customized weather parameters (p, t, w, and c) -> use them instead of default. there are four, pipe-delimited, one for each parameter: ?p=v1|v2&t=v1|v2|v3|v4&w=v1|v2&c=v1|v2 """ try: # make the rangeDict rangesDictJson = request.cookies.get(RANGES_COOKIE_NAME) rangeDictFromCookie = json.loads( rangesDictJson) if rangesDictJson else None rangeDictFromQuery = rangesDictFromRequestArgs(request.args) if request.args.get('p') \ else None # check for at least one rangeDict = rangeDictFromQuery or rangeDictFromCookie or Forecast.PARAM_RANGE_STEPS_DEFAULT # make the Forecast zipOrLatLonList = zipOrLatLon.split( '|') if '|' in zipOrLatLon else zipOrLatLon forecast = Forecast(Location(zipOrLatLonList), rangeDict) # render the forecast hideIcons = request.cookies.get(HIDE_ICONS_COOKIE_NAME) queryParamsDict = queryParamsDictFromRangeDict(rangeDict) fullUrl = fullUrlForEndpoint('showForecast', zipOrLatLon, queryParamsDict) embedUrl = fullUrlForEndpoint('embedForecast', zipOrLatLon, queryParamsDict) template = "forecast-list.html" if request.args.get( 'list') else "forecast.html" return render_template(template, forecast=forecast, hideIcons=hideIcons, fullUrl=fullUrl, embedUrl=embedUrl, zipOrLatLon=zipOrLatLon) except Exception as ex: return render_template("message.html", title="Error getting forecast", message=ex.args[0], isError=True)