blob: 206121951f820792a546a6c89901db4ee1740e2e [file] [log] [blame]
Sapan Bhatia7148c082017-06-16 15:04:11 -07001from core.models.xosbase import *
Scott Baker4839dec2019-02-27 16:50:37 -08002from core.models import ServiceInstance
Scott Bakerbe2a5172019-04-10 18:02:50 -07003{% set ns=namespace(any_legacy_tag = '') %}
Sapan Bhatia504cc972017-04-27 01:56:28 +02004{% for m in proto.messages %}
5{% if file_exists(m.name|lower+'_header.py') -%}from {{m.name|lower }}_header import *{% endif %}
6{% if file_exists(m.name|lower+'_top.py') -%}{{ include_file(m.name|lower+'_top.py') }} {% endif %}
7
Sapan Bhatia3cfdf632017-06-08 05:14:03 +02008{%- for l in m.links -%}{% set peer_name=l.peer.name %}
Sapan Bhatia00274172017-07-20 09:05:46 -04009
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020010{% if peer_name not in proto.message_names -%}
11from core.models import {{ peer_name }}
Sapan Bhatia504cc972017-04-27 01:56:28 +020012{%- endif -%}
13{%- endfor -%}
14{%- for b in m.bases -%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020015{%- if b.name!='XOSBase' and 'Mixin' not in b.name %}
Sapan Bhatia00274172017-07-20 09:05:46 -040016{% if b.name not in proto.message_names %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020017from core.models import {{ b.name }}
Sapan Bhatia00274172017-07-20 09:05:46 -040018{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +020019{%- endif -%}
20{% endfor %}
21
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040022{% for policy,error in xproto_validations(m.options) %}
23{{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }}
24{% endfor %}
25
Sapan Bhatia504cc972017-04-27 01:56:28 +020026{% endfor %}
27
Scott Bakerbe2a5172019-04-10 18:02:50 -070028{# Compute any_legacy_tag by looking to see if any model in the service has legacy sete #}
Sapan Bhatia504cc972017-04-27 01:56:28 +020029{% for m in proto.messages %}
Scott Bakerbe2a5172019-04-10 18:02:50 -070030{%- if xproto_list_evaluates_true([m.options.custom_python, m.options.legacy, options.custom_python, options.legacy]) -%}
31{% set ns.any_legacy_tag = '_decl' %}
32{% endif %}
33{% endfor %}
34
35{% for m in proto.messages %}
36{%- if xproto_list_evaluates_true([m.options.custom_python, m.options.legacy, options.custom_python, options.legacy]) -%}
37{% set legacy_tag = '_decl' %}
38{% set legacy = True %}
39{% else %}
40{% set legacy = False %}
41{% set legacy_tag = '' %}
42{% endif %}
43class {{ m.name }}{{ ns.any_legacy_tag }}{{ xproto_base_def(m.name, m.bases, legacy_tag, proto.message_names) }}:
Sapan Bhatia2941a052017-07-10 15:10:03 -040044 plural_name = "{{ xproto_pluralize(m) }}"
Sapan Bhatia504cc972017-04-27 01:56:28 +020045
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080046 {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True') %}
47 {%- if feedback_state_fields|length > 0 %}
48 feedback_state_fields = {{ feedback_state_fields }}
49 {%- endif %}
50
Sapan Bhatia504cc972017-04-27 01:56:28 +020051 KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }}
52
Scott Baker0d2dd982018-02-20 09:27:52 -080053 {% if m.options.owner_class_name %}
54 OWNER_CLASS_NAME = {{ m.options.owner_class_name }}
55 {% endif %}
56
Sapan Bhatia504cc972017-04-27 01:56:28 +020057 class Meta:
58 app_label = {{ xproto_first_non_empty([m.options.app_label, options.app_label, options.name, "Set an app label in your xproto!"]) | lower}}
59 # name = {{ xproto_first_non_empty([m.options.name, options.name, "Set a name in your xproto!"]) }}
Matteo Scandoloe425f9d2017-08-15 15:56:19 -070060 verbose_name = "{{ xproto_unquote(xproto_first_non_empty([m.options.verbose_name, m.name])) }}"
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070061 {%- set uniques = xproto_field_graph_components(m.fields, m) %}
Scott Bakereb782fc2018-04-27 13:50:21 -070062 {%- if uniques %}
63 unique_together = {{ xproto_tuplify(uniques) }}
64 {%- endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +020065
66 # Primitive Fields (Not Relations)
67 {% for f in m.fields %}
68 {%- if not f.link -%}
69 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
70 {% endif %}
71 {%- endfor %}
72
73 # Relations
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020074 {% for l in m.links %}{% set peer_name=l.peer.name %}
Sapan Bhatia7148c082017-06-16 15:04:11 -070075 {% if legacy and peer_name in proto.message_names %}{% set peer_tag = legacy_tag %}{% else %}{% set peer_tag = '' %}{% endif -%}
76 {{ 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 +020077 {%- endfor %}
78
79 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
80 pass
81
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040082 # Generated methods
83 def save(self, *args, **kwds):
Sapan Bhatia113c2b92017-07-25 08:41:58 -040084 if not self.leaf_model_name:
85 self.leaf_model_name = "{{ m.name }}"
86
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040087 {% for policy,error in xproto_validations(m.options) %}
88 policy_{{policy}}_validator(self, None)
89 {% endfor %}
Sapan Bhatia0d6990a2017-09-20 06:42:38 -070090
91 try:
92 base_save_in_attic = self.__xos_save_base(*args, **kwds)
93 except AttributeError:
94 base_save_in_attic = False
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 Bhatia0d6990a2017-09-20 06:42:38 -070099 if not base_save_in_attic:
Scott Bakerbe2a5172019-04-10 18:02:50 -0700100 super({{ m.name }}{{ ns.any_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 = True
108 return verdict,"xos_default_policy"
109 {% endif %}
Sapan Bhatia9227b4d2017-07-25 23:14:48 -0400110
Scott Bakerbe2a5172019-04-10 18:02:50 -0700111{# To maintain compatibility with migrations, we need to ensure that both the _decl and the non-_decl model #}
112{# exist. So we automatically create all models as _decl, and then add these trivial stubs #}
113{% if (not legacy) and (ns.any_legacy_tag) %}
114class {{ m.name }}({{ m. name }}{{ ns.any_legacy_tag }}):
115 class Meta:
116 proxy = True
117{% endif %}
118
Sapan Bhatia7148c082017-06-16 15:04:11 -0700119{% if file_exists(m.name|lower+'_bottom.py') -%}{{ include_file(m.name|lower+'_bottom.py') }}{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +0200120{% endfor %}
Scott Bakerbe2a5172019-04-10 18:02:50 -0700121+++ models{{ ns.any_legacy_tag }}.py