Beispiel #1
0
def ml_maillist_upload(request):
    if request.method == "POST":
        file = request.FILES[u'files[]']
        is_disorder = request.POST.get('is_disorder', '')
        is_ignore = request.POST.get('is_ignore', '')
        maillist_id = int(request.GET.get('maillist', 0))
        is_disorder = True if is_disorder.lower() == 'true' else False
        is_ignore = True if is_ignore.lower() == 'true' else False

        if not maillist_id or (maillist_id and MailList.objects.filter(
                id=maillist_id, customer=request.user)):
            filepath = os.path.join(
                settings.ADDRESS_IMPORT_PATH,
                '{}_{}.{}'.format(int(time.time()),
                                  random.randint(10000, 99999),
                                  file.name.split('.')[-1]))
            with open(filepath, 'w') as fw:
                fw.write(file.read())
            cr = connections['mm-pool'].cursor()
            address_sqls.checkTable(cr, request.user.id)
            AddressImportLog.objects.create(maillist_id=maillist_id,
                                            customer=request.user,
                                            filename=file.name,
                                            filepath=filepath,
                                            is_disorder=is_disorder,
                                            is_newimport=True,
                                            is_ignore=is_ignore)
        return JsonResponse({'result': {}})
    return render(request, 'address/ml_maillist_upload.html', context={})
Beispiel #2
0
def ml_unsubscribe_list(request, list_id):
    obj = model_addresses.get_address_obj(request, list_id)
    user_id = obj.customer_id
    subject = model_addresses.get_subject(request, list_id, obj)
    cr = connections['mm-pool'].cursor()
    address_sqls.checkTable(cr, user_id)
    if request.method == "POST":
        obj2 = get_object(MailList, request.user, list_id)
        tablename = 'ml_unsubscribe_' + str(request.user.id)
        address = request.POST.get('address', '')
        id = request.POST.get('id', False)
        status = int(request.POST.get('status', False))
        if int(status) == -2:  # 删除
            sql = u"DELETE FROM {0} WHERE list_id={1} AND address='{2}'".format(
                tablename, id, address)
            cr.execute(sql)
            redis = get_redis_connection()
            redis.rpush(EDM_WEB_USER_MAIL_IMPORT_COUNT_QUEUE,
                        '{}_{}'.format(request.user.id, list_id))
            messages.add_message(request, messages.SUCCESS, _(u'删除成功'))
        return HttpResponseRedirect("/address/unsubscribe/{}/".format(list_id))
    return render(request,
                  'address/ml_unsubscribe_list.html',
                  context={
                      'subject': subject,
                      'list_id': list_id
                  })
Beispiel #3
0
def ml_subscribe_list(request, list_id):
    obj = model_addresses.get_address_obj(request, list_id)
    user_id = obj.customer_id
    if request.user.id == user_id:
        is_modify_flag = obj.is_allow_export
    else:
        is_modify_flag = False
    # obj = get_object(MailList, request.user, list_id)
    # is_modify_flag = obj.is_allow_export
    subject = model_addresses.get_subject(request, list_id, obj)
    is_subscribe = request.GET.get('is_subscribe', '')
    cr = connections['mm-pool'].cursor()
    address_sqls.checkTable(cr, user_id)
    if request.method == "POST":
        obj2 = get_object(MailList, request.user, list_id)
        tablename = 'ml_subscriber_' + str(request.user.id)
        id = request.POST.get('id', False)
        ids = request.POST.get('ids', '')
        status = int(request.POST.get('status', False))
        redis = get_redis_connection()
        if int(status) == -2:  # 单个删除
            sql = "DELETE FROM {0} WHERE address_id={1}".format(tablename, id)
            cr.execute(sql)
            messages.add_message(request, messages.SUCCESS, _(u'成功删除'))
            redis.rpush(EDM_WEB_USER_MAIL_IMPORT_COUNT_QUEUE,
                        '{}_{}'.format(request.user.id, list_id))
            return HttpResponseRedirect(
                "/address/subscribe/{}/".format(list_id))
        if int(status) == -1:  # 批量删除
            sql = "DELETE FROM {0} WHERE address_id IN ({1})".format(
                tablename, ids)
            cr.execute(sql)
            messages.add_message(request, messages.SUCCESS, _(u'成功删除'))
            redis.rpush(EDM_WEB_USER_MAIL_IMPORT_COUNT_QUEUE,
                        '{}_{}'.format(request.user.id, list_id))
            return HttpResponseRedirect(
                "/address/subscribe/{}/".format(list_id))

    var_lists = get_addr_var_fields(cr, request.user.id)
    field_lists = []
    for i in xrange(len(var_lists) - 10):
        field_lists.append(u'变量{}'.format(11 + i))
    return render(request,
                  'address/ml_subscribe_list.html',
                  context={
                      'subject': subject,
                      'list_id': list_id,
                      'is_subscribe': is_subscribe,
                      'field_lists': field_lists,
                      'is_modify_flag': is_modify_flag,
                  })
