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

View File

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

View File

@ -1,14 +1,21 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head>
<meta charset="utf-8">
<title>Actix Admin</title>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head> <head>
<body> <meta charset="utf-8">
{% block content %} <title>Actix Admin</title>
{% endblock content %}
</body> <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> </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)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {} 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![
]
}
}