rustio-admin-cli 0.27.5

Command-line tools for rustio-admin: project scaffolding, migrations, user management.
//! Demo `Post` model. Rename or delete when you no longer need it.
//!
//! - `#[derive(RustioAdmin)]` produces the `AdminModel` impl that
//!   drives every list / form / cell rendering.
//! - The hand-written `impl Model` is the contract with the Postgres
//!   schema (table name, columns, row decoder, insert binder).
//! - `impl ModelAdmin for Post { … }` opts into Django-style
//!   customisation. Override only the methods you care about; the
//!   rest inherit framework defaults.

use chrono::{DateTime, Utc};
use rustio_admin::{Error, Model, ModelAdmin, Row, RustioAdmin, Value};

#[derive(RustioAdmin)]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub body: String,
    pub published: bool,
    pub created_at: DateTime<Utc>,
}

impl Model for Post {
    const TABLE: &'static str = "posts";
    const COLUMNS: &'static [&'static str] =
        &["id", "title", "body", "published", "created_at"];
    const INSERT_COLUMNS: &'static [&'static str] =
        &["title", "body", "published", "created_at"];

    fn id(&self) -> i64 {
        self.id
    }

    fn from_row(row: Row<'_>) -> Result<Self, Error> {
        Ok(Self {
            id: row.get_i64("id")?,
            title: row.get_string("title")?,
            body: row.get_string("body")?,
            published: row.get_bool("published")?,
            created_at: row.get_datetime("created_at")?,
        })
    }

    fn insert_values(&self) -> Vec<Value> {
        vec![
            self.title.clone().into(),
            self.body.clone().into(),
            self.published.into(),
            self.created_at.into(),
        ]
    }
}

impl ModelAdmin for Post {
    fn list_display() -> &'static [&'static str] {
        &["title", "published", "created_at"]
    }
    fn list_filter() -> &'static [&'static str] {
        &["published"]
    }
    fn search_fields() -> &'static [&'static str] {
        &["title", "body"]
    }
    fn ordering() -> &'static [&'static str] {
        &["-created_at"]
    }
}