Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Samuel Dev Candidate Submission #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions RUNTIME_INSTRUCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# WeatherAlert - Integrating Alertus Solutions With Weather Data
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

## Description
WeatherAlert is a simple web application that utilizes the Alertus Solutions and forecast data based on geographical location. The website will display a table holding different forecasts and wether or not they have generated an alert. The alert is stored in a sqlite3 database with a unique ID.

## Installation
##### Note: this project assumes a default python version of 3 or later. It has not been tested on any 2.x distribution.

1. We're using a tool called [virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/install.html) to manage our work environment. To start it simply type ```workon weatheralert```.

![Virtual environment setup](https://github.com/snaraj/weatheralert/blob/master/assets/images/virtual_env_setup.png?raw=true)

2. After our virtual environment has been set up, we can install our project dependencies. Run ```pip3 install -r requirements.txt``` while on the root of the project.

3. Since we are using Flask, we need to set up a few environment variables so we can operate out of the command line without issues.

![Flask setup](https://github.com/snaraj/weatheralert/blob/master/assets/images/flask_setup.png?raw=true)

## Usage

1. To start our local server, we type ```flask run``` on the root folder. It should look like this.
![Flask run](https://github.com/snaraj/weatheralert/blob/master/assets/images/flask_run.png?raw=true)

2. Once loaded, you will be greeted with two different components; a settings, and a data table component.
![Settings](https://github.com/snaraj/weatheralert/blob/master/assets/images/settings.png?raw=true)

![Data](https://github.com/snaraj/weatheralert/blob/master/assets/images/data.png?raw=true)

3. You can interact with the webpage by providing different (valid) coordinates as well as changing the threshold value. The Threshold Value is important since it determines wether the Alertus Solutions generates an alert or not. The screenshot below shows how it would look like after updating the website with a Threshold Value of 37.

![Data Updated](https://github.com/snaraj/weatheralert/blob/master/assets/images/data_updated.png?raw=true)

## Credits & Miscellanea
- Alertus: https://www.alertus.com/
- NOAA: https://www.weather.gov/documentation/services-web-api

- List of libraries: See requirements.txt

- Challenges:
- Being able to update the data table dynamically based on the new settings passed by the user.
- Updating Jinja2 template dynamically using JavaScript.
- Moving data through different API's while conserving integrity.
- Authenticating our API usage.

- Highlights:
- Was able to solve all of the problems above.
- I was able to figure out many things that I had never implemented before.
- App works as expected, described by the documentation posted.
- Overall this project was an amazing learning experience.
Binary file added assets/.DS_Store
Binary file not shown.
Binary file added assets/images/.DS_Store
Binary file not shown.
Binary file added assets/images/data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/data_updated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/flask_run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/flask_setup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/virtual_env_setup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[build-system]
requires = ['setuptools>=40.8.0', 'wheel']
build-backend = 'setuptools.build_meta:__legacy__'

[tools.pytest.ini_options]
addopts = '--cov=weatheralert'
testpaths = [
'test',
]

[tool.mypy]
mypy_path = 'weatheralert'
check_untyped_defs = true
disallow_any_generics = true
ignore_missing_imports = true
no_implicit_optional = true
show_error_codes = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
no_implicit_reexport = true
34 changes: 34 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
attrs==21.4.0
certifi==2021.10.8
charset-normalizer==2.0.10
click==8.0.3
distlib==0.3.4
filelock==3.4.2
flake8==4.0.1
Flask==2.0.2
idna==3.3
iniconfig==1.1.1
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
mccabe==0.6.1
mypy==0.931
mypy-extensions==0.4.3
packaging==21.3
platformdirs==2.4.1
pluggy==1.0.0
py==1.11.0
pycodestyle==2.8.0
pydantic==1.9.0
pyflakes==2.4.0
pyparsing==3.0.6
pytest==6.2.5
requests==2.27.1
six==1.16.0
toml==0.10.2
tomli==2.0.0
tox==3.24.5
typing_extensions==4.0.1
urllib3==1.26.8
virtualenv==20.13.0
Werkzeug==2.0.2
37 changes: 37 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[metadata]
name = weatheralert
author = Samuel Naranjo
description = Alertus Python Developer Candidate.
classifiers =
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10

project_urls =
Source = https://github.com/snaraj/weatheralert

[options]
python_requires = >=3.8
packages = find:
install_requires =
Flask >= 2.0.2
Jinja2 >= 3.0.3
zip_safe = no

[options.extras_require]
testing =
pytest>=6.2.5
pytest-cov>=2.0
mypy>=0.931
flak8>=4.0.1
tox>=3.24.5

[options.package_data]
weatheralert = py.typed

[flake8]
exclude = build,.git,.tox,./tests/.env
ignore = W504,W601
max-line-length = 119
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from setuptools import setup

if __name__ == "__main__":
setup()
Binary file added weatheralert.db
Binary file not shown.
81 changes: 81 additions & 0 deletions weatheralert/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Flask stuff:
instance/
.webassets-cache

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# mkdocs documentation
/site

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

node_modules
.DS_Store
.cache

**/__pycache__
.vscode/
35 changes: 35 additions & 0 deletions weatheralert/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

from flask import Flask


def create_app(test_config=None):
# creates the flask instance and configures the app
app = Flask("weatheralert", instance_relative_config=True)
# setting default configuration
app.config.from_mapping(
# to be made more secure doing deployment
SECRET_KEY="dev",
# location and name for the database
DATABASE=os.path.join(app.instance_path, "weatheralert.sqlite"),
)

if test_config is None:
# load our custom config file if it exists, store SECRET_KEY here
app.config.from_pyfile("config.py", silent=True)

else:
# load test_config otherwise
app.config.from_mapping(test_config)

# ensures the existence of the instance folder
try:
os.makedirs(app.instance_path)
except OSError:
pass

from . import weatheralert

app.register_blueprint(weatheralert.bp)

return app
32 changes: 32 additions & 0 deletions weatheralert/alertus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import requests
import json

from base64 import b64encode


url = "https://demo.alertustech.com/alertusmw/services/rest/activation/preset/"


# launches a Alertus preset alert with a given preset id, basic auth and a custom message
def activate_alert(alert_message: str):
res = requests.post(
url,
headers={"Content-Type": "application/json", "Authorization": authorization()},
data=json.dumps(
{
"sender": "Dev Candidate - Samuel",
"presetId": 2207,
"text": alert_message,
}
),
)

id = int(res.content.decode("utf-8"))

return id


# basic authorization using username:password with encode and decode
def authorization() -> str:
auth_token = b64encode(("devcandidate:gooWmJQe").encode()).decode("ascii")
return f"Basic {auth_token}"
Loading