Skip to content

ibuttimer/weather-zone

Repository files navigation

Weather-Zone

Weather-Zone is a weather forecast application utilising multiple third-party weather forecasters, to provide address-based weather forecasts.

Development and Local Deployment

Environment

The development environment requires:

Artifact Download and installation instructions
Node.js https://nodejs.org/en/download/
npm Included with Node.js installation
git https://git-scm.com/downloads
Python https://www.python.org/downloads/
Poetry https://python-poetry.org/docs/#installation
Django https://www.djangoproject.com/download/

Setup

Clone Repository

In an appropriate folder, run the following commands:

> git clone https://github.com/ibuttimer/weather-zone.git
> cd weather-zone

Alternatively, most IDEs provide an option to create a project from Version Control.

Virtual Environment

It is recommended that a virtual environment be used for development purposes. There are numerous options available; e.g. poetry, venv, pyenv etc.

Please see Switching between environments.

Environment Setup

In a terminal window, in the weather-zone folder, run the following command to setup required environment artifacts:

> npm install

Python Setup

In the weather-zone folder, run the following command to install the necessary python packages:

> poetry install
Table 1: Configuration settings
Key Value
ENV_FILE If using an environment file, specifies the file to use. Defaults to .env in the project root folder.
PORT Port application is served on; default 8000
DEBUG A boolean that turns on/off debug mode; see Boolean environment variables
SECRET_KEY Secret key for a particular Django installation. See Secret Key Generation
DATABASE_URL Database url
REMOTE_DATABASE_URL Url of remote PostgreSQL database resource.
Note: Only required for admin purposes, see database configuration under Cloud-based Deployment
FONTAWESOME_URL Fontawesome kit url. See Use a Kit
REQUESTS_TIMEOUT Requests timeout in seconds
FORECAST_PROVIDERS List of forecast providers to use, see FORECAST_PROVIDERS environment variable.
LOCATIONFORECAST_<provider id> Individual forecast provider configuration settings, see LOCATIONFORECAST_<provider id> environment variables.
DEFAULT_SEND_EMAIL Email address to send emails from. Only valid when development mode is enabled, in production mode emails are sent from EMAIL_HOST_USER
EMAIL_HOST SMTP server to send email. Only valid when production mode is enabled.
EMAIL_USE_TLS Use Transport Layer Security (TLS) flag; see Boolean environment variables, default true. Only valid when production mode is enabled.
EMAIL_PORT SMTP server port. Only valid when production mode is enabled.
EMAIL_HOST_USER Email user account to send email. Only valid when production mode is enabled.
EMAIL_HOST_PASSWORD Email user account password. Only valid when production mode is enabled.
STORAGE_PROVIDER Storage provider; set to s3 to use an Amazon Web Service S3 bucket for storage or default to use Django's default storage
GOOGLE_API_KEY Google Geocoding API key
MAXMIND_GEOIP_ACCOUNT MaxMind GeoIP2 Web Services Account ID
MAXMIND_GEOIP_KEY MaxMind GeoIP2 Web Services License key
EXTERNAL_HOSTNAME Hostname of application on hosting service.
Note: To specify multiple hosts, use a comma-separated list with no spaces.
Note: Set to localhost,127.0.0.1 in local development mode
Amazon S3-specific
AWS_ACCESS_KEY_ID The access key for your AWS account.
AWS_SECRET_ACCESS_KEY The secret key for your AWS account.
AWS_STORAGE_BUCKET_NAME AWS S3 bucket name.
Development-specific configuration
CACHED_GEOCODE_RESULT Cached Google Geocoding response to use; e.g. '[{"address_components": [{"long_name": "50", ... }]'
CACHED_MET_EIREANN_RESULT Path relative to project root of forecast response to use; e.g. 'dev/data/met_eireann/cached_resp.xml'

Boolean environment variables

Set environment variables evaluating a boolean value, should be set to any of true, on, ok, y, yes or 1 to set true, otherwise the variable is evaluated as false.

FORECAST_PROVIDERS environment variable

The locationforecast application may be configured with multiple providers which utilise the Locationforecast weather forecast API developed by Met Norway.

The FORECAST_PROVIDERS environment variable is a comma-separated list of providers to use, of the form <provider app name>_<provider id>.

The convention is to name the provider class in the form <provider id>Provider where <provider id> is camel-cased, e.g. met_eireann becomes MetEireannProvider. And the module name should be <provider id>.py e.g. met_eireann.py.

E.g. the following configures two providers; met_eireann and met_norway_classic, which will load the MetEireannProvider and MetNorwayClassicProvider classes respectively.

FORECAST_PROVIDERS=locationforecast_met_eireann,locationforecast_met_norway_classic

See Locationforecast for more details on the providers.

LOCATIONFORECAST_<provider id> environment variables

The configuration for each provider in the FORECAST_PROVIDERS environment variable must be provided by an environment variable of the following format:

  • Environment variable name

    LOCATIONFORECAST_<provider id>; where <provider id> is the id of the provider, e.g. met_eireann.

  • Environment variable value

    A semicolon-seperated list of key-value pairs as outlined in the following table.

