mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-27 10:22:17 +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 RIGS.models import RevisionMixin
|
||||||
|
|
||||||
|
from django.template import Context,Template
|
||||||
|
|
||||||
@reversion.register
|
@reversion.register
|
||||||
class Type(models.Model, RevisionMixin):
|
class Type(models.Model, RevisionMixin):
|
||||||
@@ -73,6 +74,28 @@ class Form(models.Model, RevisionMixin):
|
|||||||
|
|
||||||
data = models.TextField(blank=False, null=False, default="{}")
|
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):
|
def clean(self):
|
||||||
try:
|
try:
|
||||||
jsonData = json.loads(self.data)
|
jsonData = json.loads(self.data)
|
||||||
@@ -89,7 +112,6 @@ class Form(models.Model, RevisionMixin):
|
|||||||
except jsonschema.ValidationError as e: #raise a django exception
|
except jsonschema.ValidationError as e: #raise a django exception
|
||||||
raise ValidationError('Data is not valid, cannot save: '+e.message)
|
raise ValidationError('Data is not valid, cannot save: '+e.message)
|
||||||
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""Call :meth:`full_clean` before saving."""
|
"""Call :meth:`full_clean` before saving."""
|
||||||
self.full_clean()
|
self.full_clean()
|
||||||
|
|||||||
@@ -66,9 +66,9 @@
|
|||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
angular.module('myModule', ['schemaForm']).controller('FormController', function($scope) {
|
angular.module('myModule', ['schemaForm']).controller('FormController', function($scope) {
|
||||||
{% autoescape off %} {# otherwise quotes get replaced with HTML entities #}
|
{% 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 %}
|
{% 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
|
//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
|
Expects kwarg "type_pk" to contain PK of required type
|
||||||
"""
|
"""
|
||||||
def dispatch(self, *args, **kwargs):
|
def dispatch(self, *args, **kwargs):
|
||||||
|
|
||||||
schemaType = get_object_or_404(models.Type, pk=kwargs['type_pk'])
|
schemaType = get_object_or_404(models.Type, pk=kwargs['type_pk'])
|
||||||
currentSchema = models.Schema.objects.current_schema(schemaType)
|
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)
|
return super(FormCreate, self).dispatch(*args, **kwargs)
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(FormCreate, self).get_context_data()
|
context = super(FormCreate, self).get_context_data()
|
||||||
|
|
||||||
context["object"] = {
|
context["object"] = self.initial_object
|
||||||
"schema": self.schema,
|
|
||||||
"event": self.event,
|
|
||||||
"data": "{}"
|
|
||||||
}
|
|
||||||
context["edit"] = True
|
context["edit"] = True
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
self.object = form.save(commit=False)
|
self.object = self.initial_object
|
||||||
self.object.event = self.event
|
|
||||||
self.object.schema = self.schema
|
self.object.data = form.save(commit=False).data
|
||||||
|
|
||||||
self.object.save()
|
self.object.save()
|
||||||
return HttpResponseRedirect(self.get_success_url())
|
return HttpResponseRedirect(self.get_success_url())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user