Browse Source

update index page to show first album image

master
cinnaboot 5 years ago
parent
commit
c502bd2b51
  1. 23
      gallery/static/style.css
  2. 16
      gallery/templates/album.j2
  3. 31
      gallery/templates/index.j2
  4. 57
      gallery/views/default_routes.py

23
gallery/static/style.css

@ -1,27 +1,14 @@
.nav ul { padding: 0; }
.nav li { display: inline; }
.flex_container {
display: flex;
flex-wrap: wrap;
flex-flow: row wrap;
width: 100%;
gap: 0 4px;
}
.infocard {
margin: 0.5em;
background-color: lightgrey;
font-size: 0.7em;
}
.infocard img { border: 1px solid grey; }
.infocard ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.infocard li {
width: 160px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.infocard { background-color: lightgrey; }
.infocard ul { padding: 0; list-style-type: none; }

16
gallery/templates/album.j2

@ -8,29 +8,25 @@
</head>
<body>
<h1>Gallery</h1>
<div class="nav">
<ul>
<li><a href="/">Home</a> &gt;</li>
<li><a href="/">{{ album }}</a> &gt;</li>
<li><a href="/">Home</a> /</li>
<li><a href="/">{{ album }}</a></li>
</ul>
</div>
<div class="flex_container">
{% for image in images %}
<div class="infocard">
<a href="{{ img_root + image.name }}">
<a href="{{ img_root + image }}">
<img src="{{ url_for(
'default_routes.thumbs',
path=album,
img=image.name)
img=image)
}}" />
</a>
<ul>
<li>name: {{ image.name }}</li>
<li>date: ...</li>
<li>dimensions: ...</li>
<li>filesize: ...</li>
</ul>
</div>
{% endfor %}
</div>

31
gallery/templates/index.j2

@ -8,13 +8,38 @@
</head>
<body>
<div>
<h1>Gallery</h1>
<div class="nav">
<ul>
<li><a href="/">Home</a></li>
</ul>
</div>
<h2>Albums</h2>
<div class="flex_container">
{% for album in albums %}
<li><a href="/album/{{ album|urlencode }}">{{ album }}</a></li>
{% endfor %}
<div class="infocard">
<a href="/album/{{ album.name|urlencode }}">
{% if album.count > 0 %}
<img src="{{ url_for(
'default_routes.thumbs',
path=album.name,
img=album.image)
}}" />
{% else %}
<img src="{{
url_for('static', filename='default_image.png')
}}" />
{% endif %}
</a>
<ul>
<li>{{ album.name }}</li>
<li>items: {{ album.count }}</li>
</ul>
</div>
{% endfor %}
</div>
</body>
</html>

57
gallery/views/default_routes.py

@ -4,12 +4,13 @@ import re
from PIL import Image
from flask import Blueprint, current_app, redirect, render_template, url_for
from flask import send_from_directory, Response
from flask import (Blueprint, current_app, redirect, render_template, url_for,
send_from_directory, Response)
bp = Blueprint('default_routes', __name__)
VALID_CHARS = re.compile(r"^[a-zA-Z0-9_\-\.]+$")
IMAGE_DIR = 'images'
#
# Routes
@ -17,20 +18,18 @@ VALID_CHARS = re.compile(r"^[a-zA-Z0-9_\-\.]+$")
@bp.route('/')
def index() -> Response:
album_dir = os.path.join(current_app.instance_path, 'images')
album_dirs = []
image_dir = getImageDir()
albums = []
entries = sorted(os.listdir(image_dir))
if os.path.isdir(album_dir):
entries = os.listdir(album_dir)
for e in entries:
if os.path.isdir(os.path.join(album_dir, e)):
album_dirs.append(e)
albums.append(getAlbumDetails(entries, e))
return render_template('index.j2', albums = sorted(album_dirs))
return render_template('index.j2', albums = albums)
@bp.route('/album/<path>')
def album(path:str = '') -> Response:
image_dir = joinPath(current_app.instance_path, 'images')
image_dir = getImageDir()
album_dir = joinPath(image_dir, path)
images = []
@ -40,24 +39,23 @@ def album(path:str = '') -> Response:
for entry in os.scandir(album_dir):
if entry.is_file():
createThumbnail(entry, path)
images.append(getImageDetails(entry))
images.append(entry.name)
return render_template('album.j2',
img_root = f'/image/{path}/',
album = path,
images = images
images = sorted(images)
)
@bp.route('/image/<path>/<img>')
def image(path:str, img:str) -> Response:
base_dir = os.path.join(current_app.instance_path, 'images')
album_dir = os.path.join(base_dir, os.path.basename(path))
album_dir = joinPath(getImageDir(), path)
return send_from_directory(album_dir, img)
@bp.route('/thumbs/<path>/<img>')
def thumbs(path:str, img:str) -> Response:
base_dir = os.path.join(current_app.instance_path, 'thumbnails')
album_dir = os.path.join(base_dir, os.path.basename(path))
base_dir = joinPath(current_app.instance_path, 'thumbnails')
album_dir = joinPath(base_dir, path)
return send_from_directory(album_dir, img)
@ -65,6 +63,9 @@ def thumbs(path:str, img:str) -> Response:
# Helpers
#
def getImageDir() -> os.PathLike:
return joinPath(current_app.instance_path, IMAGE_DIR)
def joinPath(base:os.PathLike, path:str) -> os.PathLike:
return os.path.join(base, os.path.basename(path))
@ -83,6 +84,27 @@ def validateFile(base:os.PathLike, path:str) -> bool:
return False
def getAlbumDetails(dir_list:list, album:str) -> dict:
image_dir = getImageDir()
if not validateDir(image_dir, album):
return None
album_dir = joinPath(image_dir, album)
entries = os.scandir(album_dir)
entries = sorted(os.scandir(album_dir),
key=lambda e: e.name)
if len(entries) > 0:
image = entries[0].name
createThumbnail(entries[0], album)
else:
image = None
return { 'name': album, 'image': image, 'count': len(entries) }
def getImageDetails(entry:os.PathLike) -> dict:
return {
'name': entry.name,
@ -104,7 +126,6 @@ def createThumbnail(image:os.DirEntry, album:str) -> None:
except OSError: pass
with Image.open(image.path) as im:
im.thumbnail([160, 160])
im.thumbnail([300, 300])
im.save(joinPath(thumb_path, image.name), "JPEG")

Loading…
Cancel
Save