add fields
This commit is contained in:
parent
619ec79b9c
commit
fe6de58583
4 changed files with 101 additions and 1 deletions
47
oxdjango/fields.py
Normal file
47
oxdjango/fields.py
Normal file
|
@ -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)
|
||||||
|
|
|
@ -2,7 +2,10 @@
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
|
||||||
from django.http import HttpResponse
|
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
|
from django.conf import settings
|
||||||
|
|
||||||
def render_to_json_response(dictionary, content_type="text/json"):
|
def render_to_json_response(dictionary, content_type="text/json"):
|
||||||
|
|
41
oxdjango/utils.py
Normal file
41
oxdjango/utils.py
Normal file
|
@ -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
|
||||||
|
|
9
oxdjango/widgets.py
Normal file
9
oxdjango/widgets.py
Normal file
|
@ -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"""<h1>This should be a Firefogg widget for $name, current value: $value</h1>""")
|
||||||
|
return mark_safe(tpl.substitute(name=name, value=value))
|
||||||
|
|
Loading…
Reference in a new issue