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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
Maják
Commits
d50aa43b
Commit
d50aa43b
authored
3 years ago
by
OndraRehounek
Committed by
jan.bednarik
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
working import prototype for at least second web
parent
fa5777f8
No related branches found
No related tags found
2 merge requests
!442
Release
,
!432
Feature/majak imports
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
district/jekyll_import.py
+37
-17
37 additions, 17 deletions
district/jekyll_import.py
with
37 additions
and
17 deletions
district/jekyll_import.py
+
37
−
17
View file @
d50aa43b
...
@@ -18,6 +18,7 @@ from markdown import Markdown
...
@@ -18,6 +18,7 @@ from markdown import Markdown
from
markdown.extensions
import
Extension
from
markdown.extensions
import
Extension
from
markdown.inlinepatterns
import
InlineProcessor
from
markdown.inlinepatterns
import
InlineProcessor
from
wagtail.contrib.redirects.models
import
Redirect
from
wagtail.contrib.redirects.models
import
Redirect
from
wagtail.core.rich_text
import
RichText
from
wagtail.images.models
import
Image
from
wagtail.images.models
import
Image
# Wagtail to portrebuje https://docs.wagtail.io/en/stable/extending/rich_text_internals.html#data-format
# Wagtail to portrebuje https://docs.wagtail.io/en/stable/extending/rich_text_internals.html#data-format
...
@@ -54,7 +55,7 @@ class ImgProcessor(InlineProcessor):
...
@@ -54,7 +55,7 @@ class ImgProcessor(InlineProcessor):
el
.
attrib
[
"
format
"
]
=
"
left
"
el
.
attrib
[
"
format
"
]
=
"
left
"
collection
=
get_collection
()
collection
=
get_collection
()
image_obj
=
get_or_create_image
(
image_obj
=
get_or_create_image
(
params
[
"
path
"
],
m
.
group
(
2
),
collection
=
collection
params
[
"
path
"
],
m
.
group
(
2
),
collection
=
collection
,
repo_name
=
""
)
)
el
.
attrib
[
"
id
"
]
=
str
(
image_obj
.
pk
)
el
.
attrib
[
"
id
"
]
=
str
(
image_obj
.
pk
)
return
el
,
m
.
start
(
0
),
m
.
end
(
0
)
return
el
,
m
.
start
(
0
),
m
.
end
(
0
)
...
@@ -84,7 +85,7 @@ def get_site_config(path) -> dict:
...
@@ -84,7 +85,7 @@ def get_site_config(path) -> dict:
return
config
return
config
def
import_post
(
path
,
file_path
,
parent
,
title_suffix
,
dry_run
):
def
import_post
(
path
,
file_path
,
parent
,
title_suffix
,
dry_run
,
repo_name
):
from
district.models
import
DistrictArticlePage
from
district.models
import
DistrictArticlePage
with
open
(
os
.
path
.
join
(
path
,
file_path
),
"
rt
"
)
as
f
:
with
open
(
os
.
path
.
join
(
path
,
file_path
),
"
rt
"
)
as
f
:
...
@@ -104,7 +105,8 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
...
@@ -104,7 +105,8 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
article
=
DistrictArticlePage
()
article
=
DistrictArticlePage
()
article
.
text
=
html
# article.text = html
article
.
content
=
[(
"
text
"
,
RichText
(
html
))]
article
.
perex
=
get_perex
(
md
)
article
.
perex
=
get_perex
(
md
)
meta_date
=
meta
[
"
date
"
]
meta_date
=
meta
[
"
date
"
]
...
@@ -123,7 +125,10 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
...
@@ -123,7 +125,10 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
# article.tags.add(tag)
# article.tags.add(tag)
collection
=
get_collection
()
collection
=
get_collection
()
article
.
image
=
get_or_create_image
(
path
,
meta
[
"
image
"
],
collection
=
collection
)
if
meta
.
get
(
"
image
"
,
None
):
article
.
image
=
get_or_create_image
(
path
,
meta
[
"
image
"
],
collection
=
collection
,
repo_name
=
repo_name
)
if
dry_run
:
if
dry_run
:
return
article
,
True
return
article
,
True
...
@@ -132,7 +137,7 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
...
@@ -132,7 +137,7 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
parent
.
add_child
(
instance
=
article
)
parent
.
add_child
(
instance
=
article
)
stdout
.
write
(
"
Creating article: %s
"
%
article
)
stdout
.
write
(
"
Creating article: %s
"
%
article
)
rev
=
article
.
save_revision
()
rev
=
article
.
save_revision
()
if
meta
[
"
published
"
]
:
if
meta
.
get
(
"
published
"
,
True
)
:
rev
.
publish
()
rev
.
publish
()
except
Exception
as
e
:
except
Exception
as
e
:
message_list
.
append
(
message_list
.
append
(
...
@@ -150,22 +155,36 @@ def get_collection():
...
@@ -150,22 +155,36 @@ def get_collection():
return
params
[
"
kolekce
"
]
return
params
[
"
kolekce
"
]
def
get_path
(
url
:
str
,
use_git
:
bool
)
->
str
:
def
get_path
_and_repo_name
(
url
:
str
,
use_git
:
bool
)
->
(
str
,
str
)
:
if
use_git
:
if
use_git
:
return
clone_repo
(
url
)
return
clone_repo
(
url
)
else
:
else
:
return
download_repo_as_zip
(
url
)
return
download_repo_as_zip
(
url
)
def
get_or_create_image
(
path
,
file_path
,
collection
):
def
get_or_create_image
(
path
,
file_path
,
collection
,
repo_name
):
file_path
=
file_path
.
lstrip
(
"
/
"
)
file_path
=
file_path
.
lstrip
(
"
/
"
)
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
:
else
:
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
:
img_name
=
file_path
.
split
(
"
/
"
)[
-
1
]
img_assets_folder
=
repo_name
.
split
(
"
.
"
)[
0
]
# TODO make as form field
img_url
=
"
https://a.pirati.cz/{}/img/posts/{}
"
.
format
(
img_assets_folder
,
img_name
)
img_path
=
os
.
path
.
join
(
path
,
img_name
)
urllib
.
request
.
urlretrieve
(
img_url
,
img_path
)
file
=
ImageFile
(
open
(
img_path
,
"
rb
"
),
name
=
img_path
)
image
=
Image
(
title
=
file_path
,
file
=
file
,
collection
=
collection
)
image
.
save
()
return
image
def
get_title_from_site_config
(
site_config
:
dict
)
->
str
:
def
get_title_from_site_config
(
site_config
:
dict
)
->
str
:
...
@@ -174,7 +193,7 @@ def get_title_from_site_config(site_config: dict) -> str:
...
@@ -174,7 +193,7 @@ def get_title_from_site_config(site_config: dict) -> str:
return
""
return
""
def
clone_repo
(
url
:
str
)
->
str
:
def
clone_repo
(
url
:
str
)
->
(
str
,
str
)
:
"""
"""
Naclonuje repo do tmp s využitím gitu a vrátí cestu k němu.
Naclonuje repo do tmp s využitím gitu a vrátí cestu k němu.
Pokud URL končí lomítkem, odebereme ho, a vezmeme jako název repozitáře
Pokud URL končí lomítkem, odebereme ho, a vezmeme jako název repozitáře
...
@@ -193,10 +212,10 @@ def clone_repo(url: str) -> str:
...
@@ -193,10 +212,10 @@ def clone_repo(url: str) -> str:
os
.
chdir
(
path
)
os
.
chdir
(
path
)
os
.
system
(
"
git clone --depth 1 {}
"
.
format
(
url
))
os
.
system
(
"
git clone --depth 1 {}
"
.
format
(
url
))
return
repo_path
return
repo_path
,
repo_name
def
download_repo_as_zip
(
url
:
str
)
->
str
:
def
download_repo_as_zip
(
url
:
str
)
->
(
str
,
str
)
:
"""
"""
Stáhne .zip repa, extrahuje a vrátí cestu k extrahovanému repu.
Stáhne .zip repa, extrahuje a vrátí cestu k extrahovanému repu.
Hodně nešikovné je, že extrahovaná složka má ještě suffix
"
-gh-pages
"
Hodně nešikovné je, že extrahovaná složka má ještě suffix
"
-gh-pages
"
...
@@ -215,7 +234,7 @@ def download_repo_as_zip(url: str) -> str:
...
@@ -215,7 +234,7 @@ def download_repo_as_zip(url: str) -> str:
with
zipfile
.
ZipFile
(
zip_path
,
"
r
"
)
as
zip_ref
:
with
zipfile
.
ZipFile
(
zip_path
,
"
r
"
)
as
zip_ref
:
zip_ref
.
extractall
(
path
)
zip_ref
.
extractall
(
path
)
return
os
.
path
.
join
(
path
,
"
{}-gh-pages
"
.
format
(
repo_name
))
return
os
.
path
.
join
(
path
,
"
{}-gh-pages
"
.
format
(
repo_name
))
,
repo_name
def
perform_import
(
def
perform_import
(
...
@@ -231,7 +250,8 @@ def perform_import(
...
@@ -231,7 +250,8 @@ def perform_import(
params
[
"
kolekce
"
]
=
collection
params
[
"
kolekce
"
]
=
collection
site
=
article_parent_page
.
get_site
()
site
=
article_parent_page
.
get_site
()
path
=
params
[
"
path
"
]
=
get_path
(
url
=
url
,
use_git
=
use_git
)
params
[
"
path
"
],
repo_name
=
get_path_and_repo_name
(
url
=
url
,
use_git
=
use_git
)
path
=
params
[
"
path
"
]
site_config
=
get_site_config
(
path
)
site_config
=
get_site_config
(
path
)
title_suffix
=
get_title_from_site_config
(
site_config
)
title_suffix
=
get_title_from_site_config
(
site_config
)
...
@@ -249,7 +269,7 @@ def perform_import(
...
@@ -249,7 +269,7 @@ def perform_import(
if
ext
==
"
md
"
:
if
ext
==
"
md
"
:
article
,
success
=
import_post
(
article
,
success
=
import_post
(
path
,
fname
,
article_parent_page
,
title_suffix
,
dry_run
path
,
fname
,
article_parent_page
,
title_suffix
,
dry_run
,
repo_name
)
)
if
not
success
:
if
not
success
:
...
...
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