template error logging, css, better rendering

This commit is contained in:
mtrx 2024-08-10 21:30:51 +02:00
parent 2e7a81d7c7
commit 0a85a81950
9 changed files with 51 additions and 39 deletions

View file

@ -10,6 +10,10 @@ html {
padding-left: 20%; padding-left: 20%;
} }
.side-nav {
text-align: right;
}
.social-icon { .social-icon {
height: 1rem; height: 1rem;
width: 1rem; width: 1rem;
@ -20,11 +24,16 @@ html {
padding: 20px 0px 20px 0px; padding: 20px 0px 20px 0px;
} }
article { .blog-article {
padding-top: 2rem; padding-top: 2rem;
display: flex; display: flex;
} }
.hidden-link {
text-decoration: none;
color: black;
}
.post-link { .post-link {
text-decoration: none; text-decoration: none;
color: black; color: black;

View file

@ -7,7 +7,7 @@
{% endblock head %} {% endblock head %}
{% block content %} {% block content %}
<h1>Hi, I'm {{ username }}</h1> <h1><a class="hidden-link" href="/">Hi, I'm {{ username }}</a></h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/">Back to the blog</a> <a href="/">Back to the blog</a>
</p> </p>

View file

@ -1,5 +1,5 @@
{% macro article(id, title, body, publish_date) %} {% macro article(id, title, body, publish_date) %}
<article> <article class="blog-article">
<div class="post-content"> <div class="post-content">
<a href="/id/{{ id }}" class="post-link"> <a href="/id/{{ id }}" class="post-link">
<h2 class="post-title">{{ title }}</h2> <h2 class="post-title">{{ title }}</h2>

View file

@ -12,7 +12,10 @@
</head> </head>
<body> <body>
<div id="content">{% block content %}{% endblock content %}</div> <div id="content">
{% block content %}
{% endblock content %}
</div>
</body> </body>
</html> </html>

View file

@ -11,7 +11,7 @@
{% block content %} {% block content %}
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }} blog</a></h1> <h1><a href="/" class="hidden-link">{{ username }} blog</a></h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/about">About</a> <a href="/about">About</a>
<a href="/">Last 5 posts</a> <a href="/">Last 5 posts</a>

View file

@ -1,28 +1,21 @@
{% extends "base.html" %} {% extends "base.html" %}
{% import "article.html.macro" as article_macro %}
{% block head %} {% block head %}
{{ super() }} {{ super() }}
<meta property="og:title" content=" {{ post.title }} | {{ username }}' blog" /> <meta property="og:title" content=" {{ post.title }} | {{ username }} blog" />
<meta property="og:description" content="{{ post.body | striptags }}" /> <meta property="og:description" content="{{ post.body | striptags }}" />
<title> {{ post.title }} | {{ username }}' blog </title> <title> {{ post.title }} | {{ username }}' blog </title>
{% endblock head %} {% endblock head %}
{% block content %} {% block content %}
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }} blog</a></h1> <h1><a href="/" class="hidden-link">{{ username }} blog</a></h1>
<p style="text-align: right"> <aside class="side-nav">
<a href="/">Home</a> <a href="/">Home</a>
<a href="/about">About</a> <a href="/about">About</a>
<a href="/all">All Posts</a> <a href="/all">All Posts</a>
</p> </aside>
<ul>
<article> {{ article_macro::article(id=post.id,title=post.title,body=post.body,publish_date=post.publish_date) }}
<div class="post-content">
<a href="/id/{{ post.id }}" class="post-link">
<h2 class="post-title">{{ post.title }}</h2>
</a>
<sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
<p class="post-body">{{ post.body }}</p>
</div>
</article>
</ul>
{% endblock content %} {% endblock content %}

View file

@ -9,7 +9,7 @@
{% endblock head %} {% endblock head %}
{% block content %} {% block content %}
<h1><a href="/" class="post-link">{{ username }} blog</a></h1> <h1><a href="/" class="hidden-link">{{ username }} blog</a></h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/about">About</a> <a href="/about">About</a>
<a href="/all">All Posts</a> <a href="/all">All Posts</a>

View file

