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
b250825a
Commit
b250825a
authored
3 years ago
by
OndraRehounek
Committed by
jan.bednarik
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
messages refactor
parent
00154e84
Branches
Branches containing commit
No related tags found
2 merge requests
!442
Release
,
!432
Feature/majak imports
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
district/forms.py
+17
-19
17 additions, 19 deletions
district/forms.py
district/jekyll_import.py
+49
-30
49 additions, 30 deletions
district/jekyll_import.py
district/wagtail_hooks.py
+5
-4
5 additions, 4 deletions
district/wagtail_hooks.py
with
71 additions
and
53 deletions
district/forms.py
+
17
−
19
View file @
b250825a
from
django
import
forms
from
django
import
forms
from
django.contrib.messages
import
ERROR
,
WARNING
from
wagtail.admin.forms
import
WagtailAdminPageForm
from
wagtail.admin.forms
import
WagtailAdminPageForm
from
wagtail.core.models.collections
import
Collection
from
wagtail.core.models.collections
import
Collection
...
@@ -63,29 +64,26 @@ class JekyllImportForm(WagtailAdminPageForm):
...
@@ -63,29 +64,26 @@ class JekyllImportForm(WagtailAdminPageForm):
return
cleaned_data
return
cleaned_data
if
cleaned_data
.
get
(
"
dry_run
"
):
if
cleaned_data
.
get
(
"
dry_run
"
):
error_list
=
perform_import
(
import_message_list
=
self
.
handle_import
()
article_parent_page
=
self
.
instance
,
for
message
in
import_message_list
:
collection
=
self
.
cleaned_data
[
"
collection
"
],
if
message
[
"
level
"
]
in
(
WARNING
,
ERROR
):
url
=
self
.
cleaned_data
[
"
jekyll_repo_url
"
],
self
.
add_error
(
"
jekyll_repo_url
"
,
message
[
"
text
"
])
dry_run
=
True
,
use_git
=
self
.
cleaned_data
[
"
use_git
"
],
)
# self.instance.import_error_list = error_list
for
error
in
error_list
:
self
.
add_error
(
"
jekyll_repo_url
"
,
error
)
return
cleaned_data
return
cleaned_data
def
handle_import
(
self
):
import_message_list
=
perform_import
(
article_parent_page
=
self
.
instance
,
collection
=
self
.
cleaned_data
[
"
collection
"
],
url
=
self
.
cleaned_data
[
"
jekyll_repo_url
"
],
dry_run
=
self
.
cleaned_data
[
"
dry_run
"
],
use_git
=
self
.
cleaned_data
[
"
use_git
"
],
)
self
.
instance
.
import_message_list
=
import_message_list
return
import_message_list
def
save
(
self
,
commit
=
True
):
def
save
(
self
,
commit
=
True
):
if
self
.
cleaned_data
.
get
(
"
do_import
"
)
and
not
self
.
cleaned_data
[
"
dry_run
"
]:
if
self
.
cleaned_data
.
get
(
"
do_import
"
)
and
not
self
.
cleaned_data
[
"
dry_run
"
]:
error_list
=
perform_import
(
self
.
handle_import
()
article_parent_page
=
self
.
instance
,
collection
=
self
.
cleaned_data
[
"
collection
"
],
url
=
self
.
cleaned_data
[
"
jekyll_repo_url
"
],
dry_run
=
False
,
use_git
=
self
.
cleaned_data
[
"
use_git
"
],
)
self
.
instance
.
import_error_list
=
error_list
return
super
().
save
(
commit
=
commit
)
return
super
().
save
(
commit
=
commit
)
This diff is collapsed.
Click to expand it.
district/jekyll_import.py
+
49
−
30
View file @
b250825a
...
@@ -11,6 +11,7 @@ from typing import List
...
@@ -11,6 +11,7 @@ from typing import List
import
markdown.serializers
import
markdown.serializers
import
yaml
import
yaml
from
django.contrib.messages
import
ERROR
,
INFO
,
SUCCESS
,
WARNING
from
django.core.files.images
import
ImageFile
from
django.core.files.images
import
ImageFile
from
django.utils.dateparse
import
parse_date
from
django.utils.dateparse
import
parse_date
from
markdown
import
Markdown
from
markdown
import
Markdown
...
@@ -22,6 +23,8 @@ from wagtail.images.models import Image
...
@@ -22,6 +23,8 @@ 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
markdown
.
serializers
.
HTML_EMPTY
.
add
(
"
embed
"
)
markdown
.
serializers
.
HTML_EMPTY
.
add
(
"
embed
"
)
message_list
=
[]
# Plain format pro perex
# Plain format pro perex
def
unmark_element
(
element
,
stream
=
None
):
def
unmark_element
(
element
,
stream
=
None
):
...
@@ -93,13 +96,11 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
...
@@ -93,13 +96,11 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
if
DistrictArticlePage
.
objects
.
filter
(
title
=
meta
[
"
title
"
]).
exists
():
if
DistrictArticlePage
.
objects
.
filter
(
title
=
meta
[
"
title
"
]).
exists
():
for
article
in
DistrictArticlePage
.
objects
.
filter
(
title
=
meta
[
"
title
"
]):
for
article
in
DistrictArticlePage
.
objects
.
filter
(
title
=
meta
[
"
title
"
]):
if
article
.
date
==
parse_date
(
meta
[
"
date
"
].
split
()[
0
]):
if
article
.
date
==
parse_date
(
meta
[
"
date
"
].
split
()[
0
]):
warning
=
"
Article already imported: %s
"
%
article
msg
=
"
Článek již existuje: %s
"
%
article
stdout
.
write
(
warning
)
stdout
.
write
(
msg
)
message_list
.
append
({
"
level
"
:
INFO
,
"
text
"
:
msg
})
if
dry_run
:
return
article
,
warning
return
article
,
""
return
article
,
False
article
=
DistrictArticlePage
()
article
=
DistrictArticlePage
()
...
@@ -125,18 +126,24 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
...
@@ -125,18 +126,24 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
article
.
image
=
get_or_create_image
(
path
,
meta
[
"
image
"
],
collection
=
collection
)
article
.
image
=
get_or_create_image
(
path
,
meta
[
"
image
"
],
collection
=
collection
)
if
dry_run
:
if
dry_run
:
return
article
,
""
return
article
,
True
else
:
try
:
try
:
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
[
"
published
"
]:
rev
.
publish
()
rev
.
publish
()
except
Exception
as
e
:
except
Exception
as
e
:
return
article
,
"
Nelze uložit článek {}: {}
"
.
format
(
article
.
title
,
str
(
e
))
message_list
.
append
(
{
"
level
"
:
WARNING
if
dry_run
else
ERROR
,
"
text
"
:
"
Nelze uložit článek {}: {}
"
.
format
(
article
.
title
,
str
(
e
)),
}
)
return
article
,
False
return
article
,
""
return
article
,
True
def
get_collection
():
def
get_collection
():
...
@@ -213,9 +220,13 @@ def download_repo_as_zip(url: str) -> str:
...
@@ -213,9 +220,13 @@ def download_repo_as_zip(url: str) -> str:
def
perform_import
(
def
perform_import
(
article_parent_page
,
collection
,
url
:
str
,
dry_run
:
bool
,
use_git
:
bool
article_parent_page
,
collection
,
url
:
str
,
dry_run
:
bool
,
use_git
:
bool
)
->
"
List[str]
"
:
)
->
"
List[dict]
"
:
"""
error_list
=
[]
Přijímá parent page pro články, kolekci pro obrázky, url pro stažení (zip nebo git
repo, boolean jestli jde o testovací běh a boolean, zda použít git (anebo zip)).
Vrací list dict pro requests messages (klíče level, text).
"""
success_counter
=
0
params
[
"
kolekce
"
]
=
collection
params
[
"
kolekce
"
]
=
collection
site
=
article_parent_page
.
get_site
()
site
=
article_parent_page
.
get_site
()
...
@@ -237,15 +248,15 @@ def perform_import(
...
@@ -237,15 +248,15 @@ def perform_import(
ext
=
match
.
group
(
5
)
ext
=
match
.
group
(
5
)
if
ext
==
"
md
"
:
if
ext
==
"
md
"
:
article
,
error
=
import_post
(
article
,
success
=
import_post
(
path
,
fname
,
article_parent_page
,
title_suffix
,
dry_run
path
,
fname
,
article_parent_page
,
title_suffix
,
dry_run
)
)
if
error
:
if
not
success
:
error_list
.
append
(
error
)
continue
continue
if
dry_run
:
if
dry_run
:
success_counter
+=
1
continue
continue
Redirect
.
objects
.
get_or_create
(
Redirect
.
objects
.
get_or_create
(
...
@@ -254,15 +265,23 @@ def perform_import(
...
@@ -254,15 +265,23 @@ def perform_import(
%
(
articlepath
,
y
,
m
.
zfill
(
2
),
d
.
zfill
(
2
),
slug
),
%
(
articlepath
,
y
,
m
.
zfill
(
2
),
d
.
zfill
(
2
),
slug
),
defaults
=
{
"
is_permanent
"
:
True
,
"
redirect_page
"
:
article
},
defaults
=
{
"
is_permanent
"
:
True
,
"
redirect_page
"
:
article
},
)
)
success_counter
+=
1
else
:
else
:
error
=
"
ERROR:
This extension is not implemented
: %s
"
%
ext
msg
=
"
ERROR:
Nepodporovaná přípona souboru
: %s
"
%
ext
error
_list
.
append
(
error
)
message
_list
.
append
(
{
"
level
"
:
ERROR
,
"
text
"
:
msg
}
)
stdout
.
write
(
error
)
stdout
.
write
(
msg
)
else
:
else
:
warning
=
"
WARNING:
Skipp
ing
: %s
"
%
fn
msg
=
"
Skipp
ed
: %s
"
%
fn
stdout
.
write
(
warnin
g
)
stdout
.
write
(
ms
g
)
if
dry_run
:
if
dry_run
:
error_list
.
append
(
warning
)
message_list
.
append
({
"
level
"
:
WARNING
,
"
text
"
:
msg
})
if
success_counter
:
base_msg
=
"
Lze importovat
"
if
dry_run
else
"
Úspěšně naimportováno
"
message_list
.
append
(
{
"
level
"
:
SUCCESS
,
"
text
"
:
"
{} {} článků
"
.
format
(
base_msg
,
success_counter
)}
)
return
error
_list
return
message
_list
This diff is collapsed.
Click to expand it.
district/wagtail_hooks.py
+
5
−
4
View file @
b250825a
from
django.contrib.messages
import
ERROR
,
SUCCESS
,
WARNING
,
add_message
from
wagtail.admin
import
messages
from
wagtail.admin
import
messages
from
wagtail.core
import
hooks
from
wagtail.core
import
hooks
from
.models
import
District
Home
Page
from
.models
import
District
Articles
Page
@hooks.register
(
"
after_edit_page
"
)
@hooks.register
(
"
after_edit_page
"
)
...
@@ -9,9 +10,9 @@ from .models import DistrictHomePage
...
@@ -9,9 +10,9 @@ from .models import DistrictHomePage
def
handle_page_import
(
request
,
page
):
# def after_create_page(request, page):
def
handle_page_import
(
request
,
page
):
# def after_create_page(request, page):
"""
Block awesome page deletion and show a message.
"""
"""
Block awesome page deletion and show a message.
"""
if
request
.
method
==
"
POST
"
and
page
.
specific_class
in
[
District
Home
Page
]:
if
request
.
method
==
"
POST
"
and
page
.
specific_class
in
[
District
Articles
Page
]:
i
f
getattr
(
page
,
"
import_message
s
"
,
None
):
f
or
message
in
getattr
(
page
,
"
import_message
_list
"
,
[]
):
message
s
.
warning
(
request
,
str
(
page
.
import_error_list
)
)
add_
message
(
request
,
message
[
"
level
"
],
message
[
"
text
"
]
)
# import re
# import re
...
...
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