add fields

This commit is contained in:
j 2010-01-23 10:06:33 +11:00
parent 619ec79b9c
commit fe6de58583
4 changed files with 101 additions and 1 deletions

47
oxdjango/fields.py Normal file
View 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)

View file

@ -2,6 +2,9 @@
# vi:si:et:sw=4:sts=4:ts=4
from django.http import HttpResponse
try:
import simplejson
except ImportError:
from django.utils import simplejson
from django.conf import settings

41
oxdjango/utils.py Normal file
View 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
View 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))