import contact finish

remotes/origin/1203
Nazar Kotjuk 10 years ago
parent 995955e308
commit e79a292abb
  1. 34
      emencia/django/newsletter/management/commands/fill_contacts.py

@ -1,25 +1,25 @@
from datetime import timedelta, datetime
import MySQLdb import MySQLdb
from MySQLdb.cursors import DictCursor from MySQLdb.cursors import DictCursor
import xlrd
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from django.conf import settings from django.conf import settings
from emencia.django.newsletter.models import Contact, ContactSettings from emencia.django.newsletter.models import Contact, ContactSettings
import xlrd
from accounts.models import User from accounts.models import User
BAD_EMAILS_PATH = settings.SITE_ROOT + '/emencia/django/newsletter/management/commands/bad_emails.xlsx' 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.validators import validate_email
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
try: try:
validate_email( email ) validate_email(email)
return True return True
except ValidationError: except ValidationError:
return False return False
def get_validated_emails(emails): def get_validated_emails(emails):
# remove not emails # remove not emails
result = [email for email in emails if validateEmail(email)] 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] != 'admin@']
result = [email for email in result if email[:6] != 'sales@'] result = [email for email in result if email[:6] != 'sales@']
result = [email for email in result if email[:5] != 'sale@'] result = [email for email in result if email[:5] != 'sale@']
# bad from file # bad emails from file
f = open(BAD_EMAILS_PATH) f = open(BAD_EMAILS_PATH)
book = xlrd.open_workbook(file_contents=f.read()) book = xlrd.open_workbook(file_contents=f.read())
sheet = book.sheet_by_index(0) sheet = book.sheet_by_index(0)
@ -37,21 +37,29 @@ def get_validated_emails(emails):
result = list(set(result) - set(bad_emails)) result = list(set(result) - set(bad_emails))
return result return result
def handle_email(email, cursor): def handle_email(email, cursor):
from theme.models import Theme from theme.models import Theme
# get theme ids # get theme ids
sql = "select categories_id from cat_to_subsc WHERE email='{0}';" sql = "select categories_id from cat_to_subsc WHERE email='{0}';"
cursor.execute(sql.format(email)) try:
theme_ids = [item['categories_id'] for item in cursor.fetchall() if item.get('categories_id')] 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 # create contact
try: try:
user = User.objects.get(username=email) user = User.objects.get(username=email)
except User.DoesNotExist: except User.DoesNotExist:
user = None user = None
contact = Contact.objects.create(email=email, activated=True, user=user) try:
setting = ContactSettings.objects.create(contact=contact) contact = Contact.objects.create(email=email, activated=True, user=user)
setting.theme = Theme.objects.filter(id__in=theme_ids) except:
pass
else:
setting = ContactSettings.objects.create(contact=contact)
setting.theme = Theme.objects.filter(id__in=theme_ids)
print(email) print(email)
@ -59,8 +67,8 @@ def handle_email(email, cursor):
class Command(NoArgsCommand): class Command(NoArgsCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
db = MySQLdb.connect(host="localhost", db = MySQLdb.connect(host="localhost",
user="kotzilla", user=settings.DATABASES['default']['USER'],
passwd="qazedc", passwd=settings.DATABASES['default']['PASSWORD'],
db="old_expomap", db="old_expomap",
charset='utf8', charset='utf8',
cursorclass=DictCursor) cursorclass=DictCursor)
@ -71,7 +79,7 @@ class Command(NoArgsCommand):
# validate emails # validate emails
unique_emails = [item['email'].strip() for item in result] unique_emails = [item['email'].strip() for item in result]
correct_emails = get_validated_emails(unique_emails) correct_emails = get_validated_emails(unique_emails)
# for testing
for email in correct_emails: for email in correct_emails:
handle_email(email, cursor) handle_email(email, cursor)

Loading…
Cancel
Save