Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 1 | {% for m in proto.messages %}{% if not m.options.skip_django -%} |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 2 | {% 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] | 3 | {% if file_exists(xproto_base_name(m.name)|lower+'_top.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_top.py') }} {% endif %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 4 | {%- for l in m.links %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 5 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 6 | {% if l.peer.name != m.name %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 7 | from {{ l.peer.name | lower }} import {{ l.peer.name }} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 8 | {% endif %} |
| 9 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 10 | {%- endfor %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 11 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
| 12 | import security |
| 13 | from privilege import Privilege |
| 14 | {% endif %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 15 | {% for b in m.bases %} |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 16 | {% if b.name!='XOSBase' and 'Mixin' not in b.name %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 17 | from {{b.name | lower}} import {{ b.name }} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 18 | {% endif %} |
| 19 | {% endfor %} |
| 20 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 21 | {% for policy,error in xproto_validations(m.options) %} |
| 22 | {{ xproto_fol_to_python_validator(policy, proto.policies[policy], m, error) }} |
| 23 | {% endfor %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 24 | |
Sapan Bhatia | 4e80a26 | 2017-05-19 23:10:51 +0200 | [diff] [blame] | 25 | class {{ m.name }}{{ xproto_base_def(m.name, m.bases) }}: |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 26 | plural_name = "{{ xproto_pluralize(m) }}" |
| 27 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 28 | # Primitive Fields (Not Relations) |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 29 | {% for f in m.fields %} |
| 30 | {%- if not f.link -%} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 31 | {{ 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] | 32 | {% endif %} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 33 | {%- endfor %} |
| 34 | |
| 35 | # Relations |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 36 | {% for l in m.links %}{% set peer_name=l.peer.name %} |
| 37 | {{ 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] | 38 | {%- endfor %} |
| 39 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 40 | # Meta |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame^] | 41 | {%- set uniques = xproto_field_graph_components(m.fields, m) %} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 42 | {%- if uniques %} |
| 43 | class Meta: |
| 44 | unique_together = {{ xproto_tuplify(uniques) }} |
| 45 | {%- endif %} |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 46 | {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 47 | pass |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 48 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 49 | {% if m.name!='XOSBase' and 'Mixin' not in m.name %} |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 50 | # Generated methods |
| 51 | def save(self, *args, **kwds): |
Sapan Bhatia | 113c2b9 | 2017-07-25 08:41:58 -0400 | [diff] [blame] | 52 | if not self.leaf_model_name: |
| 53 | self.leaf_model_name = "{{ m.name }}" |
| 54 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 55 | try: |
| 56 | self.__xos_save_base(*args, **kwds) |
| 57 | except AttributeError: |
| 58 | pass |
| 59 | |
| 60 | {% for policy,error in xproto_validations(m.options) %} |
| 61 | policy_{{policy}}_validator(self, None) |
| 62 | {% endfor %} |
| 63 | super({{ m.name }}, self).save(*args, **kwds) |
| 64 | |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 65 | def can_access(self, ctx): |
| 66 | {% if m.policy %} |
| 67 | verdict = security.{{m.policy}}_security_check(self, ctx) |
| 68 | return verdict,"{{ m.policy }}" |
| 69 | {% else %} |
| 70 | verdict = XOS_GLOBAL_DEFAULT_SECURITY_POLICY |
| 71 | return verdict,"xos_default_policy" |
| 72 | {% endif %} |
| 73 | |
Sapan Bhatia | 9227b4d | 2017-07-25 23:14:48 -0400 | [diff] [blame] | 74 | {% endif %} |
Sapan Bhatia | b5ce186 | 2017-07-31 15:48:19 -0400 | [diff] [blame] | 75 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 76 | {% if file_exists(xproto_base_name(m.name)|lower+'_bottom.py') -%}{{ include_file(xproto_base_name(m.name)|lower+'_bottom.py') }}{% endif %} |
| 77 | +++ {{m.name|lower}}.py |
Sapan Bhatia | 2941a05 | 2017-07-10 15:10:03 -0400 | [diff] [blame] | 78 | {% endif %}{% endfor %} |