Merge branch 'master' into nanterre-recette

This commit is contained in:
Thomas NOËL 2018-01-24 10:59:10 +01:00
commit a949da298f
3 changed files with 25 additions and 20 deletions

View File

@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib import admin
from django.forms import ModelForm
from rangefilter.filter import DateTimeRangeFilter
@ -22,22 +23,29 @@ from .models import Transaction, Entity, Relation, Log, Job
from .widgets import JSONEditor
class JSONEditorForm(ModelForm):
def __init__(self, *args, **kwargs):
super(JSONEditorForm, self).__init__(*args, **kwargs)
if self.instance and hasattr(self.instance, 'schema'):
for key in self.fields:
if key == 'content':
self.fields[key].widget = JSONEditor(schema=self.instance.schema.schema)
class JSONEditorMixin(object):
json_fields = ()
form = JSONEditorForm
def get_form(self, request, obj=None, **kwargs):
widgets = kwargs.setdefault('widgets', {})
for field in self.json_fields:
widgets[field] = JSONEditor()
if obj:
schema = obj.schema.schema
else:
schema = {}
widgets[field] = JSONEditor(schema=schema)
return super(JSONEditorMixin, self).get_form(request, obj=obj, **kwargs)
def get_formset(self, request, obj=None, **kwargs):
widgets = kwargs.setdefault('widgets', {})
for field in self.json_fields:
widgets[field] = JSONEditor()
return super(JSONEditorMixin, self).get_formset(request, obj=obj, **kwargs)
class LeftRelationInlineAdmin(JSONEditorMixin, admin.TabularInline):
fk_name = 'left'

View File

@ -28,11 +28,8 @@ class JSONEditor(forms.Textarea):
def render(self, name, value, attrs=None):
default_schema = {
'type': 'object',
'additionalProperties': {
'type': 'string'
}
'additionalProperties': True,
}
default_schema = {}
attrs['style'] = 'display: none'
s = super(JSONEditor, self).render(name, value, attrs=attrs)
s += mark_safe(
@ -46,11 +43,10 @@ class JSONEditor(forms.Textarea):
{
theme: "foundation",
schema: schema,
disable_properties: true,
show_errors: "always",
});
input = document.getElementById("%(id)s");
content = document.getElementById("%(id)s").value;
var input = document.getElementById("%(id)s");
var content = document.getElementById("%(id)s").value;
jsoneditor.on('change', function () {
input.value = JSON.stringify(jsoneditor.getValue());
})
@ -59,7 +55,7 @@ class JSONEditor(forms.Textarea):
}
})();
</script>''' % {
'schema': json.dumps(self.schema or default_schema),
'schema': json.dumps(self.schema or default_schema),
'id': attrs['id'],
})
return s

View File

@ -42,13 +42,14 @@ class QF(object):
})
except requests.RequestException as e:
return None, u'Implicit connexion impossible: %r' % e
if response.status_code != 200:
return None, u'Implicit réponse non 200: %s %r' % (
response.status_code, response.content[:1024])
try:
data = response.json()
except ValueError as e:
return None, u'Implicit contenu non JSON: %s %r' % (e, response.content[:1024])
return None, u'Implicit contenu non JSON: %s %r' % (response.status_code, response.content[:1024])
if response.status_code != 200:
return None, data
return data, None
def lire_quotients_valides(self, date_de_reference):