Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
15
Issues
15
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
La Quadrature du Net
rpteam
rp
Commits
ba1a8794
Commit
ba1a8794
authored
Apr 02, 2019
by
Okhin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding a import_old_rp manager comand
parent
a4f84f9b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
97 additions
and
0 deletions
+97
-0
apps/core/management/commands/import_old_rp.py
apps/core/management/commands/import_old_rp.py
+97
-0
No files found.
apps/core/management/commands/import_old_rp.py
0 → 100644
View file @
ba1a8794
import
re
import
MySQLdb
as
ms
from
MySQLdb.cursors
import
DictCursor
from
django.core.management.base
import
BaseCommand
from
rp.models
import
Article
class
Command
(
BaseCommand
):
help
=
"""
Import data from the old press review. Should only be used by
LQDN staff since the dataformat of the old website only make
sense in their context.
It takes three arguments:
--host hostname
--sql_user username
--sql_password password
host defaults to 127.0.0.1.
"""
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'--host'
,
default
=
'127.0.0.1'
,
dest
=
'host'
,
nargs
=
'?'
)
parser
.
add_argument
(
'--sql_user'
,
dest
=
'user'
,
nargs
=
'+'
)
parser
.
add_argument
(
'--sql_password'
,
dest
=
'password'
,
nargs
=
'+'
)
def
handle
(
self
,
*
args
,
**
options
):
db
=
ms
.
connect
(
host
=
options
[
'host'
],
user
=
options
[
'user'
][
0
],
password
=
options
[
'password'
][
0
],
db
=
'site'
)
c
=
db
.
cursor
(
DictCursor
)
# First, let's get the data from presse table
presse
=
c
.
execute
(
"SELECT * FROM presse"
)
print
(
"Importing 0/{} from previous database"
.
format
(
presse
))
# And here we go
done
=
0
for
item
in
c
.
fetchall
():
done
+=
1
print
(
"Importing {}/{} from previous database"
.
format
(
done
,
presse
))
c
.
execute
(
"SELECT nid, vid FROM node WHERE nid=%s"
,
(
item
[
'nid'
],))
node
=
c
.
fetchone
()
if
node
is
None
:
continue
c
.
execute
(
"SELECT body FROM node_revisions WHERE vid=%s"
,
(
node
[
'vid'
],))
revision
=
c
.
fetchone
()
if
revision
is
None
:
continue
# Récupérons l'article si il existe en base
article
=
Article
.
add_new_url
(
url
=
item
[
'url'
])
if
item
[
'lang'
]
!=
""
:
article
.
lang
=
item
[
'lang'
]
article
.
published_at
=
item
[
'date_publi'
]
article
.
title
=
item
[
'title'
]
# Let's extract the website from the title
website
=
re
.
search
(
r
'\[(.*)]'
,
item
[
'title'
])
if
website
:
article
.
website
=
website
.
group
(
1
)
# Augmentons le score si nécessaire
if
item
[
'note'
]
>
0
:
article
.
und_score_up
=
item
[
'note'
]
if
item
[
'note'
]
<
0
:
article
.
und_score_down
=
abs
(
item
[
'note'
])
article
.
save
()
article
.
refresh_from_db
()
if
item
[
'published'
]
>=
1
:
# Let's get the extracts
article
.
extracts
=
revision
[
'body'
]
try
:
article
.
fetch_content
()
article
.
fetch_image
()
except
Exception
:
pass
if
article
.
status
not
in
(
"DRAFT"
,
"PUBLISHED"
,
):
article
.
recover
()
if
item
[
'published'
]
>=
2
:
if
article
.
status
!=
"PUBLISHED"
:
article
.
publish
()
article
.
save
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment