reciprocals

master
Ondřej Hruška 4 years ago
parent cd82d5465a
commit d8bfc19986
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 14
      yopa-web/resources/templates/index.html.tera
  2. 30
      yopa-web/src/routes.rs
  3. 5
      yopa/src/lib.rs

@ -39,13 +39,12 @@
</ul> </ul>
{% endif %} {% endif %}
{% if model.relations %} {% if model.relations %}
Relations: Relations:
<ul> <ul>
{% for rel in model.relations %} {% for rel in model.relations %}
<li> <li>
<span title="{{rel.model.id}}">"{{rel.model.name}}", pointing to: <i>{{rel.related_name}}</i> (reciprocal as "{{rel.model.reciprocal_name}}")</span> <span title="{{rel.model.id}}">"{{rel.model.name}}" -&gt; <i>{{rel.related_name}} (reciprocally as "{{rel.model.reciprocal_name}}")</i></span>
{%- if rel.model.optional %}, OPTIONAL{% endif %} {%- if rel.model.optional %}, OPTIONAL{% endif %}
{%- if rel.model.multiple %}, MULTIPLE{% endif %} {%- if rel.model.multiple %}, MULTIPLE{% endif %}
<br> <br>
@ -68,6 +67,17 @@
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% if model.reciprocal_relations %}
Incoming relations:
<ul>
{% for rel in model.reciprocal_relations %}
<li>
<span title="{{rel.model.id}}">"{{rel.model.reciprocal_name}}" &lt;- <i>{{rel.related_name}}</i> (reciprocally as "{{rel.model.name}}")</span>
</li>
{% endfor %}
</ul>
{% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

@ -17,6 +17,7 @@ struct ObjectModelDisplay<'a> {
name : &'a str, name : &'a str,
properties: Vec<&'a PropertyModel>, properties: Vec<&'a PropertyModel>,
relations: Vec<RelationModelDisplay<'a>>, relations: Vec<RelationModelDisplay<'a>>,
reciprocal_relations: Vec<RelationModelDisplay<'a>>,
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
@ -73,14 +74,13 @@ pub(crate) async fn index(session : Session, store : crate::YopaStoreWrapper) ->
let mut model_props = rg.get_grouped_prop_models(); let mut model_props = rg.get_grouped_prop_models();
let mut model_relations = rg.get_grouped_relation_models(); let mut model_relations = rg.get_grouped_relation_models();
let mut model_rec_relations = rg.get_grouped_reciprocal_relation_models();
let mut models = vec![]; let mut models = vec![];
for om in models_iter { for om in models_iter {
let mut oprops = model_props.remove(&om.id).unwrap_or_default();
let mut relations = model_relations.remove(&om.id).unwrap_or_default(); let mut relations = model_relations.remove(&om.id).unwrap_or_default();
let mut relations = relations.into_iter().map(|rm| {
let rel_displays = relations.into_iter().map(|rm| { let mut rprops = model_props.get(&rm.id).cloned().unwrap_or_default();
let mut rprops = model_props.remove(&rm.id).unwrap_or_default();
rprops.sort_by_key(|m| &m.name); rprops.sort_by_key(|m| &m.name);
RelationModelDisplay { RelationModelDisplay {
@ -88,16 +88,32 @@ pub(crate) async fn index(session : Session, store : crate::YopaStoreWrapper) ->
related_name: rg.get_model_name(rm.related), related_name: rg.get_model_name(rm.related),
properties: rprops properties: rprops
} }
}).collect::<Vec<_>>();
relations.sort_by_key(|d| &d.model.name);
// Relations coming INTO this model
let mut reciprocal_relations = model_rec_relations.remove(&om.id).unwrap_or_default();
let mut reciprocal_relations = reciprocal_relations.into_iter().map(|rm| {
let mut rprops = model_props.get(&rm.id).cloned().unwrap_or_default();
rprops.sort_by_key(|m| &m.name);
RelationModelDisplay {
model: rm,
related_name: rg.get_model_name(rm.object),
properties: rprops
}
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
reciprocal_relations.sort_by_key(|d| &d.model.reciprocal_name);
oprops.sort_by_key(|m| &m.name); let mut properties = model_props.remove(&om.id).unwrap_or_default();
properties.sort_by_key(|m| &m.name);
models.push(ObjectModelDisplay { models.push(ObjectModelDisplay {
id: om.id, id: om.id,
name: &om.name, name: &om.name,
properties: oprops, properties,
relations: rel_displays, relations,
reciprocal_relations
}) })
} }

@ -370,4 +370,9 @@ impl Storage {
self.rel_models.values() self.rel_models.values()
.into_group_map_by(|model| model.object) .into_group_map_by(|model| model.object)
} }
pub fn get_grouped_reciprocal_relation_models(&self) -> HashMap<ID, Vec<&RelationModel>> {
self.rel_models.values()
.into_group_map_by(|model| model.related)
}
} }

Loading…
Cancel
Save