Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 1 | {%- 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 Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 8 | {% for m in proto.messages %}{% if not m.options.skip_django -%} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 9 | {% if legacy %} |
| 10 | {# handle models that use custom headers rather than deriving from xosbase #} |
| 11 | {% if m.options.custom_header %} |
| 12 | from {{ m.options.custom_header|replace('"','') }} import * |
| 13 | {% else %} |
| 14 | from core.models.xosbase import * |
| 15 | {% endif %} |
| 16 | {% else %} |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 17 | {% 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 Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 18 | {% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 19 | {% endif %} |
| 20 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 21 | {%- for l in m.links %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 22 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 23 | {% if l.peer.name != m.name %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 24 | from {{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 25 | {% endif %} |
| 26 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 27 | {%- endfor %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 28 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
| 29 | import security |
| 30 | {% if m.name!='Privilege' %} |
| 31 | from privilege import Privilege |
| 32 | {% endif %} |
| 33 | {% endif %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 34 | {% for b in m.bases %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 35 | {% if b.name!='XOSBase' and 'Mixin' not in b.name %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 36 | from {{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 37 | {% endif %} |
| 38 | {% endfor %} |
| 39 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 40 | {% for policy,error in xproto_validations(m.options) %} |
| 41 | {{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }} |
| 42 | {% endfor %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 43 | |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 44 | class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases) }}: |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 45 | plural_name = "{{ xproto_pluralize(m) }}" |
| 46 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 47 | {%- 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 Scandolo | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 52 | {# {% 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 Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 65 | # Primitive Fields (Not Relations) |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 66 | {% for f in m.fields %} |
| 67 | {%- if not f.link -%} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 68 | {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} ) |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 69 | {% endif %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 70 | {%- endfor %} |
| 71 | |
| 72 | # Relations |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 73 | {% 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 Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 75 | {%- endfor %} |
| 76 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 77 | # Meta |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 78 | class Meta: |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 79 | {%- set uniques = xproto_field_graph_components(m.fields, m) %} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 80 | {%- if uniques %} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 81 | unique_together = {{ xproto_tuplify(uniques) }} |
| 82 | {%- endif %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 83 | {%- if xproto_is_true(m.options.abstract) %} |
| 84 | abstract=True |
| 85 | {%- endif %} |
| 86 | pass |
| 87 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 88 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 89 | pass |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 90 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 91 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
| 92 | # Generated methods |
| 93 | def save(self, *args, **kwds): |
Sapan Bhatia | 113c2b9 | 2017-07-25 08:41:58 -0400 | [diff] [blame] | 94 | if not self.leaf_model_name: |
| 95 | self.leaf_model_name = "{{ m.name }}" |
| 96 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 97 | try: |
| 98 | self.__xos_save_base(*args, **kwds) |
| 99 | except AttributeError: |
| 100 | pass |
| 101 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame^] | 102 | self.full_clean() |
| 103 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 104 | {% for policy,error in xproto_validations(m.options) %} |
| 105 | policy_{{policy}}_validator(self, None) |
| 106 | {% endfor %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 107 | super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds) |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 108 | |
| 109 | def can_access(self, ctx): |
| 110 | {% if m.policy %} |
| 111 | verdict = security.{{m.policy}}_security_check(self, ctx) |
| 112 | return verdict,"{{ m.policy }}" |
| 113 | {% else %} |
| 114 | verdict = XOS_GLOBAL_DEFAULT_SECURITY_POLICY |
| 115 | return verdict,"xos_default_policy" |
| 116 | {% endif %} |
| 117 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 118 | {% endif %} |
| 119 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 120 | {% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %} |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 121 | {% endif %}{% endfor %} |