Exemple #1
0
    def post(self):
        """Create business for a couple of users.
        """
        fields = DotDict(group_id="",
                         cnum="",
                         ctype="",
                         ccolor="",
                         cbrand="",
                         tmobile="",
                         begintime="",
                         endtime="",
                         uname="",
                         umobile="",
                         password="",
                         address="",
                         email="",
                         ecmobile="")
        for key in fields.iterkeys():
            fields[key] = self.get_argument(key,'')
            #if not check_sql_injection(fields[key]):
            #    logging.error("Create business condition contain SQL inject. %s : %s", key, fields[key])
            #    self.render('errors/error.html',
            #        message=ErrorCode.ERROR_MESSAGE[ErrorCode.CREATE_CONDITION_ILLEGAL])
            #    return

        white_list = check_zs_phone(fields.tmobile, self.db)
        if not white_list:
            logging.error("Create business error, %s is not whitelist", fields.tmobile)
            self.render('errors/error.html',
                message=ErrorCode.ERROR_MESSAGE[ErrorCode.MOBILE_NOT_ORDERED])
            return

        try:
            # 1: add user
            user_info = dict(umobile=fields.umobile,
                             password=fields.password,
                             uname=fields.uname,
                             address=fields.address,
                             email=fields.email)
            add_user(user_info, self.db, self.redis)

            # record the add action
            bind_info = dict(tid=fields.tmobile,
                             tmobile=fields.tmobile,
                             umobile=fields.umobile,
                             group_id=-1,
                             cid=fields.ecmobile,
                             add_time=int(time.time()))
            record_add_action(bind_info, self.db)

            # 2: add terminal
            if not fields.umobile:
                user_mobile = fields.ecmobile
            else:
                user_mobile = fields.umobile


            terminal_info = dict(tmobile=fields.tmobile,
                                 owner_mobile=user_mobile,
                                 begintime=fields.begintime,
                                 offline_time=fields.begintime,
                                 endtime=4733481600, # 2120.1.1
                                 cnum=fields.cnum,
                                 ctype=fields.ctype,
                                 ccolor=fields.ccolor,
                                 cbrand=fields.cbrand)
            add_terminal(terminal_info, self.db, self.redis)

            # 4: send message to terminal
            register_sms = SMSCode.SMS_REGISTER % (fields.umobile, fields.tmobile) 
            ret = SMSHelper.send_to_terminal(fields.tmobile, register_sms)
            ret = DotDict(json_decode(ret))
            sms_status = 0
            if ret.status == ErrorCode.SUCCESS:
                self.db.execute("UPDATE T_TERMINAL_INFO"
                                "  SET msgid = %s"
                                "  WHERE mobile = %s",
                                ret['msgid'], fields.tmobile)
            #convert front desk need format 
                sms_status = 1
            else:
                sms_status = 0
                logging.error("Create business sms send failure. terminal mobile: %s, owner mobile: %s", fields.tmobile, fields.mobile)
            fields.sms_status = sms_status
            fields.service_status = 1
            self.render('business/list.html',
                        business=fields,
                        status=ErrorCode.SUCCESS,
                        message='')
        except Exception as e:
            logging.exception("Create business failed. Exception: %s.",
                              e.args)
            self.render('errors/error.html',
                        message=ErrorCode.ERROR_MESSAGE[ErrorCode.CREATE_USER_FAILURE])
