示例#1
0
文件: admintask.py 项目: jcsy521/ydws
 def handle_dead_terminal(db, redis):
     """For the terminals to be removed, delete the associated info of it.
     @params: db, database
     """
     terminals = db.query("select tid, mobile from T_TERMINAL_INFO where service_status = 2")
     logging.info("Handle the to be removed terminals, the count of terminals: %s", len(terminals))
     for terminal in terminals:
         logging.info("Delete the to be removed terminal:%s", terminal.mobile)
         delete_terminal(terminal.tid, db, redis, del_user=True)
示例#2
0
文件: misc.py 项目: jcsy521/ydws
    def get(self, mobile):
        """
        """

        ret = DotDict(status=ErrorCode.SUCCESS,
                      message=None)
        corp = self.db.get("SELECT id, tid, service_status"
                           "  FROM T_TERMINAL_INFO"
                           "  WHERE mobile = %s"
                           "   LIMIT 1",
                           mobile)
        if corp:
            if corp.service_status == UWEB.SERVICE_STATUS.ON:
                ret.status = ErrorCode.TERMINAL_ORDERED
                ret.message = ErrorCode.ERROR_MESSAGE[ret.status]
            else:
                delete_terminal(corp.tid, self.db, self.redis, del_user=True)
        self.set_header(*self.JSON_HEADER)
        self.write(json_encode(ret))
示例#3
0
文件: batch.py 项目: jcsy521/ydws
    def post(self):
        """Read excel, parse the data and provide to browser.
        """
        try:
            upload_file = self.request.files['upload_file'][0]
            logging.info("[UWEB] batch import, corp_id: %s",
                         self.current_user.cid)
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.exception("[UWEB] Invalid data format. Exception: %s",
                              e.args)
            self.write_ret(status)
            return

        try:
            status = ErrorCode.SUCCESS
            original_fname = upload_file['filename']
            extension = os.path.splitext(original_fname)[1]
            if extension not in ['.xlsx', '.xls']:
                status = ErrorCode.ILLEGAL_EXCEL_FILE
                self.write_ret(status)
                return

            # write into tmp file
            fname = ''.join(
                random.choice(string.ascii_lowercase + string.digits) for x in range(6))
            final_filename = fname + extension
            file_path = final_filename
            output_file = open(file_path, 'w')
            output_file.write(upload_file['body'])
            output_file.close()

            res = []
            # read from tmp file
            wb = xlrd.open_workbook(file_path)
            for sheet in wb.sheets():
                # NOTE: first line is title, and is ignored
                for j in range(1, sheet.nrows):
                    row = sheet.row_values(j)
                    tmobile = unicode(row[0])
                    tmobile = tmobile[0:11]
                    umobile = ''
                    if len(row) > 1:
                        umobile = unicode(row[1])
                        umobile = umobile[0:11]
                    biz_type = UWEB.BIZ_TYPE.YDWS
                    if len(row) > 2:
                        biz_type = unicode(row[2])
                        biz_type = biz_type[0:1]
                        biz_type = biz_type if biz_type else UWEB.BIZ_TYPE.YDWS

                    r = DotDict(tmobile=tmobile,
                                umobile=umobile,
                                biz_type=int(biz_type),
                                status=UWEB.TERMINAL_STATUS.UNJH)

                    if not check_phone(tmobile) or (umobile and not check_phone(umobile)):
                        r.status = UWEB.TERMINAL_STATUS.INVALID
                        res.append(r)
                        continue

                    # check tmobile is whitelist or not
                    white_list = check_zs_phone(tmobile, self.db)
                    if not white_list:
                        logging.error(
                            "[UWEB] mobile: %s is not whitelist.", tmobile)
                        r['status'] = UWEB.TERMINAL_STATUS.MOBILE_NOT_ORDERED
                        res.append(r)
                        continue

                    existed_terminal = self.db.get("SELECT id, tid, service_status"
                                                   "  FROM T_TERMINAL_INFO"
                                                   "  WHERE mobile = %s", tmobile)
                    if existed_terminal:
                        if existed_terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
                            delete_terminal(
                                existed_terminal.tid, self.db, self.redis)
                            res.append(r)
                        else:
                            r.status = UWEB.TERMINAL_STATUS.EXISTED
                            res.append(r)
                    else:
                        res.append(r)
            # remove tmp file
            os.remove(file_path)
            self.render("fileUpload.html",
                        status=ErrorCode.SUCCESS,
                        res=res)

        except Exception as e:
            logging.exception("[UWEB] Batch import failed. cid: %s, Exception: %s",
                              self.current_user.cid, e.args)
            status = ErrorCode.ILLEGAL_FILE
            self.render("fileUpload.html",
                        status=status,
                        message=ErrorCode.ERROR_MESSAGE[status])