Key Description
name Display name of provider
url Provider website url.
data_url API url. See Using unsafe characters in URLs
latitude Latitude query parameter name, e.g. latitude=lat
longitude Longitude query parameter name, e.g. longitude=long
from From date/time query parameter name
to To date/time query parameter name
tz A valid IANA timezone; e.g. tz=Europe/Dublin
country ISO 3166-1 alpha-2 country code for which provider provides forecasts; e.g. country=IE

E.g. the following configure the met_eireann provider;

LOCATIONFORECAST_MET_EIREANN="name=Met Éireann;url=http://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast;latitude=lat;longitude=long;from=from;to=to;tz=UTC;country=IE"

Note: Keys not required by a particular provider should be omitted.

See Met Éireann Timezone anomaly for more details on the tz setting.

Environment variables

Set environment variables corresponding to the keys in Table 1: Configuration settings.

Secret Key Generation

A convenient method of generating a secret key is to run the following command and copy its output.

$ python -c "import secrets; print(secrets.token_urlsafe())"

Email

Procedures differ depending on the email provider used. for example, in order to configure Gmail as the email provider, the following actions must be performed.

  • Login to Gmail
  • Goto the Google Account settings and select Security
  • Under How you sign in to Google ensure 2-Step Verification is enabled
  • Search for App passwords and create a new app password
  • Copy the generated app password, and store securely

Before first run

Before running the application for the first time following cloning from the repository and setting up a new database, the following steps must be performed, from a terminal window, in the weather-zone folder.

Initialise the database

$ python manage.py migrate

Create a superuser

Enter Username, Password and optionally Email address.

$ python manage.py createsuperuser

Configure authentication

From django-allauth Post-Installation

  • Add a Site for your domain in the database
    • Login to http://&lt;domain&gt;/admin/sites/site/ as the previously created superuser, e.g. http://127.0.0.1:8000/admin/sites/site/

    • Add a Site for your domain (django.contrib.sites app).

      E.g.

      Domain name Display name
      127.0.0.1:8000 my domain

      Note: The id (primary key) of the site must be added to the application configuration. See SITE_ID in Table 1: Configuration settings.

Run server

In order to run the development server, run the following command from the weather-zone folder:

$ python manage.py runserver

By default, the server runs on port 8000 on the IP address 127.0.0.1. See runserver for details on passing an IP address and port number explicitly.

Application structure

The application structure is as follows:

├─ README.md            - this file
├─ docs                 - documentation
├─ data                 - application data
├─ locale               - translation files
├─ manage.py            - application entry point
├─ weather_zone         - main Django application
├─ addresses            - address application
├─ base                 - base application
├─ broker               - service broker application
├─ forecast             - application generating forecast data from provider data  
├─ locationforecast     - Locationforecast provider application
├─ user                 - user application
├─ utils                - utility functions
├─ weather_warning      - weather warning application
├─ static               - static files
└─ templates            - application templates

Cloud-based Deployment

Render

The site was deployed on Render.

Deployment

The following steps were followed to deploy the website:

  • Login to Render in a browser

  • From the dashboard select New -> Web Service

  • Connect to the git repository

  • Set following

    • Name, (e.g. weather-zone)
    • Choose an appropriate region
    • Select the git branch to deploy
    • Select Python 3 runtime,
    • Set the Build command to ./build.sh
    • Set the Start command to gunicorn weather-zone.wsgi:application
    • Select Create Web Service
  • To provision the application with a database, such as an ElephantSQL database.

  • Create an Amazon S3 bucket using Storing Django Static and Media Files on Amazon S3. See Configuring cross-origin resource sharing (CORS) and CORS configuration regarding setting the CORS configuration for the S3 bucket.

  • Under Environment -> Environment Variables add the following environment variables

    Key Value
    PYTHON_VERSION Python version
    Note: At time of writing the Render Python3 environment does not include the necessary Python headers to compile the psycopg2 library for Python version 3.10.10, use Python version 3.9.16
    POETRY_VERSION Poetry version
    • Under Environment -> Secret Files add a file with the name .env with the same environment variables as specified in Table 1: Configuration settings with the following differences:
      • The following variables are NOT required
        • Development-specific configuration
      • Add the following variables:
        • DATABASE_URL
      • Add Amazon S3-specific settings

See Table 1: Configuration settings for details.

If any other settings vary from the defaults outlined in Table 1: Configuration settings they must be added as well.

  • Select the Manual Deploy to deploy the application.

  • Initialise the database and Create a superuser

    Involves the same procedure as outlined in Initialise the database and Create a superuser but may be run from the local machine.

    • From a Development and Local Deployment

      • Initialise the database

        $ python manage.py migrate --database=remote
      • Create a superuser

        Enter Username, Password and optionally Email address.

        $ python manage.py createsuperuser --database=remote

      Note: Ensure to specify the --database=remote option to apply the change to the database specified by the REMOTE_DATABASE_URL environment variable.

  • Configure authentication

    Follow the same procedure as outlined in Configure authentication using the Heroku domain as <domain>, e.g. weather-zone.onrender.com

The live website is available at https://weather-zone.onrender.com

Credits

The following resources were used to build the website.

Content

Code

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published