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
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)

Loading…
Cancel
Save