Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame^] | 1 | {% 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 Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 3 | {%- set legacy_tag = '_decl' -%} |
| 4 | {%- set legacy = True -%} |
| 5 | {%- else -%} |
| 6 | {%- set legacy_tag = '' -%} |
| 7 | {%- set legacy = False -%} |
| 8 | {%- endif -%} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 9 | {% if m.options.custom_header %} |
| 10 | from {{ m.options.custom_header|replace('"','') }} import * |
| 11 | {% else %} |
| 12 | from core.models.xosbase import * |
| 13 | {% endif %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 14 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 15 | {%- for l in m.links %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 16 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 17 | {% if l.peer.name != m.name %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 18 | from {{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 19 | {% endif %} |
| 20 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 21 | {%- endfor %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 22 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
| 23 | import security |
| 24 | {% if m.name!='Privilege' %} |
| 25 | from privilege import Privilege |
| 26 | {% endif %} |
| 27 | {% endif %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 28 | {% for b in m.bases %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 29 | {% if b.name!='XOSBase' and 'Mixin' not in b.name %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 30 | from {{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +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 %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 37 | |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame^] | 38 | class {{ m.name }}_decl{{ xproto_base_def(m.name, m.bases) }}: |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 39 | plural_name = "{{ xproto_pluralize(m) }}" |
| 40 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 41 | {%- 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 Scandolo | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 46 | {# {% 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 Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 59 | # Primitive Fields (Not Relations) |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 60 | {% for f in m.fields %} |
| 61 | {%- if not f.link -%} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 62 | {{ 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] | 63 | {% endif %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 64 | {%- endfor %} |
| 65 | |
| 66 | # Relations |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 67 | {% 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 Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 69 | {%- endfor %} |
| 70 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 71 | # Meta |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 72 | class Meta: |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 73 | {%- set uniques = xproto_field_graph_components(m.fields, m) %} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 74 | {%- if uniques %} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 75 | unique_together = {{ xproto_tuplify(uniques) }} |
| 76 | {%- endif %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 77 | {%- if xproto_is_true(m.options.abstract) %} |
| 78 | abstract=True |
| 79 | {%- endif %} |
| 80 | pass |
| 81 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 82 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 83 | pass |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 84 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 85 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
| 86 | # Generated methods |
| 87 | def save(self, *args, **kwds): |
Sapan Bhatia | 113c2b9 | 2017-07-25 08:41:58 -0400 | [diff] [blame] | 88 | if not self.leaf_model_name: |
| 89 | self.leaf_model_name = "{{ m.name }}" |
| 90 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 91 | try: |
| 92 | self.__xos_save_base(*args, **kwds) |
| 93 | except AttributeError: |
| 94 | pass |
| 95 | |
Scott Baker | dd8fff4 | 2018-09-04 09:07:51 -0700 | [diff] [blame] | 96 | if not self.deleted: |
| 97 | self.full_clean() |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 98 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 99 | {% for policy,error in xproto_validations(m.options) %} |
| 100 | policy_{{policy}}_validator(self, None) |
| 101 | {% endfor %} |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame^] | 102 | super({{ m.name }}_decl, self).save(*args, **kwds) |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 103 | |
| 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 Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 113 | {% endif %} |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame^] | 114 | |
| 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 %} |
| 118 | class {{ m.name }}({{ m. name }}_decl): |
| 119 | class Meta: |
| 120 | proxy = True |
| 121 | {% endif %} |
| 122 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 123 | {% 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] | 124 | {% endif %}{% endfor %} |