From fe6de585835da7d2b388e24b6637fbbe7e1c13c4 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 23 Jan 2010 10:06:33 +1100 Subject: [PATCH] add fields --- oxdjango/fields.py | 47 +++++++++++++++++++++++++++++++++++++++++++ oxdjango/shortcuts.py | 5 ++++- oxdjango/utils.py | 41 +++++++++++++++++++++++++++++++++++++ oxdjango/widgets.py | 9 +++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 oxdjango/fields.py create mode 100644 oxdjango/utils.py create mode 100644 oxdjango/widgets.py diff --git a/oxdjango/fields.py b/oxdjango/fields.py new file mode 100644 index 0000000..e556ecc --- /dev/null +++ b/oxdjango/fields.py @@ -0,0 +1,47 @@ +from django.db import models +from django.utils import simplejson as json + + +class DictField(models.TextField): + """DictField is a textfield that contains JSON-serialized dictionaries.""" + + # Used so to_python() is called + __metaclass__ = models.SubfieldBase + + def to_python(self, value): + """Convert our string value to python after we load it from the DB""" + if isinstance(value, dict): + return value + + value = json.loads(value) + assert isinstance(value, dict) + return value + + def get_db_prep_save(self, value): + """Convert our JSON object to a string before we save""" + assert isinstance(value, dict) + value = json.dumps(value) + return super(DictField, self).get_db_prep_save(value) + + +class TupleField(models.TextField): + """TupleField is a textfield that contains JSON-serialized tuples.""" + + # Used so to_python() is called + __metaclass__ = models.SubfieldBase + + def to_python(self, value): + """Convert our string value to JSON after we load it from the DB""" + if isinstance(value, tuple): + return value + + value = json.loads(value) + assert isinstance(value, list) + return tuple(value) + + def get_db_prep_save(self, value): + """Convert our JSON object to a string before we save""" + assert isinstance(value, tuple) + value = json.dumps(value) + return super(TupleField, self).get_db_prep_save(value) + diff --git a/oxdjango/shortcuts.py b/oxdjango/shortcuts.py index 4505b53..1c6913f 100644 --- a/oxdjango/shortcuts.py +++ b/oxdjango/shortcuts.py @@ -2,7 +2,10 @@ # vi:si:et:sw=4:sts=4:ts=4 from django.http import HttpResponse -from django.utils import simplejson +try: + import simplejson +except ImportError: + from django.utils import simplejson from django.conf import settings def render_to_json_response(dictionary, content_type="text/json"): diff --git a/oxdjango/utils.py b/oxdjango/utils.py new file mode 100644 index 0000000..876ac94 --- /dev/null +++ b/oxdjango/utils.py @@ -0,0 +1,41 @@ +from django.http import HttpResponse,Http404 +from django.core.servers.basehttp import FileWrapper +from django.conf import settings + +import mimetypes +import os + +def basic_sendfile(fname,download_name=None): + if not os.path.exists(fname): + raise Http404 + + wrapper = FileWrapper(open(fname,"r")) + + content_type = mimetypes.guess_type(fname)[0] + response = HttpResponse(wrapper, content_type=content_type) + response['Content-Length'] = os.path.getsize(fname) + + if download_name: + response['Content-Disposition'] = "attachment; filename=%s"%download_name + + return response + +def x_sendfile(fname,download_name=None): + if not os.path.exists(fname): + raise Http404 + + content_type = mimetypes.guess_type(fname)[0] + response = HttpResponse('', content_type=content_type) + response['Content-Length'] = os.path.getsize(fname) + response['X-Sendfile'] = fname + + if download_name: + response['Content-Disposition'] = "attachment; filename=%s"%download_name + + return response + +if getattr(settings,'SENDFILE',False) == 'x_sendfile': + sendfile = x_sendfile +else: + sendfile = basic_sendfile + diff --git a/oxdjango/widgets.py b/oxdjango/widgets.py new file mode 100644 index 0000000..b22e57e --- /dev/null +++ b/oxdjango/widgets.py @@ -0,0 +1,9 @@ +import django.newforms as forms +from string import Template +from django.utils.safestring import mark_safe + +class FirefoggWidget(forms.FileInput): + def render(self, name, value, attrs=None): + tpl = Template(u"""