示例#4
0
文件: batch.py 项目: jcsy521/ydws
    def post(self):
        try:
            data = DotDict(json_decode(self.request.body))
            gid = data.gid
            mobiles = list(data.mobiles)
            logging.info("[UWEB] batch jh: %s, corp_id: %s",
                         data, self.current_user.cid)
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.exception("[UWEB] Invalid data format. Exception: %s",
                              e.args)
            self.write_ret(status)
            return

        try:
            status = ErrorCode.SUCCESS
            begintime = int(time.time())
            now_ = datetime.datetime.now()
            endtime = now_ + relativedelta(years=1)
            endtime = int(time.mktime(endtime.timetuple()))
            res = []
            for item in mobiles:
                tmobile = item['tmobile']
                if item['umobile']:
                    umobile = item['umobile']
                else:
                    corp = self.db.get(
                        "SELECT cid, mobile FROM T_CORP WHERE cid = %s", self.current_user.cid)
                    umobile = corp.mobile

                biz_type = item['biz_type']
                r = DotDict(tmobile=tmobile,
                            status=ErrorCode.SUCCESS)
                # 1. add terminal

                terminal = self.db.get("SELECT id, tid, service_status FROM T_TERMINAL_INFO WHERE mobile = %s",
                                       tmobile)
                if terminal:
                    if terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
                        delete_terminal(terminal.tid, self.db, self.redis)
                    else:
                        logging.error("[UWEB] mobile: %s already existed.", tmobile)
                        r['status'] = ErrorCode.TERMINAL_ORDERED
                        continue

                terminal_info = dict(tid=tmobile,
                                     group_id=gid,
                                     tmobile=tmobile,
                                     owner_mobile=umobile,
                                     mannual_status=UWEB.DEFEND_STATUS.YES,
                                     begintime=begintime,
                                     endtime=4733481600,
                                     offline_time=begintime,
                                     login_permit=0,
                                     biz_type=biz_type,
                                     service_status=UWEB.SERVICE_STATUS.ON)

                if int(biz_type) == UWEB.BIZ_TYPE.YDWS:
                    register_sms = SMSCode.SMS_REGISTER % (umobile, tmobile)
                    ret = SMSHelper.send_to_terminal(tmobile, register_sms)
                else:
                    activation_code = QueryHelper.get_activation_code(self.db)
                    tid = get_tid_from_mobile_ydwq(tmobile)
                    terminal_info['tid'] = tid
                    terminal_info['activation_code'] = activation_code
                    terminal_info[
                        'service_status'] = UWEB.SERVICE_STATUS.TO_BE_ACTIVATED

                    register_sms = SMSCode.SMS_REGISTER_YDWQ % (
                        ConfHelper.UWEB_CONF.url_out, activation_code)
                    ret = SMSHelper.send(tmobile, register_sms)

                add_terminal(terminal_info, self.db, self.redis)
                # record the add action, enterprise
                bind_info = dict(tid=terminal_info['tid'],
                                 tmobile=tmobile,
                                 umobile=umobile,
                                 group_id=gid,
                                 cid=self.current_user.cid,
                                 add_time=int(time.time()))
                record_add_action(bind_info, self.db)

                ret = DotDict(json_decode(ret))
                if ret.status == ErrorCode.SUCCESS:
                    self.db.execute("UPDATE T_TERMINAL_INFO"
                                    "  SET msgid = %s"
                                    "  WHERE tid = %s",
                                    ret['msgid'], terminal_info['tid'])
                else:
                    r['status'] = ErrorCode.FAILED

                # 2. add user
                user_info = dict(umobile=umobile,
                                 password='******',
                                 uname=umobile)
                add_user(user_info, self.db, self.redis)
                res.append(r)
            self.write_ret(status,
                           dict_=DotDict(res=res))
        except Exception as e:
            logging.exception("[UWEB] cid: %s batch jh failed. Exception: %s",
                              self.current_user.cid, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
示例#5
0
文件: batch.py 项目: jcsy521/ydws
    def post(self):
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            logging.info("[UWEB] batch delete request: %s, corp_id: %s",
                         data, self.current_user.cid)
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.exception("[UWEB] Invalid data format. Exception: %s",
                              e.args)
            self.write_ret(status)
            return

        try:
            status = ErrorCode.SUCCESS
            tids = list(data.tids)
            flag = data.flag
            res = []
            for tid in tids:
                r = DotDict(tid=tid,
                            status=ErrorCode.SUCCESS)

                terminal = QueryHelper.get_available_terminal(tid, self.db)
                if not terminal:
                    r.status = ErrorCode.SUCCESS
                    res.append(r)
                    logging.error(
                        "[UWEB] The terminal with tid: %s does not exist!", tid)
                    continue

                key = get_del_data_key(tid)
                self.redis.set(key, flag)
                biz_type = QueryHelper.get_biz_type_by_tmobile(
                    terminal.mobile, self.db)
                if int(biz_type) == UWEB.BIZ_TYPE.YDWQ:
                    delete_terminal(tid, self.db, self.redis)
                    res.append(r)
                    continue
                elif int(biz_type) == UWEB.BIZ_TYPE.YDWS:
                    if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE:
                        if terminal.mobile == tid:
                            delete_terminal(tid, self.db, self.redis)
                        else:
                            r.status = self.send_jb_sms(
                                terminal.mobile, terminal.owner_mobile, tid)
                        res.append(r)
                        continue

                # NOT: unbind. TODO: It should be re-factor some day.
                seq = str(int(time.time() * 1000))[-4:]
                args = DotDict(seq=seq,
                               tid=tid)
                response = GFSenderHelper.forward(
                    GFSenderHelper.URLS.UNBIND, args)
                response = json_decode(response)
                if response['success'] == ErrorCode.SUCCESS:
                    logging.info("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind successfully",
                                 self.current_user.uid, tid, terminal.mobile)
                    res.append(r)
                else:
                    # unbind failed. clear sessionID for relogin, then unbind
                    # it again
                    sessionID_key = get_terminal_sessionID_key(tid)
                    self.redis.delete(sessionID_key)
                    logging.error('[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind failed, message: %s, send JB sms...',
                                  self.current_user.uid, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[response['success']])
                    unbind_sms = SMSCode.SMS_UNBIND
                    biz_type = QueryHelper.get_biz_type_by_tmobile(
                        terminal.mobile, self.db)
                    if biz_type != UWEB.BIZ_TYPE.YDWS:
                        ret = DotDict(status=ErrorCode.SUCCESS)
                    else:
                        ret = SMSHelper.send_to_terminal(
                            terminal.mobile, unbind_sms)
                        ret = DotDict(json_decode(ret))
                    if ret.status == ErrorCode.SUCCESS:
                        res.append(r)
                        self.db.execute("UPDATE T_TERMINAL_INFO"
                                        "  SET service_status = %s"
                                        "  WHERE id = %s",
                                        UWEB.SERVICE_STATUS.TO_BE_UNBIND,
                                        terminal.id)
                        logging.info("[UWEB] uid: %s, tid: %s, tmobile: %s SMS unbind successfully.",
                                     self.current_user.uid, tid, terminal.mobile)
                    else:
                        r.status = ErrorCode.FAILED
                        res.append(r)
                        logging.error("[UWEB] uid: %s, tid: %s, tmobile: %s SMS unbind failed. Message: %s",
                                      self.current_user.uid, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[status])

            self.write_ret(status, dict_=DotDict(res=res))
        except Exception as e:
            logging.exception("[UWEB] cid: %s batch delete failed. Exception: %s",
                              self.current_user.cid, e.args)
            status = ErrorCode.ILLEGAL_FILE
            self.write_ret(status)
示例#6
0
    def post(self):
        """Regist a pair of umobile and tmobile.
        """
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            logging.info("[UWEB] Register request: %s", data)
            umobile = data.umobile
            tmobile = data.tmobile
            captcha = data.captcha
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.exception("[UWEB] Invalid data format. Exception: %s",
                              e.args)
            self.write_ret(status)
            return

        try:
            # check tmobile is whitelist or not
            white_list = check_zs_phone(tmobile, self.db)
            if not white_list:
                logging.info("[UWEB] Mobile is not whitelist. tmobile: %s.", tmobile)
                status = ErrorCode.MOBILE_NOT_ORDERED
                message = ErrorCode.ERROR_MESSAGE[status] % tmobile
                self.write_ret(status, message=message)
                return

            captcha_key = get_captcha_key(umobile)
            captcha_old = self.redis.get(captcha_key)
            if captcha_old:
                if captcha == str(captcha_old):
                    terminal = QueryHelper.get_terminal_by_tmobile(
                        tmobile, self.db)
                    if terminal:
                        if terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
                            # delete to_be_unbind terminal!
                            delete_terminal(terminal.tid, self.db, self.redis)
                        else:
                            status = ErrorCode.TERMINAL_ORDERED
                            logging.info("[UWEB] Regist failed. umobile: %s, tmobile: %s  Message: %s",
                                         umobile, tmobile, ErrorCode.ERROR_MESSAGE[status])
                            self.write_ret(status)
                            return

                    register_sms = SMSCode.SMS_REGISTER % (umobile, tmobile)
                    ret = SMSHelper.send_to_terminal(tmobile, register_sms)
                    ret = DotDict(json_decode(ret))
                    if ret.status == ErrorCode.SUCCESS:
                        logging.info("[UWEB] Regist successfully. umobile: %s, tmobile: %s ",
                                     umobile, tmobile)
                        self.redis.delete(captcha_key)
                    else:
                        status = ErrorCode.REGISTER_FAILED
                        logging.error("[UWEB] Regist failed. umobile: %s, tmobile: %s. Message: %s",
                                      umobile, tmobile, ErrorCode.ERROR_MESSAGE[status])
                else:
                    status = ErrorCode.WRONG_CAPTCHA
                    logging.error("[UWEB] Regist failed. umobile: %s, captcha: %s, captcha_old: %s, Message: %s",
                                  umobile, captcha, captcha_old, ErrorCode.ERROR_MESSAGE[status])
            else:
                status = ErrorCode.NO_CAPTCHA
                logging.error("[UWEB] Register failed. umobile: %s, captcha: %s, Message: %s",
                              umobile, captcha, ErrorCode.ERROR_MESSAGE[status])
            self.write_ret(status)
        except Exception as e:
            logging.exception("[UWEB] Register failed. umobile: %s tmobile: %s , Exception: %s",
                              umobile, tmobile, e.args)
            status = ErrorCode.REGISTER_FAILED
            self.write_ret(status)
示例#7
0
def execute(mobile):
    db = DBConnection().db
    redis = MyRedis()
    terminal = db.get("SELECT tid, mobile FROM T_TERMINAL_INFO WHERE mobile = %s LIMIT 1", mobile)
    if terminal:
        delete_terminal(terminal.tid, db, redis, del_user=True)
示例#8
0
文件: bind.py 项目: jcsy521/ydws
    def post(self):
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            tmobile = data.get('tmobile', None)
            umobile = data.get('umobile', None)
            group_id = data.get('group_id', None)
            if int(group_id) == -1:
                group = QueryHelper.get_default_group_by_cid(self.current_user.cid, self.db)
                group_id = group.gid
            cnum = data.get('cnum', None)
            avatar = None
            avatar_ = data.get('avatar', None)
            if avatar_:
                avatar = base64.urlsafe_b64decode(str(avatar_))
            logging.info("[BIND] umobile: %s, tmobile: %s",
                         umobile, tmobile)
        except Exception as e:
            logging.exception("[BIND] Illegal format, body: %s, Exception: %s",
                              self.request.body, e.args)
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            self.write_ret(status)
            return

        try:
            # record the add action 
            begintime = int(time.time())
            now_ = datetime.datetime.now()
            endtime = now_ + relativedelta(years=1)
            endtime = int(time.mktime(endtime.timetuple()))

            # avatar
            terminal = self.db.get("SELECT id, tid, service_status FROM T_TERMINAL_INFO WHERE mobile = %s",
                                   tmobile)
            if terminal:
                if terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
                    delete_terminal(terminal.tid, self.db, self.redis)
                else:
                    status = ErrorCode.TERMINAL_ORDERED
                    logging.error("[UWEB] mobile: %s already existed.", tmobile)
                    self.write_ret(status)
                    return
 
            tid = get_tid_from_mobile_ydwq(tmobile)
            activation_code = QueryHelper.get_activation_code(self.db)
            terminal_info = dict(tid=tid,
                                 group_id=group_id,
                                 tmobile=tmobile,
                                 owner_mobile=umobile,
                                 mannual_status=UWEB.DEFEND_STATUS.YES,
                                 begintime=begintime,
                                 endtime=4733481600,
                                 offline_time=begintime,
                                 cnum=cnum,
                                 icon_type=0,
                                 login_permit=1,
                                 push_status=1,
                                 vibl=1,
                                 use_scene=3,
                                 activation_code=activation_code,
                                 service_status=UWEB.SERVICE_STATUS.TO_BE_ACTIVATED)
            add_terminal(terminal_info, self.db, self.redis)
            bind_info = dict(tid=tmobile, 
                             tmobile=tmobile,
                             umobile=umobile,
                             group_id=group_id,
                             cid=self.current_user.cid, 
                             add_time=begintime)
            record_add_action(bind_info, self.db)
            avatar_full_path, avatar_path, avatar_name, avatar_time = self.get_avatar_info(tmobile)

            register_sms = SMSCode.SMS_REGISTER_YDWQ % (ConfHelper.UWEB_CONF.url_out, activation_code)
            ret = SMSHelper.send(tmobile, register_sms)
            ret = DotDict(json_decode(ret))
            if ret.status == ErrorCode.SUCCESS:
                self.db.execute("UPDATE T_TERMINAL_INFO"
                                "  SET msgid = %s"
                                "  WHERE mobile = %s",
                                ret['msgid'], tmobile)

                if avatar:
                    avatar_name = tmobile + '.png'
                    avatar_path = self.application.settings['avatar_path'] + avatar_name
                    avatar_full_path = self.application.settings['server_path'] + avatar_path

                    img = open(avatar_full_path, 'w')
                    img.write(avatar)
                    img.close()
                    avatar_time = self.update_avatar_time(tmobile)
                    logging.info("[BIND] avatar_time: %s, tmobile: %s, user: %s",
                                 avatar_time, tmobile, self.current_user.cid)
                else:
                    logging.error("[BIND] Terminal: %s has no avatar.",
                                  tmobile)
            else:
                logging.error("[BIND] Send %s to terminal %s failed.", register_sms, tmobile)

            self.write_ret(status,
                           dict_=dict(avatar_path=avatar_path,
                                      avatar_time=avatar_time))
        except Exception as e:
            logging.exception("[BIND] Bind terminal failed, user: %s. Exception: %s",
                              self.current_user.uid, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
示例#9
0
文件: terminal.py 项目: jcsy521/ydws
    def delete(self):
        """Delete a terminal.
        """
        try:
            status = ErrorCode.SUCCESS
            tid = self.get_argument('tid', None)
            flag = self.get_argument('flag', 0)
            logging.info("[UWEB] Corp delete terminal request. tid: %s, flag: %s, cid: %s",
                         tid, flag, self.current_user.cid)

            terminal = QueryHelper.get_available_terminal(tid, self.db)
            if not terminal:
                logging.error("[UWEB] The terminal with tid: %s does not exist!", 
                              tid)
                status = ErrorCode.TERMINAL_NOT_EXISTED
                self.write_ret(status)
                return

            t_info = QueryHelper.get_terminal_basic_info(tid, self.db)
            key = get_del_data_key(tid)
            self.redis.set(key, flag)
            biz_type = QueryHelper.get_biz_type_by_tmobile(
                terminal.mobile, self.db)
            if int(biz_type) == UWEB.BIZ_TYPE.YDWS:
                if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE:
                    if terminal.mobile == tid:
                        delete_terminal(tid, self.db, self.redis)
                    else:
                        status = self.send_jb_sms(
                            terminal.mobile, terminal.owner_mobile, tid)

                    if status == ErrorCode.SUCCESS:
                        WSPushHelper.pushS3(tid, self.db, self.redis, t_info)
                    self.write_ret(status)
                    return
            else:
                delete_terminal(tid, self.db, self.redis)
                if status == ErrorCode.SUCCESS:
                    WSPushHelper.pushS3(tid, self.db, self.redis, t_info)
                self.write_ret(status)
                return

            # unbind terminal
            seq = str(int(time.time() * 1000))[-4:]
            args = DotDict(seq=seq,
                           tid=tid)
            response = GFSenderHelper.forward(GFSenderHelper.URLS.UNBIND, args)
            response = json_decode(response)
            logging.info(
                "[UWEB] UNBind terminal: %s, response: %s", tid, response)
            if response['success'] == ErrorCode.SUCCESS:
                logging.info("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind successfully",
                             self.current_user.uid, tid, terminal.mobile)
            else:
                status = response['success']
                # unbind failed. clear sessionID for relogin, then unbind it
                # again
                clear_sessionID(self.redis, tid)
                logging.error('[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind failed, message: %s, send JB sms...',
                              self.current_user.uid, tid, terminal.mobile,
                              ErrorCode.ERROR_MESSAGE[status])
                status = self.send_jb_sms(
                    terminal.mobile, terminal.owner_mobile, tid)

            if status == ErrorCode.SUCCESS:
                WSPushHelper.pushS3(tid, self.db, self.redis, t_info)
            self.write_ret(status)
        except Exception as e:
            logging.exception("[UWEB] Delete terminal failed. cid: %s, Exception: %s",
                              self.current_user.cid, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
示例#10
0
文件: terminal.py 项目: jcsy521/ydws
    def post(self):
        """Add a terminal.
        """
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            logging.info("[UWEB] Corp add terminal request: %s, cid: %s",
                         data, self.current_user.cid)
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            self.write_ret(status)
            return

        try:
            if data.has_key('cnum') and not check_cnum(data.cnum):
                status = ErrorCode.ILLEGAL_CNUM
                self.write_ret(status)
                return

            # 1 year
            begintime = int(time.time())
            now_ = datetime.datetime.now()
            endtime = now_ + relativedelta(years=1)
            endtime = int(time.mktime(endtime.timetuple()))

            # 1: add terminal
            #umobile = data.umobile if data.umobile else self.current_user.cid
            if data.umobile:
                umobile = data.umobile
            else:
                corp = QueryHelper.get_corp_by_cid(self.current_user.cid, self.db)
                umobile = corp.get('c_mobile', '')

            terminal = QueryHelper.get_terminal_by_tmobile(data.tmobile, self.db)
            if terminal:
                if terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND:
                    delete_terminal(terminal.tid, self.db, self.redis)
                else:
                    logging.error(
                        "[UWEB] mobile: %s already existed.", data.tmobile)
                    status = ErrorCode.TERMINAL_ORDERED
                    self.write_ret(status)
                    return

            vibl = data.get("vibl")
            use_scene = get_use_scene_by_vibl(vibl)

            biz_type = data.get('biz_type', UWEB.BIZ_TYPE.YDWS)
            tid = data.tmobile

            terminal_info = dict(tid=tid,
                                 group_id=data.group_id,
                                 tmobile=data.tmobile,
                                 owner_mobile=umobile,
                                 mannual_status=UWEB.DEFEND_STATUS.YES,
                                 begintime=begintime,
                                 endtime=4733481600,
                                 offline_time=begintime,
                                 cnum=data.cnum,
                                 icon_type=data.icon_type,
                                 login_permit=data.login_permit,
                                 push_status=data.push_status,
                                 vibl=data.vibl,
                                 use_scene=use_scene,
                                 biz_type=biz_type,
                                 speed_limit=data.speed_limit,
                                 stop_interval=data.stop_interval,
                                 service_status=UWEB.SERVICE_STATUS.ON)

            if int(biz_type) == UWEB.BIZ_TYPE.YDWS:
                # 0. check tmobile is whitelist or not
                white_list = check_zs_phone(data.tmobile, self.db)
                if not white_list:
                    logging.error("[UWEB] mobile: %s is not whitelist.", data.tmobile)
                    status = ErrorCode.MOBILE_NOT_ORDERED
                    message = ErrorCode.ERROR_MESSAGE[status] % data.tmobile
                    self.write_ret(status, message=message)
                    return

                # 4: send message to terminal
                register_sms = SMSCode.SMS_REGISTER % (umobile, data.tmobile)
                ret = SMSHelper.send_to_terminal(data.tmobile, register_sms)
            else:
                tid = get_tid_from_mobile_ydwq(data.tmobile)
                activation_code = QueryHelper.get_activation_code(self.db)
                terminal_info['tid'] = tid
                terminal_info['activation_code'] = activation_code
                terminal_info['service_status'] = UWEB.SERVICE_STATUS.TO_BE_ACTIVATED
                register_sms = SMSCode.SMS_REGISTER_YDWQ % (ConfHelper.UWEB_CONF.url_out, activation_code)
                ret = SMSHelper.send(data.tmobile, register_sms)

            add_terminal(terminal_info, self.db, self.redis)
            # record the add action
            bind_info = dict(tid=data.tmobile,
                             tmobile=data.tmobile,
                             umobile=umobile,
                             group_id=data.group_id,
                             cid=self.current_user.cid,
                             add_time=int(time.time()))
            record_add_action(bind_info, self.db)

            if ret:
                ret = DotDict(json_decode(ret))
                if ret.status == ErrorCode.SUCCESS:
                    self.db.execute("UPDATE T_TERMINAL_INFO"
                                    "  SET msgid = %s"
                                    "  WHERE mobile = %s",
                                    ret['msgid'], data.tmobile)
                else:
                    logging.error("[UWEB] Send %s to terminal %s failed.", 
                                  register_sms, data.tmobile)
            else:
                logging.error("[UWEB] Send %s to terminal %s failed.", 
                              register_sms, data.tmobile)

            # NOTE: add user
            user_info = dict(umobile=umobile,
                             password='******',
                             uname=umobile)
            add_user(user_info, self.db, self.redis)

            self.write_ret(status)
        except Exception as e:
            logging.exception("[UWEB] Update terminal info failed. cid:%s, Exception: %s",
                              self.current_user.cid, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
示例#11
0
文件: message.py 项目: jcsy521/ydws
    def post(self):
        status = ErrorCode.SUCCESS
        try:
            data = json_decode(self.request.body)
            logging.info("[LOG] message request: %s", data)
            sms_type = data.get('sms_type')
            tmobile = data.get('tmobile')
            content = ''
            if check_phone(tmobile) is None:
                status = ErrorCode.ILLEGAL_MOBILE
                self.write_ret(status)
            else:
                if sms_type == 'JH':
                    umobile = data.get('umobile')
                    if check_phone(umobile):
                        content = ':SIM' + ' ' + umobile + ':' + tmobile
                        SMSHelper.send_to_terminal(tmobile, content)
                        self.write_ret(status)
                    else:
                        status = ErrorCode.ILLEGAL_MOBILE
                        self.write_ret(status)
                elif sms_type == 'JB':
                    content = ':' + sms_type
                    is_clear = data.get('is_clear')
                    ret = SMSHelper.send_to_terminal(tmobile, content)
                    ret = json_decode(ret)
                    terminal = self.acbdb.get("SELECT id, tid, owner_mobile, login"
                                              "  FROM T_TERMINAL_INFO"
                                              "  WHERE mobile = %s"
                                              "    AND service_status = 1",
                                              tmobile)
                    if not terminal:
                        status = ErrorCode.TERMINAL_NOT_EXISTED
                        logging.error("The terminal with tmobile: %s does not exist!", 
                                      tmobile)
                        self.write_ret(status)
                        return
                    umobile = terminal.owner_mobile

                    if ret['status'] == 0:
                        self.acbdb.execute("UPDATE T_TERMINAL_INFO"
                                           "  SET service_status = 2"
                                           "  WHERE mobile = %s",
                                           tmobile)
                        # terminals = self.acbdb.query("SELECT id FROM T_TERMINAL_INFO"
                        #                              "  WHERE owner_mobile = %s"
                        #                              "    AND service_status = 1",
                        #                              umobile)
                        # clear user
                        # if len(terminals) == 0:
                        #    self.acbdb.execute("DELETE FROM T_USER"
                        #                        "  WHERE mobile = %s",
                        #                        umobile)
                    if is_clear == 1:
                        clear_data(terminal['tid'], self.acbdb, self.redis)
                    self.write_ret(status)

                elif sms_type == 'CQ':
                    content = ':' + sms_type
                    SMSHelper.send_to_terminal(tmobile, content)
                    self.write_ret(status)
                elif sms_type == 'REBOOT':
                    content = ':' + sms_type
                    # SMSHelper.send_to_terminal(tmobile,content)
                    SMSHelper.send_update_to_terminal(tmobile, content)
                    self.write_ret(status)
                elif sms_type == 'TEST':
                    content = u'尊敬的顾客,您好:这是一条测试短信,收到本条短信,说明短信提示服务正常,本短信不需要回复,如有问题,请和客服人员联系。感谢使用我们的产品,您的移动卫士。'
                    SMSHelper.send(tmobile, content)
                    self.write_ret(status)
                elif sms_type == 'KQLY':
                    content = ':%s 30' % sms_type
                    SMSHelper.send_to_terminal(tmobile, content)
                    self.write_ret(status)
                elif sms_type == 'LQGZ':
                    content = ':%s 30' % sms_type
                    SMSHelper.send_to_terminal(tmobile, content)
                    self.write_ret(status)
                elif sms_type == 'DW':
                    content = ':' + sms_type
                    SMSHelper.send_to_terminal(tmobile, content)
                    self.write_ret(status)

                elif sms_type == 'UPDATE':
                    content = ':' + sms_type
                    SMSHelper.send_update_to_terminal(tmobile, content)
                    self.write_ret(status)

                elif sms_type == 'DEL':
                    terminal = self.acbdb.get(
                        'SELECT tid FROM T_TERMINAL_INFO WHERE mobile=%s', tmobile)
                    if terminal:
                        delete_terminal(
                            terminal.tid, self.acbdb, self.redis, del_user=False)
                    self.write_ret(status)

                elif sms_type == 'DOMAIN':
                    ip = data.get('domain')
                    content = ':DOMAIN ' + ip
                    info = self.acbdb.get(
                        'SELECT * FROM T_TERMINAL_INFO WHERE mobile=%s', tmobile)
                    if info:
                        self.acbdb.execute("UPDATE T_TERMINAL_INFO"
                                           " SET domain=%s WHERE mobile=%s",
                                           ip, tmobile)
                        SMSHelper.send_to_terminal(tmobile, content)
                        self.write_ret(status)
                    else:
                        status = ErrorCode.TERMINAL_NOT_EXISTED
                        self.write_ret(status)
        except Exception, e:
            logging.exception("acb-->sms post exception : %s", e)
            self.render('errors/error.html', 
                        message=ErrorCode.ERROR_MESSAGE[ErrorCode.FAILED])