Package: django-giftbox
Descriptiondjango-giftbox is an app for the Django web framework that provides an easy
wrapper for X-Sendfile functionality in Apache.This lets users protect files by not allowing them to be downloaded
directly, but allows Django to programmatically send a redirect and let the
webserver handle the transaction.The current implementation is compatible with Django 1.11+ (tested against LTS
releases including 2.2) and py2/3 compatible.The only required dependency is Django itself andmod_xsendfileinstalled for
Apache.InstallationYou can simply download from PyPi using pip:pip install django-giftboxTo install extrapython-magicfunctionality:pip install django-giftbox[magic]You’ll need to havelibmagic(orlibmagic-devon many Linux distributions)
installed to use this, otherwisepython-magicwill break.Or feel free to clone from themasterbranch:pip install git+https://github.com/bwhicks/django-giftbox.git@master#egg=giftboxThat’s it.ConfigurationThere are two ‘modes’ for giftbox. One of them isdev, and this is the
default when running using the Django development server. Giftbox should auto-detect
this and run accordingly.The other isprod, which assumes you are routing your Django appplication through
a web server like ApacheIn Djangosettings.py, define a dictionary calledGIFTBOX_SETTINGS.
You must definedoc_root, which is the directory
where the files you wish t
Usageo serve via Giftbox are located. This can be a relative
path under your vhost’s doc root or an absolute file path.You can also specify these at run time, but you must least haveGIFTBOX_SETTINGSwith some sane defaults for one of those settings:GIFTBOX_SETTINGS = {
  'type': 'prod',  # will still detect dev server locally
  'doc_root': '/path/to/protected/files',
}A corresponding Apache entry in a Vhost or other configuration would be:XSendFile on
XSendFilePath /path/to/protected/filesThe major advantage of this is that you can block regular access to this path
or leave it out of Apache’s docroot.Optional python-magicIflibmagicandpython-magicare installed, Giftbox will set theContent-Typeheader when passing information to your HTTP server. If you
don’t want this functionality (serving many files quickly or large ones), you can
disable it and your HTTP server’s mime handling will apply:GIFTBOX_SETTINGS = {
  # other settings...
  'use_magic': False,
}In a view or view function, create an instance as follows:from gitfbox import GiftBox

def my_view_func(request):
  box = GiftBox(request)
  return box.send('filename')boxin this case is an instance ofGiftBox, which can have itsself.kwargsdict modified in any way, as well as havingkwargspassed via its constructor.
By default it looks tosettings.pyfor its defaults.box.send()returns an instance ofdjango.httpd.HttpReponse(orFileResponse) with
appropriate headers set andContent-Typecleared so that your web server
can use its own MIME handling to set the type appropriately (unless you use
the optionalpython-magicfunctionality). You can manually
specify this before returning theHttpResponseobject, too.All of this depends on a correct server setup for Apache that
properly creates a protected url that allows sendfile type requests.The object allows flexible settings of virtually every kwarg at any point. If
you need to set thedoc_root`dynamically, either when you
instantiate the box or when you callGiftbox.send(), you can do that.TestsAll tests can be run usingtoxorpython setup.py pytest.