Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Maják
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
Maják
Commits
4c293354
Commit
4c293354
authored
3 years ago
by
OndraRehounek
Browse files
Options
Downloads
Patches
Plain Diff
district & region: Try to find images in img/posts folder on disk
parent
374b58b6
Branches
Branches containing commit
No related tags found
2 merge requests
!444
Release
,
!443
district & region: Try to find images in img/posts folder on disk
Pipeline
#7439
passed
3 years ago
Stage: build
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
district/forms.py
+10
-5
10 additions, 5 deletions
district/forms.py
region/forms.py
+10
-5
10 additions, 5 deletions
region/forms.py
shared/jekyll_import.py
+72
-61
72 additions, 61 deletions
shared/jekyll_import.py
with
92 additions
and
71 deletions
district/forms.py
+
10
−
5
View file @
4c293354
...
@@ -51,7 +51,12 @@ class JekyllImportForm(WagtailAdminPageForm):
...
@@ -51,7 +51,12 @@ class JekyllImportForm(WagtailAdminPageForm):
if
not
cleaned_data
.
get
(
"
do_import
"
):
if
not
cleaned_data
.
get
(
"
do_import
"
):
return
cleaned_data
return
cleaned_data
else
:
if
cleaned_data
.
get
(
"
do_import
"
)
and
not
self
.
instance
.
id
:
self
.
add_error
(
"
do_import
"
,
"
Import proveďte prosím až po vytvoření stránky
"
)
if
not
cleaned_data
.
get
(
"
collection
"
):
if
not
cleaned_data
.
get
(
"
collection
"
):
self
.
add_error
(
"
collection
"
,
"
Pro import je toto pole povinné
"
)
self
.
add_error
(
"
collection
"
,
"
Pro import je toto pole povinné
"
)
if
not
cleaned_data
.
get
(
"
jekyll_repo_url
"
):
if
not
cleaned_data
.
get
(
"
jekyll_repo_url
"
):
...
...
This diff is collapsed.
Click to expand it.
region/forms.py
+
10
−
5
View file @
4c293354
...
@@ -51,7 +51,12 @@ class JekyllImportForm(WagtailAdminPageForm):
...
@@ -51,7 +51,12 @@ class JekyllImportForm(WagtailAdminPageForm):
if
not
cleaned_data
.
get
(
"
do_import
"
):
if
not
cleaned_data
.
get
(
"
do_import
"
):
return
cleaned_data
return
cleaned_data
else
:
if
cleaned_data
.
get
(
"
do_import
"
)
and
not
self
.
instance
.
id
:
self
.
add_error
(
"
do_import
"
,
"
Import proveďte prosím až po vytvoření stránky
"
)
if
not
cleaned_data
.
get
(
"
collection
"
):
if
not
cleaned_data
.
get
(
"
collection
"
):
self
.
add_error
(
"
collection
"
,
"
Pro import je toto pole povinné
"
)
self
.
add_error
(
"
collection
"
,
"
Pro import je toto pole povinné
"
)
if
not
cleaned_data
.
get
(
"
jekyll_repo_url
"
):
if
not
cleaned_data
.
get
(
"
jekyll_repo_url
"
):
...
...
This diff is collapsed.
Click to expand it.
shared/jekyll_import.py
+
72
−
61
View file @
4c293354
...
@@ -100,7 +100,7 @@ def get_or_create_image(
...
@@ -100,7 +100,7 @@ def get_or_create_image(
)
->
Image
or
None
:
)
->
Image
or
None
:
"""
"""
Funkce, která se snaží najít a vrátit Wagtail Image.
Funkce, která se snaží najít a vrátit Wagtail Image.
Nejdříve hledá v existujících podle cesty...
Nejdříve hledá v existujících podle cesty
, resp. title
...
Pak zkusí najít soubor fyzicky na disku...
Pak zkusí najít soubor fyzicky na disku...
Pak zkusí ještě assets/img adresář...
Pak zkusí ještě assets/img adresář...
Pak zkusí stáhnout image z https://a.pirati.cz...
Pak zkusí stáhnout image z https://a.pirati.cz...
...
@@ -115,13 +115,15 @@ def get_or_create_image(
...
@@ -115,13 +115,15 @@ def get_or_create_image(
if
Image
.
objects
.
filter
(
title
=
file_path
).
exists
():
if
Image
.
objects
.
filter
(
title
=
file_path
).
exists
():
return
Image
.
objects
.
filter
(
title
=
file_path
).
first
(),
""
return
Image
.
objects
.
filter
(
title
=
file_path
).
first
(),
""
else
:
try
:
try
:
file
=
ImageFile
(
open
(
os
.
path
.
join
(
path
,
file_path
),
"
rb
"
),
name
=
file_path
)
file
=
ImageFile
(
open
(
os
.
path
.
join
(
path
,
file_path
),
"
rb
"
),
name
=
file_path
)
image
=
Image
(
title
=
file_path
,
file
=
file
,
collection
=
collection
)
image
=
Image
(
title
=
file_path
,
file
=
file
,
collection
=
collection
)
image
.
save
()
image
.
save
()
return
image
,
""
return
image
,
""
except
FileNotFoundError
:
except
FileNotFoundError
:
pass
# cesta pomocí file_path neexisuje, jdeme dál
try
:
try
:
file
=
ImageFile
(
file
=
ImageFile
(
open
(
os
.
path
.
join
(
path
,
"
assets/img
"
,
file_path
),
"
rb
"
),
open
(
os
.
path
.
join
(
path
,
"
assets/img
"
,
file_path
),
"
rb
"
),
...
@@ -131,14 +133,27 @@ def get_or_create_image(
...
@@ -131,14 +133,27 @@ def get_or_create_image(
image
.
save
()
image
.
save
()
return
image
,
""
return
image
,
""
except
FileNotFoundError
:
except
FileNotFoundError
:
pass
# cesta s vložením "assets/img" před file_path neexisuje, jdeme dál
try
:
file
=
ImageFile
(
open
(
os
.
path
.
join
(
path
,
"
assets/img/posts
"
,
file_path
),
"
rb
"
),
name
=
file_path
,
)
image
=
Image
(
title
=
file_path
,
file
=
file
,
collection
=
collection
)
image
.
save
()
return
image
,
""
except
FileNotFoundError
:
pass
# ani cesta "assets/img/posts" nefunguje, jdeme zkusit assets server a.pirati.cz
fallback_name
=
(
fallback_name
=
(
""
.
join
(
random
.
choice
(
string
.
ascii_lowercase
)
for
_
in
range
(
10
))
""
.
join
(
random
.
choice
(
string
.
ascii_lowercase
)
for
_
in
range
(
10
))
+
"
.jpg
"
+
"
.jpg
"
)
# někdy je název obrzau spojený s poznámkou apod., připravíme si fallback name
)
# i PNG...?
img_name
=
file_path
.
split
(
"
/
"
)[
-
1
]
or
fallback_name
img_name
=
file_path
.
split
(
"
/
"
)[
-
1
]
or
fallback_name
img_assets_folder
=
repo_name
.
split
(
"
.
"
)[
0
]
img_assets_folder
=
repo_name
.
split
(
"
.
"
)[
0
]
# např. "praha" z praha.pirati.cz
img_url
=
"
https://a.pirati.cz/{}/img/{}
"
.
format
(
img_url
=
"
https://a.pirati.cz/{}/img/{}
"
.
format
(
img_assets_folder
,
file_path
.
split
(
"
#
"
)[
0
]
img_assets_folder
,
file_path
.
split
(
"
#
"
)[
0
]
# cistime nazev od poznamek apod
)
)
img_path
=
os
.
path
.
join
(
path
,
img_name
)
img_path
=
os
.
path
.
join
(
path
,
img_name
)
try
:
try
:
...
@@ -150,14 +165,9 @@ def get_or_create_image(
...
@@ -150,14 +165,9 @@ def get_or_create_image(
img_assets_folder
,
img_name
.
split
()[
0
]
img_assets_folder
,
img_name
.
split
()[
0
]
)
)
urllib
.
request
.
urlretrieve
(
img_url
,
img_path
)
urllib
.
request
.
urlretrieve
(
img_url
,
img_path
)
except
(
except
(
HTTPError
,
UnicodeEncodeError
,
InvalidURL
,
IsADirectoryError
):
HTTPError
,
msg
=
"
Nedohledán obrázek při importu článků - ani na disku, ani na URL
"
UnicodeEncodeError
,
log_message
=
"
{}: cesta {}, URL {}
\n
"
.
format
(
msg
,
file_path
,
img_url
)
InvalidURL
,
IsADirectoryError
,
):
msg
=
"
Nedohledán obrázek při importu článků
"
log_message
=
"
{} - {}
\n
"
.
format
(
msg
,
img_url
)
logger
.
warning
(
logger
.
warning
(
msg
,
msg
,
extra
=
{
extra
=
{
...
@@ -170,6 +180,7 @@ def get_or_create_image(
...
@@ -170,6 +180,7 @@ def get_or_create_image(
file
=
ImageFile
(
open
(
img_path
,
"
rb
"
),
name
=
img_path
)
file
=
ImageFile
(
open
(
img_path
,
"
rb
"
),
name
=
img_path
)
image
=
Image
(
title
=
file_path
,
file
=
file
,
collection
=
collection
)
image
=
Image
(
title
=
file_path
,
file
=
file
,
collection
=
collection
)
try
:
try
:
image
.
save
()
image
.
save
()
except
Exception
as
e
:
except
Exception
as
e
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment