Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cot/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async fn view_model(
let total_object_counts = manager.get_total_object_counts(&request).await?;
let total_pages = total_object_counts.div_ceil(page_size);

if page == 0 || page > total_pages {
if (page == 0 || page > total_pages) && total_pages > 0 {
return Err(Error::not_found_message(format!("page {page} not found")));
}

Expand Down
3 changes: 2 additions & 1 deletion examples/admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ name = "example-admin"
version = "0.1.0"
publish = false
description = "Admin panel - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
async-trait = "0.1"
cot = { path = "../../cot", features = ["live-reload"] }
rinja = "0.3.5"
48 changes: 43 additions & 5 deletions examples/admin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
use cot::__private::async_trait;
use cot::admin::AdminApp;
mod migrations;

use std::fmt::{Display, Formatter};

use async_trait::async_trait;
use cot::admin::{AdminApp, AdminModel, AdminModelManager, DefaultAdminModelManager};
use cot::auth::db::{DatabaseUser, DatabaseUserApp};
use cot::cli::CliMetadata;
use cot::config::{DatabaseConfig, MiddlewareConfig, ProjectConfig, SessionMiddlewareConfig};
use cot::config::{
AuthBackendConfig, DatabaseConfig, MiddlewareConfig, ProjectConfig, SessionMiddlewareConfig,
};
use cot::db::migrations::SyncDynMigration;
use cot::db::{Auto, Model, model};
use cot::form::Form;
use cot::middleware::{AuthMiddleware, LiveReloadMiddleware, SessionMiddleware};
use cot::project::{MiddlewareContext, RegisterAppsContext};
use cot::request::extractors::RequestDb;
use cot::response::{Response, ResponseExt};
use cot::router::{Route, Router, Urls};
use cot::static_files::StaticFilesMiddleware;
use cot::{App, AppBuilder, Body, BoxedHandler, Project, ProjectContext, StatusCode};
use rinja::Template;

#[derive(Debug, Clone, Form, AdminModel)]
#[model]
struct TodoItem {
#[model(primary_key)]
id: Auto<i32>,
title: String,
}

impl Display for TodoItem {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.title)
}
}

#[derive(Debug, Template)]
#[template(path = "index.html")]
struct IndexTemplate<'a> {
urls: &'a Urls,
todo_items: Vec<TodoItem>,
}

