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
14
Issues
14
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
4ca3bc56
Commit
4ca3bc56
authored
Apr 03, 2019
by
Okhin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding a parse_metadata action on the Article model
parent
b56b812d
Pipeline
#2540
passed with stages
in 2 minutes and 52 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
12 deletions
+26
-12
apps/core/management/commands/import_old_rp.py
apps/core/management/commands/import_old_rp.py
+8
-4
apps/rp/models.py
apps/rp/models.py
+15
-8
apps/rp/templates/rp/article_form.html
apps/rp/templates/rp/article_form.html
+1
-0
apps/rp/views/articles.py
apps/rp/views/articles.py
+2
-0
No files found.
apps/core/management/commands/import_old_rp.py
View file @
4ca3bc56
...
...
@@ -41,13 +41,15 @@ class Command(BaseCommand):
# First, let's get the data from presse table
presse
=
c
.
execute
(
"SELECT * FROM presse"
)
print
(
"Importing 0/{} from previous database"
.
format
(
presse
))
print
(
"Importing 0/{} from previous database
\r
"
.
format
(
presse
))
# And here we go
done
=
0
errors
=
0
for
item
in
c
.
fetchall
():
done
+=
1
print
(
"Importing {}/{} from previous database"
.
format
(
done
,
presse
))
print
(
"Importing {}/{} from previous database - {} errors
\r
"
.
format
(
done
,
presse
,
errors
))
c
.
execute
(
"SELECT nid, vid FROM node WHERE nid=%s"
,
(
item
[
'nid'
],))
node
=
c
.
fetchone
()
if
node
is
None
:
...
...
@@ -57,6 +59,7 @@ class Command(BaseCommand):
(
node
[
'vid'
],))
revision
=
c
.
fetchone
()
if
revision
is
None
:
errors
+=
1
continue
# Récupérons l'article si il existe en base
...
...
@@ -85,8 +88,9 @@ class Command(BaseCommand):
try
:
article
.
fetch_content
()
article
.
fetch_image
()
article
.
fetch_metadata
()
except
Exception
:
pass
errors
+=
1
if
article
.
status
not
in
(
"DRAFT"
,
"PUBLISHED"
,
):
article
.
recover
()
...
...
apps/rp/models.py
View file @
4ca3bc56
...
...
@@ -6,7 +6,6 @@ from taggit.managers import TaggableManager
from
newspaper
import
Article
as
ArticleParser
from
django_und.models
import
VoteMixin
from
django_fsm
import
FSMField
,
transition
,
RETURN_VALUE
import
opengraph_py3
as
og
from
io
import
BytesIO
from
datetime
import
datetime
...
...
@@ -184,13 +183,6 @@ class Article(VoteMixin):
if
by
is
not
None
:
article
.
upvote
(
by
)
# Let's get metadata import
try
:
metadata
=
og
.
OpenGraph
(
url
=
url
.
decode
())
article
.
metadata
=
metadata
.
to_json
()
except
Exception
:
# There's nothing behind the URL, so no metadata for us
pass
article
.
save
()
return
article
...
...
@@ -208,6 +200,21 @@ class Article(VoteMixin):
self
.
extracts
=
article
.
text
self
.
save
()
def
fetch_metadata
(
self
):
import
opengraph_py3
as
og
if
self
.
lang
!=
"NA"
:
article
=
ArticleParser
(
url
=
self
.
url
,
language
=
self
.
lang
.
lower
())
else
:
article
=
ArticleParser
(
url
=
self
.
url
)
try
:
metadata
=
og
.
OpenGraph
(
url
=
self
.
url
)
article
.
metadata
=
metadata
.
to_json
()
article
.
save
()
except
Exception
:
pass
def
fetch_image
(
self
):
import
requests
import
imghdr
...
...
apps/rp/templates/rp/article_form.html
View file @
4ca3bc56
...
...
@@ -25,6 +25,7 @@
<a
class=
"btn btn-outline-primary"
href=
"?fetch_content"
>
Fetch content
</a>
<a
class=
"btn btn-outline-primary"
href=
"?fetch_image"
>
Fetch image
</a>
<a
class=
"btn btn-outline-primary"
href=
"?fetch_metadata"
>
Fetch metadata
</a>
</p>
<div
class=
"ml-auto"
>
<span>
Save and
</span>
...
...
apps/rp/views/articles.py
View file @
4ca3bc56
...
...
@@ -102,6 +102,8 @@ class ArticleEdit(PermissionRequiredMixin, UpdateView):
self
.
object
.
fetch_content
()
elif
'fetch_image'
in
self
.
request
.
GET
:
self
.
object
.
fetch_image
()
elif
'fetch_metadata'
in
self
.
request
.
GET
:
self
.
object
.
fetch_metadata
()
context
=
self
.
get_context_data
(
object
=
self
.
object
)
return
self
.
render_to_response
(
context
)
...
...
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