modify modelgen and templates to use jinja2 instead of django templates

Change-Id: I309cf353bd2d0f188941a61bb2b75ccb32c5d101
diff --git a/xos/tools/apigen/api.template.py b/xos/tools/apigen/api.template.py
index 9f5123c..ef56885 100644
--- a/xos/tools/apigen/api.template.py
+++ b/xos/tools/apigen/api.template.py
@@ -38,30 +38,30 @@
     return patterns('',
     # legacy - deprecated
         url(r'^xos/$', api_root),
-    {% for object in generator.all %}
-        url(r'xos/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list-legacy'),
-        url(r'xos/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail-legacy'),
+    {% for object in generator.all() %}
+        url(r'xos/{{ object.rest_name() }}/$', {{ object.camel() }}List.as_view(), name='{{ object.singular() }}-list-legacy'),
+        url(r'xos/{{ object.rest_name() }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel() }}Detail.as_view(), name ='{{ object.singular() }}-detail-legacy'),
     {% endfor %}
     ) + patterns('',
     # new - use these instead of the above
         url(r'^api/core/$', api_root),
-    {% for object in generator.all %}
-        url(r'api/core/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list'),
-        url(r'api/core/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail'),
+    {% for object in generator.all() %}
+        url(r'api/core/{{ object.rest_name() }}/$', {{ object.camel() }}List.as_view(), name='{{ object.singular() }}-list'),
+        url(r'api/core/{{ object.rest_name() }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel() }}Detail.as_view(), name ='{{ object.singular() }}-detail'),
     {% endfor %}
     )
 
 @api_view(['GET'])
 def api_root_legacy(request, format=None):
     return Response({
-        {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list-legacy', request=request, format=format),
+        {% for object in generator.all() %}'{{ object.plural() }}': reverse('{{ object }}-list-legacy', request=request, format=format),
         {% endfor %}
     })
 
 @api_view(['GET'])
 def api_root(request, format=None):
     return Response({
-        {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format),
+        {% for object in generator.all() %}'{{ object.plural() }}': reverse('{{ object }}-list', request=request, format=format),
         {% endfor %}
     })
 
@@ -120,13 +120,13 @@
                    newModel = through(**{local_fieldName: obj, remote_fieldName: item})
                    newModel.save()
 
-{% for object in generator.all %}
+{% for object in generator.all() %}
 
-class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
+class {{ object.camel() }}Serializer(serializers.HyperlinkedModelSerializer):
     id = IdField()
     {% for ref in object.refs %}
     {% if ref.multi %}
-    {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
+    {{ ref.plural() }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
     {% else %}
     {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail')
     {% endif %}
@@ -141,16 +141,16 @@
         except:
             return None
     class Meta:
-        model = {{ object.camel }}
-        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
+        model = {{ object.camel() }}
+        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural() }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
 
-class {{ object.camel }}IdSerializer(XOSModelSerializer):
+class {{ object.camel() }}IdSerializer(XOSModelSerializer):
     id = IdField()
     {% for ref in object.refs %}
     {% if ref.multi %}
-    {{ ref.plural }} = serializers.PrimaryKeyRelatedField(many=True,  required=False, queryset = {{ ref.camel }}.objects.all())
+    {{ ref.plural() }} = serializers.PrimaryKeyRelatedField(many=True,  required=False, queryset = {{ ref.camel() }}.objects.all())
     {% else %}
-    {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel }}.objects.all())
+    {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel() }}.objects.all())
     {% endif %}
     {% endfor %}
     humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
@@ -163,28 +163,28 @@
         except:
             return None
     class Meta:
-        model = {{ object.camel }}
-        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
+        model = {{ object.camel() }}
+        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural() }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
 
 
 {% endfor %}
 
 serializerLookUp = {
-{% for object in generator.all %}
-                 {{ object.camel }}: {{ object.camel }}Serializer,
+{% for object in generator.all() %}
+                 {{ object.camel() }}: {{ object.camel() }}Serializer,
 {% endfor %}
                  None: None,
                 }
 
 # Based on core/views/*.py
-{% for object in generator.all %}
+{% for object in generator.all() %}
 
