From e79a292abb1519f35ac2ef8e5fdeb578b72ebaf2 Mon Sep 17 00:00:00 2001 From: Nazar Kotjuk Date: Thu, 29 Oct 2015 21:39:03 +0200 Subject: [PATCH] import contact finish --- .../management/commands/fill_contacts.py | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/emencia/django/newsletter/management/commands/fill_contacts.py b/emencia/django/newsletter/management/commands/fill_contacts.py index 1664c7a4..bf41bea4 100644 --- a/emencia/django/newsletter/management/commands/fill_contacts.py +++ b/emencia/django/newsletter/management/commands/fill_contacts.py @@ -1,25 +1,25 @@ -from datetime import timedelta, datetime import MySQLdb from MySQLdb.cursors import DictCursor +import xlrd from django.core.management.base import NoArgsCommand from django.conf import settings from emencia.django.newsletter.models import Contact, ContactSettings -import xlrd from accounts.models import User BAD_EMAILS_PATH = settings.SITE_ROOT + '/emencia/django/newsletter/management/commands/bad_emails.xlsx' -def validateEmail( email ): +def validateEmail(email): from django.core.validators import validate_email from django.core.exceptions import ValidationError try: - validate_email( email ) + validate_email(email) return True except ValidationError: return False + def get_validated_emails(emails): # remove not emails result = [email for email in emails if validateEmail(email)] @@ -28,7 +28,7 @@ def get_validated_emails(emails): result = [email for email in result if email[:6] != 'admin@'] result = [email for email in result if email[:6] != 'sales@'] result = [email for email in result if email[:5] != 'sale@'] - # bad from file + # bad emails from file f = open(BAD_EMAILS_PATH) book = xlrd.open_workbook(file_contents=f.read()) sheet = book.sheet_by_index(0) @@ -37,21 +37,29 @@ def get_validated_emails(emails): result = list(set(result) - set(bad_emails)) return result + def handle_email(email, cursor): from theme.models import Theme # get theme ids sql = "select categories_id from cat_to_subsc WHERE email='{0}';" - cursor.execute(sql.format(email)) - theme_ids = [item['categories_id'] for item in cursor.fetchall() if item.get('categories_id')] + try: + cursor.execute(sql.format(email)) + except UnicodeEncodeError: + return + theme_ids = [item['categories_id'] for item in cursor.fetchall() if item.get('categories_id')] # create contact try: user = User.objects.get(username=email) except User.DoesNotExist: user = None - contact = Contact.objects.create(email=email, activated=True, user=user) - setting = ContactSettings.objects.create(contact=contact) - setting.theme = Theme.objects.filter(id__in=theme_ids) + try: + contact = Contact.objects.create(email=email, activated=True, user=user) + except: + pass + else: + setting = ContactSettings.objects.create(contact=contact) + setting.theme = Theme.objects.filter(id__in=theme_ids) print(email) @@ -59,8 +67,8 @@ def handle_email(email, cursor): class Command(NoArgsCommand): def handle(self, *args, **options): db = MySQLdb.connect(host="localhost", - user="kotzilla", - passwd="qazedc", + user=settings.DATABASES['default']['USER'], + passwd=settings.DATABASES['default']['PASSWORD'], db="old_expomap", charset='utf8', cursorclass=DictCursor) @@ -71,7 +79,7 @@ class Command(NoArgsCommand): # validate emails unique_emails = [item['email'].strip() for item in result] correct_emails = get_validated_emails(unique_emails) - # for testing + for email in correct_emails: handle_email(email, cursor)