From 23f7ad4e4f0c70680aed4381585b9e848a8dcedd Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Tue, 29 Sep 2015 17:41:31 +0300 Subject: [PATCH] import articles from wordpress blog script added --- .../management/commands/articles_from_blog.py | 77 +++++++++++++++++++ core/views.py | 14 ++-- 2 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 article/management/commands/articles_from_blog.py diff --git a/article/management/commands/articles_from_blog.py b/article/management/commands/articles_from_blog.py new file mode 100644 index 00000000..7380727d --- /dev/null +++ b/article/management/commands/articles_from_blog.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +import MySQLdb +from MySQLdb.cursors import DictCursor +from django.core.management.base import BaseCommand, CommandError +from functions.translate import fill_with_signal +from article.models import Article +from accounts.models import User +from theme.models import Theme,Tag + + +class Command(BaseCommand): + def handle(self, *args, **options): + db = MySQLdb.connect(host="localhost", + user="kotzilla", + passwd="qazedc", + db="expomap_old_blog", + charset='utf8', + cursorclass=DictCursor) + + cursor = db.cursor() + + sql = """SELECT post.post_date as publish_date, + post.post_content as description, + post.post_modified as modified, + post.post_title as main_title, + post.post_name as slug, + tag.name as tag_name + FROM wp_posts post + INNER JOIN wp_term_relationships rel ON rel.object_id = post.ID + INNER JOIN wp_terms tag ON tag.term_id=rel.term_taxonomy_id + WHERE post_status = "publish";""" + + cursor.execute(sql) + result = cursor.fetchall() + user = User.objects.get(id=1) + + tags = {x.name: x.id for x in Tag.objects.language('ru')} + + clear_result = {} + + errors = [] + for a in result: + if a.get('tag_name') in tags.keys(): + tag_id = tags[a['tag_name']] + a['tag_id'] = [tag_id] + print a + else: + tag_id = None + + if not clear_result.get(a['main_title']): + clear_result[a['main_title']] = a + else: + if clear_result[a['main_title']].get('paid_id') and tag_id: + clear_result[a['main_title']]['tag_id'].append(tag_id) + + + for a in clear_result.values(): + article = Article(type=1, + created = a['publish_date'], + publish_date= a['publish_date'], + modified = a['modified'], + ) + article.author = user + article.translate('ru') + article.main_title = a['main_title'] + article.description = a['description'] + article.slug = a['slug'] + if a.get('tag_id'): + article.tag = [Tag.objects.language('ru').get(id=id) for id in a['tag_id']] + try: + article.save() + except Exception as e: + errors.append(e) + + print errors + + diff --git a/core/views.py b/core/views.py index 8da905d5..d762dc01 100644 --- a/core/views.py +++ b/core/views.py @@ -260,8 +260,7 @@ from django.utils.translation import get_language from .utils import queryset_to_workbook from exposition.models import Exposition from conference.models import Conference -from django.core.urlresolvers import reverse - +from django.conf import settings def download_workbook(request): lang = get_language() @@ -269,9 +268,9 @@ def download_workbook(request): if data: qs = [] for i,obj in enumerate(data): - if data.get('data[%i][name]'%i) == 'expo': + if data.get('data[%i][name]' % i) == 'expo': qs.append(Exposition.objects.language(lang).get(id=data['data[%i][value]'%i])) - elif data.get('data[%i][name]'%i) == 'conf': + elif data.get('data[%i][name]' % i) == 'conf': qs.append(Conference.objects.language(lang).get(id=data['data[%i][value]'%i])) earliest_event = qs[0].data_begin @@ -279,7 +278,12 @@ def download_workbook(request): if obj.data_begin < earliest_event: earliest_event = obj.data_begin setattr(obj, 'number', i) - setattr(obj, 'dates', u'%s - %s'%(obj.data_begin.strftime('%d %B %Y'),obj.data_end.strftime('%d %B %Y'))) + setattr(obj, 'dates', u'%s %s %s - %s %s %s'%(obj.data_begin.strftime('%d'), + settings.MONTHES[obj.data_begin.strftime("%b").lower()]['name'], + obj.data_begin.strftime('%Y'), obj.data_end.strftime('%d'), + settings.MONTHES[obj.data_end.strftime("%b").lower()]['name'], + obj.data_end.strftime('%Y')) + ) setattr(obj, 'full_place', u'%s, %s, %s' % (obj.country, obj.city, getattr(obj.place, 'name', ''))) try: setattr(obj, 'link', u'http://www.expomap.ru%s'%obj.get_absolute_url())