Commit ba1a8794 authored by okhin's avatar okhin 🚴

Adding a import_old_rp manager comand

parent a4f84f9b
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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment