Skip to content

Commit

Permalink
Merge pull request #15 from blackfyre/polishing-artworks-and-artists
Browse files Browse the repository at this point in the history
Polishing artworks and artists
  • Loading branch information
blackfyre authored Oct 11, 2023
2 parents 2148860 + 4401833 commit bb1abe0
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 61 deletions.
44 changes: 44 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 500
exclude_dir = ["tmp", "vendor", "testdata", "web_src", "dist", ".idea", ".github"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "10s"
log = "build-errors.log"
poll = false
poll_interval = 0
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ wga_data
.env

dist/
tmp/
2 changes: 1 addition & 1 deletion assets/public/css/style.css

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions assets/public/css/viewer.min.css

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

1 change: 1 addition & 0 deletions assets/public/js/htmx.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions assets/public/js/viewer.min.js

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions assets/views/layout.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{{define "layout"}}
<!DOCTYPE html>
<html>
<html class="has-navbar-fixed-top">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{block "title" .}}Web Gallery of Art{{end}}</title>
<link rel="stylesheet" href="/assets/css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.11.6/viewer.min.css"
integrity="sha512-za6IYQz7tR0pzniM/EAkgjV1gf1kWMlVJHBHavKIvsNoUMKWU99ZHzvL6lIobjiE2yKDAKMDSSmcMAxoiWgoWA=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="/assets/css/viewer.min.css" />
<link rel="apple-touch-icon" sizes="180x180" href="/assets/images/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/images/favicon-16x16.png">
Expand Down Expand Up @@ -41,10 +39,8 @@
<div id="dual-right" hx-get="/htmx/right_default.html" hx-trigger="revealed"></div>
<button class="delete is-large" hx-on="click: ToggleDualMode()"></button>
</section> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.11.6/viewer.min.js"
integrity="sha512-EC3CQ+2OkM+ZKsM1dbFAB6OGEPKRxi6EDRnZW9ys8LghQRAq6cXPUgXCCujmDrXdodGXX9bqaaCRtwj4h4wgSQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="/assets/js/viewer.min.js"></script>
<script src="/assets/js/htmx.min.js"></script>
<script src="/assets/js/site.js"></script>
</body>

Expand Down
45 changes: 29 additions & 16 deletions assets/views/pages/artist.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@

{{define "body"}}
{{block "artist:content" .}}
<section class="hero is-primary is-sticky">
<div class="hero-body">
<div class="container">
<p class="title">
{{ .Name }}
</p>
<p class="subtitle">
(b. 1833, Odense, d. 1895, København)
</p>
<section class="section">
<div class="container">
<!-- Main container -->
<div class="level">
<!-- Left side -->
<div class="bottom-level">
<div>
<h1 class="is-size-1">
{{ .Name }}
</h1>
</div>
<div>
({{.BioExcerpt}})
</div>
</div>


</div>
</div>
</section>
<section class="section">
<div class="container">
<div class="divider">Biography</div>
</div>
<div class="container block">
<div class="box">
<div class="content">
{{ safeHTML .Bio}}
</div>
<!-- <div class="box"> -->
<div class="content">
{{ safeHTML .Bio}}
</div>
<!-- </div> -->
</div>
<div class="container block">

Expand All @@ -50,12 +56,19 @@
<div class="card-content">
<div class="content">
<h1 class="title">{{ .Title}}</h1>
<p class="subtitle"></p>
<p class="subtitle">{{ .Technique }}</p>
<div class="line-clamp-3">
{{safeHTML .Comment}}
</div>
</div>
</div>
<footer class="card-footer">
<p class="card-footer-item">
<span>
<a href="/artwork/{{$.Slug}}/{{.Id}}">Learn More</a>
</span>
</p>
</footer>
</article>
</div>
{{ else }}
Expand Down
4 changes: 2 additions & 2 deletions assets/views/partials/nav.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{define "partial:top-nav"}}
<nav class="navbar" role="navigation" aria-label="main navigation">
<nav class="navbar is-fixed-top" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" id="nav-logo" href="/">
<a class="navbar-item" id="nav-logo" href="/" hx-get="/" hx-target="#mc-area">
<img src="/assets/images/logo.png" width="318" height="45" alt="Web Gallery of Art" title="Go to Home">
</a>

Expand Down
153 changes: 121 additions & 32 deletions handlers/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,147 @@ package handlers

import (
"net/http"
"strconv"
"strings"

"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
)

