mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 05:22:16 +00:00
Added template tags functionality to schema/layout (to allow specifying default values based on event)
This commit is contained in:
@@ -10,6 +10,7 @@ import datetime
|
||||
|
||||
from RIGS.models import RevisionMixin
|
||||
|
||||
from django.template import Context,Template
|
||||
|
||||
@reversion.register
|
||||
class Type(models.Model, RevisionMixin):
|
||||
@@ -73,6 +74,28 @@ class Form(models.Model, RevisionMixin):
|
||||
|
||||
data = models.TextField(blank=False, null=False, default="{}")
|
||||
|
||||
@property
|
||||
def renderedSchema(self):
|
||||
template = Template(self.schema.schema)
|
||||
|
||||
context = Context({
|
||||
"event": self.event, # allow stuff to be auto-filled
|
||||
"SfCurrentIndex": "{{ $index +1 }}" # Convenience for schemaform array index
|
||||
})
|
||||
|
||||
return template.render(context)
|
||||
|
||||
@property
|
||||
def renderedLayout(self):
|
||||
template = Template(self.schema.layout)
|
||||
|
||||
context = Context({
|
||||
"event": self.event, # allow stuff to be auto-filled
|
||||
"SfCurrentIndex": "{{ $index +1 }}" # Convenience for schemaform array index
|
||||
})
|
||||
|
||||
return template.render(context)
|
||||
|
||||
def clean(self):
|
||||
try:
|
||||
jsonData = json.loads(self.data)
|
||||
@@ -89,7 +112,6 @@ class Form(models.Model, RevisionMixin):
|
||||
except jsonschema.ValidationError as e: #raise a django exception
|
||||
raise ValidationError('Data is not valid, cannot save: '+e.message)
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Call :meth:`full_clean` before saving."""
|
||||
self.full_clean()
|
||||
|
||||
@@ -66,9 +66,9 @@
|
||||
$(document).ready(function () {
|
||||
angular.module('myModule', ['schemaForm']).controller('FormController', function($scope) {
|
||||
{% autoescape off %} {# otherwise quotes get replaced with HTML entities #}
|
||||
$scope.schema = {{ object.schema.schema }};
|
||||
$scope.schema = {{ object.renderedSchema }};
|
||||
|
||||
$scope.form = {{ object.schema.layout }};
|
||||
$scope.form = {{ object.renderedLayout }};
|
||||
|
||||
{% comment %}
|
||||
//This is useful for development, allowing the schema & layout to reside in the file system (you need to make the relevant files in the templates directory). Commented out for production
|
||||
|
||||
@@ -30,31 +30,29 @@ class FormCreate(generic.CreateView):
|
||||
Expects kwarg "type_pk" to contain PK of required type
|
||||
"""
|
||||
def dispatch(self, *args, **kwargs):
|
||||
|
||||
schemaType = get_object_or_404(models.Type, pk=kwargs['type_pk'])
|
||||
currentSchema = models.Schema.objects.current_schema(schemaType)
|
||||
|
||||
self.schema = currentSchema
|
||||
event = get_object_or_404(RIGS.models.Event, pk=kwargs['event_pk'])
|
||||
|
||||
self.event = get_object_or_404(RIGS.models.Event, pk=kwargs['event_pk'])
|
||||
self.initial_object = models.Form(event=event, schema=currentSchema)
|
||||
|
||||
return super(FormCreate, self).dispatch(*args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(FormCreate, self).get_context_data()
|
||||
|
||||
context["object"] = {
|
||||
"schema": self.schema,
|
||||
"event": self.event,
|
||||
"data": "{}"
|
||||
}
|
||||
context["object"] = self.initial_object
|
||||
context["edit"] = True
|
||||
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
self.object = form.save(commit=False)
|
||||
self.object.event = self.event
|
||||
self.object.schema = self.schema
|
||||
self.object = self.initial_object
|
||||
|
||||
self.object.data = form.save(commit=False).data
|
||||
|
||||
self.object.save()
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user