add picocss

This commit is contained in:
Manuel Gugger 2022-04-25 17:23:38 +02:00
parent b844315bf9
commit 657ad8ffc9
5 changed files with 102 additions and 70 deletions

View File

@ -6,34 +6,34 @@ pub fn derive_crud_fns(_input: proc_macro::TokenStream) -> proc_macro::TokenStre
let expanded = quote! {
use std::convert::From;
use async_trait::async_trait;
use std::pin::Pin;
use actix_admin::{ ActixAdminModelTrait, ActixAdminModel };
impl From<Entity> for ActixAdminModel {
fn from(entity: Entity) -> Self {
ActixAdminModel {
fields: Vec::new()
}
}
}
#[async_trait]
impl ActixAdminModelTrait for Entity {
async fn list(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel> {
use sea_orm::{ query::* };
let paginator = Entity::find()
.order_by_asc(Column::Id)
.paginate(db, posts_per_page);
let entities = paginator
.fetch_page(page - 1)
.await
.expect("could not retrieve entities");
//entities to ActixAdminModel
vec![
ActixAdminModel {
fields: Vec::new()
}
]
}
}
// impl From<Entity> for ActixAdminModel {
// fn from(entity: Entity) -> Self {
// ActixAdminModel {
// fields: Vec::new()
// }
// }
// }
// #[async_trait]
// impl ActixAdminModelTrait for Entity {
// async fn list(&self, db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str> {
// use sea_orm::{ query::* };
// let paginator = Entity::find()
// .order_by_asc(Column::Id)
// .paginate(db, posts_per_page);
// let entities = paginator
// .fetch_page(page - 1)
// .await
// .expect("could not retrieve entities");
// //entities to ActixAdminModel
// vec![
// ]
// }
// }
};
proc_macro::TokenStream::from(expanded)

View File

@ -1,7 +1,7 @@
use actix_web::{error, guard, web, Error, HttpRequest, HttpResponse};
use actix_web::{ dev, App, FromRequest};
use actix_web::error::ErrorBadRequest;
use serde_derive::Deserialize;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tera::{Context, Tera};
use futures::future::{ok, err, Ready};
@ -9,6 +9,8 @@ use lazy_static::lazy_static;
use sea_orm::DatabaseConnection;
use sea_orm::EntityTrait;
use sea_orm::ModelTrait;
use std::pin::Pin;
use std::any::Any;
use async_trait::async_trait;
@ -29,7 +31,7 @@ pub struct Params {
}
// Fields
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum Field {
Text
}
@ -42,28 +44,21 @@ pub trait AppDataTrait {
// ActixAdminModel
#[async_trait]
pub trait ActixAdminModelTrait {
async fn list(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel>;
pub trait ActixAdminModelTrait : Clone {
async fn list(&self, db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str>;
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ActixAdminModel {
pub fields: Vec<(&'static str, Field)>
pub fields: Vec<(&'static str, Field)>,
}
// ActixAdminViewModel
pub trait ActixAdminViewModelTrait : Clone {
fn get_model_name(&self) -> &str;
//fn get_entities() -> Vec<ActixAdminModel>;
}
impl ActixAdminViewModelTrait for ActixAdminViewModel {
fn get_model_name(&self) -> &str {
&self.entity_name
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ActixAdminViewModel {
pub entity_name: &'static str,
pub admin_model: ActixAdminModel
@ -106,10 +101,10 @@ impl ActixAdmin {
}
async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Error> {
let keys = Vec::from_iter(data.get_view_model_map().keys());
let view_models = Vec::from_iter(data.get_view_model_map().values());
let mut ctx = Context::new();
ctx.insert("view_models", &keys);
ctx.insert("view_models", &view_models);
let body = TERA
.render("index.html", &ctx)
@ -118,6 +113,8 @@ async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Erro
}
async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error> {
let view_model = data.get_view_model_map().get("posts").unwrap();
let db = &data.get_db();
let params = web::Query::<Params>::from_query(req.query_string()).unwrap();
@ -126,28 +123,17 @@ async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<H
let columns: Vec<String> = Vec::new();
// let paginator = post::Entity::find()
// .order_by_asc(post::Column::Id)
// .paginate(db, posts_per_page);
//let num_pages = paginator.num_pages().await.ok().unwrap();
let entities: Vec<&str> = Vec::new(); // view_model.get_entities()
let posts: Vec<&str> = Vec::new();
//let posts = paginator
// .fetch_page(page - 1)
// .await
// .expect("could not retrieve posts");
let mut ctx = Context::new();
ctx.insert("posts", &posts);
ctx.insert("posts", &entities);
ctx.insert("page", &page);
ctx.insert("posts_per_page", &posts_per_page);
ctx.insert("num_pages", "5" /*&num_pages*/);
ctx.insert("columns", &columns);
// let body = data.tmpl
// .render("list.html", &ctx)
// .map_err(|_| error::ErrorInternalServerError("Template error"))?;
//Ok(HttpResponse::Ok().content_type("text/html").body(body))
Ok(HttpResponse::Ok()
.content_type("text/html")
.body("<html></html>"))
let body = TERA
.render("list.html", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}

View File

@ -1,14 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Actix Admin</title>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
{% block content %}
{% endblock content %}
</body>
<head>
<meta charset="utf-8">
<title>Actix Admin</title>
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.classless.min.css">
</head>
<body>
{% include "header.html" %}
<main>
<div>
{% block content %}
{% endblock content %}
</div>
</main>
</body>
</html>

View File

@ -0,0 +1,12 @@
<header>
<nav>
<ul>
<li>Actix Admin</li>
</ul>
<ul>
{% for view_model in view_models -%}
<li><a href="/admin/{{ view_model.entity_name}}/list" class="secondary">{{ view_model.entity_name }}</a></li>
{%- endfor %}
</ul>
</nav>
</header>

View File

@ -17,4 +17,31 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl ActiveModelBehavior for ActiveModel {}
impl From<Entity> for ActixAdminModel {
fn from(entity: Entity) -> Self {
ActixAdminModel {
fields: Vec::new()
}
}
}
#[async_trait]
impl ActixAdminModelTrait for Entity {
async fn list(&self, db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str> {
use sea_orm::{ query::* };
let paginator = Entity::find()
.order_by_asc(Column::Id)
.paginate(db, posts_per_page);
let entities = paginator
.fetch_page(page - 1)
.await
.expect("could not retrieve entities");
//entities to ActixAdminModel
vec![
]
}
}