Exemple #2
0
    def post(self):
        """Create business for a couple of users.
        """
        fields = DotDict(ecid="",
                         cnum="",
                         ctype="",
                         ccolor="",
                         cbrand="",
                         tmobile="",
                         begintime="",
                         endtime="",
                         uname="",
                         umobile="",
                         password="",
                         address="",
                         email="",
                         ecmobile="",
                         biz_type="")
        for key in fields.iterkeys():
            fields[key] = self.get_argument(key, '')
            # if not check_sql_injection(fields[key]):
            #    logging.error("Create business condition contain SQL inject. %s : %s", key, fields[key])
            #    self.render('errors/error.html',
            #        message=ErrorCode.ERROR_MESSAGE[ErrorCode.CREATE_CONDITION_ILLEGAL])
            #    return
        white_list = check_zs_phone(fields.tmobile, self.db)
        if not white_list:
            logging.error(
                "Create business error, %s is not whitelist", fields.tmobile)
            self.render('errors/error.html',
                        message=ErrorCode.ERROR_MESSAGE[ErrorCode.MOBILE_NOT_ORDERED])
            return

        try:
            # 1: add user
            user = self.db.get(
                "SELECT id FROM T_USER WHERE mobile = %s", fields.umobile)
            if not user:
                self.db.execute("INSERT INTO T_USER(id, uid, password, name, mobile, address, email, remark)"
                                "  VALUES(NULL, %s, password(%s), %s, %s, %s, %s, NULL)",
                                fields.umobile, '111111',
                                fields.uname, fields.umobile,
                                fields.address, fields.email)
                self.db.execute("INSERT INTO T_SMS_OPTION(uid)"
                                "  VALUES(%s)",
                                fields.umobile)
            # 2: add terminal
            group = self.db.get("SELECT id FROM T_GROUP"
                                "  WHERE corp_id = %s AND type = 0 LIMIT 1",
                                fields.ecid)
            if not group:
                gid = self.db.execute("INSERT INTO T_GROUP(corp_id, name, type)"
                                      "  VALUES(%s, default, default)",
                                      fields.ecid)
            else:
                gid = group.id

            # record the add action, enterprise
            bind_info = dict(tid=fields.tmobile,
                             tmobile=fields.tmobile,
                             umobile=fields.umobile,
                             group_id=gid,
                             cid=fields.ecmobile,
                             add_time=int(time.time()))
            record_add_action(bind_info, self.db)

            if not fields.umobile:
                user_mobile = fields.ecmobile
            else:
                user_mobile = fields.umobile

            # 3: send message to terminal
            biz_type = int(fields.biz_type)
            if biz_type == UWEB.BIZ_TYPE.YDWS:
                self.db.execute("INSERT INTO T_TERMINAL_INFO(tid, group_id, mobile, owner_mobile,"
                                "  begintime, endtime, offline_time, login_permit)"
                                "  VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                                fields.tmobile, gid,
                                fields.tmobile, user_mobile,
                                fields.begintime, 4733481600, fields.begintime, 0)
                register_sms = SMSCode.SMS_REGISTER % (
                    fields.umobile, fields.tmobile)
                ret = SMSHelper.send_to_terminal(fields.tmobile, register_sms)
                self.db.execute("INSERT INTO T_CAR(tid, cnum, type, color, brand)"
                                "  VALUES(%s, %s, %s, %s, %s)",
                                fields.tmobile, fields.cnum,
                                fields.ctype, fields.ccolor, fields.cbrand)

            else:
                tid = get_tid_from_mobile_ydwq(fields.tmobile)
                activation_code = QueryHelper.get_activation_code(self.db)
                self.db.execute("INSERT INTO T_TERMINAL_INFO(tid, group_id, mobile, owner_mobile,"
                                "  begintime, endtime, offline_time, login_permit,"
                                "  biz_type, activation_code, service_status)"
                                "  VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                                tid, gid,
                                fields.tmobile, user_mobile,
                                fields.begintime, 4733481600,
                                fields.begintime, 0, biz_type,
                                activation_code, UWEB.SERVICE_STATUS.TO_BE_ACTIVATED)

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

                self.db.execute("INSERT INTO T_CAR(tid, cnum, type, color, brand)"
                                "  VALUES(%s, %s, %s, %s, %s)",
                                tid, fields.cnum,
                                fields.ctype, fields.ccolor, fields.cbrand)

            ret = DotDict(json_decode(ret))
            sms_status = 0
            if ret.status == ErrorCode.SUCCESS:
                self.db.execute("UPDATE T_TERMINAL_INFO"
                                "  SET msgid = %s"
                                "  WHERE mobile = %s",
                                ret['msgid'], fields.tmobile)
                # convert front desk need format
                sms_status = 1
            else:
                sms_status = 0
                logging.error("[ADMIN] Create business sms send failure."
                              "  terminal mobile: %s, owner mobile: %s",
                              fields.tmobile, fields.umobile)

            fields.sms_status = sms_status
            fields.service_status = 1
            self.render('business/list.html',
                        business=fields,
                        status=ErrorCode.SUCCESS,
                        message='')
        except Exception as e:
            logging.exception("Add terminal failed. Exception: %s.",
                              e.args)
            self.render('errors/error.html',
                        message=ErrorCode.ERROR_MESSAGE[ErrorCode.CREATE_USER_FAILURE])