blob: ceb173c41060633554da94d0b55aeae2b6783c1c [file] [log] [blame]
Sapan Bhatia7148c082017-06-16 15:04:11 -07001{% if options.legacy =='"True"' -%}
2{% set legacy_tag = '_decl' %}
3{% set legacy = True %}
4from core.models.xosbase import *
Sapan Bhatiae596ea92017-10-09 11:55:06 -04005from core.models import ServiceInstance
6from django.db import models
7from django.db.models import *
8from core.models.xosbase import StrippedCharField
Sapan Bhatia7148c082017-06-16 15:04:11 -07009{% else %}
10{% set legacy = False %}
11{% set legacy_tag = '' %}
Sapan Bhatiae596ea92017-10-09 11:55:06 -040012{% if file_exists('header.py') -%}from header import *{% endif %}
Sapan Bhatia7148c082017-06-16 15:04:11 -070013{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +020014
15{% for m in proto.messages %}
16{% if file_exists(m.name|lower+'_header.py') -%}from {{m.name|lower }}_header import *{% endif %}
17{% if file_exists(m.name|lower+'_top.py') -%}{{ include_file(m.name|lower+'_top.py') }} {% endif %}
18
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020019{%- for l in m.links -%}{% set peer_name=l.peer.name %}
Sapan Bhatia00274172017-07-20 09:05:46 -040020
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020021{% if peer_name not in proto.message_names -%}
22from core.models import {{ peer_name }}
Sapan Bhatia504cc972017-04-27 01:56:28 +020023{%- endif -%}
24{%- endfor -%}
25{%- for b in m.bases -%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020026{%- if b.name!='XOSBase' and 'Mixin' not in b.name %}
Sapan Bhatia00274172017-07-20 09:05:46 -040027{% if b.name not in proto.message_names %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020028from core.models import {{ b.name }}
Sapan Bhatia00274172017-07-20 09:05:46 -040029{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +020030{%- endif -%}
31{% endfor %}
32
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040033{% for policy,error in xproto_validations(m.options) %}
34{{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }}
35{% endfor %}
36
Sapan Bhatia504cc972017-04-27 01:56:28 +020037{% endfor %}
38
39{% for m in proto.messages %}
Sapan Bhatiaa3d2e622017-07-31 22:25:55 -040040class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases, legacy_tag, proto.message_names) }}:
Sapan Bhatia2941a052017-07-10 15:10:03 -040041 plural_name = "{{ xproto_pluralize(m) }}"
Sapan Bhatia504cc972017-04-27 01:56:28 +020042
43 KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }}
44
45 class Meta:
46 app_label = {{ xproto_first_non_empty([m.options.app_label, options.app_label, options.name, "Set an app label in your xproto!"]) | lower}}
47 # name = {{ xproto_first_non_empty([m.options.name, options.name, "Set a name in your xproto!"]) }}
Matteo Scandoloe425f9d2017-08-15 15:56:19 -070048 verbose_name = "{{ xproto_unquote(xproto_first_non_empty([m.options.verbose_name, m.name])) }}"
Sapan Bhatia504cc972017-04-27 01:56:28 +020049
50 # Primitive Fields (Not Relations)
51 {% for f in m.fields %}
52 {%- if not f.link -%}
53 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
54 {% endif %}
55 {%- endfor %}
56
57 # Relations
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020058 {% for l in m.links %}{% set peer_name=l.peer.name %}
Sapan Bhatia7148c082017-06-16 15:04:11 -070059 {% if legacy and peer_name in proto.message_names %}{% set peer_tag = legacy_tag %}{% else %}{% set peer_tag = '' %}{% endif -%}
60 {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }}{{ peer_tag }} {%- endif -%}, {{ xproto_django_options_str(l, l.dst_port ) }} )
Sapan Bhatia504cc972017-04-27 01:56:28 +020061 {%- endfor %}
62
63 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
64 pass
65
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040066 # Generated methods
67 def save(self, *args, **kwds):
Sapan Bhatia113c2b92017-07-25 08:41:58 -040068 if not self.leaf_model_name:
69 self.leaf_model_name = "{{ m.name }}"
70
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040071 {% for policy,error in xproto_validations(m.options) %}
72 policy_{{policy}}_validator(self, None)
73 {% endfor %}
Sapan Bhatia0d6990a2017-09-20 06:42:38 -070074
75 try:
76 base_save_in_attic = self.__xos_save_base(*args, **kwds)
77 except AttributeError:
78 base_save_in_attic = False
79
80 if not base_save_in_attic:
81 super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds)
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040082
83 def can_access(self, ctx):
84 {% if m.policy %}
85 verdict = security.{{m.policy}}_security_check(self, ctx)
86 return verdict,"{{ m.policy }}"
87 {% else %}
88 verdict = True
89 return verdict,"xos_default_policy"
90 {% endif %}
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040091
Sapan Bhatia7148c082017-06-16 15:04:11 -070092{% if file_exists(m.name|lower+'_bottom.py') -%}{{ include_file(m.name|lower+'_bottom.py') }}{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +020093{% endfor %}
Sapan Bhatia2941a052017-07-10 15:10:03 -040094+++ models{{ legacy_tag }}.py