You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.8 KiB
85 lines
2.8 KiB
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
|
|
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 emails 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}';"
|
|
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
|
|
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)
|
|
|
|
|
|
class Command(NoArgsCommand):
|
|
def handle(self, *args, **options):
|
|
db = MySQLdb.connect(host="localhost",
|
|
user=settings.DATABASES['default']['USER'],
|
|
passwd=settings.DATABASES['default']['PASSWORD'],
|
|
db="old_db",
|
|
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 email in correct_emails:
|
|
handle_email(email, cursor)
|
|
|
|
|