def sms_modem_send_sms(outbox_message, message, party):
    number_of_message = (len(message.content) + (SHORT_LINK_LENGTH if message.is_apply_tips else 0) + BASIC_MESSAGE_LENGTH - 1) / BASIC_MESSAGE_LENGTH
    userprofile = party.creator.get_profile()
    try:
        phone_list = outbox_message.address.split(',')
        
        #排除创建者的手机号
        try:
            u_index = phone_list.index(userprofile.phone)
            phone_list.pop(u_index)
        except Excepiton:
            pass
        
        if message.is_apply_tips:
            for phone in phone_list:
                content = message.content
                enroll_link = DOMAIN_NAME + '/parties/%d/enroll/?key=%s' % (party.id, hashlib.md5('%d:%s' % (party.id, phone)).hexdigest())
                short_link = transfer_to_shortlink(enroll_link)
                content = u'【爱热闹】%s 快来报名:%s' % (content, short_link)
                data = {'Mobile':regPhoneNum(phone), 'Content':content.encode('gbk')}
                
                #短信扣除
                userprofile.used_sms_count = userprofile.used_sms_count + number_of_message
                userprofile.available_sms_count = userprofile.available_sms_count - number_of_message
                userprofile.save()
                try:
                    res = _post_api_request_sendSMS(data)
                    if res != '1':
                        logger.error(res)
                except:
                    userprofile.used_sms_count = userprofile.used_sms_count - number_of_message
                    userprofile.available_sms_count = userprofile.available_sms_count + number_of_message
                    userprofile.save()
                    logger.info('return avalibale sms count ,user:'******'number:' + str(number_of_message))
                    logger.exception('send sms error!')
        else:
            for phone in phone_list:
                content = u'【爱热闹】' + message.content
                data = {'Mobile':regPhoneNum(phone), 'Content':content.encode('gbk')}

                #短信扣除
                userprofile.used_sms_count = userprofile.used_sms_count + number_of_message
                userprofile.available_sms_count = userprofile.available_sms_count - number_of_message
                userprofile.save()
                try:
                    res = _post_api_request_sendSMS(data)
                    if res != '1':
                        logger.error(res)
                except:
                    userprofile.used_sms_count = userprofile.used_sms_count - number_of_message
                    userprofile.available_sms_count = userprofile.available_sms_count + number_of_message
                    userprofile.save()
                    logger.info('return avalibale sms count ,user:'******'number:' + str(number_of_message))
                    logger.exception('send sms error!')
    except:
        logger.exception('send sms error!')
    finally:
        outbox_message.delete()
def send_forget_pwd_sms(instance):
    phone = instance.user.userprofile.phone
    content = u'【爱热闹】您的临时密码为: %s 该密码仅生效一次,请您尽快登录应用/网站,修改您的密码。' % instance.temp_password
    data = {'Mobile':regPhoneNum(phone) , 'Content':content.encode('gbk')}
    try:
        res = _post_api_request_sendSMS(data)
        if res != '1':
            logger.error(res)
    except:
        logger.exception('send send_forget_pwd_sms error!')
def sendsmsBindingmessage(UserBindingTemp):
    phone = UserBindingTemp.binding_address
    content = u'【爱热闹】' + u'您的手机验证码:' + UserBindingTemp.key
    data = {'Mobile':regPhoneNum(phone) , 'Content':content.encode('gbk')}
    try:
        res = _post_api_request_sendSMS(data)
        if res != '1':
            logger.error(res)
    except:
        logger.exception('send sendsmsBindingmessage error!')
Exemple #4
0
def forgetPassword(request):
    if request.method == "POST" and "value" in request.POST:
        value = request.POST["value"]
        if re_email.match(value):
            try:
                user = User.objects.get(userprofile__email=value, userprofile__email_binding_status="bind")
            except Exception:
                raise myException(ERROR_FORGETPASSWORD_NO_USER_BY_EMAIL)
            sending_type = "email"
        elif re_username.match(value):
            try:
                user = User.objects.get(username=value)
            except Exception:
                raise myException(ERROR_FORGETPASSWORD_NO_USER_BY_USERNAME)
            if user.userprofile.phone and user.userprofile.phone_binding_status == "bind":
                sending_type = "sms"
                value = user.userprofile.phone
            elif user.userprofile.email and user.userprofile.email_binding_status == "bind":
                sending_type = "email"
                value = user.userprofile.email
            else:
                raise myException(ERROR_FORGETPASSWORD_NO_USER_BY_USERNAME_NO_BINDING)
        elif re_phone.match(regPhoneNum(value)):
            try:
                user = User.objects.get(userprofile__phone=regPhoneNum(value), userprofile__phone_binding_status="bind")
            except Exception:
                raise myException(ERROR_FORGETPASSWORD_NO_USER_BY_SMS)
            sending_type = "sms"
        else:
            raise myException(ERROR_FORGETPASSWORD_NO_USER_BY_USERNAME)
        temp_password = generate_phone_code()
        temp_pwd_data, created = AccountTempPassword.objects.get_or_create(
            user=user, defaults={"temp_password": temp_password, "sending_type": sending_type}
        )
        if not created:
            temp_pwd_data.sending_type = sending_type
            temp_pwd_data.save()
