1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from django.conf import settings
8 from django.db import models
9 from django.db.models.fields.files import FieldFile
10 from django import forms
11 from django.utils import simplejson as json
12 from django.utils.translation import ugettext_lazy as _
15 class JSONEncoder(json.JSONEncoder):
16 def default(self, obj):
17 if isinstance(obj, datetime.datetime):
18 return obj.strftime('%Y-%m-%d %H:%M:%S')
19 elif isinstance(obj, datetime.date):
20 return obj.strftime('%Y-%m-%d')
21 elif isinstance(obj, datetime.time):
22 return obj.strftime('%H:%M:%S')
23 return json.JSONEncoder.default(self, obj)
27 return JSONEncoder().encode(data)
31 return json.loads(str, encoding=settings.DEFAULT_CHARSET)
34 class JSONFormField(forms.CharField):
35 widget = forms.Textarea
37 def clean(self, value):
42 raise forms.ValidationError(_('Enter a valid JSON value. Error: %s') % e)
45 class JSONField(models.TextField):
46 def formfield(self, **kwargs):
47 defaults = {'form_class': JSONFormField}
48 defaults.update(kwargs)
49 return super(JSONField, self).formfield(**defaults)
51 def db_type(self, connection):
54 def get_internal_type(self):
57 def contribute_to_class(self, cls, name):
58 super(JSONField, self).contribute_to_class(cls, name)
60 def get_value(model_instance):
61 return loads(getattr(model_instance, self.attname, None))
62 setattr(cls, 'get_%s_value' % self.name, get_value)
64 def set_value(model_instance, json):
65 return setattr(model_instance, self.attname, dumps(json))
66 setattr(cls, 'set_%s_value' % self.name, set_value)
69 class OverwritingFieldFile(FieldFile):
71 Deletes the old file before saving the new one.
74 def save(self, name, content, *args, **kwargs):
75 leave = kwargs.pop('leave', None)
76 # delete if there's a file already and there's a new one coming
77 if not leave and self and (not hasattr(content, 'path') or
78 content.path != self.path):
79 self.delete(save=False)
80 return super(OverwritingFieldFile, self).save(
81 name, content, *args, **kwargs)
84 class OverwritingFileField(models.FileField):
85 attr_class = OverwritingFieldFile
90 from south.modelsinspector import add_introspection_rules
92 add_introspection_rules([], ["^catalogue\.fields\.JSONField"])
93 add_introspection_rules([], ["^catalogue\.fields\.OverwritingFileField"])