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
b250825a
You need to sign in or sign up before continuing.
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
No related branches found
No related tags found
2 merge requests
!442
Release
,
!432
Feature/majak imports
Changes
3
Show 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.contrib.messages
import
ERROR
,
WARNING
from
wagtail.admin.forms
import
WagtailAdminPageForm
from
wagtail.core.models.collections
import
Collection
...
...
@@ -63,29 +64,26 @@ class JekyllImportForm(WagtailAdminPageForm):
return
cleaned_data
if
cleaned_data
.
get
(
"
dry_run
"
):
error_list
=
perform_import
(
article_parent_page
=
self
.
instance
,
collection
=
self
.
cleaned_data
[
"
collection
"
],
url
=
self
.
cleaned_data
[
"
jekyll_repo_url
"
],
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
)
import_message_list
=
self
.
handle_import
()
for
message
in
import_message_list
:
if
message
[
"
level
"
]
in
(
WARNING
,
ERROR
):
self
.
add_error
(
"
jekyll_repo_url
"
,
message
[
"
text
"
])
return
cleaned_data
def
save
(
self
,
commit
=
True
):
if
self
.
cleaned_data
.
get
(
"
do_import
"
)
and
not
self
.
cleaned_data
[
"
dry_run
"
]:
error_list
=
perform_import
(
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
=
False
,
dry_run
=
self
.
cleaned_data
[
"
dry_run
"
]
,
use_git
=
self
.
cleaned_data
[
"
use_git
"
],
)
self
.
instance
.
import_error_list
=
error_list
self
.
instance
.
import_message_list
=
import_message_list
return
import_message_list
def
save
(
self
,
commit
=
True
):
if
self
.
cleaned_data
.
get
(
"
do_import
"
)
and
not
self
.
cleaned_data
[
"
dry_run
"
]:
self
.
handle_import
()
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
import
markdown.serializers
import
yaml
from
django.contrib.messages
import
ERROR
,
INFO
,
SUCCESS
,
WARNING
from
django.core.files.images
import
ImageFile
from
django.utils.dateparse
import
parse_date
from
markdown
import
Markdown
...
...
@@ -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
markdown
.
serializers
.
HTML_EMPTY
.
add
(
"
embed
"
)
message_list
=
[]
# Plain format pro perex
def
unmark_element
(
element
,
stream
=
None
):
...
...
@@ -93,13 +96,11 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
if
DistrictArticlePage
.
objects
.
filter
(
title
=
meta
[
"
title
"
]).
exists
():
for
article
in
DistrictArticlePage
.
objects
.
filter
(
title
=
meta
[
"
title
"
]):
if
article
.
date
==
parse_date
(
meta
[
"
date
"
].
split
()[
0
]):
warning
=
"
Article already imported: %s
"
%
article
stdout
.
write
(
warning
)
if
dry_run
:
return
article
,
warning
msg
=
"
Článek již existuje: %s
"
%
article
stdout
.
write
(
msg
)
message_list
.
append
({
"
level
"
:
INFO
,
"
text
"
:
msg
})
return
article
,
""
return
article
,
False
article
=
DistrictArticlePage
()
...
...
@@ -125,8 +126,8 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
article
.
image
=
get_or_create_image
(
path
,
meta
[
"
image
"
],
collection
=
collection
)
if
dry_run
:
return
article
,
""
else
:
return
article
,
True
try
:
parent
.
add_child
(
instance
=
article
)
stdout
.
write
(
"
Creating article: %s
"
%
article
)
...
...
@@ -134,9 +135,15 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
if
meta
[
"
published
"
]:
rev
.
publish
()
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
():
...
...
@@ -213,9 +220,13 @@ def download_repo_as_zip(url: str) -> str:
def
perform_import
(
article_parent_page
,
collection
,
url
:
str
,
dry_run
:
bool
,
use_git
:
bool
)
->
"
List[str]
"
:
error_list
=
[]
)
->
"
List[dict]
"
:
"""
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
site
=
article_parent_page
.
get_site
()
...
...
@@ -237,15 +248,15 @@ def perform_import(
ext
=
match
.
group
(
5
)
if
ext
==
"
md
"
:
article
,
error
=
import_post
(
article
,
success
=
import_post
(
path
,
fname
,
article_parent_page
,
title_suffix
,
dry_run
)
if
error
:
error_list
.
append
(
error
)
if
not
success
:
continue
if
dry_run
:
success_counter
+=
1
continue
Redirect
.
objects
.
get_or_create
(
...
...
@@ -254,15 +265,23 @@ def perform_import(
%
(
articlepath
,
y
,
m
.
zfill
(
2
),
d
.
zfill
(
2
),
slug
),
defaults
=
{
"
is_permanent
"
:
True
,
"
redirect_page
"
:
article
},
)
success_counter
+=
1
else
:
error
=
"
ERROR:
This extension is not implemented
: %s
"
%
ext
error
_list
.
append
(
error
)
stdout
.
write
(
error
)
msg
=
"
ERROR:
Nepodporovaná přípona souboru
: %s
"
%
ext
message
_list
.
append
(
{
"
level
"
:
ERROR
,
"
text
"
:
msg
}
)
stdout
.
write
(
msg
)
else
:
warning
=
"
WARNING:
Skipp
ing
: %s
"
%
fn
stdout
.
write
(
warnin
g
)
msg
=
"
Skipp
ed
: %s
"
%
fn
stdout
.
write
(
ms
g
)
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.core
import
hooks
from
.models
import
District
Home
Page
from
.models
import
District
Articles
Page
@hooks.register
(
"
after_edit_page
"
)
...
...
@@ -9,9 +10,9 @@ from .models import DistrictHomePage
def
handle_page_import
(
request
,
page
):
# def after_create_page(request, page):
"""
Block awesome page deletion and show a message.
"""
if
request
.
method
==
"
POST
"
and
page
.
specific_class
in
[
District
Home
Page
]:
i
f
getattr
(
page
,
"
import_message
s
"
,
None
):
message
s
.
warning
(
request
,
str
(
page
.
import_error_list
)
)
if
request
.
method
==
"
POST
"
and
page
.
specific_class
in
[
District
Articles
Page
]:
f
or
message
in
getattr
(
page
,
"
import_message
_list
"
,
[]
):
add_
message
(
request
,
message
[
"
level
"
],
message
[
"
text
"
]
)
# 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