Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hypha
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
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
ots
hypha
Commits
66f5ef98
Commit
66f5ef98
authored
6 years ago
by
Todd Dembrey
Committed by
Fredrik Jonsson
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Create the news import
parent
b65720f3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
opentech/public/news/management/commands/__init__.py
+0
-0
0 additions, 0 deletions
opentech/public/news/management/commands/__init__.py
opentech/public/news/management/commands/migrate_news.py
+121
-0
121 additions, 0 deletions
opentech/public/news/management/commands/migrate_news.py
with
121 additions
and
0 deletions
opentech/public/news/management/commands/__init__.py
0 → 100644
+
0
−
0
View file @
66f5ef98
This diff is collapsed.
Click to expand it.
opentech/public/news/management/commands/migrate_news.py
0 → 100644
+
121
−
0
View file @
66f5ef98
import
argparse
import
json
from
datetime
import
datetime
,
timezone
from
django.core.management.base
import
BaseCommand
from
django.db
import
transaction
from
django.db.utils
import
IntegrityError
from
wagtail.admin.rich_text.converters.editor_html
import
EditorHTMLConverter
from
wagtail.core.rich_text
import
RichText
from
opentech.apply.categories.models
import
Category
,
Option
from
opentech.apply.categories.categories_seed
import
CATEGORIES
from
opentech.public.projects.models
import
ProjectPage
from
opentech.public.news.models
import
(
NewsIndex
,
NewsPage
,
NewsType
,
NewsPageNewsType
,
NewsProjectRelatedPage
,
)
class
Command
(
BaseCommand
):
help
=
"
News migration script. Requires a source JSON file.
"
data
=
[]
terms
=
{}
whitelister
=
EditorHTMLConverter
().
whitelister
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'
source
'
,
type
=
argparse
.
FileType
(
'
r
'
),
help
=
'
Migration source JSON file
'
)
@transaction.atomic
def
handle
(
self
,
*
args
,
**
options
):
# Prepare the list of categories.
for
item
in
CATEGORIES
:
category
,
_
=
Category
.
objects
.
get_or_create
(
name
=
item
[
'
category
'
])
option
,
_
=
Option
.
objects
.
get_or_create
(
value
=
item
[
'
name
'
],
category
=
category
)
self
.
terms
[
item
[
'
tid
'
]]
=
option
self
.
parent_page
=
NewsIndex
.
objects
.
first
()
if
not
self
.
parent_page
:
raise
NewsIndex
.
DoesNotExist
(
'
News Index Page must exist to import News
'
)
self
.
types
=
{
'
4
'
:
NewsType
.
objects
.
get_or_create
(
title
=
'
Press Clip
'
)[
0
],
'
5
'
:
NewsType
.
objects
.
get_or_create
(
title
=
'
Program Update
'
)[
0
],
'
388
'
:
NewsType
.
objects
.
get_or_create
(
title
=
'
Research
'
)[
0
],
}
with
options
[
'
source
'
]
as
json_data
:
self
.
data
=
json
.
load
(
json_data
)
counter
=
0
for
id
in
self
.
data
:
self
.
process
(
id
)
counter
+=
1
self
.
stdout
.
write
(
f
"
Imported
{
counter
}
submissions.
"
)
def
process
(
self
,
id
):
node
=
self
.
data
[
id
]
try
:
news
=
NewsPage
.
objects
.
get
(
drupal_id
=
node
[
'
nid
'
])
except
NewsPage
.
DoesNotExist
:
news
=
NewsPage
(
drupal_id
=
node
[
'
nid
'
])
# TODO timezone?
news
.
submit_time
=
datetime
.
fromtimestamp
(
int
(
node
[
'
created
'
]),
timezone
.
utc
)
news
.
publication_date
=
datetime
.
fromtimestamp
(
int
(
node
[
'
created
'
]),
timezone
.
utc
)
print
(
news
.
publication_date
)
news
.
title
=
node
[
'
title
'
]
news
.
introduction
=
self
.
get_field
(
node
,
'
field_preamble
'
)
cleaned_body
=
self
.
whitelister
.
clean
(
self
.
get_field
(
node
,
'
body
'
))
news
.
body
=
[(
'
paragraph
'
,
RichText
(
cleaned_body
))]
news
.
news_types
.
clear
()
for
news_type
in
self
.
ensure_iterable
(
node
[
'
field_article_type
'
]):
news
.
news_types
.
add
(
NewsPageNewsType
(
news_type
=
self
.
types
[
news_type
[
'
tid
'
]],
))
news
.
related_projects
.
clear
()
for
project
in
self
.
ensure_iterable
(
node
[
'
field_article_project
'
]):
try
:
project_page
=
ProjectPage
.
objects
.
get
(
drupal_id
=
project
[
'
target_id
'
])
except
ProjectPage
.
DoesNotExist
:
self
.
stdout
.
write
(
f
"
Missing project ID
{
project
[
'
target_id
'
]
}
"
)
else
:
news
.
related_projects
.
add
(
NewsProjectRelatedPage
(
page
=
project_page
,
))
try
:
if
not
news
.
get_parent
():
self
.
parent_page
.
add_child
(
instance
=
news
)
news
.
save_revision
().
publish
()
self
.
stdout
.
write
(
f
"
Processed
\"
{
node
[
'
title
'
].
encode
(
'
utf8
'
)
}
\"
(
{
node
[
'
nid
'
]
}
)
"
)
except
IntegrityError
:
self
.
stdout
.
write
(
f
"
*** Skipped
\"
{
node
[
'
title
'
]
}
\"
(
{
node
[
'
nid
'
]
}
) due to IntegrityError
"
)
def
ensure_iterable
(
self
,
value
):
if
isinstance
(
value
,
dict
):
value
=
[
value
]
return
value
def
get_field
(
self
,
node
,
field
):
try
:
return
node
[
field
][
'
safe_value
'
]
except
TypeError
:
pass
try
:
return
node
[
field
][
'
value
'
]
except
TypeError
:
return
''
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