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 * |
| 5 | {% else %} |
| 6 | {% set legacy = False %} |
| 7 | {% set legacy_tag = '' %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 8 | from header import * |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 9 | {% endif %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 10 | |
| 11 | {% for m in proto.messages %} |
| 12 | {% if file_exists(m.name|lower+'_header.py') -%}from {{m.name|lower }}_header import *{% endif %} |
| 13 | {% if file_exists(m.name|lower+'_top.py') -%}{{ include_file(m.name|lower+'_top.py') }} {% endif %} |
| 14 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 15 | {%- for l in m.links -%}{% set peer_name=l.peer.name %} |
Sapan Bhatia | 0027417 | 2017-07-20 09:05:46 -0400 | [diff] [blame] | 16 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 17 | {% if peer_name not in proto.message_names -%} |
| 18 | from core.models import {{ peer_name }} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 19 | {%- endif -%} |
| 20 | {%- endfor -%} |
| 21 | {%- for b in m.bases -%} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 22 | {%- if b.name!='XOSBase' and 'Mixin' not in b.name %} |
Sapan Bhatia | 0027417 | 2017-07-20 09:05:46 -0400 | [diff] [blame] | 23 | {% if b.name not in proto.message_names %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 24 | from core.models import {{ b.name }} |
Sapan Bhatia | 0027417 | 2017-07-20 09:05:46 -0400 | [diff] [blame] | 25 | {% endif %} |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 26 | {%- endif -%} |
| 27 | {% endfor %} |
| 28 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 29 | {% for policy,error in xproto_validations(m.options) %} |
| 30 | {{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }} |
| 31 | {% endfor %} |
| 32 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 33 | {% endfor %} |
| 34 | |
| 35 | {% for m in proto.messages %} |
Sapan Bhatia | a3d2e62 | 2017-07-31 22:25:55 -0400 | [diff] [blame] | 36 | 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] | 37 | plural_name = "{{ xproto_pluralize(m) }}" |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 38 | |
| 39 | KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }} |
| 40 | |
| 41 | class Meta: |
| 42 | app_label = {{ xproto_first_non_empty([m.options.app_label, options.app_label, options.name, "Set an app label in your xproto!"]) | lower}} |
| 43 | # name = {{ xproto_first_non_empty([m.options.name, options.name, "Set a name in your xproto!"]) }} |
| 44 | verbose_name = {{ xproto_first_non_empty([m.options.verbose_name, options.verbose_name, "Set a verbose name in your xproto!"]) }} |
| 45 | |
| 46 | # Primitive Fields (Not Relations) |
| 47 | {% for f in m.fields %} |
| 48 | {%- if not f.link -%} |
| 49 | {{ f.name }} = {{ xproto_django_type(f.type, f.options) }}( {{ xproto_django_options_str(f) }} ) |
| 50 | {% endif %} |
| 51 | {%- endfor %} |
| 52 | |
| 53 | # Relations |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 54 | {% for l in m.links %}{% set peer_name=l.peer.name %} |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 55 | {% if legacy and peer_name in proto.message_names %}{% set peer_tag = legacy_tag %}{% else %}{% set peer_tag = '' %}{% endif -%} |
| 56 | {{ 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] | 57 | {%- endfor %} |
| 58 | |
| 59 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
| 60 | pass |
| 61 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 62 | # Generated methods |
| 63 | def save(self, *args, **kwds): |
Sapan Bhatia | 113c2b9 | 2017-07-25 08:41:58 -0400 | [diff] [blame] | 64 | if not self.leaf_model_name: |
| 65 | self.leaf_model_name = "{{ m.name }}" |
| 66 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 67 | try: |
| 68 | self.__xos_save_base(*args, **kwds) |
| 69 | except AttributeError: |
| 70 | pass |
| 71 | |
| 72 | {% for policy,error in xproto_validations(m.options) %} |
| 73 | policy_{{policy}}_validator(self, None) |
| 74 | {% endfor %} |
| 75 | super({{ m.name }}{{ legacy_tag }}, self).save(*args, **kwds) |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 76 | |
| 77 | def can_access(self, ctx): |
| 78 | {% if m.policy %} |
| 79 | verdict = security.{{m.policy}}_security_check(self, ctx) |
| 80 | return verdict,"{{ m.policy }}" |
| 81 | {% else %} |
| 82 | verdict = True |
| 83 | return verdict,"xos_default_policy" |
| 84 | {% endif %} |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 85 | |
Sapan Bhatia | 7148c08 | 2017-06-16 15:04:11 -0700 | [diff] [blame] | 86 | {% 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] | 87 | {% endfor %} |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 88 | +++ models{{ legacy_tag }}.py |