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 | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 47 | {# {% if m.options.no_sync or m.options.no_policy %}#} |
| 48 | {# {% if m.options.no_sync -%}#} |
| 49 | {# # Removing synchronizer feedback state from model#} |
| 50 | {# backend_status = None#} |
| 51 | {# backend_code = None#} |
| 52 | {# {%- endif %}#} |
| 53 | {# {% if m.options.no_policy -%}#} |
| 54 | {# # Removing model policy feedback state from model#} |
| 55 | {# policy_code = None#} |
| 56 | {# policy_status = None#} |
| 57 | {# {%- endif %}#} |
| 58 | {# {% endif %}#} |
| 59 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 60 | # Primitive Fields (Not Relations) |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 61 | {% for f in m.fields %} |
| 62 | {%- if not f.link -%} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 63 | {{ 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] | 64 | {% endif %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 65 | {%- endfor %} |
| 66 | |
| 67 | # Relations |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 68 | {% for l in m.links %}{% set peer_name=l.peer.name %} |
| 69 | {{ 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] | 70 | {%- endfor %} |
| 71 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 72 | # Meta |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 73 | class Meta: |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 74 | {%- set uniques = xproto_field_graph_components(m.fields) %} |
| 75 | {%- if uniques %} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 76 | unique_together = {{ xproto_tuplify(uniques) }} |
| 77 | {%- endif %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 78 | {%- if xproto_is_true(m.options.abstract) %} |
| 79 | abstract=True |
| 80 | {%- endif %} |
| 81 | pass |
| 82 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 83 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 84 | pass |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 85 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 86 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
| 87 | # Generated methods |
| 88 | def save(self, *args, **kwds): |
Sapan Bhatia | 113c2b9 | 2017-07-25 08:41:58 -0400 | [diff] [blame] | 89 | if not self.leaf_model_name: |
| 90 | self.leaf_model_name = "{{ m.name }}" |
| 91 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 92 | try: |
| 93 | self.__xos_save_base(*args, **kwds) |
| 94 | except AttributeError: |
| 95 | pass |
| 96 | |
| 97 | {% for policy,error in xproto_validations(m.options) %} |
| 98 | policy_{{policy}}_validator(self, None) |
| 99 | {% endfor %} |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 100 | super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds) |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 101 | |
| 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 = XOS_GLOBAL_DEFAULT_SECURITY_POLICY |
| 108 | return verdict,"xos_default_policy" |
| 109 | {% endif %} |
| 110 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 111 | {% endif %} |
| 112 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 113 | {% 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] | 114 | {% endif %}{% endfor %} |