async fn index(urls: Urls) -> cot::Result<Response> {
let index_template = IndexTemplate { urls: &urls };
async fn index(urls: Urls, RequestDb(db): RequestDb) -> cot::Result<Response> {
let todo_items = TodoItem::objects().all(&db).await?;
let index_template = IndexTemplate {
urls: &urls,
todo_items,
};
let rendered = index_template.render()?;

Ok(Response::new_html(StatusCode::OK, Body::fixed(rendered)))
Expand All @@ -42,6 +71,14 @@ impl App for HelloApp {
Ok(())
}

fn migrations(&self) -> Vec<Box<SyncDynMigration>> {
cot::db::migrations::wrap_migrations(migrations::MIGRATIONS)
}

fn admin_model_managers(&self) -> Vec<Box<dyn AdminModelManager>> {
vec![Box::new(DefaultAdminModelManager::<TodoItem>::new())]
}

fn router(&self) -> Router {
Router::with_urls([Route::with_handler("/", index)])
}
Expand All @@ -62,6 +99,7 @@ impl Project for AdminProject {
.url("sqlite://db.sqlite3?mode=rwc")
.build(),
)
.auth_backend(AuthBackendConfig::Database)
.middlewares(
MiddlewareConfig::builder()
.session(SessionMiddlewareConfig::builder().secure(false).build())
Expand Down
7 changes: 7 additions & 0 deletions examples/admin/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! List of migrations for the current app.
//!
//! Generated by cot CLI 0.2.0 on 2025-03-28 19:43:54+00:00

pub mod m_0001_initial;
/// The list of migrations for current app.
pub const MIGRATIONS: &[&::cot::db::migrations::SyncDynMigration] = &[&m_0001_initial::Migration];
35 changes: 35 additions & 0 deletions examples/admin/src/migrations/m_0001_initial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! Generated by cot CLI 0.2.0 on 2025-03-28 19:43:54+00:00

#[derive(Debug, Copy, Clone)]
pub(super) struct Migration;
impl ::cot::db::migrations::Migration for Migration {
const APP_NAME: &'static str = "example-admin";
const MIGRATION_NAME: &'static str = "m_0001_initial";
const DEPENDENCIES: &'static [::cot::db::migrations::MigrationDependency] = &[];
const OPERATIONS: &'static [::cot::db::migrations::Operation] =
&[::cot::db::migrations::Operation::create_model()
.table_name(::cot::db::Identifier::new("todo_item"))
.fields(&[
::cot::db::migrations::Field::new(
::cot::db::Identifier::new("id"),
<cot::db::Auto<i32> as ::cot::db::DatabaseField>::TYPE,
)
.auto()
.primary_key()
.set_null(<cot::db::Auto<i32> as ::cot::db::DatabaseField>::NULLABLE),
::cot::db::migrations::Field::new(
::cot::db::Identifier::new("title"),
<String as ::cot::db::DatabaseField>::TYPE,
)
.set_null(<String as ::cot::db::DatabaseField>::NULLABLE),
])
.build()];
}

#[derive(::core::fmt::Debug)]
#[::cot::db::model(model_type = "migration")]
struct _TodoItem {
#[model(primary_key)]
id: cot::db::Auto<i32>,
title: String,
}
17 changes: 15 additions & 2 deletions examples/admin/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@
<title>Admin Panel example</title>
</head>
<body>
<h1>Hello!</h1>
<p>Go to the <a href="{{ cot::reverse!(urls, "cot_admin:login")? }}">admin panel</a>.</p>
<h1>Admin panel-controlled TODO list</h1>

{% if todo_items.is_empty() %}
<p>There are no TODO items.</p>
{% else %}
<ul id="todo-list">
{% for todo in todo_items %}
<li>
{{ todo.title }}
</li>
{% endfor %}
</ul>
{% endif %}

<p>Go to the <a href="{{ cot::reverse!(urls, "cot_admin:login")? }}">admin panel</a> to manage the TODO items.</p>
<p>The username is <strong>admin</strong> and the password is <strong>admin</strong>.</p>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/custom-error-pages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "example-custom-error-pages"
version = "0.1.0"
publish = false
description = "Custom Error Pages - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
cot = { path = "../../cot" }
2 changes: 1 addition & 1 deletion examples/custom-task/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "example-custom-task"
version = "0.1.0"
publish = false
description = "Custom tasks - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
async-trait = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "example-hello-world"
version = "0.1.0"
publish = false
description = "Hello World - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
cot = { path = "../../cot" }
2 changes: 1 addition & 1 deletion examples/json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "example-json"
version = "0.1.0"
publish = false
description = "JSON - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
cot = { path = "../../cot" }
Expand Down
2 changes: 1 addition & 1 deletion examples/sessions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "example-sessions"
version = "0.1.0"
publish = false
description = "Sessions - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
cot = { path = "../../cot" }
Expand Down
2 changes: 1 addition & 1 deletion examples/sessions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cot::request::Request;
use cot::response::{Response, ResponseExt};
use cot::router::{Route, Router, Urls};
use cot::session::Session;
use cot::{reverse_redirect, App, AppBuilder, Body, BoxedHandler, Project, StatusCode};
use cot::{App, AppBuilder, Body, BoxedHandler, Project, StatusCode, reverse_redirect};
use rinja::Template;

#[derive(Debug, Template)]
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "example-todo-list"
version = "0.1.0"
publish = false
description = "TODO List - Cot example."
edition = "2021"
edition = "2024"

[dependencies]
cot = { path = "../../cot" }
Expand Down
4 changes: 2 additions & 2 deletions examples/todo-list/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use cot::auth::db::DatabaseUserApp;
use cot::cli::CliMetadata;
use cot::config::{DatabaseConfig, ProjectConfig};
use cot::db::migrations::SyncDynMigration;
use cot::db::{model, query, Auto, Model};
use cot::db::{Auto, Model, model, query};
use cot::form::Form;
use cot::project::{MiddlewareContext, RegisterAppsContext};
use cot::request::extractors::{Path, RequestDb, RequestForm};
use cot::response::{Response, ResponseExt};
use cot::router::{Route, Router, Urls};
use cot::static_files::StaticFilesMiddleware;
use cot::{reverse_redirect, App, AppBuilder, Body, BoxedHandler, Project, StatusCode};
use cot::{App, AppBuilder, Body, BoxedHandler, Project, StatusCode, reverse_redirect};
use rinja::Template;

#[derive(Debug, Clone)]
Expand Down
Loading