VernonCo / ps_utils

Promostandards utilities -- Public repository for accessing PS services
1 stars 0 forks source link

PS Utilities -- A JSON REST API for Promo Standard Services

(including forms for manual entry)

While PO's may be entered manually in a form eventually, I'm assuming that automated processes will be the main usage, and only have it working for a json POST (no manual form) - see exampleSimplePO.json The json POST is validated by schema, field names, and their value types before sending a soap request. See the 'Instructions for PO' under the 'Info' tab on the nav bar.

The PO also only works with the vernonco docker image (vernonco/ps-utils:dev) as it has a fix for suds-py3 included (Dockerfile line 24) - Waiting for fix to module - see suds-py3 issue #41. If you build the image your self, you must add the following lines to 171,172 in site-packages/suds/xsd/sxbase.py for it to correctly parse "ref:..." in the WSDLs if self.ref and self.ref in self.schema.elements.keys(): ns = self.ref

clone repository

git clone https://github.com/VernonCo/ps_utils.git

Run locally or with docker-compose

To run locally and/or use with vscode

Requires python 3.7 +

first create venv python3 -m venv venv

activate venv source venv/bin/activate to deactivate: deactivate

install requirements into virtualenv pip install --upgrade pip pip install -r requirements.txt

run locally on vscode using F5 and view at http://localhost:5000 or in terminal using venv: python run.py to setup the database to test or run on production, it needs to be accessible to the local run

to run docker container with docker-compose

docker-compose [-f dev-docker-compose.yml] up -d

if using weavenet to provide secure WAN to databases etc. docker-compose $(weave config) [-f dev-docker-compose.yml] up -d

docker-compose files have the option of running the db as a container or comment out db and adminer services and set variables in for remote db access...make sure of persistent volume for production if running in k8 or similar

If you run gunicorn on production, make sure to set the following on the docker-coompose.yml

need the following to run production as gunicorn with multiple workers. This prevents a race condition of multiple works trying to set up permissions.

create admin user to view additional tabs in web page

in other words, this needs ran to access the utilities.

local

export FLASK_APP="run:app"
flask fab create-admin

on container

docker exec -it ps_utils[_dev] bash
export FLASK_APP="run:app"
flask fab create-admin

Add available views to users

Make the app available to operators by either putting on limited network or adding users

Behind limited network

The config.py (or config-4passwords.py) have configuration to allow public access to the forms. However, the config has been commented out as it was not working as expected. Therefore, for public role use the security model below.

If desiring granular control over access, comment out the FAB_ROLES, and use the security model.

Open network

Don't forget to change SECRET_KEY in config.py or config-4passwords.py

View user registration and roles at flask_appbuilder security. The flask_appbuilder security also shows how to change the authentication to use LDAP and create roles in line with your AD groups.

if using vscode...set interpeter to the one with the path to the venv

ctl+shft+p
python:select interpeter

add pylint in venv terminal

pip install pylint

set path on vscode to run file locally (will need local database connection in config file)

Click Debug -> Open current configurations. Add following to end of configurations if not one created for 'Python: Flask'

        ,{
            "name": "Flask",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "program": "${workspaceFolder}/ps_utils/vs_debug.py",
            "envFile": "absolute_path_to/.env",
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }

add to ../.env to keep envirnoment variables out of your code

FLASK_APP=ps_utils/app
DB_AUTH=youruser
DB_PASS=yourpassword
DB_HOST=yourhost
DB_PORT=yourhostport
SERVER_PATH=/path to/ps_utils

CONFIG_FILE='config'   # or config-4passwords to move existing credentials
#for passwords move
OLD_DB_AUTH=youruser
OLD_DB_PASS=yourpassword
OLD_DB_HOST=yourhost
OLD_DB_PORT=yourhostport

Soap requests conditional sequence

Test Coverage

Currently, test coverage is very basic...adding as I have the time or get pull requests

To run tests. cd app; python tests/run_tests.py