Search code examples
pythondjangopackaging

How to package a Django app to be test-friendly?


I've been working on this side project that's effectively a replacement for Django's default FileField and ImageField classes. More of a thin wrapper really, it lets you change this:

    attachment = models.FileField(upload_to="attachments")
    image = models.ImageField(
        upload_to="images",
        width_field="image_width",
        height_field="image_height"
    )

into this:

    attachment = EncryptedFileField(upload_to="attachments")
    image = EncryptedImageField(
        upload_to="images",
        width_field="image_width",
        height_field="image_height"
    )

and magically Encrypt All The Things.

The thing is, while it works well so far, and I have tests for it, packaging is still troublesome.

Specifically:

  • I don't know how to run the tests outside of the demo. You need to cd into demo and run ./manage.py test and there has to be a symlink from that directory to ../django_encrypted_filefield. This can't be the "right" way to do this... right?
  • I think I've got setup.py working as I can do pip install -e git+... and things work just fine, but I don't think the demo should be included, right? But if I exclude it, what about the tests?
  • Ideally, I'd like to setup tox to do the usual pep8 & unit test run, but I don't know how to do that for a project that's Django-dependent.

TL;DR: Can someone point me to a simple django module that does tests & packaging "right"?


Solution

  • I don't know how to run the tests outside of the demo. You need to cd into demo and run ./manage.py test and there has to be a symlink from that directory to ../django_encrypted_filefield. This can't be the "right" way to do this... right?

    You could move the contents of demo/demo to demo, and move demo/manage.py to the base folder.

    Now you can simply run ./manage.py test in the base folder

    Ideally, I'd like to setup tox to do the usual pep8 & unit test run, but I don't know how to do that for a project that's Django-dependent.

    Maybe use a Makefile where you call make test, and run ./manage.py tests automatically. Same for pep8

    TL;DR: Can someone point me to a simple django module that does tests & packaging "right"?

    I know it's possible to have an external app and use it in your own django project, which is what you're going for if I'm correct. You can simply include an app (like yours) in your project by including it in your INSTALLED_APPS in settings.py.

    Here's an example. (django-leaflet) Another example specifically about the tests.

    I'm not sure about the packaging though.