Skip to content

Commit

Permalink
Merge pull request #12 from Brown-University-Library/dj4p2
Browse files Browse the repository at this point in the history
merges django 4.2 and other upgrades into main branch.
  • Loading branch information
birkin authored Nov 22, 2024
2 parents 10acca0 + 96662c3 commit 65132d2
Show file tree
Hide file tree
Showing 20 changed files with 1,189 additions and 907 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pyc
__pycache__
.DS_Store
.hg
.svn
env
*.pyc
env
94 changes: 2 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
[![BUL Code Best-Practices](https://library.brown.edu/good_code/project_image/site-checker/)](https://library.brown.edu/good_code/project_info/site-checker/)

##### Overview
## Info

On this page...
- Info
- Installation
- Usage
- Other

---


##### Info

This is a [django](https://www.djangoproject.com) webapp that offers a publicly-viewable webpage of the current status of the [Brown University Library's](https://library.brown.edu) checked-services, and an admin interface to add a service to be checked.
This is a [django](https://www.djangoproject.com) webapp that offers a publicly-viewable [webpage](https://library.brown.edu/sitechecker/) of the current status of the [Brown University Library's](https://library.brown.edu) checked-services, and an admin interface to add a service to be checked.

The admin-view allows you to set up:
- a url to be checked
Expand All @@ -31,81 +19,3 @@ If a failure is detected, the check frequency will reset to check again a couple
An email doesn't go out on that first failure in case it's just some weird temporary blip. And once the site is back up, a 'back-up' email goes out, and the check-frequency returns to the normal user-specified frequency.

---


##### Installation

- create a 'site_checker_stuff' directory, and cd into it

- in that create a virtual environment

$ /path/to/python3 -m venv ./env_chkr

- git clone this project

$ git clone https://github.com/birkin/site_checker_project.git ./site_checker_project

- cd into the project

$ cd ./site_checker_project

- activate the virtual environment and load it up

$ source ../env_chkr/bin/activate
$ (env_chkr) pip install -r ./config/requirements.txt

- create a `sitechecker_env_settings.sh` file somewhere where it will not get accidentally checked-in to version-control

- at the bottom of the `env_chkr/bin/activate` file, add

export SITECHKR__SETTINGS_PATH="/path/to/sitechecker_env_settings.sh"
source $SITECHKR__SETTINGS_PATH

- begin running `python3 ./manage.py check`; the errors will tell you what you need to add to the sitechecker_env_settings.sh file
- add those settings to the sitechecker_env_settings.sh file in the form of:

export SITECHKR__KEY="value"

- note that each time you add an entry to the `sitechecker_env_settings.sh` file, you'll need to source the virtual environment again

TODO: add a default/sample `sitechecker_env_settings.sh` file

- after you've created a db, and configured the settings, set it up via

$ python ./manage.py makemigrations
$ python ./manage.py migrate

TODO: add a default/sample sqlite3.db

- set yourself up as a superuser

$ python ./manage.py createsuperuser

- start the app on your local devbox

$ python ./manage.py runserver

You'll then be able to access it in a browser via `http://127.0.0.1:8000/`

---


##### Usage

- add an entry via the admin

- run the cron-script.py file (see instructions in its docstring)

---


##### Other

- contact: [email protected]

- TODO
- add a default/sample `sitechecker_env_settings.sh` file
- add a default/sample sqlite3.db
- enable the login/shib module for effortless entry into the admin

---
Binary file removed config/mysqlclient-2.0.3-cp38-cp38-linux_x86_64.whl
Binary file not shown.
44 changes: 8 additions & 36 deletions config/passenger_wsgi.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,23 @@
# -*- coding: utf-8 -*-

"""
WSGI config.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

"""
Note: no need to activate the virtual-environment here for passenger.
- the project's httpd/passenger.conf section allows specification of the python-path via `PassengerPython`, which auto-activates it.
- the auto-activation provides access to modules, but not, automatically, env-vars.
- passenger env-vars loading under python3.x is enabled via the `SenEnv` entry in the project's httpd/passenger.conf section.
- usage: `SetEnv SITECHKR__SETTINGS_PATH /path/to/SITECHKR__env_settings.sh`
- `SenEnv` requires apache env_module; info: <https://www.phusionpassenger.com/library/indepth/environment_variables.html>,
enabled by default on macOS 10.12.4, and our dev and production servers.
For activating the virtual-environment manually, don't source the settings file directly. Instead, add to `project_env/bin/activate`:
export SITECHKR__SETTINGS_PATH="/path/to/SITECHKR__env_settings.sh"
source $SITECHKR__SETTINGS_PATH
This allows not only the sourcing, but also creates the env-var used below by shellvars.
"""
import os
import pathlib
import sys

import os, pprint, sys
import shellvars
from django.core.wsgi import get_wsgi_application

PROJECT_DIR_PATH = pathlib.Path(__file__).resolve().parent.parent
# print( f'PROJECT_DIR_PATH, ``{PROJECT_DIR_PATH}``' )

# print( 'the initial env, ```{}```'.format( pprint.pformat(dict(os.environ)) ) )

PROJECT_DIR_PATH = os.path.dirname( os.path.dirname(os.path.abspath(__file__)) )
ENV_SETTINGS_FILE = os.environ['SITECHKR__SETTINGS_PATH'] # set in `httpd/passenger.conf`, and `env/bin/activate`

## update path
sys.path.append( PROJECT_DIR_PATH )

## reference django settings
os.environ[u'DJANGO_SETTINGS_MODULE'] = 'config.settings' # so django can access its settings

## load up env vars
var_dct = shellvars.get_vars( ENV_SETTINGS_FILE )
for ( key, val ) in var_dct.items():
os.environ[key.decode('utf-8')] = val.decode('utf-8')
sys.path.append(str(PROJECT_DIR_PATH))

# print( 'the final env, ```{}```'.format( pprint.pformat(dict(os.environ)) ) )
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings' # so django can access its settings

## gogogo
application = get_wsgi_application()
32 changes: 32 additions & 0 deletions config/requirements_base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file was autogenerated by uv via the following command:
# uv pip compile ./config/requirements_base.in --output-file ./config/requirements_base.txt --universal --python 3.8
asgiref==3.8.1
# via django
backports-zoneinfo==0.2.1 ; python_full_version < '3.9'
# via django
certifi==2024.8.30
# via requests
charset-normalizer==3.4.0
# via requests
django==4.2.16
# via -r ./config/requirements_base.in
idna==3.10
# via requests
pip==24.3.1
# via -r ./config/requirements_base.in
python-dotenv==1.0.1
# via -r ./config/requirements_base.in
requests==2.32.3
# via -r ./config/requirements_base.in
sqlparse==0.5.2
# via django
typing-extensions==4.12.2 ; python_full_version < '3.11'
# via asgiref
tzdata==2024.2 ; sys_platform == 'win32'
# via django
urllib3==1.26.20
# via
# -r ./config/requirements_base.in
# requests
uv==0.5.4
# via -r ./config/requirements_base.in
2 changes: 2 additions & 0 deletions config/requirements_server.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r requirements_base.txt
mysqlclient~=2.1.0
50 changes: 50 additions & 0 deletions config/requirements_server.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This file was autogenerated by uv via the following command:
# uv pip compile ./config/requirements_server.in --output-file ./config/requirements_server.txt --universal --python 3.8
asgiref==3.8.1
# via
# -r ./config/requirements_base.txt
# django
backports-zoneinfo==0.2.1 ; python_full_version < '3.9'
# via
# -r ./config/requirements_base.txt
# django
certifi==2024.8.30
# via
# -r ./config/requirements_base.txt
# requests
charset-normalizer==3.4.0
# via
# -r ./config/requirements_base.txt
# requests
django==4.2.16
# via -r ./config/requirements_base.txt
idna==3.10
# via
# -r ./config/requirements_base.txt
# requests
mysqlclient==2.1.1
# via -r ./config/requirements_server.in
pip==24.3.1
# via -r ./config/requirements_base.txt
python-dotenv==1.0.1
# via -r ./config/requirements_base.txt
requests==2.32.3
# via -r ./config/requirements_base.txt
sqlparse==0.5.2
# via
# -r ./config/requirements_base.txt
# django
typing-extensions==4.12.2 ; python_full_version < '3.11'
# via
# -r ./config/requirements_base.txt
# asgiref
tzdata==2024.2 ; sys_platform == 'win32'
# via
# -r ./config/requirements_base.txt
# django
urllib3==1.26.20
# via
# -r ./config/requirements_base.txt
# requests
uv==0.5.4
# via -r ./config/requirements_base.txt
60 changes: 36 additions & 24 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import json, logging, os
import json
import logging
import os
import pathlib

from dotenv import find_dotenv, load_dotenv

## load envars ------------------------------------------------------
dotenv_path = pathlib.Path(__file__).resolve().parent.parent.parent / '.env'
assert dotenv_path.exists(), f'file does not exist, ``{dotenv_path}``'
load_dotenv(find_dotenv(str(dotenv_path), raise_error_if_not_found=True), override=True)


## set settings -----------------------------------------------------
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -25,11 +36,11 @@
SECRET_KEY = os.environ['SITECHKR__SECRET_KEY']

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = json.loads( os.environ['SITECHKR__DEBUG_JSON'] ) # will be True or False
DEBUG = json.loads(os.environ['SITECHKR__DEBUG_JSON']) # will be True or False

ADMINS = json.loads( os.environ['SITECHKR__ADMINS_JSON'] )
ADMINS = json.loads(os.environ['SITECHKR__ADMINS_JSON'])

ALLOWED_HOSTS = json.loads( os.environ['SITECHKR__ALLOWED_HOSTS'] ) # list
ALLOWED_HOSTS = json.loads(os.environ['SITECHKR__ALLOWED_HOSTS']) # list


# Application definition
Expand Down Expand Up @@ -57,7 +68,7 @@

ROOT_URLCONF = 'config.urls'

template_dirs = json.loads( os.environ['SITECHKR__TEMPLATES_JSON'] )
template_dirs = json.loads(os.environ['SITECHKR__TEMPLATES_JSON'])
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand All @@ -77,10 +88,16 @@
WSGI_APPLICATION = 'config.passenger_wsgi.application'


# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = json.loads( os.environ['SITECHKR__DATABASES_JSON'] )
DATABASES = json.loads(os.environ['SITECHKR__DATABASES_JSON'])


# Password validation
Expand Down Expand Up @@ -111,7 +128,6 @@

USE_I18N = True

USE_L10N = True

USE_TZ = True

Expand All @@ -126,7 +142,7 @@
# Email
SERVER_EMAIL = '[email protected]'
EMAIL_HOST = os.environ['SITECHKR__EMAIL_HOST']
EMAIL_PORT = int( os.environ['SITECHKR__EMAIL_PORT'] )
EMAIL_PORT = int(os.environ['SITECHKR__EMAIL_PORT'])


# sessions
Expand All @@ -142,45 +158,41 @@
## disable module loggers
# existing_logger_names = logging.getLogger().manager.loggerDict.keys()
# print '- EXISTING_LOGGER_NAMES, `%s`' % existing_logger_names
logging.getLogger('requests').setLevel( logging.WARNING )
logging.getLogger('requests').setLevel(logging.WARNING)

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': "[%(asctime)s] %(levelname)s [%(module)s-%(funcName)s()::%(lineno)d] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"
'format': '[%(asctime)s] %(levelname)s [%(module)s-%(funcName)s()::%(lineno)d] %(message)s',
'datefmt': '%d/%b/%Y %H:%M:%S',
},
},
'handlers': {
'logfile': {
'level':'DEBUG',
'class':'logging.FileHandler', # note: configure server to use system's log-rotate to avoid permissions issues
'filename': os.environ.get(u'SITECHKR__LOG_PATH'),
'level': 'DEBUG',
'class': 'logging.FileHandler', # note: configure server to use system's log-rotate to avoid permissions issues
'filename': os.environ.get('SITECHKR__LOG_PATH'),
'formatter': 'standard',
},
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'standard'
},
'console': {'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard'},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
}
},
},
'loggers': {
'site_checker_app': {
'handlers': ['logfile', 'console'],
'level': os.environ.get(u'SITECHKR__LOG_LEVEL'),
'propagate': False
'level': os.environ.get('SITECHKR__LOG_LEVEL'),
'propagate': False,
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
}
}
},
},
}
Loading

0 comments on commit 65132d2

Please sign in to comment.