implement date

This commit is contained in:
Manuel Gugger 2022-08-05 18:33:05 +02:00
parent fb4cd9f7d7
commit e79ae0a592
6 changed files with 31 additions and 3 deletions

View File

@ -264,6 +264,11 @@ pub fn get_fields_for_validate_model(fields: &Vec<ModelField>) -> Vec<TokenStrea
model.get_datetime(#ident_name, #is_option_or_string).map_err(|err| errors.insert(#ident_name.to_string(), err)).ok()
}
},
(_, "Date") => {
quote! {
model.get_date(#ident_name, #is_option_or_string).map_err(|err| errors.insert(#ident_name.to_string(), err)).ok()
}
},
(_, "bool") => {
quote! {
model.get_bool(#ident_name, #is_option_or_string).map_err(|err| errors.insert(#ident_name.to_string(), err)).ok()
@ -313,6 +318,16 @@ pub fn get_fields_for_create_model(fields: &Vec<ModelField>) -> Vec<TokenStream>
#ident: Set(model.get_datetime(#ident_name, #is_option_or_string).unwrap().unwrap())
}
},
(true , _, "Date") => {
quote! {
#ident: Set(model.get_date(#ident_name, #is_option_or_string).unwrap())
}
},
(false , _, "Date") => {
quote! {
#ident: Set(model.get_date(#ident_name, #is_option_or_string).unwrap().unwrap())
}
},
(_ , _, "bool") => {
quote! {
#ident: Set(model.get_bool(#ident_name, #is_option_or_string).unwrap().unwrap())
@ -373,6 +388,16 @@ pub fn get_fields_for_edit_model(fields: &Vec<ModelField>) -> Vec<TokenStream> {
entity.#ident = Set(model.get_datetime(#ident_name, #is_option_or_string).unwrap().unwrap())
}
},
(true , _, "Date") => {
quote! {
entity.#ident = Set(model.get_date(#ident_name, #is_option_or_string).unwrap())
}
},
(false , _, "Date") => {
quote! {
entity.#ident = Set(model.get_date(#ident_name, #is_option_or_string).unwrap().unwrap())
}
},
(true, _, _) => {
let inner_ty = model_field.inner_type.to_owned().unwrap();
quote! {

View File

@ -7,8 +7,6 @@ use actix_multipart:: {Multipart, MultipartError} ;
use futures_util::stream::StreamExt as _;
use chrono::{NaiveDateTime, NaiveDate};
use sea_orm::prelude::*;
use std::str::FromStr;
#[async_trait]
pub trait ActixAdminModelTrait {
@ -77,6 +75,10 @@ impl ActixAdminModel {
self.get_value_by_closure(key, is_option_or_string, |val| NaiveDateTime::parse_from_str(val, "%Y-%m-%dT%H:%M"))
}
pub fn get_date(&self, key: &str, is_option_or_string: bool) -> Result<Option<Date>, String> {
self.get_value_by_closure(key, is_option_or_string, |val| NaiveDate::parse_from_str(val, "%Y-%m-%d"))
}
pub fn get_bool(&self, key: &str, is_option_or_string: bool) -> Result<Option<bool>, String> {
let val = self.get_value_by_closure(key, is_option_or_string, |val| if !val.is_empty() { Ok(true) } else { Ok(false) });
// not selected bool field equals to false and not to missing

View File

@ -59,7 +59,6 @@ pub struct ActixAdminViewModelField {
impl ActixAdminViewModelFieldType {
pub fn get_field_type(type_path: &str, select_list: String) -> ActixAdminViewModelFieldType {
if !select_list.is_empty() {
println!("field_type {} {}", type_path, select_list);
return ActixAdminViewModelFieldType::SelectList;
}

Binary file not shown.

View File

@ -27,6 +27,7 @@ pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(post::Column::Text).string().not_null())
.col(ColumnDef::new(post::Column::TeaMandatory).string().not_null())
.col(ColumnDef::new(post::Column::TeaOptional).string())
.col(ColumnDef::new(post::Column::InsertDate).date())
.to_owned();
let _result = create_table(db, &stmt).await;

View File

@ -21,6 +21,7 @@ pub struct Model {
pub tea_mandatory: Tea,
#[actix_admin(select_list="Tea")]
pub tea_optional: Option<Tea>,
pub insert_date: Date,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]