Skip to content

Commit 8124354

Browse files
committed
Process CD Covers seperatly
1 parent 81ef406 commit 8124354

17 files changed

+96
-70
lines changed

library_db/library.db

0 Bytes
Binary file not shown.

library_db/routes/auth/auth.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ def ret_error(error: str):
5353
return redirect(request.args.get("next", "/"))
5454

5555

56-
@auth_bluep.route("/signin", methods=["GET", "POST"])
57-
def signin():
56+
@auth_bluep.route("/signup", methods=["GET", "POST"])
57+
def signup():
5858
template_vars = get_template_vars(session)
5959
if request.method == "GET":
60-
return render_template("auth/signin.html", **template_vars)
60+
return render_template("auth/signup.html", **template_vars)
6161

6262
def ret_error(error: str):
63-
return render_template("auth/signin.html", error=error, **template_vars)
63+
return render_template("auth/signup.html", error=error, **template_vars)
6464

6565
try:
6666
email = request.form["email"]

library_db/routes/auth/templates/auth/login.html

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
value="Submit"
5959
class="form-control"
6060
/>
61+
<span class="mt-1">No Account? Signup <a href="{{url_for('auth_bluep.signup')}}">here</a></span>
6162
</form>
6263
</div>
6364
{% endblock %}

library_db/routes/auth/templates/auth/signin.html library_db/routes/auth/templates/auth/signup.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "base.html"%} {% block title %} Signin {% endblock %} {% block head
1+
{% extends "base.html"%} {% block title %} SignUp {% endblock %} {% block head
22
%} {{ super() }}
33
<style>
44
{% if darkmode %}
@@ -29,7 +29,7 @@
2929
class="position-absolute top-50 start-50 translate-middle sign-in-container"
3030
>
3131
<form
32-
action="/signin"
32+
action="{{url_for('auth_bluep.signup')}}"
3333
method="post"
3434
id="form"
3535
class="rounded bg-light p-3 d-flex flex-column {%if darkmode%} bg-dark text-white {% else %} shadow-lg border {% endif%}"

library_db/routes/general/templates/general/index.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<div class="d-flex flex-column">
7474
<div class="d-flex flex-column align-self-center">
7575
<span class="display-4"
76-
><img src="/static/images/books.png" alt="books emoticon" /> SQL
76+
><img src="{{url_for('static', filename='images/books.png')}}" alt="books emoticon" /> SQL
7777
Library</span
7878
>
7979
<span class="text-muted sub-title"
@@ -88,7 +88,7 @@
8888
>
8989
<button
9090
class="btn btn-primary mt-3"
91-
onclick="window.location.pathname = '/medialist'"
91+
onclick="window.location.pathname = '{{url_for('media_bluep.show_medialist')}}'"
9292
>
9393
Medialist
9494
</button>
@@ -118,15 +118,15 @@
118118
{%else%}
119119
<button
120120
class="btn btn-outline-primary mt-3"
121-
onclick="window.location.pathname = '/login'"
121+
onclick="window.location.pathname = '{{url_for('auth_bluep.login')}}'"
122122
>
123123
Login
124124
</button>
125125
<button
126126
class="btn btn-outline-primary mt-3"
127-
onclick="window.location.pathname = '/signin'"
127+
onclick="window.location.pathname = '{{url_for('auth_bluep.signup')}}'"
128128
>
129-
Signin
129+
SignUp
130130
</button>
131131
{%endif%}
132132
</div>
@@ -138,7 +138,7 @@
138138
<img
139139
id="header-image"
140140
class="rounded img-fluid shadow-lg"
141-
src="/static/images/library.webp"
141+
src="{{url_for('static', filename='images/library.webp')}}"
142142
alt="picture of library"
143143
/>
144144
</div>

library_db/routes/panel/admin/templates/admin/user_mng.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ <h5 for="mediaSearch" class="{%if darkmode %} text-white {%endif%}">
131131
>
132132
Create new Admin/Staff Account
133133
</h5>
134-
<a href="{{url_for('auth_bluep.signin')}}">
134+
<a href="{{url_for('auth_bluep.signup')}}">
135135
<div class="d-flex flex-column">
136136
<button class="btn btn-primary">Create</button>
137137
</div></a

library_db/routes/panel/staff/staff.py

+37-30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
is_admin,
1818
is_staff,
1919
process_cover_image,
20+
process_cd_cover,
2021
download_image
2122
)
2223
from library_db.utils.db_utils import (
@@ -38,7 +39,7 @@
3839

3940

4041

41-
ALLOWED_EXTENSIONS = {'.png', '.jpg', '.jpeg'}
42+
ALLOWED_EXTENSIONS = {'.png', '.jpg', '.jpeg', '.webp'}
4243

4344
def allowed_file(filename):
4445
return Path(filename).suffix in ALLOWED_EXTENSIONS
@@ -115,17 +116,17 @@ def ret_error(error: str):
115116
return ret_error("Bad Form Data")
116117

117118
media = get_media(id)
118-
119+
119120
if not media:
120121
return ret_error("Bad Form Data")
121-
122+
122123
if not media.title == title:
123124
media_title_id = get_media_id(title)
124125
if media_title_id:
125126
return ret_error("Media With this Title already exsists")
126-
127-
if not age_limit:
128-
return ret_error("Bad Form Data")
127+
128+
if age_limit is None:
129+
age_limit = 0
129130

130131
if not media_type in get_media_types():
131132
return ret_error("Media Type doesnt exsist")
@@ -226,26 +227,6 @@ def ret_error(error: str):
226227
except KeyError:
227228
return ret_error("Bad Form Data")
228229

229-
filename = None
230-
if "image" in request.files or "url" in request.form:
231-
file = request.files["image"]
232-
url = request.form["url"]
233-
filename = uuid.uuid1().hex + ".jpg"
234-
save_path = Path(current_app.config['UPLOAD_FOLDER']).joinpath(filename).__str__()
235-
236-
if file.filename != '' and url != '':
237-
return ret_error("Can't upload Image and use Scraper Image")
238-
elif not file.filename == '' and allowed_file(file.filename):
239-
file.save(save_path)
240-
process_cover_image(save_path)
241-
elif not url == '':
242-
if download_image(url, save_path) != 1:
243-
filename = None
244-
else:
245-
process_cover_image(save_path)
246-
else:
247-
filename = None
248-
249230
if isbn:
250231
try:
251232
isbn = int(isbn)
@@ -261,18 +242,44 @@ def ret_error(error: str):
261242
if not media_type in get_media_types():
262243
return ret_error("Media Type doesnt exsist")
263244

264-
media_type = get_media_type_id(media_type)
245+
media_type_id = get_media_type_id(media_type)
265246

266247
if not author_exsists(author):
267248
return ret_error("Author does not exsist")
268249

269-
author = get_author_id(author)
250+
author_id = get_author_id(author)
251+
252+
filename = None
253+
if "image" in request.files or "url" in request.form:
254+
file = request.files["image"]
255+
url = request.form["url"]
256+
filename = uuid.uuid1().hex + ".jpg"
257+
save_path = Path(current_app.config['UPLOAD_FOLDER']).joinpath(filename).__str__()
258+
259+
if file.filename != '' and url != '':
260+
return ret_error("Can't upload Image and use Scraper Image")
261+
elif not file.filename == '' and allowed_file(file.filename):
262+
file.save(save_path)
263+
if media_type == "CD":
264+
process_cd_cover(save_path)
265+
else:
266+
process_cover_image(save_path)
267+
elif not url == '':
268+
if download_image(url, save_path) != 1:
269+
filename = None
270+
else:
271+
if media_type == "CD":
272+
process_cd_cover(save_path)
273+
else:
274+
process_cover_image(save_path)
275+
else:
276+
filename = None
270277

271278
new_id = add_media_item(
272279
title=title,
273-
author_id=author,
280+
author_id=author_id,
274281
age_limit=age_limit,
275-
media_type_id=media_type,
282+
media_type_id=media_type_id,
276283
isbn=isbn,
277284
image=filename
278285
)

library_db/routes/panel/staff/templates/staff/alter.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ <h5 class="underlined {%if darkmode %} text-white {%endif%}">
155155
id="mediaAuthorSearch"
156156
autocomplete="off"
157157
name="author"
158-
value="{{media_item.author if media_item.author}}"
158+
value="{{media_item.author}}"
159159
/>
160160
<ul
161161
class="list-group d-none"
@@ -176,7 +176,7 @@ <h5 class="underlined {%if darkmode %} text-white {%endif%}">
176176
name="age_limit"
177177
placeholder="0"
178178
autocomplete="off"
179-
value="{{media_item.age_limit if media_item.age_limit}}"
179+
value="{{media_item.age_limit}}"
180180
/>
181181
</div>
182182
<div class="d-flex flex-column flex-fill ms-1">
@@ -204,7 +204,7 @@ <h5 class="underlined {%if darkmode %} text-white {%endif%}">
204204
name="isbn"
205205
placeholder="Optional"
206206
autocomplete="off"
207-
value="{{media_item.isbn if media_item.isbn}}"
207+
value="{{media_item.isbn}}"
208208
/>
209209
<div class="d-flex flex-fill">
210210
<div>

library_db/static/css/skeleton.min.css

-1
This file was deleted.
Loading
Loading
Loading
Loading
Loading

library_db/templates/base.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</li>
8484
{% endif %} {% else %}
8585
<li><a class="dropdown-item" href="{{url_for('auth_bluep.login')}}">Login</a></li>
86-
<li><a class="dropdown-item" href="{{url_for('auth_bluep.signin')}}">Signin</a></li>
86+
<li><a class="dropdown-item" href="{{url_for('auth_bluep.signup')}}">SignUp</a></li>
8787

8888
{% endif %}
8989
</ul>

library_db/utils/db_utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ def update_media(
438438
image: str,
439439
isbn: Union[int, None] = None,
440440
):
441-
print(image)
442441
statement = """
443442
UPDATE media
444443
SET title = ?,

library_db/utils/utils.py

+41-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22
import requests
3-
from PIL import Image
3+
from PIL import Image, ImageFilter
44
from lxml import html
55
from urllib.parse import urlparse, parse_qs, urlunparse, urlencode
66
from library_db.utils.db_utils import get_user_data, get_user_type, get_media_types
@@ -68,27 +68,47 @@ def update_query_params(url: str, **params):
6868
)
6969

7070

71+
def crop_image(img: Image.Image, w: int, h: int) -> Image.Image:
72+
crop_w = w
73+
crop_h = h
74+
75+
new_w = int(crop_h * (img.width / img.height))
76+
img_resized = img.resize((new_w, crop_h))
77+
img_resized = img_resized.convert('RGB')
78+
79+
if img_resized.width >= crop_w:
80+
(left, upper) = (int(img_resized.width / 2 - crop_w / 2), 0)
81+
(right, lower) = (
82+
int(img_resized.width / 2 + crop_w / 2),
83+
img_resized.height,
84+
)
85+
86+
img_cropped = img_resized.crop((left, upper, right, lower))
87+
return img_cropped
88+
else:
89+
img_resized = img_resized.resize((crop_w, img_resized.height))
90+
return img_resized
91+
92+
7193
def process_cover_image(path: str):
72-
with Image.open(path) as img:
73-
new_h = 400
74-
new_w = int(new_h * (img.width / img.height))
75-
img_resized = img.resize((new_w, new_h))
76-
img_resized = img_resized.convert('RGB')
77-
78-
crop_w = 270
79-
if img_resized.width >= crop_w:
80-
(left, upper) = (int(img_resized.width / 2 - crop_w / 2), 0)
81-
(right, lower) = (
82-
int(img_resized.width / 2 + crop_w / 2),
83-
img_resized.height,
84-
)
85-
86-
img_cropped = img_resized.crop((left, upper, right, lower))
87-
img_cropped.save(path, "JPEG")
88-
else:
89-
img_resized = img_resized.resize((crop_w, img_resized.height))
90-
img_resized.save(path, "JPEG")
94+
image = Image.open(path)
95+
image = crop_image(image, 270, 400)
96+
image.save(path, "JPEG")
9197

98+
def process_cd_cover(path: str):
99+
cover = Image.open(path)
100+
cover = crop_image(cover, 270, 270)
101+
102+
background = Image.open(path)
103+
background = crop_image(background, 270, 400)
104+
background = background.filter(ImageFilter.GaussianBlur(8))
105+
106+
background.paste(
107+
cover, (0, int((background.height - cover.height) / 2))
108+
)
109+
background.save(path, "JPEG")
110+
111+
92112
def goodreads_search(query: str):
93113
res = requests.get(
94114
"https://www.goodreads.com/book/auto_complete", params={'format': 'json', 'q': query}
@@ -126,4 +146,4 @@ def download_image(url: str, path: str):
126146
f.write(chunk)
127147
return 1
128148
else:
129-
return 0
149+
return 0

0 commit comments

Comments
 (0)