def post(self): account_number = self.get_argument("account_number") account = self.db.query(models.TrAccount).get(account_number) user = self.query_account(account_number) form = account_forms.account_cancel_form() if account.status != 1: return self.render("account_form.html", user=user, form=form, msg=u"无效用户状态") if not form.validates(source=self.get_params()): return self.render("account_form.html", user=user, form=form) accept_log = models.TrAcceptLog() accept_log.accept_type = 'cancel' accept_log.accept_source = 'console' accept_log.account_number = form.d.account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = self.current_user.username accept_log.accept_desc = u"用户销户退费%s(元);%s" % ( form.d.fee_value, utils.safeunicode(form.d.operate_desc)) self.db.add(accept_log) self.db.flush() self.db.refresh(accept_log) old_expire_date = account.expire_date order = models.TrCustomerOrder() order.order_id = utils.gen_order_id() order.customer_id = user.customer_id order.product_id = user.product_id order.account_number = form.d.account_number order.order_fee = 0 order.actual_fee = -utils.yuan2fen(form.d.fee_value) order.pay_status = 1 order.order_source = 'console' order.accept_id = accept_log.id order.create_time = utils.get_currtime() order.order_desc = accept_log.accept_desc self.db.add(order) account.status = 3 self.db.commit() dispatch.pub(ACCOUNT_CHANNEL_EVENT, account.account_number, async=True) dispatch.pub(db_cache.CACHE_DELETE_EVENT, account_cache_key(account.account_number), async=True) self.redirect(self.detail_url_fmt(account_number))
def post(self): uname = self.get_argument("username") upass = self.get_argument("password") if not uname: return self.render_json(code=1, msg=u"请填写用户名") if not upass: return self.render_json(code=1, msg=u"请填写密码") enpasswd = md5(upass.encode()).hexdigest() opr = self.db.query(models.TrOperator).filter_by( operator_name=uname, operator_pass=enpasswd ).first() if not opr: return self.render_json(code=1, msg=u"用户名密码不符") if opr.operator_status == 1: return self.render_json(code=1, msg=u"该操作员账号已被停用") self.set_session_user(uname, self.request.remote_ip, opr.operator_type, utils.get_currtime()) if opr.operator_type == 1: for rule in self.db.query(models.TrOperatorRule).filter_by(operator_name=uname): permit.bind_opr(rule.operator_name, rule.rule_path) self.add_oplog(u'操作员(%s)登陆' % (uname)) self.db.commit() self.render_json(code=0, msg="ok")
def get_current_user(self): session_opr = ObjectDict() session_opr.username = '******' session_opr.ipaddr = self.request.remote_ip session_opr.opr_type = 0 session_opr.login_time = utils.get_currtime() return session_opr
def post(self): account_number = self.get_argument("account_number") account = self.db.query(models.TrAccount).get(account_number) if account.status != 2: return self.render_json(code=1, msg=u"用户当前状态不允许复机") account.status = 1 _datetime = datetime.datetime.now() _pause_time = datetime.datetime.strptime(account.last_pause, "%Y-%m-%d %H:%M:%S") _expire_date = datetime.datetime.strptime( account.expire_date + ' 23:59:59', "%Y-%m-%d %H:%M:%S") days = (_expire_date - _pause_time).days new_expire = (_datetime + datetime.timedelta(days=int(days))).strftime("%Y-%m-%d") account.expire_date = new_expire accept_log = models.TrAcceptLog() accept_log.accept_type = 'resume' accept_log.accept_source = 'console' accept_log.accept_desc = u"用户复机:上网账号:%s" % (account_number) accept_log.account_number = account.account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = self.current_user.username self.db.add(accept_log) self.db.commit() dispatch.pub(db_cache.CACHE_DELETE_EVENT, account_cache_key(account.account_number), async=True) return self.render_json(msg=u"操作成功")
def post(self): account_number = self.get_argument("account_number") account = self.db.query(models.TrAccount).get(account_number) if account.status != 1: return self.render_json(code=1, msg=u"用户当前状态不允许停机") _datetime = utils.get_currtime() account.last_pause = _datetime account.status = 2 accept_log = models.TrAcceptLog() accept_log.accept_type = 'pause' accept_log.accept_source = 'console' accept_log.accept_desc = u"用户停机:上网账号:%s" % (account_number) accept_log.account_number = account.account_number accept_log.accept_time = _datetime accept_log.operator_name = self.current_user.username self.db.add(accept_log) self.db.commit() dispatch.pub(ACCOUNT_PAUSE_EVENT, account.account_number, async=True) dispatch.pub(db_cache.CACHE_DELETE_EVENT,account_cache_key(account.account_number), async=True) return self.render_json(msg=u"操作成功")
def post(self): form = product_forms.product_update_form() if not form.validates(source=self.get_params()): return self.render("product_form.html", form=form) product = self.db.query(models.TrProduct).get(form.d.id) product.product_name = form.d.product_name product.product_status = form.d.product_status product.fee_months = int(form.d.get("fee_months", 0)) product.fee_times = utils.hour2sec(form.d.get("fee_times", 0)) product.fee_flows = utils.mb2kb(form.d.get("fee_flows", 0)) product.bind_mac = form.d.bind_mac product.bind_vlan = form.d.bind_vlan product.concur_number = form.d.concur_number product.fee_period = '' #form.d.fee_period or '' product.fee_price = utils.yuan2fen(form.d.fee_price) product.input_max_limit = utils.mbps2bps(form.d.input_max_limit) product.output_max_limit = utils.mbps2bps(form.d.output_max_limit) product.update_time = utils.get_currtime() self.add_oplog(u'修改资费信息:%s' % utils.safeunicode(product.product_name)) self.db.commit() self.redirect("/admin/product", permanent=False)
def post(self): account_number = self.get_argument("account_number") account = self.db.query(models.TrAccount).get(account_number) user = self.query_account(account_number) form = account_forms.account_next_form() form.product_id.set_value(user.product_id) if account.status not in (1, 4): return render("account_next_form", user=user, form=form, msg=u"无效用户状态") if not form.validates(source=self.get_params()): return render("account_next_form", user=user, form=form) accept_log = models.TrAcceptLog() accept_log.accept_type = 'next' accept_log.accept_source = 'console' accept_log.accept_desc = u"用户续费:上网账号:%s,续费%s元;%s" % ( account_number, form.d.fee_value, form.d.operate_desc or '') accept_log.account_number = form.d.account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = self.current_user.username self.db.add(accept_log) self.db.flush() self.db.refresh(accept_log) order_fee = 0 product = self.db.query(models.TrProduct).get(user.product_id) # 预付费包月 if product.product_policy == PPMonth: order_fee = decimal.Decimal(product.fee_price) * decimal.Decimal( form.d.months) order_fee = int(order_fee.to_integral_value()) # 买断包月,买断流量,买断时长 elif product.product_policy in (BOMonth, BOTimes, BOFlows): order_fee = int(product.fee_price) order = models.TrCustomerOrder() order.order_id = utils.gen_order_id() order.customer_id = user.customer_id order.product_id = user.product_id order.account_number = form.d.account_number order.order_fee = order_fee order.actual_fee = utils.yuan2fen(form.d.fee_value) order.pay_status = 1 order.accept_id = accept_log.id order.order_source = 'console' order.create_time = utils.get_currtime() old_expire_date = account.expire_date account.status = 1 account.expire_date = form.d.expire_date if product.product_policy == BOTimes: account.time_length += product.fee_times elif product.product_policy == BOFlows: account.flow_length += product.fee_flows order.order_desc = u"用户续费,续费前到期:%s,续费后到期:%s, 赠送天数: %s" % ( old_expire_date, account.expire_date, form.d.giftdays) self.db.add(order) self.add_oplog(order.order_desc) self.db.commit() dispatch.pub(ACCOUNT_NEXT_EVENT, order.account_number, async=True) dispatch.pub(db_cache.CACHE_DELETE_EVENT, account_cache_key(account.account_number), async=True) self.redirect(self.detail_url_fmt(account_number))
def post(self): form = customer_add_vform() try: request = self.parse_form_request() if not form.validates(**request): raise Exception(form.errors) if self.db.query(models.TrAccount).filter_by( account_number=form.d.account_number).count() > 0: raise Exception("account already exists") except Exception as err: self.render_result(code=1, msg=utils.safeunicode(err.message)) return try: customer = models.TrCustomer() customer.node_id = form.d.node_id customer.realname = form.d.realname customer.idcard = form.d.idcard customer.customer_name = form.d.customer_name or form.d.account_number customer.password = md5(form.d.password.encode()).hexdigest() customer.sex = '1' customer.age = '0' customer.email = form.d.email customer.mobile = form.d.mobile customer.address = form.d.address customer.create_time = form.d.begin_date + ' 00:00:00' customer.update_time = utils.get_currtime() customer.email_active = 1 customer.mobile_active = 1 customer.active_code = utils.get_uuid() self.db.add(customer) self.db.flush() self.db.refresh(customer) accept_log = models.TrAcceptLog() accept_log.accept_type = 'open' accept_log.accept_source = 'api' accept_log.accept_desc = u"API开通账号:%s" % form.d.account_number accept_log.account_number = form.d.account_number accept_log.accept_time = customer.update_time accept_log.operator_name = 'api' self.db.add(accept_log) self.db.flush() self.db.refresh(accept_log) order_fee = 0 actual_fee = 0 balance = 0 time_length = 0 flow_length = 0 expire_date = form.d.expire_date product = self.db.query(models.TrProduct).get(form.d.product_id) # 买断时长 if product.product_policy == BOTimes: time_length = int(form.d.time_length) # 买断流量 elif product.product_policy == BOFlows: flow_length = int(form.d.flow_length) # 预付费时长,预付费流量 elif product.product_policy in (PPTimes, PPFlow): balance = utils.yuan2fen(form.d.balance) expire_date = MAX_EXPIRE_DATE order = models.TrCustomerOrder() order.order_id = utils.gen_order_id() order.customer_id = customer.customer_id order.product_id = product.id order.account_number = form.d.account_number order.order_fee = order_fee order.actual_fee = actual_fee order.pay_status = 1 order.accept_id = accept_log.id order.order_source = 'console' order.create_time = customer.update_time order.order_desc = u"API开通账号" self.db.add(order) account = models.TrAccount() account.account_number = form.d.account_number account.customer_id = customer.customer_id account.product_id = order.product_id account.install_address = customer.address account.ip_address = '' account.mac_addr = '' account.password = self.aes.encrypt(form.d.password) account.status = 1 account.balance = balance account.time_length = time_length account.flow_length = flow_length account.expire_date = expire_date account.user_concur_number = product.concur_number account.bind_mac = product.bind_mac account.bind_vlan = product.bind_vlan account.vlan_id1 = 0 account.vlan_id2 = 0 account.create_time = customer.create_time account.update_time = customer.update_time self.db.add(account) self.add_oplog(u"API开户,%s" % form.d.account_number) self.db.commit() self.render_result(code=0, msg='success') except Exception as e: self.render_result(code=1, msg=utils.safeunicode(e.message)) import traceback traceback.print_exc()
def init_db(db): node = models.TrNode() node.id = 1 node.node_name = 'default' node.node_desc = u'默认区域' db.add(node) params = [ ('system_name', u'管理系统名称', u'UniFi Cloud Key Radius管理控制台'), ('system_ticket_expire_days', u'上网日志保留天数', '90'), ('is_debug', u'DEBUG模式', u'0'), ('expire_notify_days', '到期提醒提前天数', u'7'), ('expire_notify_interval', '到期提醒提前间隔(分钟)', u'1440'), ('expire_notify_tpl', '到期提醒邮件模板', u'账号到期通知\n尊敬的会员您好:\n您的账号#account#即将在#expire#到期,请及时续费!'), ('expire_notify_url', u'到期通知url', u'http://your_notify_url?account={account}&expire={expire}&email={email}&mobile={mobile}' ), ('expire_addrpool', u'到期提醒下发地址池', u'expire'), ('expire_session_timeout', u'到期用户下发最大会话时长(秒)', u'120'), ('smtp_server', u'SMTP服务器地址', u'smtp.ubnt.com.cn'), ('smtp_port', u'SMTP服务器端口', u'25'), ('smtp_user', u'SMTP用户名', u'*****@*****.**'), ('smtp_pwd', u'SMTP密码', u'service'), ('smtp_sender', u'SMTP发送人名称', u'运营中心'), ('smtp_from', u'SMTP邮件发送地址', u'*****@*****.**'), ('radius_bypass', u'Radius认证密码模式', u'1'), ('radius_acct_interim_intelval', u'Radius记账间隔(秒)', u'120'), ('radius_max_session_timeout', u'Radius最大会话时长(秒)', u'86400'), ('radius_auth_auto_unlock', u'并发自动解锁', '1') ] for p in params: param = models.TrParam() param.param_name = p[0] param.param_desc = p[1] param.param_value = p[2] db.add(param) opr = models.TrOperator() opr.id = 1 opr.operator_name = u'admin' opr.operator_type = 0 opr.operator_pass = md5('root').hexdigest() opr.operator_desc = 'admin' opr.operator_status = 0 db.add(opr) bas = models.TrBas() bas.ip_addr = '127.0.0.1' bas.vendor_id = '0' bas.bas_name = 'local bras' bas.bas_secret = 'secret' bas.coa_port = 3799 bas.time_type = 0 db.add(bas) product = models.TrProduct() product.product_name = u"测试2M包月20元" product.product_policy = 0 product.product_status = 0 product.fee_months = 0 product.fee_times = 0 product.fee_flows = 0 product.bind_mac = 0 product.bind_vlan = 0 product.concur_number = 0 product.fee_price = 2000 product.fee_period = '' product.input_max_limit = 1048576 product.output_max_limit = 1048576 * 2 product.create_time = utils.get_currtime() product.update_time = utils.get_currtime() db.add(product) db.commit() db.close()
def post(self): products = [(n.id, n.product_name) for n in self.get_opr_products()] form = account_forms.account_open_form(products) if not form.validates(source=self.get_params()): return self.render("account_open_form.html", form=form) if self.db.query(models.TrAccount).filter_by( account_number=form.d.account_number).count() > 0: return self.render("account_open_form.html", form=form, msg=u"上网账号已经存在") if form.d.ip_address and self.db.query(models.TrAccount).filter_by( ip_address=form.d.ip_address).count() > 0: return self.render("account_open_form.html", form=form, msg=u"ip%s已经被使用" % form.d.ip_address) accept_log = models.TrAcceptLog() accept_log.accept_type = 'open' accept_log.accept_source = 'console' accept_log.account_number = form.d.account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = self.current_user.username accept_log.accept_desc = u"用户新增账号:上网账号:%s" % (form.d.account_number) self.db.add(accept_log) self.db.flush() self.db.refresh(accept_log) _datetime = utils.get_currtime() order_fee = 0 balance = 0 expire_date = form.d.expire_date product = self.db.query(models.TrProduct).get(form.d.product_id) # 预付费包月 if product.product_policy == PPMonth: order_fee = decimal.Decimal(product.fee_price) * decimal.Decimal( form.d.months) order_fee = int(order_fee.to_integral_value()) # 买断包月,买断时长,买断流量 elif product.product_policy in (BOMonth, BOTimes, BOFlows): order_fee = int(product.fee_price) # 预付费时长,预付费流量 elif product.product_policy in (PPTimes, PPFlow): balance = utils.yuan2fen(form.d.fee_value) expire_date = MAX_EXPIRE_DATE order = models.TrCustomerOrder() order.order_id = utils.gen_order_id() order.customer_id = form.d.customer_id order.product_id = product.id order.account_number = form.d.account_number order.order_fee = order_fee order.actual_fee = utils.yuan2fen(form.d.fee_value) order.pay_status = 1 order.accept_id = accept_log.id order.order_source = 'console' order.create_time = _datetime order.order_desc = u"用户新开账号,赠送天数:%s" % (form.d.giftdays) self.db.add(order) account = models.TrAccount() account.account_number = form.d.account_number account.ip_address = form.d.ip_address account.customer_id = int(form.d.customer_id) account.product_id = order.product_id account.install_address = form.d.address account.mac_addr = '' account.password = self.aes.encrypt(form.d.password) account.status = form.d.status account.balance = balance account.time_length = int(product.fee_times) account.flow_length = int(product.fee_flows) account.expire_date = expire_date account.user_concur_number = product.concur_number account.bind_mac = product.bind_mac account.bind_vlan = product.bind_vlan account.vlan_id1 = 0 account.vlan_id2 = 0 account.create_time = _datetime account.update_time = _datetime account.account_desc = form.d.account_desc self.db.add(account) self.add_oplog(u"用户增开子账号 %s, 赠送天数:%s " % (account.account_number, form.d.giftdays)) self.db.commit() dispatch.pub(ACCOUNT_OPEN_EVENT, account.account_number, async=True) dispatch.pub(db_cache.CACHE_DELETE_EVENT, account_cache_key(account.account_number), async=True) self.redirect(self.detail_url_fmt(account.account_number))
def post(self): account_number = self.get_argument("account_number") products = [(n.id, n.product_name) for n in self.get_opr_products()] form = account_forms.account_change_form(products=products) account = self.db.query(models.TrAccount).get(account_number) user = self.query_account(account_number) if account.status not in (1, 4): return self.render("account_change_form.html", user=user, form=form, msg=u"无效用户状态") if not form.validates(source=self.get_params()): return self.render("account_change_form.html", user=user, form=form) product = self.db.query(models.TrProduct).get(form.d.product_id) accept_log = models.TrAcceptLog() accept_log.accept_type = 'change' accept_log.accept_source = 'console' accept_log.account_number = form.d.account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = self.current_user.username accept_log.accept_desc = u"用户资费变更为:%s;%s" % ( product.product_name, utils.safeunicode(form.d.operate_desc)) self.db.add(accept_log) self.db.flush() self.db.refresh(accept_log) old_exoire_date = account.expire_date account.product_id = product.id # (PPMonth,PPTimes,BOMonth,BOTimes,PPFlow,BOFlows) if product.product_policy in (PPMonth, BOMonth): account.expire_date = form.d.expire_date account.balance = 0 account.time_length = 0 account.flow_length = 0 elif product.product_policy in (PPTimes, PPFlow): account.expire_date = MAX_EXPIRE_DATE account.balance = utils.yuan2fen(form.d.balance) account.time_length = 0 account.flow_length = 0 elif product.product_policy == BOTimes: account.expire_date = MAX_EXPIRE_DATE account.balance = 0 account.time_length = utils.hour2sec(form.d.time_length) account.flow_length = 0 elif product.product_policy == BOFlows: account.expire_date = MAX_EXPIRE_DATE account.balance = 0 account.time_length = 0 account.flow_length = utils.mb2kb(form.d.flow_length) order = models.TrCustomerOrder() order.order_id = utils.gen_order_id() order.customer_id = account.customer_id order.product_id = account.product_id order.account_number = account.account_number order.order_fee = 0 order.actual_fee = utils.yuan2fen(form.d.add_value) - utils.yuan2fen( form.d.back_value) order.pay_status = 1 order.accept_id = accept_log.id order.order_source = 'console' order.create_time = utils.get_currtime() order.order_desc = u"用户变更资费,变更前到期:%s,变更后到期:%s" % (old_exoire_date, account.expire_date) self.db.add(order) self.add_oplog(accept_log.accept_desc) self.db.commit() dispatch.pub(db_cache.CACHE_DELETE_EVENT, account_cache_key(account.account_number), async=True) self.redirect(self.detail_url_fmt(account_number))
def post(self): nodes = [(n.id, n.node_desc) for n in self.get_opr_nodes()] products = [(n.id, n.product_name) for n in self.get_opr_products()] iform = customer_forms.customer_import_form(nodes, products) node_id = self.get_argument('node_id') product_id = self.get_argument('product_id') f = self.request.files['import_file'][0] impctx = utils.safeunicode(f['body']) lines = impctx.split("\n") _num = 0 impusers = [] for line in lines: _num += 1 line = line.strip() if not line or u"用户姓名" in line: continue attr_array = line.split(",") if len(attr_array) < 11: return self.render("customer_import_form.html", form=iform, msg=u"line %s error: length must 11 " % _num) vform = customer_forms.customer_import_vform() if not vform.validates( dict(realname=attr_array[0], idcard=attr_array[1], mobile=attr_array[2], address=attr_array[3], account_number=attr_array[4], password=attr_array[5], begin_date=attr_array[6], expire_date=attr_array[7], balance=attr_array[8], time_length=utils.hour2sec(attr_array[9]), flow_length=utils.mb2kb(attr_array[10]))): return self.render("customer_import_form.html", form=iform, msg=u"line %s error: %s" % (_num, vform.errors)) impusers.append(vform) _unums = 0 for form in impusers: try: customer = models.TrCustomer() customer.node_id = node_id customer.realname = form.d.realname customer.idcard = form.d.idcard customer.customer_name = form.d.account_number customer.password = md5(form.d.password.encode()).hexdigest() customer.sex = '1' customer.age = '0' customer.email = '' customer.mobile = form.d.mobile customer.address = form.d.address customer.create_time = form.d.begin_date + ' 00:00:00' customer.update_time = utils.get_currtime() customer.email_active = 0 customer.mobile_active = 0 customer.active_code = utils.get_uuid() self.db.add(customer) self.db.flush() self.db.refresh(customer) accept_log = models.TrAcceptLog() accept_log.accept_type = 'open' accept_log.accept_source = 'console' _desc = u"用户导入账号:%s" % form.d.account_number accept_log.accept_desc = _desc accept_log.account_number = form.d.account_number accept_log.accept_time = customer.update_time accept_log.operator_name = self.current_user.username self.db.add(accept_log) self.db.flush() self.db.refresh(accept_log) order_fee = 0 actual_fee = 0 balance = 0 time_length = 0 flow_length = 0 expire_date = form.d.expire_date product = self.db.query(models.TrProduct).get(product_id) # 买断时长 if product.product_policy == BOTimes: time_length = int(form.d.time_length) # 买断流量 elif product.product_policy == BOFlows: flow_length = int(form.d.flow_length) # 预付费时长,预付费流量 elif product.product_policy in (PPTimes, PPFlow): balance = utils.yuan2fen(form.d.balance) expire_date = MAX_EXPIRE_DATE order = models.TrCustomerOrder() order.order_id = utils.gen_order_id() order.customer_id = customer.customer_id order.product_id = product.id order.account_number = form.d.account_number order.order_fee = order_fee order.actual_fee = actual_fee order.pay_status = 1 order.accept_id = accept_log.id order.order_source = 'console' order.create_time = customer.update_time order.order_desc = u"用户导入开户" self.db.add(order) account = models.TrAccount() account.account_number = form.d.account_number account.customer_id = customer.customer_id account.product_id = order.product_id account.install_address = customer.address account.ip_address = '' account.mac_addr = '' account.password = self.aes.encrypt(form.d.password) account.status = 1 account.balance = balance account.time_length = time_length account.flow_length = flow_length account.expire_date = expire_date account.user_concur_number = product.concur_number account.bind_mac = product.bind_mac account.bind_vlan = product.bind_vlan account.vlan_id1 = 0 account.vlan_id2 = 0 account.create_time = customer.create_time account.update_time = customer.update_time self.db.add(account) _unums += 1 except Exception as e: return self.render("customer_import_form.html", form=iform, msg=u"error : %s" % str(e)) self.add_oplog(u"导入开户,用户数:%s" % _unums) self.db.commit() self.redirect("/admin/customer")