Commit 395b8217 authored by Vasyl Bodnaruk's avatar Vasyl Bodnaruk Committed by Andrii Marynets

fix db

parent 7bc62183
class EsiDatabaseRouter:
default_apps = (
'admin',
'auth',
'contenttypes',
'sessions',
'messages',
)
def db_for_read(self, model, **hints):
if model._meta.app_label in self.default_apps:
return 'default'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.default_apps:
return 'default'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label in self.default_apps or \
obj2._meta.app_label in self.default_apps:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.default_apps:
return db == 'default'
return None
\ No newline at end of file
......@@ -37,6 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webapp.apps.WebappConfig',
]
MIDDLEWARE = [
......@@ -88,6 +90,8 @@ DATABASES = {
}
}
DATABASE_ROUTERS = ['webapp.router.WebappDatabaseRouter', 'esi.router.EsiDatabaseRouter']
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
......
from django.apps import AppConfig
class AppConfig(AppConfig):
name = 'app'
class WebappConfig(AppConfig):
name = 'webapp'
from .wp import *
from .news import News
\ No newline at end of file
from django.db import models
class News(models.Model):
title = models.CharField(max_length=255)
url = models.TextField(max_length=2083)
description = models.TextField()
media_id = models.IntegerField()
type_id = models.IntegerField()
region_id = models.IntegerField()
post_id = models.BigIntegerField()
publish_date = models.DateTimeField()
record_date = models.DateTimeField()
company_id = models.BigIntegerField()
is_accepted = models.BooleanField(default=False)
class Meta:
db_table = 'wp_esi_news_accept'
......@@ -443,7 +443,7 @@ class WpIclContentStatus(models.Model):
class WpIclCoreStatus(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
rid = models.BigIntegerField()
module = models.CharField(max_length=16)
origin = models.CharField(max_length=64)
......@@ -456,7 +456,7 @@ class WpIclCoreStatus(models.Model):
class WpIclFlags(models.Model):
id = models.IntegerField()
id = models.IntegerField(primary_key=True)
lang_code = models.CharField(max_length=10)
flag = models.CharField(max_length=32)
from_template = models.IntegerField()
......@@ -467,7 +467,7 @@ class WpIclFlags(models.Model):
class WpIclLanguages(models.Model):
id = models.IntegerField()
id = models.IntegerField(primary_key=True)
code = models.CharField(max_length=7)
english_name = models.CharField(max_length=128)
major = models.IntegerField()
......@@ -482,7 +482,7 @@ class WpIclLanguages(models.Model):
class WpIclLanguagesTranslations(models.Model):
id = models.IntegerField()
id = models.IntegerField(primary_key=True)
language_code = models.CharField(max_length=7)
display_language_code = models.CharField(max_length=7)
name = models.CharField(max_length=255)
......@@ -502,7 +502,7 @@ class WpIclLocaleMap(models.Model):
class WpIclMessageStatus(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
rid = models.BigIntegerField()
object_id = models.BigIntegerField()
from_language = models.CharField(max_length=10)
......@@ -528,7 +528,7 @@ class WpIclNode(models.Model):
class WpIclReminders(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
message = models.TextField()
url = models.TextField()
can_delete = models.IntegerField()
......@@ -540,7 +540,7 @@ class WpIclReminders(models.Model):
class WpIclStringPositions(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
string_id = models.BigIntegerField()
kind = models.IntegerField(blank=True, null=True)
position_in_page = models.CharField(max_length=255)
......@@ -551,7 +551,7 @@ class WpIclStringPositions(models.Model):
class WpIclStringStatus(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
rid = models.BigIntegerField()
string_translation_id = models.BigIntegerField()
timestamp = models.DateTimeField()
......@@ -563,7 +563,7 @@ class WpIclStringStatus(models.Model):
class WpIclStringTranslations(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
string_id = models.BigIntegerField()
language = models.CharField(max_length=10)
status = models.IntegerField()
......@@ -579,7 +579,7 @@ class WpIclStringTranslations(models.Model):
class WpIclStrings(models.Model):
id = models.BigIntegerField()
id = models.BigIntegerField(primary_key=True)
language = models.CharField(max_length=7)
context = models.CharField(max_length=160)
name = models.CharField(max_length=160)
......@@ -627,7 +627,7 @@ class WpIclTranslateJob(models.Model):
class WpIclTranslationBatches(models.Model):
id = models.IntegerField()
id = models.IntegerField(primary_key=True)
batch_name = models.TextField()
tp_id = models.IntegerField(blank=True, null=True)
ts_url = models.TextField(blank=True, null=True)
......@@ -865,7 +865,7 @@ class WpUsers(models.Model):
class WpWpdatacharts(models.Model):
id = models.AutoField(unique=True)
id = models.AutoField(primary_key=True)
wpdatatable_id = models.IntegerField()
title = models.CharField(max_length=255)
engine = models.CharField(max_length=10)
......@@ -878,7 +878,7 @@ class WpWpdatacharts(models.Model):
class WpWpdatatables(models.Model):
id = models.AutoField(unique=True)
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
show_title = models.IntegerField()
table_type = models.CharField(max_length=55)
......@@ -916,7 +916,7 @@ class WpWpdatatables(models.Model):
class WpWpdatatablesColumns(models.Model):
id = models.AutoField(unique=True)
id = models.AutoField(primary_key=True)
table_id = models.IntegerField()
orig_header = models.CharField(max_length=255)
display_header = models.CharField(max_length=255)
......
class WebappDatabaseRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == 'webapp':
return 'esi'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'webapp':
return 'esi'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'webapp' or \
obj2._meta.app_label == 'webapp':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'webapp':
return db == 'esi'
return None
\ No newline at end of file
......@@ -15,6 +15,10 @@ class Database:
self.db = MySQLdb.connect(self.host, self.user, self.pwd, self.database)
self.db.autocommit(True)
self.cursor = self.db.cursor()
self.db.set_character_set('utf8')
self.cursor.execute('SET NAMES utf8;')
self.cursor.execute('SET CHARACTER SET utf8;')
self.cursor.execute('SET character_set_connection=utf8;')
def __del__(self):
self.db.close()
......
......@@ -25,12 +25,12 @@ class ExaPipeline(object):
print(item)
item['description'] = ''.join(item['description']).replace('\n', ' ')
data = (item['title'], item['description'], item['url'], item['media_id'], item['type_id'],
item['region_id'], item['post_id'], item['date'], datetime.now().date(),)
query = """INSERT INTO wp_esi_news (title, description, URL, media_id, type_id, region_id, post_id, publish_date, record_date)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s);\n"""
item['region_id'], item['post_id'], item['date'], datetime.now().date(), item['company_id'], 0)
query = """INSERT INTO wp_esi_news_accept (title, description, URL, media_id, type_id, region_id, post_id,
publish_date, record_date, company_id, is_accepted) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);\n"""
print(item)
news = self.db.insert(query, data)
self._insert_news_entiry(news, item['company_id'])
# self._insert_news_entiry(news, item['company_id'])
# self.out.write(query)
return item
......
......@@ -9,7 +9,7 @@ from ..items import ExaItem
db_settings = get_project_settings().get('DB')
db = Database(**db_settings)
comp = db.select("SELECT id, name, country FROM wp_esi_entity WHERE id < 300;")
comp = db.select("SELECT id, name, country FROM wp_esi_entity WHERE id = 3;")
class MobiHealthNewsSpider(scrapy.Spider):
......
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