func registerArtist(app *pocketbase.PocketBase) {
const (
Yes string = "yes"
No string = "no"
NotApplicable string = "n/a"
)

app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
func normalizedBioExcerpt(r *models.Record) string {
s := []string{}

e.Router.GET("artists/:name", func(c echo.Context) error {
slug := c.PathParam("name")
yob := r.GetInt("year_of_birth")
eyob := r.GetString("exact_year_of_birth")
pob := r.GetString("place_of_birth")
kpob := r.GetString("known_place_of_birth")

artist, err := app.Dao().FindRecordsByFilter("artists", "slug = '"+slug+"'", "+name", 1, 0)
yod := r.GetInt("year_of_death")
eyod := r.GetString("exact_year_of_death")
pod := r.GetString("place_of_death")
kpod := r.GetString("known_place_of_death")

if err != nil {
return apis.NewNotFoundError("", err)
}
if yob > 0 {

works, err := app.Dao().FindRecordsByFilter("artworks", "author = '"+artist[0].GetString("id")+"'", "+title", 100, 0)
c := []string{}

if err != nil {
return apis.NewNotFoundError("", err)
}
prefix := "b."

data := map[string]any{
"Name": artist[0].GetString("name"),
"Bio": artist[0].GetString("bio"),
"Works": []map[string]string{},
}
if eyob == No {
prefix = "bc."
}

for _, w := range works {
data["Works"] = append(data["Works"].([]map[string]string), map[string]string{
"Title": w.GetString("title"),
"Comment": w.GetString("comment"),
"Image": generateFileUrl(app, "artworks", w.GetString("id"), w.GetString("image")),
"Thumb": generateThumbUrl(app, "artworks", w.GetString("id"), w.GetString("image"), "320x240"),
})
}
c = append(c, prefix)
c = append(c, strconv.Itoa(yob))

html := ""
if kpob == No {
pob = pob + "?"
}

c = append(c, pob)

s = append(s, strings.Join(c, " "))

}

if yod > 0 {

c := []string{}

prefix := "d."

if eyod == No {
prefix = "dc."
}

c = append(c, prefix)
c = append(c, strconv.Itoa(yod))

if kpod == No {
pod = pod + "?"
}

c = append(c, pod)

s = append(s, strings.Join(c, " "))

}

return strings.Join(s, ", ")
}

func registerArtist(app *pocketbase.PocketBase) {

app.OnBeforeServe().Add(func(e *core.ServeEvent) error {

e.Router.GET("artists/:name", func(c echo.Context) error {
slug := c.PathParam("name")
cacheKey := "artist:" + slug

if isHtmxRequest(c) {
html, err = renderBlock("artist:content", data)
} else {
html, err = renderPage("artist", data)
cacheKey = cacheKey + "-htmx"
}

if err != nil {
// or redirect to a dedicated 404 HTML page
return apis.NewNotFoundError("", err)
html := ""

// found := app.Cache().Has(cacheKey)
found := false

if found {
html = app.Cache().Get(cacheKey).(string)
} else {
artist, err := app.Dao().FindRecordsByFilter("artists", "slug = '"+slug+"'", "+name", 1, 0)

if err != nil {
return apis.NewNotFoundError("", err)
}

works, err := app.Dao().FindRecordsByFilter("artworks", "author = '"+artist[0].GetString("id")+"'", "+title", 100, 0)

if err != nil {
return apis.NewNotFoundError("", err)
}

data := map[string]any{
"Name": artist[0].GetString("name"),
"Bio": artist[0].GetString("bio"),
"Works": []map[string]string{},
"Slug": slug,
"BioExcerpt": normalizedBioExcerpt(artist[0]),
}

for _, w := range works {
data["Works"] = append(data["Works"].([]map[string]string), map[string]string{
"Id": w.GetId(),
"Title": w.GetString("title"),
"Comment": w.GetString("comment"),
"Technique": w.GetString("technique"),
"Image": generateFileUrl(app, "artworks", w.GetString("id"), w.GetString("image")),
"Thumb": generateThumbUrl(app, "artworks", w.GetString("id"), w.GetString("image"), "320x240"),
})
}

if isHtmxRequest(c) {
html, err = renderBlock("artist:content", data)
} else {
html, err = renderPage("artist", data)
}

if err != nil {
// or redirect to a dedicated 404 HTML page
return apis.NewNotFoundError("", err)
}

app.Cache().Set(cacheKey, html)
}

c.Response().Header().Set("HX-Push-Url", "/artists/"+slug)
Expand Down
9 changes: 7 additions & 2 deletions migrations/1696479339_add_complementary_artists.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrations

import (
"encoding/json"
"strings"

"blackfyre.ninja/wga/assets"
"github.com/pocketbase/dbx"
Expand Down Expand Up @@ -48,11 +49,15 @@ func init() {
_, err = q.Execute()

if err != nil {
// if error contains "UNIQUE constraint failed: artists.slug" then ignore
errString := err.Error()

// if errString contains "UNIQUE constraint failed: artists.slug" then ignore
// otherwise return error
if err.Error() != "UNIQUE constraint failed: artists.slug" {

if !strings.Contains(errString, "UNIQUE constraint failed: artists.slug") {
return err
}

}

}
Expand Down
11 changes: 11 additions & 0 deletions web_src/sitebuild/src/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ html.is-clipped {
-webkit-box-orient: vertical;
overflow: hidden;
}

.bottom-level {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: baseline;

> div {
margin-right: 1rem;
}
}

0 comments on commit bb1abe0

Please sign in to comment.