Beispiel #4
0
def ml_addr_multi_upload(request, list_id):
    user_id = request.POST.get('user_id', '')
    # is_disorder = request.POST.get('is_disorder', '')
    # is_disorder = True if is_disorder.lower() == 'true' else False
    is_disorder = True
    is_ignore = request.POST.get('is_ignore', '')
    is_ignore = True if is_ignore.lower() == 'true' else False
    try:
        obj = MailList.objects.get(customer_id=user_id, id=list_id)
    except:
        return HttpResponse(json.dumps({'status': 'M'}),
                            content_type="application/json")

    attachfile = request.FILES.get('filedata', None)
    if not attachfile:
        return HttpResponse(json.dumps({'status': 'N'}),
                            content_type="application/json")

    filename = attachfile.name
    suffix = filename.split('.')[-1]
    if suffix.lower() not in ('xls', 'xlsx', 'csv', 'txt', 'zip', 'rar',
                              'docx'):
        return HttpResponse(json.dumps({'status': 'S'}),
                            content_type="application/json")

    filepath = os.path.join(
        settings.ADDRESS_IMPORT_PATH,
        '{}_{}.{}'.format(int(time.time()), random.randint(10000, 99999),
                          suffix))
    with open(filepath, 'w') as fw:
        fw.write(attachfile.read())
    cr = connections['mm-pool'].cursor()
    address_sqls.checkTable(cr, request.user.id)
    # 标志列表正在导入
    obj.is_importing = True
    obj.save()
    AddressImportLog.objects.create(maillist_id=list_id,
                                    customer_id=user_id,
                                    filename=filename,
                                    filepath=filepath,
                                    is_disorder=is_disorder,
                                    is_newimport=True,
                                    is_ignore=is_ignore)
    if request.user.service().is_auto_duplicate:
        redis = get_redis_connection()
        redis.rpush(EDM_WEB_MAIL_DUPLICATE_QUEUE, int(user_id))
    return HttpResponse(json.dumps({'status': 'Y'}),
                        content_type="application/json")
