relationship stuff

This commit is contained in:
Daniel Tsvetkov 2021-05-08 14:14:54 +02:00
parent b680ca8bbd
commit 699c8910a2
6 changed files with 46 additions and 8 deletions

View File

@ -15,15 +15,25 @@ access:
show_in_nav: yes show_in_nav: yes
columns: columns:
- name: body|is_<bool>|<date>_dt - name: body|is_<bool>|<date>_dt
type: boolean|integer|text|long_text|datetime|filename| type: boolean|integer|text|long_text|datetime
default: "{}" default: "{}"
- name: filesomething
type: file|audio|video|picture|image|img
accept: .doc,.docx # OPTIONAL: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept
- name: related - name: related
type: relationship # type relationship type: relationship # type relationship
to: other_model # OPTIONAL: otherwise we get it from the name
backref: backref_model # OPTIONAL: name of the backref model, otherwise from name
foreign_key: fk_1 # OPTIONAL: MUST do it, if several related to the same column
multiple: true # could be many-to-many multiple: true # could be many-to-many
- name: choisy - name: choisy
type: choice type: choice
choices: choices:
INT_INFO: int_info # key:value INT_INFO: int_info # key:value
backrefs:
- name: related_to
model: song # list of backrefs that should be displayed
resolve: '.song_to' # OPTIONAL: in case it's a additional table that needs to be resolved further
display: display:
primary: type primary: type
secondary: text secondary: text

View File

