parent
b8ff4735c2
commit
1daa320e2a
2 changed files with 77 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,77 @@ |
||||
from datetime import timedelta, datetime |
||||
import MySQLdb |
||||
from MySQLdb.cursors import DictCursor |
||||
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 ): |
||||
from django.core.validators import validate_email |
||||
from django.core.exceptions import ValidationError |
||||
try: |
||||
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)] |
||||
# bad emails |
||||
result = [email for email in result if email[:5] != 'info@'] |
||||
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 |
||||
f = open(BAD_EMAILS_PATH) |
||||
book = xlrd.open_workbook(file_contents=f.read()) |
||||
sheet = book.sheet_by_index(0) |
||||
bad_emails = [sheet.row_values(row_number)[0] for row_number in range(sheet.nrows)] |
||||
|
||||
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')] |
||||
|
||||
# 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) |
||||
|
||||
print(email) |
||||
|
||||
|
||||
class Command(NoArgsCommand): |
||||
def handle(self, *args, **options): |
||||
db = MySQLdb.connect(host="localhost", |
||||
user="kotzilla", |
||||
passwd="qazedc", |
||||
db="old_expomap", |
||||
charset='utf8', |
||||
cursorclass=DictCursor) |
||||
cursor = db.cursor() |
||||
sql = "select email from cat_to_subsc GROUP BY `email`" |
||||
cursor.execute(sql) |
||||
result = cursor.fetchall() |
||||
# 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…
Reference in new issue