Beispiel #5
0
def ajax_add_address(request, list_id):
    data = request.POST
    post_data = data.get('post_data', '')
    cr = connections['mm-pool'].cursor()
    customer_id = request.user.id
    address_sqls.checkTable(cr, customer_id)
    tablename = 'ml_subscriber_' + str(customer_id)
    values, _addresses = [], []
    success, fail, repeat, valid = 0, 0, 0, 0
    p = re.compile('^(\w|[-+=.])+@\w+([-.]\w+)*\.(\w+)$')
    p_phone = re.compile(
        r'((\+?86)|(\(\+86\)))?(\s)?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|14[57][0-9]{8}|1349[0-9]{7}|177[0-9]{8})'
    )
    var_lists = get_addr_var_fields(cr, request.user.id)
    # field_str = u'(list_id, address, fullname, sex, birthday, phone, area, {}, created)'.format(','.join(var_lists))
    mongo = pymongo.MongoClient(
        host='mongodb://{username}:{password}@{host}:{port}/{dbname}'.format(
            **mongo_cfg))
    db = mongo['mm-mc'].badmail

    import_obj = AddressImportLog.objects.create(
        maillist_id=list_id,
        customer=request.user,
        filename=None,
        filepath=None,
        status='1',
        count_all=0,
        count_err_1=0,
        count_err_2=0,
        time_import=time.strftime("%Y-%m-%d %H:%M:%S"),
        time_finish=time.strftime("%Y-%m-%d %H:%M:%S"))
    # 错误类型 1
    err_t1_name = '{}_maillist_err_t1.txt'.format(import_obj.id)
    err_t1_path = os.path.join("/usr/local/mail-import/data/", err_t1_name)
    fp_err_t1 = open(err_t1_path, "a")

    # 错误类型 2
    err_t2_name = '{}_maillist_err_t2.txt'.format(import_obj.id)
    err_t2_path = os.path.join("/usr/local/mail-import/data/", err_t2_name)
    fp_err_t2 = open(err_t2_path, "a")

    redis = get_redis_connection()
    for d in post_data.split('\n'):
        l = d.strip().replace('\r', '').replace(u';', ';')
        l = l.split(";")
        length = len(l)
        if (length == 1 and not l[0].strip()) or (not l):
            continue

        # 判断邮箱地址格式
        if l and not p.match(l[0].strip()):
            fail += 1
            address_tools.save_error_addr(fp_err_t1, l[0])
            continue

        address = l[0].strip()
        if address.split('@')[-1] in (u'yahoo.com.cn', u'yahoo.cn'):
            valid += 1
            address_tools.save_error_addr(fp_err_t1, l[0])
            continue

        if db.find_one({"addr": address}):
            valid += 1
            address_tools.save_error_addr(fp_err_t1, l[0])
            continue

        # 判断重复
        cr.execute(
            u"SELECT address_id FROM {} WHERE address='{}' AND list_id={} LIMIT 1;"
            .format(tablename, address, list_id))
        if cr.fetchone():
            repeat += 1
            address_tools.save_error_addr(fp_err_t2, l[0])
            continue

        if address in _addresses:
            repeat += 1
            address_tools.save_error_addr(fp_err_t2, l[0])
            continue

        if check_qq_addr(address):
            redis.lpush(GLB_REDIS_REMOTE_GET_QQ, address)

        _addresses.append(address)
        try:
            fullname = l[1].strip() if l[1].strip() else '@'.join(
                address.split("@")[:-1])
        except:
            fullname = '@'.join(address.split("@")[:-1])

        sex = l[2].strip() if length > 2 else ''
        sex = address_tools.handleSex(sex)

        birthday = l[3].strip() if length > 3 else ''
        birthday = address_tools.hanfBirthday(birthday)

        phone = l[4].strip() if length > 4 else ''
        m = p_phone.search(phone)
        phone = m.group() if m else ''

        area = l[5].strip() if length > 5 else ''
        vars = l[6:]
        sql_parts, sql_args = get_fields_args(var_lists, vars)
        sql = "INSERT INTO `mm-pool`.`ml_subscriber_%s` SET list_id=%s, address=%s, fullname=%s, sex=%s, birthday=%s, phone=%s, area=%s, created=%s{}".format(
            sql_parts)
        args = [
            customer_id, list_id, address, fullname, sex, birthday, phone,
            area,
            time.strftime("%Y-%m-%d %H:%M:%S")
        ] + sql_args
        cr.execute(sql, args)
        success += 1
    msg = _(
        u'成功提交%(success)d条记录, 其中有%(repeat)d条重复记录, %(fail)d条格式错误, %(valid)d条无效地址'
    ) % {
        'success': success,
        'repeat': repeat,
        'fail': fail,
        'valid': valid,
    }

    obj = MailList.objects.filter(id=list_id).first()
    if obj:
        obj.count_all = F('count_all') + success + fail + valid
        obj.count_err = F('count_err') + fail + valid
        obj.updated = time.strftime("%Y-%m-%d %H:%M:%S")
        obj.save()

    import_obj.count_all = success + fail + valid + repeat
    import_obj.count_err_1 = fail + valid
    import_obj.count_err_2 = repeat
    import_obj.time_finish = time.strftime("%Y-%m-%d %H:%M:%S")
    import_obj.save()
    if int(success) > 0:
        redis.rpush(EDM_WEB_USER_MAIL_IMPORT_COUNT_QUEUE,
                    '{}_{}'.format(customer_id, list_id))
        if request.user.service().is_auto_duplicate:
            redis.rpush(EDM_WEB_MAIL_DUPLICATE_QUEUE, int(customer_id))

    return HttpResponse(json.dumps({'msg': msg}),
                        content_type="application/json")