-class {{ object.camel }}List(XOSListCreateAPIView):
-    queryset = {{ object.camel }}.objects.select_related().all()
-    serializer_class = {{ object.camel }}Serializer
-    id_serializer_class = {{ object.camel }}IdSerializer
+class {{ object.camel() }}List(XOSListCreateAPIView):
+    queryset = {{ object.camel() }}.objects.select_related().all()
+    serializer_class = {{ object.camel() }}Serializer
+    id_serializer_class = {{ object.camel() }}IdSerializer
     filter_backends = (filters.DjangoFilterBackend,)
-    filter_fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
+    filter_fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural() }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
 
     def get_serializer_class(self):
         no_hyperlinks=False
@@ -198,13 +198,13 @@
     def get_queryset(self):
         if (not self.request.user.is_authenticated()):
             raise XOSNotAuthenticated()
-        return {{ object.camel }}.select_by_user(self.request.user)
+        return {{ object.camel() }}.select_by_user(self.request.user)
 
 
-class {{ object.camel }}Detail(XOSRetrieveUpdateDestroyAPIView):
-    queryset = {{ object.camel }}.objects.select_related().all()
-    serializer_class = {{ object.camel }}Serializer
-    id_serializer_class = {{ object.camel }}IdSerializer
+class {{ object.camel() }}Detail(XOSRetrieveUpdateDestroyAPIView):
+    queryset = {{ object.camel() }}.objects.select_related().all()
+    serializer_class = {{ object.camel() }}Serializer
+    id_serializer_class = {{ object.camel() }}IdSerializer
 
     def get_serializer_class(self):
         no_hyperlinks=False
@@ -218,7 +218,7 @@
     def get_queryset(self):
         if (not self.request.user.is_authenticated()):
             raise XOSNotAuthenticated()
-        return {{ object.camel }}.select_by_user(self.request.user)
+        return {{ object.camel() }}.select_by_user(self.request.user)
 
     # update() is handled by XOSRetrieveUpdateDestroyAPIView
 