@ -2,30 +2,31 @@
{% block head %} {% block head %}
{{ super() }} {{ super() }}
<title>Edit '{{ title }}'</title> <title>Edit '{{ post.title }}'</title>
<meta property="og:title" content="Edit '{{ title }}'" /> <meta property="og:title" content="Edit '{{ post.title }}'" />
{% endblock head %} {% endblock head %}
{% block content %} {% block content %}
<form id="submit-form" action="/api/blog/posts/edit/{{ id }}" method=POST> <h1><a href="/" class="hidden-link">{{ username }} blog</a></h1>
<form id="submit-form" action="/api/blog/posts/edit/{{ post.id }}" method=POST>
<label for="token">Password</label> <label for="token">Password</label>
<br /> <br />
<input id="token" type="password" name="token"> <input id="token" type="password" name="token">
<br /> <br />
<label for="title">Title</label> <label for="title">Title</label>
<textarea id="submit-title" type="text" name="title">{{ title }}</textarea> <textarea id="submit-title" type="text" name="title">{{ post.title }}</textarea>
<br /> <br />
<label for="submit-body">Content</label> <label for="submit-body">Content</label>
<textarea id="submit-body" type="text" name="body">{{ body }}</textarea> <textarea id="submit-body" type="text" name="body">{{ post.body }}</textarea>
<br /> <br />
<button id="submit-button" type="submit">Edit post</button> <button id="submit-button" type="submit">Edit post</button>
</form> </form>
<form action="/api/blog/posts/hide/{{ id }}" method="POST"> <form action="/api/blog/posts/hide/{{ post.id }}" method="POST">
<input class="token" type="text" name="token" hidden> <input class="token" type="text" name="token" hidden>
<button type="submit">Hide post</button> <button type="submit">Hide post</button>
</form> </form>
<form action="/api/blog/posts/delete/{{ id }}" method="POST"> <form action="/api/blog/posts/delete/{{ post.id }}" method="POST">
<input class="token" type="text" name="token" hidden> <input class="token" type="text" name="token" hidden>
<button type="submit">Delete post</button> <button type="submit">Delete post</button>
</form> </form>

View file

@ -64,7 +64,8 @@ async fn about(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let result = tmpl let result = tmpl
.render("about.html", &context) .render("about.html", &context)
.map_err(|e| error::ErrorInternalServerError(format!("Template error\n{}", e)))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(result)) Ok(HttpResponse::Ok().content_type("text/html").body(result))
} }
@ -79,7 +80,8 @@ async fn blog(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let result = tmpl let result = tmpl
.render("blog.html", &context) .render("blog.html", &context)
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(result)) Ok(HttpResponse::Ok().content_type("text/html").body(result))
} }
@ -94,7 +96,8 @@ async fn blog_all(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let result = tmpl let result = tmpl
.render("blog-all-posts.html", &context) .render("blog-all-posts.html", &context)
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(result)) Ok(HttpResponse::Ok().content_type("text/html").body(result))
} }
@ -113,12 +116,13 @@ async fn blog_by_id(
} }
let mut context = Context::new(); let mut context = Context::new();
context.insert("post", &post);
context.insert("username", &CONFIG.username); context.insert("username", &CONFIG.username);
context.insert("post", &post);
let result = tmpl let result = tmpl
.render("blog-by-id.html", &context) .render("blog-by-id.html", &context)
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
return Ok(HttpResponse::Ok().content_type("text/html").body(result)); return Ok(HttpResponse::Ok().content_type("text/html").body(result));
} else { } else {
@ -134,7 +138,8 @@ async fn blog_submit(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error>
let result = tmpl let result = tmpl
.render("submit.html", &context) .render("submit.html", &context)
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
return Ok(HttpResponse::Ok().content_type("text/html").body(result)); return Ok(HttpResponse::Ok().content_type("text/html").body(result));
} }
@ -147,7 +152,8 @@ async fn blog_edit(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let result = tmpl let result = tmpl
.render("edit.html", &context) .render("edit.html", &context)
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(result)) Ok(HttpResponse::Ok().content_type("text/html").body(result))
} }
@ -165,13 +171,13 @@ async fn blog_edit_by_id(
post.body = replace_br_tags(&post.body); post.body = replace_br_tags(&post.body);
let mut context = Context::new(); let mut context = Context::new();
context.insert("title", &post.title); context.insert("username", &CONFIG.username);
context.insert("body", &post.body); context.insert("post", &post);
context.insert("id", &id);
let result = tmpl let result = tmpl
.render("edit-form.html", &context) .render("edit-form.html", &context)
.map_err(|_| error::ErrorInternalServerError("Template error"))?; .map_err(|err| error::ErrorInternalServerError(err))
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(result)) Ok(HttpResponse::Ok().content_type("text/html").body(result))
} else { } else {