def send_apply_confirm_sms(party_client):
    party = party_client.party
    client = party_client.client
    enroll_link = DOMAIN_NAME + '/parties/%d/enroll/?key=%s' % (party.id, hashlib.md5('%d:%s' % (party.id, client.phone)).hexdigest())
    short_link = transfer_to_shortlink(enroll_link)
    content = u'【爱热闹】' + client.name=='' and client.phone or client.name + u', 您报名参加了' + party.creator.username + u'发布的活动,点击链接查看该活动:'
    content = '%s%s' % (content, short_link)
    phone = client.phone
    data = {'Mobile':regPhoneNum(phone) , 'Content':content.encode('gbk')}
    try:
        res = _post_api_request_sendSMS(data)
        if res != '1':
            logger.error(res)
    except:
        logger.exception('send send_apply_confirm_sms error!')
def sendsmsMessage(message):
    phone = message['address']
    if message['content'] == 'bindsuccess':
        message['content'] = u'手机号码绑定成功'
    elif message['content'] == 'unbindsuccess':
        message['content'] = u'手机号码解除绑定成功'
    else:
        return
    content = u'【爱热闹】' + message['content']
    data = {'Mobile':regPhoneNum(phone) , 'Content':content.encode('gbk')}
    try:
        res = _post_api_request_sendSMS(data)
        if res != '1':
            logger.error(res)
    except:
        logger.exception('send sendsmsMessage error!')        
Exemple #7
0
def bindContact(request, type):
    if request.method == "POST":
        value = request.POST["value"]
        uid = request.POST["uid"]
        try:
            user = User.objects.get(pk=uid)
        except Exception:
            raise myException(ERROR_BINDING_NO_USER)
        if type == "email":
            userkey = hashlib.md5("%s:%s" % (uid, value)).hexdigest()
        else:
            userkey = generate_phone_code()
        data = {
            "latest_status": {
                "email": user.userprofile.email,
                "email_binding_status": user.userprofile.email_binding_status,
                "phone": user.userprofile.phone,
                "phone_binding_status": user.userprofile.phone_binding_status,
            }
        }
        # 数据有效性验证
        if type == "email" and not re_email.match(value):
            raise myException(ERROR_BINDING_INVAILID_EMAIL_FORMMAT)
        if type == "phone" and not re_phone.match(regPhoneNum(value)):
            raise myException(ERROR_BINDING_INVAILID_PHONE_FORMMAT)
        # 用户已经绑定了
        if type == "email" and user.userprofile.email_binding_status == "bind":
            if user.userprofile.email == value:
                raise myException("", status=ERROR_STATUS_HAS_BINDED, data=data)
            else:
                raise myException(
                    ERROR_BINDING_BY_EMAIL_DIFFERENT_BINDED, status=ERROR_STATUS_DIFFERENT_BINDED, data=data
                )
        elif type == "phone" and user.userprofile.phone_binding_status == "bind":
            if user.userprofile.email == value:
                raise myException("", status=ERROR_STATUS_HAS_BINDED, data=data)
            else:
                raise myException(
                    ERROR_BINDING_BY_PHONE_DIFFERENT_BINDED, status=ERROR_STATUS_DIFFERENT_BINDED, data=data
                )
        # 被别人绑定
        if (
            type == "email"
            and UserProfile.objects.filter(email=value, email_binding_status="bind").exclude(user=user).count() != 0
        ):
            raise myException(
                ERROR_BINDING_BY_EMAIL_HAS_BINDED_BY_OTHER, status=ERROR_STATUS_HAS_BINDED_BY_OTHER, data=data
            )
        elif (
            type == "phone"
            and UserProfile.objects.filter(phone=value, phone_binding_status="bind").exclude(user=user).count() != 0
        ):
            raise myException(
                ERROR_BINDING_BY_PHONE_HAS_BINDED_BY_OTHER, status=ERROR_STATUS_HAS_BINDED_BY_OTHER, data=data
            )
        if type == "phone":
            value = regPhoneNum(value)
        with transaction.commit_on_success():
            profile = user.userprofile
            if type == "phone":
                profile.phone = value
                profile.phone_binding_status = "waitingbind"
            else:
                profile.email = value
                profile.email_binding_status = "waitingbind"
            profile.save()
        with transaction.commit_on_success():
            binding_temp, created = UserBindingTemp.objects.get_or_create(
                user=user, binding_type=type, defaults={"binding_address": value, "key": userkey}
            )
            if not created:
                binding_temp.binding_address = value
                binding_temp.key = userkey
                binding_temp.save()
        data = {
            "latest_status": {
                "email": user.userprofile.email,
                "email_binding_status": user.userprofile.email_binding_status,
                "phone": user.userprofile.phone,
                "phone_binding_status": user.userprofile.phone_binding_status,
            }
        }
        return data