blob: 631e8cd10a905e03ee7f6305ffab8f6841a3c8f1 [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 Scandolo23cf15f2018-03-06 18:12:36 -080047 {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True') %}
48 {%- if feedback_state_fields|length > 0 %}
49 feedback_state_fields = {{ feedback_state_fields }}
50 {%- endif %}
51
Matteo Scandolo39b4a272017-11-17 11:09:21 -080052{# {% if m.options.no_sync or m.options.no_policy %}#}
53{# {% if m.options.no_sync -%}#}
54{# # Removing synchronizer feedback state from model#}
55{# backend_status = None#}
56{# backend_code = None#}
57{# {%- endif %}#}
58{# {% if m.options.no_policy -%}#}
59{# # Removing model policy feedback state from model#}
60{# policy_code = None#}
61{# policy_status = None#}
62{# {%- endif %}#}
63{# {% endif %}#}
64
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070065 # Primitive Fields (Not Relations)
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020066 {% for f in m.fields %}
67 {%- if not f.link -%}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070068 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020069 {% endif %}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070070 {%- endfor %}
71
72 # Relations
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020073 {% for l in m.links %}{% set peer_name=l.peer.name %}
74 {{ 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 -070075 {%- endfor %}
76
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070077 # Meta
Scott Bakera33ccb02018-01-26 13:03:28 -080078 class Meta:
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070079 {%- set uniques = xproto_field_graph_components(m.fields, m) %}
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070080 {%- if uniques %}
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070081 unique_together = {{ xproto_tuplify(uniques) }}
82 {%- endif %}
Scott Bakera33ccb02018-01-26 13:03:28 -080083 {%- if xproto_is_true(m.options.abstract) %}
84 abstract=True
85 {%- endif %}
86 pass
87
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020088 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
89 pass
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070090
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040091 {% if m.name!='XOSBase' and 'Mixin' not in m.name %}
92 # Generated methods
93 def save(self, *args, **kwds):
Sapan Bhatia113c2b92017-07-25 08:41:58 -040094 if not self.leaf_model_name:
95 self.leaf_model_name = "{{ m.name }}"
96
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040097 try:
98 self.__xos_save_base(*args, **kwds)
99 except AttributeError:
100 pass
101
102 {% for policy,error in xproto_validations(m.options) %}
103 policy_{{policy}}_validator(self, None)
104 {% endfor %}
Scott Bakera33ccb02018-01-26 13:03:28 -0800105 super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds)
Sapan Bhatiab5ce1862017-07-31 15:48:19 -0400106
107 def can_access(self, ctx):
108 {% if m.policy %}
109 verdict = security.{{m.policy}}_security_check(self, ctx)
110 return verdict,"{{ m.policy }}"
111 {% else %}
112 verdict = XOS_GLOBAL_DEFAULT_SECURITY_POLICY
113 return verdict,"xos_default_policy"
114 {% endif %}
115
Sapan Bhatia9227b4d2017-07-25 23:14:48 -0400116 {% endif %}
117
Sapan Bhatiac4f803f2017-04-21 11:50:39 +0200118{% 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 -0400119{% endif %}{% endfor %}