Skip to content

Store python source code in a database field. Validates syntax of source code on saving to the database.

Notifications You must be signed in to change notification settings

ahmedelfateh/django-python-code-field

 
 

Repository files navigation

Python Source Code Field

Build Status

If you need to store python source code in a field in a django model, this is the app you need.

Obviously, you should not be accepting any old user data if you will be executing it, but for my use case (admin users only, needed complex data rules that varied between models), python code was the best choice.

So, you use it by:

* ``$ python setup.py install``

* add ``python_field`` to your ``settings.INSTALLED_APPS`` (optional, but
  required if you want syntax highlighting).

* Use as follows in your model::

    from django.db import models
    from python_field.fields import PythonCodeField
    
    class MyModel(models.Model):
        ....
        source = PythonCodeField(blank=True, null=True)
        
        ....

The text will be compiled using the inbuild python compile() function, and errors will be shown in the form errors field.

CodeMirror and Syntax Highlighting

This package uses parts of the CodeMirror package for syntax highlighting, and for replacing the textarea with an editable iframe.

Parts of the CodeMirror package that are not required have not been included, and the package has been minified. [This is not the case just yet.]

The original license file is included for your reference.

CodeMirror can be found at http://codemirror.net/

Development

Tests require the Python development headers to be installed, which you can install on Ubuntu with:

sudo apt-get install python-dev python3-dev python3.4-dev

To run unittests across multiple Python versions, install:

sudo apt-get install python3.4-minimal python3.4-dev python3.5-minimal python3.5-dev

To run all tests:

export TESTNAME=; tox

To run tests for a specific environment (e.g. Python 2.7 with Django 1.4):

export TESTNAME=; tox -e py27-django15

To run a specific test:

export TESTNAME=.testTimezone2; tox -e py27-django15

To run the documentation server locally:

mkdocs serve -a :9999

To deploy documentation, run:

mkdocs gh-deploy --clean

To build and deploy a versioned package to PyPI, verify all unittests are passing, and then run:

python setup.py sdist
python setup.py sdist upload

About

Store python source code in a database field. Validates syntax of source code on saving to the database.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 95.0%
  • Python 4.3%
  • Other 0.7%