blob: ed78ff1ee4eef553d98d51378ad33c2495b9d4e5 [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 Bhatia95f5b112017-10-09 21:51:02 -04005from core.models import ServiceInstance, TenantWithContainer
Sapan Bhatia7148c082017-06-16 15:04:11 -07006{% else %}
7{% set legacy = False %}
8{% set legacy_tag = '' %}
Sapan Bhatia95f5b112017-10-09 21:51:02 -04009{% if file_exists('../header.py') or file_exists('header.py')-%}from header import *
10{% else %}
11from core.models.xosbase import *
12from core.models import ServiceInstance, TenantWithContainer
13{% endif %}
Sapan Bhatia7148c082017-06-16 15:04:11 -070014{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +020015
16{% for m in proto.messages %}
17{% if file_exists(m.name|lower+'_header.py') -%}from {{m.name|lower }}_header import *{% endif %}
18{% if file_exists(m.name|lower+'_top.py') -%}{{ include_file(m.name|lower+'_top.py') }} {% endif %}
19
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020020{%- for l in m.links -%}{% set peer_name=l.peer.name %}
Sapan Bhatia00274172017-07-20 09:05:46 -040021
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020022{% if peer_name not in proto.message_names -%}
23from core.models import {{ peer_name }}
Sapan Bhatia504cc972017-04-27 01:56:28 +020024{%- endif -%}
25{%- endfor -%}
26{%- for b in m.bases -%}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020027{%- if b.name!='XOSBase' and 'Mixin' not in b.name %}
Sapan Bhatia00274172017-07-20 09:05:46 -040028{% if b.name not in proto.message_names %}
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020029from core.models import {{ b.name }}
Sapan Bhatia00274172017-07-20 09:05:46 -040030{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +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 %}
37
Sapan Bhatia504cc972017-04-27 01:56:28 +020038{% endfor %}
39
40{% for m in proto.messages %}
Sapan Bhatiaa3d2e622017-07-31 22:25:55 -040041class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases, legacy_tag, proto.message_names) }}:
Sapan Bhatia2941a052017-07-10 15:10:03 -040042 plural_name = "{{ xproto_pluralize(m) }}"
Sapan Bhatia504cc972017-04-27 01:56:28 +020043
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080044 {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True') %}
45 {%- if feedback_state_fields|length > 0 %}
46 feedback_state_fields = {{ feedback_state_fields }}
47 {%- endif %}
48
Sapan Bhatia504cc972017-04-27 01:56:28 +020049 KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }}
50
Scott Baker0d2dd982018-02-20 09:27:52 -080051 {% if m.options.owner_class_name %}
52 OWNER_CLASS_NAME = {{ m.options.owner_class_name }}
53 {% endif %}
54
Sapan Bhatia504cc972017-04-27 01:56:28 +020055 class Meta:
56 app_label = {{ xproto_first_non_empty([m.options.app_label, options.app_label, options.name, "Set an app label in your xproto!"]) | lower}}
57 # name = {{ xproto_first_non_empty([m.options.name, options.name, "Set a name in your xproto!"]) }}
Matteo Scandoloe425f9d2017-08-15 15:56:19 -070058 verbose_name = "{{ xproto_unquote(xproto_first_non_empty([m.options.verbose_name, m.name])) }}"
Sapan Bhatia504cc972017-04-27 01:56:28 +020059
60 # Primitive Fields (Not Relations)
61 {% for f in m.fields %}
62 {%- if not f.link -%}
63 {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} )
64 {% endif %}
65 {%- endfor %}
66
67 # Relations
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020068 {% for l in m.links %}{% set peer_name=l.peer.name %}
Sapan Bhatia7148c082017-06-16 15:04:11 -070069 {% if legacy and peer_name in proto.message_names %}{% set peer_tag = legacy_tag %}{% else %}{% set peer_tag = '' %}{% endif -%}
70 {{ 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 +020071 {%- endfor %}
72
73 {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
74 pass
75
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040076 # Generated methods
77 def save(self, *args, **kwds):
Sapan Bhatia113c2b92017-07-25 08:41:58 -040078 if not self.leaf_model_name:
79 self.leaf_model_name = "{{ m.name }}"
80
Sapan Bhatia9227b4d2017-07-25 23:14:48 -040081 {% for policy,error in xproto_validations(m.options) %}
82 policy_{{policy}}_validator(self, None)
83 {% endfor %}
Sapan Bhatia0d6990a2017-09-20 06:42:38 -070084
85 try:
86 base_save_in_attic = self.__xos_save_base(*args, **kwds)
87 except AttributeError:
88 base_save_in_attic = False
89
90 if not base_save_in_attic:
91 super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds)
Sapan Bhatiab5ce1862017-07-31 15:48:19 -040092
93 def can_access(self, ctx):
94 {% if m.policy %}
95 verdict = security.{{m.policy}}_security_check(self, ctx)
96 return verdict,"{{ m.policy }}"
97 {% else %}
98 verdict = True
99 return verdict,"xos_default_policy"
100 {% endif %}
Sapan Bhatia9227b4d2017-07-25 23:14:48 -0400101
Sapan Bhatia7148c082017-06-16 15:04:11 -0700102{% if file_exists(m.name|lower+'_bottom.py') -%}{{ include_file(m.name|lower+'_bottom.py') }}{% endif %}
Sapan Bhatia504cc972017-04-27 01:56:28 +0200103{% endfor %}
Sapan Bhatia2941a052017-07-10 15:10:03 -0400104+++ models{{ legacy_tag }}.py