diff --git a/xos/tools/apigen/deps.template.json b/xos/tools/apigen/deps.template.json
index e4164cf..ca35531 100644
--- a/xos/tools/apigen/deps.template.json
+++ b/xos/tools/apigen/deps.template.json
@@ -1,7 +1,7 @@
 {
-{% for o in generator.all %}
-    "{{ o.camel }}": [
-	{% for f in o.refs %}"{{ f.camel }}",
+{% for o in generator.all() %}
+    "{{ o.camel() }}": [
+	{% for f in o.refs %}"{{ f.camel() }}",
 	{% endfor %}]
 {% endfor %}
 }
diff --git a/xos/tools/apigen/dot.template.dot b/xos/tools/apigen/dot.template.dot
index eade386..91ff7f4 100644
--- a/xos/tools/apigen/dot.template.dot
+++ b/xos/tools/apigen/dot.template.dot
@@ -1,7 +1,7 @@
 digraph plstack {
-{% for o in generator.all %}
+{% for o in generator.all() %}
 {% for f in o.refs %}
-  "{{ o.camel }}"->"{{ f.camel }}";
+  "{{ o.camel() }}"->"{{ f.camel() }}";
 {% endfor %}
 {% endfor %}
 }
diff --git a/xos/tools/apigen/fields.template.txt b/xos/tools/apigen/fields.template.txt
index ad5a0b4..e75526d 100644
--- a/xos/tools/apigen/fields.template.txt
+++ b/xos/tools/apigen/fields.template.txt
@@ -1,4 +1,4 @@
-{% for object in generator.all %}
+{% for object in generator.all() %}
 Object {{ object }}:
 Fields:
 {% for field in object.fields %}{{ field.name }}:{{ field.type }}
diff --git a/xos/tools/apigen/hpc-api.template.py b/xos/tools/apigen/hpc-api.template.py
index ed252b8..23d27c2 100644
--- a/xos/tools/apigen/hpc-api.template.py
+++ b/xos/tools/apigen/hpc-api.template.py
@@ -38,29 +38,29 @@
     return patterns('',
         url(r'^hpcapi/$', hpc_api_root_legacy),
     # legacy - deprecated
-    {% for object in generator.rest_models %}
-        url(r'hpcapi/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list-legacy'),
-        url(r'hpcapi/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail-legacy'),
+    {% for object in generator.rest_models() %}
+        url(r'hpcapi/{{ object.rest_name() }}/$', {{ object.camel() }}List.as_view(), name='{{ object.singular() }}-list-legacy'),
+        url(r'hpcapi/{{ object.rest_name() }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel() }}Detail.as_view(), name ='{{ object.singular() }}-detail-legacy'),
     {% endfor %}
     # new api - use these
         url(r'^api/service/hpc/$', hpc_api_root),
-    {% for object in generator.rest_models %}
-        url(r'api/service/hpc/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list'),
-        url(r'api/service/hpc/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail'),
+    {% for object in generator.rest_models() %}
+        url(r'api/service/hpc/{{ object.rest_name() }}/$', {{ object.camel() }}List.as_view(), name='{{ object.singular() }}-list'),
+        url(r'api/service/hpc/{{ object.rest_name() }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel() }}Detail.as_view(), name ='{{ object.singular() }}-detail'),
     {% endfor %}
     )
 
 @api_view(['GET'])
 def hpc_api_root_legacy(request, format=None):
     return Response({
-        {% for object in generator.rest_models %}'{{ object.plural }}': reverse('{{ object }}-list-legacy', request=request, format=format),
+        {% for object in generator.rest_models() %}'{{ object.plural() }}': reverse('{{ object }}-list-legacy', request=request, format=format),
         {% endfor %}
     })
 
 @api_view(['GET'])
 def hpc_api_root(request, format=None):
     return Response({
-        {% for object in generator.rest_models %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format),
+        {% for object in generator.rest_models() %}'{{ object.plural() }}': reverse('{{ object }}-list', request=request, format=format),
         {% endfor %}
     })
 
@@ -118,13 +118,13 @@
                    newModel = through(**{local_fieldName: obj, remote_fieldName: item})
                    newModel.save()
 
-{% for object in generator.rest_models %}
+{% for object in generator.rest_models() %}
 
-class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
+class {{ object.camel() }}Serializer(serializers.HyperlinkedModelSerializer):
     id = IdField()
     {% for ref in object.refs %}
     {% if ref.multi %}
-    {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
+    {{ ref.plural() }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
     {% else %}
     {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail')
     {% endif %}
@@ -139,16 +139,16 @@
         except:
             return None
     class Meta:
-        model = {{ object.camel }}
-        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
+        model = {{ object.camel() }}
+        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural() }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
 
-class {{ object.camel }}IdSerializer(XOSModelSerializer):
+class {{ object.camel() }}IdSerializer(XOSModelSerializer):
     id = IdField()
     {% for ref in object.refs %}
     {% if ref.multi %}
-    {{ ref.plural }} = serializers.PrimaryKeyRelatedField(many=True, required=False, queryset = {{ ref.camel }}.objects.all())
+    {{ ref.plural() }} = serializers.PrimaryKeyRelatedField(many=True, required=False, queryset = {{ ref.camel() }}.objects.all())
     {% else %}
-    {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel }}.objects.all())
+    {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel() }}.objects.all())
     {% endif %}
     {% endfor %}
     humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
@@ -161,28 +161,28 @@
         except:
             return None
     class Meta:
-        model = {{ object.camel }}
-        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
+        model = {{ object.camel() }}
+        fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural() }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
 
 
 {% endfor %}
 
 serializerLookUp = {
-{% for object in generator.rest_models %}
-                 {{ object.camel }}: {{ object.camel }}Serializer,
+{% for object in generator.rest_models() %}
+                 {{ object.camel() }}: {{ object.camel() }}Serializer,
 {% endfor %}
                  None: None,
                 }
 
 # Based on core/views/*.py
-{% for object in generator.rest_models %}
+{% for object in generator.rest_models() %}
 
-class {{ object.camel }}List(XOSListCreateAPIView):
-    queryset = {{ object.camel }}.objects.select_related().all()
-    serializer_class = {{ object.camel }}Serializer
-    id_serializer_class = {{ object.camel }}IdSerializer
+class {{ object.camel() }}List(XOSListCreateAPIView):
+    queryset = {{ object.camel() }}.objects.select_related().all()
+    serializer_class = {{ object.camel() }}Serializer
+    id_serializer_class = {{ object.camel() }}IdSerializer
     filter_backends = (filters.DjangoFilterBackend,)
