parent
4e97f7a19a
commit
8b87fd0079
@ -0,0 +1,64 @@ |
||||
{% extends "_layout" %} |
||||
|
||||
{% block title -%} |
||||
Index |
||||
{%- endblock %} |
||||
|
||||
{% block nav -%} |
||||
<a href="/">Home</a> |
||||
{%- endblock %} |
||||
|
||||
{% block content -%} |
||||
|
||||
<h1>Welcome to tera on actix</h1> |
||||
|
||||
<a href="/model/object/create">New model</a> |
||||
|
||||
<h2>Defined models:</h2> |
||||
|
||||
<ul> |
||||
{% for model in models %} |
||||
<li> |
||||
<b>{{model.name}}</b><br> |
||||
|
||||
{# |
||||
|
||||
{% if !model.properties.is_empty() %} |
||||
Properties: |
||||
<ul> |
||||
{% for prop in model.properties %} |
||||
<li>{{prop.name}}, {{prop.data_type}}, def: {{prop.default}}, opt: {{prop.optional}}, mult: {{prop.multiple}}</li> |
||||
{% endfor %} |
||||
</ul> |
||||
{% endif %} |
||||
|
||||
<a href="/model/property/create/{{model.id}}">New property</a> |
||||
|
||||
{% if !model.relations.is_empty() %} |
||||
Relations: |
||||
<ul> |
||||
{% for rel in model.relations %} |
||||
<li> |
||||
{{rel.name}} -> {{rel.related_name}} |
||||
|
||||
{% if !rel.properties.is_empty() %} |
||||
Properties: |
||||
<ul> |
||||
{% for prop in rel.properties %} |
||||
<li>{{prop.name}}, {{prop.data_type}}, def: {{prop.default}}, opt: {{prop.optional}}, mult: {{prop.multiple}}</li> |
||||
{% endfor %} |
||||
</ul> |
||||
{% endif %} |
||||
</li> |
||||
{% endfor %} |
||||
</ul> |
||||
{% endif %} |
||||
|
||||
<a href="/model/relation/create/{{model.id}}">New relation</a> |
||||
|
||||
#} |
||||
</li> |
||||
{% endfor %} |
||||
</ul> |
||||
|
||||
{%- endblock %} |
@ -0,0 +1,30 @@ |
||||
{% extends "_layout" %} |
||||
|
||||
{% block title -%} |
||||
Define object |
||||
{%- endblock %} |
||||
|
||||
{% block nav -%} |
||||
<a href="/">Home</a> |
||||
{%- endblock %} |
||||
|
||||
{% block content -%} |
||||
|
||||
<h1>Define new object model</h1> |
||||
|
||||
<form action="/model/object/create" method="POST"> |
||||
<label for="parent">Parent:</label> |
||||
<select name="parent" id="parent"> |
||||
<option value="">No parent</option> |
||||
{%- for model in all_models %} |
||||
<option value="{{model.id}}">{{model.name}}</option> |
||||
{%- endfor %} |
||||
</select><br> |
||||
|
||||
<label for="name">Name:</label> |
||||
<input type="text" id="name" name="name"><br> |
||||
|
||||
<input type="submit" value="Save"> |
||||
</form> |
||||
|
||||
{%- endblock %} |
@ -0,0 +1,42 @@ |
||||
{% extends "_layout" %} |
||||
|
||||
{% block title -%} |
||||
Define property |
||||
{%- endblock %} |
||||
|
||||
{% block nav -%} |
||||
<a href="/">Home</a> |
||||
{%- endblock %} |
||||
|
||||
{% block content -%} |
||||
|
||||
<h1>Add new property to model "{{model.name}}"</h1> |
||||
|
||||
<form action="/model/property/create" method="POST"> |
||||
<input type="hidden" name="object" value="{{object}}"> |
||||
|
||||
<label for="name">Name:</label> |
||||
<input type="text" id="name" name="name"><br> |
||||
|
||||
<label for="optional">Optional:</label> |
||||
<input type="checkbox" name="optional" id="optional"> |
||||
<br> |
||||
|
||||
<label for="multiple">Multiple:</label> |
||||
<input type="checkbox" name="multiple" id="multiple"><br> |
||||
|
||||
<label for="data_type">Type:</label> |
||||
<select name="data_type" id="data_type"> |
||||
<option value="String" selected>String</option> |
||||
<option value="Integer">Integer</option> |
||||
<option value="Decimal">Decimal</option> |
||||
<option value="Boolean">Boolean</option> |
||||
</select><br> |
||||
|
||||
<label for="default">Default:</label> |
||||
<input type="text" id="default" name="default"><br> |
||||
|
||||
<input type="submit" value="Save"> |
||||
</form> |
||||
|
||||
{%- endblock %} |
@ -0,0 +1,68 @@ |
||||
use actix_web::{web, HttpRequest, Responder}; |
||||
use crate::TERA; |
||||
use crate::tera_ext::TeraExt; |
||||
use yopa::Storage; |
||||
use serde::Serialize; |
||||
use yopa::model::{PropertyModel, RelationModel}; |
||||
|
||||
#[derive(Serialize, Debug)] |
||||
struct ObjectModelDisplay<'a> { |
||||
id : yopa::ID, |
||||
name : &'a str, |
||||
properties: Vec<&'a PropertyModel>, |
||||
relations: Vec<RelationModelDisplay<'a>>, |
||||
} |
||||
|
||||
#[derive(Serialize, Debug)] |
||||
struct RelationModelDisplay<'a> { |
||||
model : &'a RelationModel, |
||||
related_name : &'a str, |
||||
properties: Vec<&'a PropertyModel>, |
||||
} |
||||
|
||||
#[get("/")] |
||||
pub(crate) async fn index(req: HttpRequest, store : crate::YopaStoreWrapper) -> actix_web::Result<impl Responder> { |
||||
|
||||
let rg = store.read().await; |
||||
|
||||
let models_iter = rg.get_object_models(); |
||||
|
||||
// object and relation props
|
||||
let mut model_props = rg.get_grouped_prop_models(); |
||||
|
||||
let mut model_relations = rg.get_grouped_relation_models(); |
||||
|
||||
let mut models = vec![]; |
||||
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 rel_displays = relations.into_iter().map(|rm| { |
||||
let mut rprops = model_props.remove(&rm.id).unwrap_or_default(); |
||||
rprops.sort_by_key(|m| &m.name); |
||||
|
||||
RelationModelDisplay { |
||||
model: rm, |
||||
related_name: rg.get_model_name(rm.related), |
||||
properties: rprops |
||||
} |
||||
|
||||
}).collect::<Vec<_>>(); |
||||
|
||||
oprops.sort_by_key(|m| &m.name); |
||||
|
||||
models.push(ObjectModelDisplay { |
||||
id: om.id, |
||||
name: &om.name, |
||||
properties: oprops, |
||||
relations: rel_displays, |
||||
}) |
||||
} |
||||
|
||||
models.sort_by_key(|m| m.name); |
||||
|
||||
let mut ctx = tera::Context::new(); |
||||
ctx.insert("models", &models); |
||||
|
||||
TERA.build_response("index", &ctx) |
||||
} |
Loading…
Reference in new issue