Skip to content

Python Src

Siqi Tian edited this page Feb 5, 2017 · 8 revisions

src/

Contains python scripts for the app.

src/admin.py

Handles admin related page requests and tasks.

Function Description
class Defines admin page view (list and detail), layout and field grouping. Seehere.
apache(), aws(), ga(), git() Handles page requests of /admin/apache/, /admin/aws/, /admin/ga/, /admin/git/.
get_dash() Retrieves analytics data for requests /admin/dash/(apache
get_stat() Retrieves statistics for requests /admin/stat/(ver
backup_form() Get backup related status from cron jobs.
admin_cmd() Kicks off call_command() for management commands from requests /admin/cmd/(upload
refresh_stat() Kicks off call_command() for management command from requests /admin/stat/(sys
versions;
backup statistics by calling get_backup_stat() from src/console.py.
backup() Handles page requests of /admin/backup/ page. Also takes POST request to set configurations.
dir(), man(), ref(), doc() Handles page requests of /admin/dir/, /admin/man/, /admin/ref/ and /admin/doc/ for django-filemanager and documentation pages.

src/api.py

Handles API job calls and GitHub webhooks.

Function Description
git_hook() Handles GitHub webhooks POST requests at /api/git_hook/. The SHA-1 digest of HMAC hash of request.body is compared to HTTP_X_HUB_SIGNATURE field from request.META for a match. Then kicks off the call_command() for the management command dist. CSRF token is disabled.
submit(), result() Allows CORS (cross-origin) requests for job submission and result retrieval. Sets Access-Control-Allow-Origin header for response. CSRF token is disabled.

src/console.py

Handles admin analytics console/dashboard data requests and error emails.

Function Description
send_notify_emails() Uses smtp email to send messages to admin about error reports. Used by cron jobs since the Django send_mail() function fails randomly.
get_backup_stat() Reads sizes of folders in data/ and sizes of files in backup/, and a list of TGZ files stored in Google Drive. Text information is saved to cache/stat_backup.json.
refresh_settings() Reloads config/*.conf files into current running environment. It writes to settings._wrapped to overwrite current Django app setting.
get_sys_crontab(), set_sys_crontab() GET/SET cron jobs. GET reads from system crontab -l instead of config/cron.conf to reflect the current setting.
get_backup_form(), set_backup_form() GET/SET backup settings. GET reads from system get_sys_crontab(); SET writes to config/cron.conf.
restyle_apache() Parses Apache status page (/server-status/) into a JSON object. Uses urllib2 for credential supply to /server-status/.
aws_stats() Handles /admin/aws/ data query. Uses gviz-api.py and boto. Calls aws_result() through aws_call() for data formatting. qs=init returns JSON, while others return Google Visualization ToJSONResponse() object. Query format:
qs : Query string; accepts init (table of basic specs on top of page), latency, request, 23xx (HTTP response 2xx and 3xx combined), 45xx (HTTP response 4xx and 5xx combined), host, status, cpu, credit, and volbytes.
sp : Period; accepts 7d (7 days) or 48h (48 hours).
tqx : Required by Google Visualization API. It should has the value like reqId:%d.
ga_stats() Handles /admin/ga/ data query. Uses gviz-api.py. Uses requests instead of Google Analytics OAuth API for simplicity. qs=init returns JSON, while others return Google Visualization ToJSONResponse() object. Makes 3 attempts if connection fails. Query format:
qs : Query string; accepts init (table of number ratings), chart (session plots), pie (categorical pie plots), and geo (world map of visitors).
sp : Period or type; qs=chart accepts 24h, 7d, 1m (1 month), or 3m (3 months), while qs=pie accepts session, user, browser, or pageview.
tqx : Same as in aws_stats().
git_stats() Handles /admin/git/ data query. Uses gviz-api.py and PyGithub. qs=init returns JSON, while others return Google Visualization ToJSONResponse() object. Makes 3 attempts if connection fails. Query format:
qs : Query string; accepts init (table of contributors data), num (repository statistics, e.g. number of branches), c (commits calendar), ad (addition/deletion timeline), and au (author contribution pie plot).
tqx : Same as in aws_stats().
dash_ssl() Uses openssl to retrieve server SSL Certificate expiration date.

src/env.py

Handles *.conf files and HTML template path.

Function Description
SYS_PATH Collection of HTML template path and data folder path. It inherits from Singleton.
reload_conf() Loads config/*.conf files and save as runtime environment variables for the Django app.
error400(), error401(), error403(), error404(), error500(), error503() Handlers for custom HTTP error response pages. Use ?status=false to suppress the HTTP error code (returns 200 instead).

src/helper.py, src/helper_form.py, src/helper_html.py

Collection of helper functions for src/wrapper_?d.py.

Function Description
prm_1d, prm_2d, prm_3d primerize constructor instances. They are constructed once and shared by src/wrapper_?d.py.
random_job_id() Generates random JOB_ID as 16-digit hexadecimal string. It prevents collision by querying all previous JOB_ID on record.
save_result_data(), save_plate_json() Saves TXT and/or ZIP results to files; saves JSON plate layouts.
form_data_clean_*() Cleans up and prepares form data for design_?d_run() jobs.
form_check_valid_*() Checks if form is valid and returns an API error code.
is_valid_name(), is_valid_email(), is_t7_present() Test functions for inputs.
create_HTML_page_*() Writes to HTML result files in data/.
HTML_elem_*() Generates HTML code for page elements (Primer suffix, header, time elapsed, and what's next).
HTML_comp_*() Generates HTML code for page components (Assembly, primer list, warnings, T7 check, illustration, and plates).

src/models.py

Handles MySQL database table schema and in-page form models.

Function Description
models.Model Database table schema. See here; contains Design1D, Design2D, Design3D, JobIDs, JobGroups, HistoryItem, and SourceDownloader.
forms.Form Front-end in-page forms. See here.
Admin site : BackupForm.
Public site : Design1DForm, Design2DForm, Design3DForm, and DownloadForm.
Login : LoginForm, PasswordForm.
debug_flag(), ga_tracker() Global context processors to convey DEBUG flag and Google Analytics tracker ID.
js_ver() Global context processors for JavaScript vendor library versions. The versions information is used to load libraries from CDN.

src/settings.py

Handles basic app settings (defined by Django). See here. Loading order is to avoid import loop.

  • X_FRAME_OPTIONS is not set to DENY due to /admin/dir/ uses <iframe> of /admin/browse/.

src/urls.py

Handles url routing (defined by Django). Uses AdminSitePlus to override Admin. See django-adminplus here.

  • Routes /site_media/ to media/, and /site_data/ to data/; to prevent direct access to files from path.
  • Redirect old and synonym urls to current ones with 301. Exposes custom error handler pages, as well as robots.txt as /robots.txt.
  • Based on IS_MAINTENANCE in config/t47_dev.py, the server can run in either normal mode or downtime mode. In downtime mode, all requests are responded with HTTP code 503.

src/user.py

Handles requests for user-related activity. Each handler is routed from src/urls.py, and takes PATH.HTML_PATH to locate HTML templates.

Function Description
user_login(), user_password(), user_logout() Pages related to account. In production, the only user in system is daslab.
browse() django-filemanger view handler, channeled as <iframe> on /admin/dir/.

src/views.py

Handles requests for non-admin pages. Each handler is routed from src/urls.py, and mostly takes PATH.HTML_PATH to locate HTML templates.

Function Description
index(), tutorial(), protocol(), docs(), about() Page handlers for corresponding URLs. Some pass additional information for page rendering or dynamic contents.
license() Handler for /license/ page. Reads in dist/Primerize-LICENSE.md for display.
download() Handler for /download/ page and form. It displays all available releases of primerize based on information from cache/stat_dist.json. For valid registration, it writes to a SourceDownloader entry and passes the demographic information back, which is useful for link().
link() Handler for direct primerize code download responses. This is a mechanism for preventing direct access/exposure of dist/Primerize-?.?.?.zip files. Requests are validated against SourceDownloader records, and the ZIP file is returned only when a match is found.
result() Handler for result retrieval. It looks up the JOB_ID in JobIDs for a match, and responses with a page for the corresponding Design?D type. Input values of the found JOB_ID record is populated to the form as initial values. When ?json=true, it passes to result_json() and returns JSON response.
result_json() Return a JSON response of a given JOB_ID. This is used by the AJAX calls for retrieving job information.
ping_test() Returns an empty 200 response. Used by AWS ELB Health Check.
test() Test handler for test server. Not accessible in production.
get_staff() Returns admin email and current user; Used by 403 error page (/error/403/).

src/wsgi.py

Handles uwsgi connections. Do not change, unless syntax changes in newer Django releases. Called by Apache mod_wsgi dispatcher.