use async_trait::async_trait;
use reinhardt_core::exception::Result;
use reinhardt_db::orm::Model;
use reinhardt_http::Request;
use serde::Serialize;
use serde_json::json;
use crate::core::Context;
#[async_trait]
pub trait MultipleObjectMixin<T>: Send + Sync
where
T: Model + Serialize + Send + Sync + Clone,
{
async fn get_objects(&self) -> Result<Vec<T>>;
fn get_ordering(&self) -> Option<Vec<String>> {
None
}
fn allow_empty(&self) -> bool {
true
}
fn get_paginate_by(&self) -> Option<usize> {
None
}
fn get_context_object_name(&self) -> Option<&str> {
None
}
fn get_context_data(&self, object_list: Vec<T>) -> Result<Context> {
let mut context = Context::new();
context.insert("object_list".to_string(), json!(object_list));
if let Some(name) = self.get_context_object_name() {
context.insert(name.to_string(), json!(object_list));
}
Ok(context)
}
}
#[async_trait]
pub trait SingleObjectMixin<T>: Send + Sync
where
T: Model + Serialize + Send + Sync + Clone,
{
fn get_slug_field(&self) -> &str {
"slug"
}
fn pk_url_kwarg(&self) -> &str {
"pk"
}
fn slug_url_kwarg(&self) -> &str {
"slug"
}
async fn get_object(&self, request: &Request) -> Result<T>;
fn get_context_object_name(&self) -> Option<&str> {
None
}
fn get_context_data(&self, object: T) -> Result<Context> {
let mut context = Context::new();
context.insert("object".to_string(), json!(object));
if let Some(name) = self.get_context_object_name() {
context.insert(name.to_string(), json!(object));
}
Ok(context)
}
}