-    filter_fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
+    filter_fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural() }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
 
     def get_serializer_class(self):
         no_hyperlinks=False
@@ -196,13 +196,13 @@
     def get_queryset(self):
         if (not self.request.user.is_authenticated()):
             raise XOSNotAuthenticated()
-        return {{ object.camel }}.select_by_user(self.request.user)
+        return {{ object.camel() }}.select_by_user(self.request.user)
 
 
-class {{ object.camel }}Detail(XOSRetrieveUpdateDestroyAPIView):
-    queryset = {{ object.camel }}.objects.select_related().all()
-    serializer_class = {{ object.camel }}Serializer
-    id_serializer_class = {{ object.camel }}IdSerializer
+class {{ object.camel() }}Detail(XOSRetrieveUpdateDestroyAPIView):
+    queryset = {{ object.camel() }}.objects.select_related().all()
+    serializer_class = {{ object.camel() }}Serializer
+    id_serializer_class = {{ object.camel() }}IdSerializer
 
     def get_serializer_class(self):
         no_hyperlinks=False
@@ -216,7 +216,7 @@
     def get_queryset(self):
         if (not self.request.user.is_authenticated()):
             raise XOSNotAuthenticated()
-        return {{ object.camel }}.select_by_user(self.request.user)
+        return {{ object.camel() }}.select_by_user(self.request.user)
 
     # update() is handled by XOSRetrieveUpdateDestroyAPIView
 
diff --git a/xos/tools/apigen/modelgen b/xos/tools/apigen/modelgen
index 068b7cb..e5e4232 100755
--- a/xos/tools/apigen/modelgen
+++ b/xos/tools/apigen/modelgen
@@ -6,7 +6,7 @@
 import sys
 import json
 import re
-from django.template import Context, Template
+import jinja2
 from optparse import OptionParser
 
 # Django set up
@@ -260,10 +260,13 @@
 		generator.add_object(m)
 
 	generator.compute_links()
-	template_contents = open(template_name).read()
-	template = Template(template_contents)
-	context = Context({'generator':generator})
-	rendered = template.render(context)
+
+        os_template_loader = jinja2.FileSystemLoader( searchpath=[os.path.split(template_name)[0]])
+        os_template_env = jinja2.Environment(loader=os_template_loader)
+
+        template = os_template_env.get_template(os.path.split(template_name)[1])
+        rendered = template.render({"generator": generator})
+
         lines = rendered.splitlines()
         current_buffer = []
         for l in lines:
diff --git a/xos/tools/apigen/modelyaml.fields.template.txt b/xos/tools/apigen/modelyaml.fields.template.txt
index 69129ed..161cd65 100644
--- a/xos/tools/apigen/modelyaml.fields.template.txt
+++ b/xos/tools/apigen/modelyaml.fields.template.txt
@@ -1,5 +1,5 @@
-{% for object in generator.all %}
-{{ object.camel }}:
+{% for object in generator.all() %}
+{{ object.camel() }}:
 fields:
   {% for f in object.fields %}
   - name: {{ f.name }}
diff --git a/xos/tools/apigen/simple.template.txt b/xos/tools/apigen/simple.template.txt
index fb00670..4dfe494 100644
--- a/xos/tools/apigen/simple.template.txt
+++ b/xos/tools/apigen/simple.template.txt
@@ -1,4 +1,4 @@
-{% for object in generator.all %}
+{% for object in generator.all() %}
 Object {{ object }}:
 Refs:
 {% for ref in object.refs %}
diff --git a/xos/tools/apigen/style.template b/xos/tools/apigen/style.template
index db80f55..2f238c1 100644
--- a/xos/tools/apigen/style.template
+++ b/xos/tools/apigen/style.template
@@ -9,7 +9,7 @@
        # this one is okay
        return False
 
-   {% for model in generator.all %}
+   {% for model in generator.all() %}
        pos = fieldName.find("{{ model }}")
        if (pos > 0) and (fieldName[pos-1] != "_"):
            return True
