Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 1 | {% if options.legacy =='"True"' -%} |
| 2 | {% set legacy_tag = '_decl' %} |
| 3 | {% set legacy = True %} |
| 4 | from core.models.xosbase import * |
Sapan Bhatia | 95f5b11 | 2017-10-09 21:51:02 -0400 | [diff] [blame] | 5 | from core.models import ServiceInstance, TenantWithContainer |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 6 | {% else %} |
| 7 | {% set legacy = False %} |
| 8 | {% set legacy_tag = '' %} |
Sapan Bhatia | 95f5b11 | 2017-10-09 21:51:02 -0400 | [diff] [blame] | 9 | {% if file_exists('../header.py') or file_exists('header.py')-%}from header import * |
| 10 | {% else %} |
| 11 | from core.models.xosbase import * |
| 12 | from core.models import ServiceInstance, TenantWithContainer |
| 13 | {% endif %} |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 14 | {% endif %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 15 | |
| 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 Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 20 | {%- for l in m.links -%}{% set peer_name=l.peer.name %} |
Sapan Bhatia | 0027417 | 2017-07-20 09:05:46 -0400 | [diff] [blame] | 21 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 22 | {% if peer_name not in proto.message_names -%} |
| 23 | from core.models import {{ peer_name }} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 24 | {%- endif -%} |
| 25 | {%- endfor -%} |
| 26 | {%- for b in m.bases -%} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 27 | {%- if b.name!='XOSBase' and 'Mixin' not in b.name %} |
Sapan Bhatia | 0027417 | 2017-07-20 09:05:46 -0400 | [diff] [blame] | 28 | {% if b.name not in proto.message_names %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 29 | from core.models import {{ b.name }} |
Sapan Bhatia | 0027417 | 2017-07-20 09:05:46 -0400 | [diff] [blame] | 30 | {% endif %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 31 | {%- endif -%} |
| 32 | {% endfor %} |
| 33 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 34 | {% for policy,error in xproto_validations(m.options) %} |
| 35 | {{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }} |
| 36 | {% endfor %} |
| 37 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 38 | {% endfor %} |
| 39 | |
| 40 | {% for m in proto.messages %} |
Sapan Bhatia | a3d2e62 | 2017-07-31 22:25:55 -0400 | [diff] [blame] | 41 | class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases, legacy_tag, proto.message_names) }}: |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 42 | plural_name = "{{ xproto_pluralize(m) }}" |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 43 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 44 | {%- 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 Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 49 | KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }} |
| 50 | |
Scott Baker | 0d2dd98 | 2018-02-20 09:27:52 -0800 | [diff] [blame] | 51 | {% if m.options.owner_class_name %} |
| 52 | OWNER_CLASS_NAME = {{ m.options.owner_class_name }} |
| 53 | {% endif %} |
| 54 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 55 | 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 Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 58 | verbose_name = "{{ xproto_unquote(xproto_first_non_empty([m.options.verbose_name, m.name])) }}" |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 59 | {%- set uniques = xproto_field_graph_components(m.fields, m) %} |
Scott Baker | eb782fc | 2018-04-27 13:50:21 -0700 | [diff] [blame] | 60 | {%- if uniques %} |
| 61 | unique_together = {{ xproto_tuplify(uniques) }} |
| 62 | {%- endif %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 63 | |
| 64 | # Primitive Fields (Not Relations) |
| 65 | {% for f in m.fields %} |
| 66 | {%- if not f.link -%} |
| 67 | {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} ) |
| 68 | {% endif %} |
| 69 | {%- endfor %} |
| 70 | |
| 71 | # Relations |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 72 | {% for l in m.links %}{% set peer_name=l.peer.name %} |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 73 | {% if legacy and peer_name in proto.message_names %}{% set peer_tag = legacy_tag %}{% else %}{% set peer_tag = '' %}{% endif -%} |
| 74 | {{ 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 Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 75 | {%- endfor %} |
| 76 | |
| 77 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 78 | pass |
| 79 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 80 | # Generated methods |
| 81 | def save(self, *args, **kwds): |
Sapan Bhatia | 113c2b9 | 2017-07-25 08:41:58 -0400 | [diff] [blame] | 82 | if not self.leaf_model_name: |
| 83 | self.leaf_model_name = "{{ m.name }}" |
| 84 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 85 | {% for policy,error in xproto_validations(m.options) %} |
| 86 | policy_{{policy}}_validator(self, None) |
| 87 | {% endfor %} |
Sapan Bhatia | 0d6990a | 2017-09-20 06:42:38 -0700 | [diff] [blame] | 88 | |
| 89 | try: |
| 90 | base_save_in_attic = self.__xos_save_base(*args, **kwds) |
| 91 | except AttributeError: |
| 92 | base_save_in_attic = False |
| 93 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 94 | self.full_clean() |
| 95 | |
Sapan Bhatia | 0d6990a | 2017-09-20 06:42:38 -0700 | [diff] [blame] | 96 | if not base_save_in_attic: |
| 97 | super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds) |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 98 | |
| 99 | def can_access(self, ctx): |
| 100 | {% if m.policy %} |
| 101 | verdict = security.{{m.policy}}_security_check(self, ctx) |
| 102 | return verdict,"{{ m.policy }}" |
| 103 | {% else %} |
| 104 | verdict = True |
| 105 | return verdict,"xos_default_policy" |
| 106 | {% endif %} |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 107 | |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 108 | {% if file_exists(m.name|lower+'_bottom.py') -%}{{ include_file(m.name|lower+'_bottom.py') }}{% endif %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 109 | {% endfor %} |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 110 | +++ models{{ legacy_tag }}.py |