@ -283,7 +283,7 @@ db_upgrade() {
db_purge_recreate() { db_purge_recreate() {
shift shift
source venv/bin/activate source venv/bin/activate
rm -rf data/db.sqlite data/search_index migrations/ rm -rf data/db.sqlite data/search_index migrations/ data/media
python manager.py db init python manager.py db init
db_migrate db_migrate
db_upgrade db_upgrade

View File

@ -2,10 +2,10 @@
[%- if not column.multiple %] [%- if not column.multiple %]
[[ column.name ]]_id = db.Column(db.Integer, db.ForeignKey('[%- if column.to %][[ column.to|camel_to_snake ]][%- else %][[ column.name ]][% endif %].id')) [[ column.name ]]_id = db.Column(db.Integer, db.ForeignKey('[%- if column.to %][[ column.to|camel_to_snake ]][%- else %][[ column.name ]][% endif %].id'))
[%- endif %] [%- endif %]
[[ column.name|pluralize if column.multiple else column.name ]] = db.relationship('[%- if column.to %][[ column.to ]][%- else %][[ column.name|snake_to_camel ]][% endif %]', [[ column.name|pluralize if column.multiple else column.name ]] = db.relationship('[%- if column.to %][[ column.to|snake_to_camel ]][%- else %][[ column.name|snake_to_camel ]][% endif %]',
[%- if column.multiple %]secondary=[[ column.secondary.name ]], [%- endif %] [%- if column.multiple %]secondary=[[ column.secondary.name ]], [%- endif %]
backref=db.backref("[%- if column.backref %][[ column.backref ]][%- else %][[ name|camel_to_snake|pluralize ]][%- endif %]"), backref=db.backref("[%- if column.backref %][[ column.backref ]][%- else %][[ name|camel_to_snake|pluralize ]][%- endif %]"),
[%- if column.foreign_keys %]foreign_keys=[ [[ column.foreign_keys ]]_id],[%- endif %] [%- if column.foreign_key %]foreign_keys=[ [[ column.foreign_key ]]_id],[%- endif %]
) )
[%- if column.multiple %] [%- if column.multiple %]

View File

@ -18,7 +18,7 @@
{% if [[ column.name|pluralize ]] is not defined and instance and instance.[[ column.name|pluralize ]] is defined %} {% if [[ column.name|pluralize ]] is not defined and instance and instance.[[ column.name|pluralize ]] is defined %}
{% set [[ column.name|pluralize ]] = instance.[[ column.name|pluralize ]] %} {% set [[ column.name|pluralize ]] = instance.[[ column.name|pluralize ]] %}
{% else %} {% else %}
{% set [[ column.name|pluralize ]] = model_views.[[ column.name ]].model.query.all() %} {% set [[ column.name|pluralize ]] = model_views.[[ column.to or column.name ]].model.query.all() %}
{% endif %} {% endif %}
{%- for sub_instance in [[ column.name|pluralize ]] %} {%- for sub_instance in [[ column.name|pluralize ]] %}
<option value="{{ sub_instance.id }}" {% if model_view.model_name == "[[ column.name ]]" and instance and instance.id == sub_instance.id %}selected="selected"{% endif %}>{{ sub_instance }}</option> <option value="{{ sub_instance.id }}" {% if model_view.model_name == "[[ column.name ]]" and instance and instance.id == sub_instance.id %}selected="selected"{% endif %}>{{ sub_instance }}</option>

View File

@ -1,6 +1,13 @@
[%- for column in columns %] [%- for column in columns %]
{% if "[[ column.name ]]" not in skip_list %} {% if "[[ column.name ]]" not in skip_list %}
<li id="display-[[ name|camel_to_snake ]]-[[ column.name ]]"><strong>{{ _("[[ column.name ]]") }}</strong>: <li id="display-[[ name|camel_to_snake ]]-[[ column.name ]]"><strong>[%- if column.type in ['relationship'] and column.multiple %]{{ _("[[ column.name|pluralize ]]") }}[%- else %]{{ _("[[ column.name ]]") }}[%- endif %]</strong>:
[%- if not column.type in ['bool', 'boolean', ] %]
[%- if column.type in ['relationship'] and column.multiple %]
{% if not instance.[[ column.name|pluralize ]] %}{% else %}
[%- else %]
{% if not instance.[[ column.name ]] %}{% else %}
[%- endif %]
[%- endif %]
[%- if column.type in ['picture', 'image', 'img'] %] [%- if column.type in ['picture', 'image', 'img'] %]
<img src="{{ url_for('oshipka_bp.get_media', filepath=instance.[[ column.name ]]) }}" id="display-[[ name|camel_to_snake ]]-[[ column.name ]]" /> <img src="{{ url_for('oshipka_bp.get_media', filepath=instance.[[ column.name ]]) }}" id="display-[[ name|camel_to_snake ]]-[[ column.name ]]" />
[%- elif column.type in ['video'] %] [%- elif column.type in ['video'] %]
@ -12,7 +19,11 @@
<audio src="{{ url_for('oshipka_bp.get_media', filepath=instance.[[ column.name ]]) }}" controls id="display-[[ name|camel_to_snake ]]-[[ column.name ]]"></audio> <audio src="{{ url_for('oshipka_bp.get_media', filepath=instance.[[ column.name ]]) }}" controls id="display-[[ name|camel_to_snake ]]-[[ column.name ]]"></audio>
[%- elif column.type in ['relationship'] %] [%- elif column.type in ['relationship'] %]
[%- if column.multiple %] [%- if column.multiple %]
{{ instance.[[ column.name|pluralize ]] }} <ul>
{% for instance in instance.[[ column.name|pluralize ]] %}
{% include "[[ column.name|camel_to_snake ]]/_list_item.html" %}
{% endfor %}
</ul>
[%- else %] [%- else %]
{{ instance.[[ column.name ]] }} {{ instance.[[ column.name ]] }}
[%- endif %] [%- endif %]
@ -23,4 +34,21 @@
[%- endif %] [%- endif %]
</li> </li>
{% endif %} {% endif %}
[%- if not column.type in ['bool', 'boolean', ] %]
{% endif %}
[%- endif %]
[%- endfor %]
[%- for backref in backrefs %]
<li id="display-[[ backref.name ]]"><strong>{{ _("[[ backref.name ]]") }}</strong>:
<ul>
{% set ex_instance = instance %}
{% for tmp_instance in ex_instance.[[ backref.name ]] %}
[%- if backref.resolve %]
{% set instance = tmp_instance[[ backref.resolve ]] %}
[%- else %]
{% set instance = tmp_instance %}
[%- endif %]
{% include "[[ backref.model ]]/_list_item.html" %}
{% endfor %}
</ul>
[%- endfor %] [%- endfor %]

View File

@ -13,7 +13,7 @@
[%- else %] [%- else %]
<select id="input-[[ name|camel_to_snake ]]-[[ column.name ]]" name="[[ column.name ]]_id"> <select id="input-[[ name|camel_to_snake ]]-[[ column.name ]]" name="[[ column.name ]]_id">
[%- endif %] [%- endif %]
{%- for sub_instance in model_views.[[ column.name ]].model.query.all() %} {%- for sub_instance in model_views.[[ column.to or column.name ]].model.query.all() %}
<option value="{{ sub_instance.id }}" <option value="{{ sub_instance.id }}"
[%- if column.multiple %] [%- if column.multiple %]
{% if sub_instance in instance.[[ column.name|pluralize ]] %}selected="selected"{% endif %}>{{ sub_instance }}</option> {% if sub_instance in instance.[[ column.name|pluralize ]] %}selected="selected"{% endif %}>{{ sub_instance }}</option>