@@ -34,13 +34,13 @@
        print "m2m field '%s.%s' is missing underscore" % (modelName, fieldName)
 
 def main():
-{% for obj in generator.all %}
-   stylecheck_model_name("{{ obj.camel }}");
+{% for obj in generator.all() %}
+   stylecheck_model_name("{{ obj.camel() }}");
 {% for ref in obj.refs %}
-   stylecheck_field_plural("{{ obj.camel }}", "{{ ref.plural }}");
+   stylecheck_field_plural("{{ obj.camel() }}", "{{ ref.plural }}");
 {% endfor %}
 {% for prop in obj.props %}
-   stylecheck_field_name("{{ obj.camel }}", "{{ prop }}");
+   stylecheck_field_name("{{ obj.camel() }}", "{{ prop }}");
 {% endfor %}
 {% endfor %}
 
diff --git a/xos/tools/apigen/synchronizer.template.txt b/xos/tools/apigen/synchronizer.template.txt
index c784e21..67f3ca1 100644
--- a/xos/tools/apigen/synchronizer.template.txt
+++ b/xos/tools/apigen/synchronizer.template.txt
@@ -1,5 +1,5 @@
-{% for app,files in generator.apps.items %}
-{% for file,models in files.items %}
+{% for app,files in generator.apps.items() %}
+{% for file,models in files.items() %}
 {% for m in models %}
 # This file implements the synchronization of the {{ m.class_name }} model
 # TODO (see below):
@@ -17,7 +17,7 @@
 
 class Sync{{m.camel.title}}(SyncStep):
     # The model synchronized
-    provides=[{{m.class_name.title}}]
+    provides=[{{m.class_name.title()}}]
     # How often do you want this to run. 0 means immediately following changes to the model
     requested_interval=0
     # The model in which changes trigger this module
@@ -55,16 +55,14 @@
   tasks:
   - task_1:
       {% for f in m.fields %}
-      {{f.name}}: {% templatetag openbrace %}{% templatetag openbrace %}{{f.name}}{% templatetag closebrace %}{% templatetag closebrace %}
+      {{f.name}}: {{ '{{' }}{{f.name}}{{ '}}' }}
       {% endfor %}
-      {% verbatim %}
-      {% if delete %}
+      {{ '{%' }} if delete {{ '%}' }}
       state: absent
-      {% else %}
+      {{ '{%' }} else {{ '%}' }}
       state: present
-      {% endif %}
-      {% endif %}
-      {% endverbatim %}
+      {{ '{%' }} endif {{ '%}' }}
+      {{ '{%' }} endif {{ '%}' }}
 +++ Sync{{ m.class_name }}.yaml
 {% endfor %}
 {% endfor %}
diff --git a/xos/tools/apigen/yang.template.txt b/xos/tools/apigen/yang.template.txt
index ad4b1b1..9dc43e1 100644
--- a/xos/tools/apigen/yang.template.txt
+++ b/xos/tools/apigen/yang.template.txt
@@ -1,5 +1,5 @@
-{% for app,files in generator.apps.items %}
-{% for file,m in files.items %}
+{% for app,files in generator.apps.items() %}
+{% for file,m in files.items() %}
 module xos-{{ app }}-{{ file }} {
     namespace "urn:xos:{{app}}.{{ file }}";
     prefix xos-cs;
@@ -12,7 +12,7 @@
     complex-type {{ m.class_name }} {
       {% for f in m.fields %}
 
-      leaf {{ f.name }} { type {{ f.type }}{% ifequal f.type "ForeignKey" %} { ct:instance-type {{f.related.model.class_name}};{% if f.null%}{%else%}require-instance true{% endif %}{% endifequal %};{% if f.max_length %} { length {{ f.max_length }};{% endif %}{% if None %}default "{{ f.default }}";{% endif %}}
+      leaf {{ f.name }} { type {{ f.type }}{% if f.type=="ForeignKey" %} { ct:instance-type {{f.related.model.class_name}};{% if f.null%}{%else%}require-instance true{% endif %}{% endif %};{% if f.max_length %} { length {{ f.max_length }};{% endif %}{% if None %}default "{{ f.default }}";{% endif %}}
       {% endfor %}
     }
 }