blob: 081fb5fb65a3148ebcea5e73595aa5a35b68b4e7 [file] [log] [blame]
Scott Bakerbe2a5172019-04-10 18:02:50 -07001{% for m in proto.messages %}{% if not m.options.skip_django -%}
2{%- if xproto_list_evaluates_true([m.options.custom_python, m.options.legacy, options.custom_python, options.legacy]) -%}
Scott Bakera33ccb02018-01-26 13:03:28 -08003{%- set legacy_tag = '_decl' -%}
4{%- set legacy = True -%}
5{%- else -%}
6{%- set legacy_tag = '' -%}
7{%- set legacy = False -%}
8{%- endif -%}
Scott Bakera33ccb02018-01-26 13:03:28 -08009{% if m.options.custom_header %}
10from {{ m.options.custom_header|replace('"','') }} import *
11{% else %}
12from core.models.xosbase import *
13{% endif %}
Scott Bakera33ccb02018-01-26 13:03:28 -080014
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070015{%- for l in m.links %}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020016
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020017{% if l.peer.name != m.name %}
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040018from {{ l.peer.name | lower }} import {{ l.peer.name }}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020019{% endif %}
20
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070021{%- endfor %}
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040022{% if m.name!='XOSBase' and 'Mixin' not in m.name %}
23import security
24{% if m.name!='Privilege' %}
25from privilege import Privilege
26{% endif %}
27{% endif %}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020028{% for b in m.bases %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020029{% if b.name!='XOSBase' and 'Mixin' not in b.name %}
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040030from {{b.name | lower}} import {{ b.name }}
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020031{% endif %}
32{% endfor %}
33
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040034{% for policy,error in xproto_validations(m.options) %}
35{{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }}
36{% endfor %}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070037
Scott Bakerbe2a5172019-04-10 18:02:50 -070038class {{ m.name }}_decl{{ xproto_base_def(m.name, m.bases) }}:
Sapan Bhatia2941a052017-07-10 15:10:03 -040039 plural_name = "{{ xproto_pluralize(m) }}"
40
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080041 {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True') %}
42 {%- if feedback_state_fields|length > 0 %}
43 feedback_state_fields = {{ feedback_state_fields }}
44 {%- endif %}
45
Matteo Scandolo39b4a272017-11-17 11:09:21 -080046{# {% if m.options.no_sync or m.options.no_policy %}#}
47{# {% if m.options.no_sync -%}#}
48{# # Removing synchronizer feedback state from model#}
49{# backend_status = None#}
50{# backend_code = None#}
51{# {%- endif %}#}
52{# {% if m.options.no_policy -%}#}
53{# # Removing model policy feedback state from model#}
54{# policy_code = None#}
55{# policy_status = None#}
56{# {%- endif %}#}
57{# {% endif %}#}
58
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070059 # Primitive Fields (Not Relations)
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020060 {% for f in m.fields %}
61 {%- if not f.link -%}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070062 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020063 {% endif %}
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070064 {%- endfor %}
65
66 # Relations
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020067 {% for l in m.links %}{% set peer_name=l.peer.name %}
68 {{ 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 -070069 {%- endfor %}
70
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070071 # Meta
Scott Bakera33ccb02018-01-26 13:03:28 -080072 class Meta:
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070073 {%- set uniques = xproto_field_graph_components(m.fields, m) %}
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070074 {%- if uniques %}
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070075 unique_together = {{ xproto_tuplify(uniques) }}
76 {%- endif %}
Scott Bakera33ccb02018-01-26 13:03:28 -080077 {%- if xproto_is_true(m.options.abstract) %}
78 abstract=True
79 {%- endif %}
80 pass
81
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020082 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
83 pass
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070084
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040085 {% if m.name!='XOSBase' and 'Mixin' not in m.name %}
86 # Generated methods
87 def save(self, *args, **kwds):
Sapan Bhatia113c2b92017-07-25 08:41:58 -040088 if not self.leaf_model_name:
89 self.leaf_model_name = "{{ m.name }}"
90
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040091 try:
92 self.__xos_save_base(*args, **kwds)
93 except AttributeError:
94 pass
95
Scott Bakerdd8fff42018-09-04 09:07:51 -070096 if not self.deleted:
97 self.full_clean()
Matteo Scandolo61a9f202018-08-01 08:58:13 -040098
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040099 {% for policy,error in xproto_validations(m.options) %}
100 policy_{{policy}}_validator(self, None)
101 {% endfor %}
Scott Bakerbe2a5172019-04-10 18:02:50 -0700102 super({{ m.name }}_decl, self).save(*args, **kwds)
Sapan Bhatiab5ce1862017-07-31 15:48:19 -0400103
104 def can_access(self, ctx):
105 {% if m.policy %}
106 verdict = security.{{m.policy}}_security_check(self, ctx)
107 return verdict,"{{ m.policy }}"
108 {% else %}
109 verdict = XOS_GLOBAL_DEFAULT_SECURITY_POLICY
110 return verdict,"xos_default_policy"
111 {% endif %}
112
Sapan Bhatia9227b4d2017-07-25 23:14:48 -0400113 {% endif %}
Scott Bakerbe2a5172019-04-10 18:02:50 -0700114
115{# To maintain compatibility with migrations, we need to ensure that both the _decl and the non-_decl model #}
116{# exist. So we automatically create all models as _decl, and then add these trivial stubs #}
117{% if not legacy %}
118class {{ m.name }}({{ m. name }}_decl):
119 class Meta:
120 proxy = True
121{% endif %}
122
Sapan Bhatiac4f803f2017-04-21 11:50:39 +0200123{% 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 -0400124{% endif %}{% endfor %}