/ISAC-SIMO-Django-Backend

Build Change - Intelligent Supervision Assistant for Construction - Sistema Inteligente de Monitoreo de Obra. Back end for managing the machine learning model pipeline.

Primary LanguageJavaScriptApache License 2.0Apache-2.0

ISAC-SIMO

License Slack Python 3.9 CII Best Practices

Pages Build and Deploymeny Website

Intelligent Supervision Assistant for Construction - Sistema Inteligente de Monitoreo de Obra

View documentation at docs.isac-simo.net

Django Backend Repository


Bash Script:

Make sure you have installed Python 3.9.0

To manage multiple Python installation use pyenv or pyenv-win (for windows). Then, pipenv should be able to automatically install required python.

pip install --upgrade pip
pip install pipenv
pipenv install --python 3.9
pipenv run install-client

For CentOS and other cloud servers that only support psycopg2-binary, use this instead:

pipenv run install-server

Setup Postgresql or any desired Django supported database.

Create .env file using .env.example and provide environment variables as required.

database_settings.py needs to be updated as per your database setup (or use DATABASE_URL in .env file)

pipenv run python manage.py migrate
pipenv run python manage.py createsuperuser

Running Tests:

pipenv run python manage.py test --debug-mode --debug-sql --parallel --buffer

OR simply run without any flags:

pipenv run python manage.py test

Running Flake8 Static Analysis:

flake8

How to Bump Application Version?

Click to view

We use SemVer scheme to manage the version number. We have created a Django Command to upgrade the version in the settings file.

Command Usage:

  • pipenv run python manage.py bump → Updates to Next Patch
  • pipenv run python manage.py bump --to=2.2.2 → Updates Version Number to 2.2.2
  • pipenv run python manage.py bump --type=major → Updates to Next Major
  • pipenv run python manage.py bump --type=minor → Updates to Next Minor
  • pipenv run python manage.py bump --type=patch → Updates to Next Patch

Releases and Tags can then be created accordingly.

Details for Pythonanywhere:

Click to view

Useful .bashrc Alias for the project if hosted in Pythonanywhere:

Click to view
alias toenv="cd /home/{{username}}/isac && pipenv shell"

alias server.log="cd /var/log && tail -f {{username}}.pythonanywhere.com.server.log"
alias error.log="cd /var/log && tail -f {{username}}.pythonanywhere.com.error.log"
alias access.log="cd /var/log && tail -f {{username}}.pythonanywhere.com.access.log"

alias server.up="toenv && sed -i 's/MAINTENANCE=True/MAINTENANCE=False/g' .env && touch /var/www/{{username}}_pythonanywhere_com_wsgi.py"
alias server.down="toenv && sed -i 's/MAINTENANCE=False/MAINTENANCE=True/g' .env && touch /var/www/{{username}}_pythonanywhere_com_wsgi.py"

alias reload="touch /var/www/{{username}}_pythonanywhere_com_wsgi.py"

Pythonanywhere wsgi.py:

Click to view
import os
import sys
from dotenv import load_dotenv

project_home = u'/home/{{username}}/isac'
if project_home not in sys.path:
    sys.path.insert(0, project_home)

load_dotenv(os.path.join(project_home, '.env'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'isac_simo.settings'

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())

Static Files:

Click to view
URL Directory
/static/ /home/{{username}}/isac/static
/media/ /home/{{username}}/isac/media

If Static Files that does not exist e.g. https://example.com/static/bad-directory keeps throwing unhandled error use this temporary fix:

Click to view

Inside env/lib/python3.7/site-packages/django/core/handlers/base.py find get_response without leading underscore and change it to as below:

from django.shortcuts import render

def get_response(self, request):
    """Return an HttpResponse object for the given HttpRequest."""
    # Setup default url resolver for this thread
    set_urlconf(settings.ROOT_URLCONF)
    try:
        response = self._middleware_chain(request)

        response._closable_objects.append(request)

        # If the exception handler returns a TemplateResponse that has not
        # been rendered, force it to be rendered.
        if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
            response = response.render()

        if response.status_code == 404:
            logger.warning(
                'Not Found: %s', request.path,
                extra={'status_code': 404, 'request': request},
            )

        return response
    except:
        return render(request, '404.html', status=404)

Note:

Contributing

Please read our contributing guidelines for details of how you can get involved and please abide by the Code of Conduct.

Contributors

License

This project is licensed under the Apache Software License, Version 2, unless otherwise stated. Separate third party code objects invoked within this project are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

A Call for Code with The Linux Foundation

Developed By: Build Change

Supported By: IBM