blob: 8ef8e5fe061a39b3e82e4e93c63c64152bb4b2b5 [file] [log] [blame]
Scott Bakera33ccb02018-01-26 13:03:28 -08001{%- if options.legacy =='"True"' -%}
2{%- set legacy_tag = '_decl' -%}
3{%- set legacy = True -%}
4{%- else -%}
5{%- set legacy_tag = '' -%}
6{%- set legacy = False -%}
7{%- endif -%}
Sapan Bhatiad022aeb2017-06-07 15:49:55 +02008{% for m in proto.messages %}{% if not m.options.skip_django -%}
Scott Bakera33ccb02018-01-26 13:03:28 -08009{% if legacy %}
10{# handle models that use custom headers rather than deriving from xosbase #}
11{% if m.options.custom_header %}
12from {{ m.options.custom_header|replace('"','') }} import *
13{% else %}
14from core.models.xosbase import *
15{% endif %}
16{% else %}
Sapan Bhatia4e80a262017-05-19 23:10:51 +020017{% if file_exists(xproto_base_name(m.name)|lower+'_header.py') -%}from {{xproto_base_name(m.name)|lower }}_header import *{%- else -%}from header import *{% endif %}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020018{% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %}
Scott Bakera33ccb02018-01-26 13:03:28 -080019{% endif %}
20
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070021{%- for l in m.links %}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020022
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020023{% if l.peer.name != m.name %}
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040024from {{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020025{% endif %}
26
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070027{%- endfor %}
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040028{% if m.name!='XOSBase' and 'Mixin' not in m.name %}
29import security
30{% if m.name!='Privilege' %}
31from privilege import Privilege
32{% endif %}
33{% endif %}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020034{% for b in m.bases %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020035{% if b.name!='XOSBase' and 'Mixin' not in b.name %}
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040036from {{b.name | lower}} import {{ b.name }}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020037{% endif %}
38{% endfor %}
39
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040040{% for policy,error in xproto_validations(m.options) %}
41{{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }}
42{% endfor %}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070043
Scott Bakera33ccb02018-01-26 13:03:28 -080044class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases) }}:
Sapan Bhatia2941a052017-07-10 15:10:03 -040045 plural_name = "{{ xproto_pluralize(m) }}"
46
Matteo Scandolo39b4a272017-11-17 11:09:21 -080047{# {% if m.options.no_sync or m.options.no_policy %}#}
48{# {% if m.options.no_sync -%}#}
49{# # Removing synchronizer feedback state from model#}
50{# backend_status = None#}
51{# backend_code = None#}
52{# {%- endif %}#}
53{# {% if m.options.no_policy -%}#}
54{# # Removing model policy feedback state from model#}
55{# policy_code = None#}
56{# policy_status = None#}
57{# {%- endif %}#}
58{# {% endif %}#}
59
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070060 # Primitive Fields (Not Relations)
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020061 {% for f in m.fields %}
62 {%- if not f.link -%}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070063 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020064 {% endif %}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070065 {%- endfor %}
66
67 # Relations
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020068 {% for l in m.links %}{% set peer_name=l.peer.name %}
69 {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }} {%- endif -%}, {{ xproto_django_link_options_str(l, l.dst_port ) }} )
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070070 {%- endfor %}
71
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070072 # Meta
Scott Bakera33ccb02018-01-26 13:03:28 -080073 class Meta:
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070074 {%- set uniques = xproto_field_graph_components(m.fields) %}
75 {%- if uniques %}
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070076 unique_together = {{ xproto_tuplify(uniques) }}
77 {%- endif %}
Scott Bakera33ccb02018-01-26 13:03:28 -080078 {%- if xproto_is_true(m.options.abstract) %}
79 abstract=True
80 {%- endif %}
81 pass
82
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020083 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
84 pass
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070085
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040086 {% if m.name!='XOSBase' and 'Mixin' not in m.name %}
87 # Generated methods
88 def save(self, *args, **kwds):
Sapan Bhatia113c2b92017-07-25 08:41:58 -040089 if not self.leaf_model_name:
90 self.leaf_model_name = "{{ m.name }}"
91
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040092 try:
93 self.__xos_save_base(*args, **kwds)
94 except AttributeError:
95 pass
96
97 {% for policy,error in xproto_validations(m.options) %}
98 policy_{{policy}}_validator(self, None)
99 {% endfor %}
Scott Bakera33ccb02018-01-26 13:03:28 -0800100 super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds)
Sapan Bhatiab5ce1862017-07-31 15:48:19 -0400101
102 def can_access(self, ctx):
103 {% if m.policy %}
104 verdict = security.{{m.policy}}_security_check(self, ctx)
105 return verdict,"{{ m.policy }}"
106 {% else %}
107 verdict = XOS_GLOBAL_DEFAULT_SECURITY_POLICY
108 return verdict,"xos_default_policy"
109 {% endif %}
110
Sapan Bhatia9227b4d2017-07-25 23:14:48 -0400111 {% endif %}
112
Sapan Bhatiac4f803f2017-04-21 11:50:39 +0200113{% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %}
Sapan Bhatia2941a052017-07-10 15:10:03 -0400114{% endif %}{% endfor %}