def do_revoke(self):
     res_id = self.params.get("res_id", 0)
     # user_id = self.params.get("user_id", 0)
     user_id = self.handler.current_user.id
     resource = self.db.query(Pro_Resource_Apply).filter(
         Pro_Resource_Apply.id == res_id).first()
     if not resource:
         return self.failure(ERROR.not_found_err)
     # 状态不为0(即:不是提交状态),不允许撤销
     if resource.status != 0:
         return self.failure(ERROR.res_revoke_err)
     resource.status = STATUS_RESOURCE.REVOKED
     self.db.add(resource)
     self.db.flush()
     mail_html = self.render_to_string(
         "admin/mail/pro_resource_apply_to_admin.html",
         resource_apply=resource,
         STATUS_RESOURCE=STATUS_RESOURCE)
     mail_title = mail_title_format % {
         "user_name": resource.user.email or resource.user.mobile,
         "pro_name": resource.project.name,
         "res_desc": resource.desc,
         "action": u"申请的",
         "todo_action": u"资源,已撤销",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title,
                    mail_html)
     task_post_pro_res_apply_history.delay(status=resource.status,
                                           content=mail_title,
                                           pro_id=resource.project.id,
                                           res_apply_id=resource.id,
                                           user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=resource)
 def do_pay(self):
     logger.info("\t [ DO PAY ]")
     res_id = self.params.get("res_id", 0)
     user_id = self.params.get("user_id", 0)
     user_id = self.handler.current_user.id
     resource_query = self.db.query(Pro_Resource_Apply).filter(Pro_Resource_Apply.id == res_id)
     logger.info("\t [QUERY] : %s" % resource_query)
     resource = resource_query.first()
     if not resource:
         # return self.failure(ERROR.not_found_err)
         return NotFoundError()
     # 状态不为0(即:不是提交状态),不允许撤销
     logger.info("\t [resource.status]: %s" % resource.status)
     logger.info("\t [STATUS_RESOURCE.CHECKED]: %s" % STATUS_RESOURCE.CHECKED)
     logger.info("\t change to [STATUS_RESOURCE.PAYED]: %s" % STATUS_RESOURCE.PAYED)
     if resource.status != STATUS_RESOURCE.CHECKED:
         return self.failure(ERROR.res_pay_err)
     resource.status = STATUS_RESOURCE.PAYED
     self.db.add(resource)
     self.db.flush()
     mail_html = self.render_to_string("admin/mail/pro_resource_apply_to_admin.html", resource_apply=resource, STATUS_RESOURCE=STATUS_RESOURCE)
     mail_title = mail_title_format % {
         "user_name": resource.user.email or resource.user.mobile,
         "pro_name": resource.project.name,
         "res_desc": resource.desc,
         "action": u"申请的",
         "todo_action": u"资源,已完成支付,请确认收款",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title, mail_html)
     task_post_pro_res_apply_history.delay(status=resource.status, content=mail_title, pro_id=resource.project.id, res_apply_id=resource.id, user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=resource)
 def do_revoke(self):
     res_id = self.params.get("res_id", 0)
     # user_id = self.params.get("user_id", 0)
     user_id = self.handler.current_user.id
     resource = self.db.query(Pro_Resource_Apply).filter(Pro_Resource_Apply.id == res_id).first()
     if not resource:
         return self.failure(ERROR.not_found_err)
     # 状态不为0(即:不是提交状态),不允许撤销
     if resource.status != 0:
         return self.failure(ERROR.res_revoke_err)
     resource.status = STATUS_RESOURCE.REVOKED
     self.db.add(resource)
     self.db.flush()
     mail_html = self.render_to_string("admin/mail/pro_resource_apply_to_admin.html", resource_apply=resource, STATUS_RESOURCE=STATUS_RESOURCE)
     mail_title = mail_title_format % {
         "user_name": resource.user.email or resource.user.mobile,
         "pro_name": resource.project.name,
         "res_desc": resource.desc,
         "action": u"申请的",
         "todo_action": u"资源,已撤销",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title, mail_html)
     task_post_pro_res_apply_history.delay(status=resource.status, content=mail_title, pro_id=resource.project.id, res_apply_id=resource.id, user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=resource)
 def do_pay(self):
     logger.info("\t [ DO PAY ]")
     res_id = self.params.get("res_id", 0)
     user_id = self.params.get("user_id", 0)
     user_id = self.handler.current_user.id
     resource_query = self.db.query(Pro_Resource_Apply).filter(
         Pro_Resource_Apply.id == res_id)
     logger.info("\t [QUERY] : %s" % resource_query)
     resource = resource_query.first()
     if not resource:
         # return self.failure(ERROR.not_found_err)
         return NotFoundError()
     # 状态不为0(即:不是提交状态),不允许撤销
     logger.info("\t [resource.status]: %s" % resource.status)
     logger.info("\t [STATUS_RESOURCE.CHECKED]: %s" %
                 STATUS_RESOURCE.CHECKED)
     logger.info("\t change to [STATUS_RESOURCE.PAYED]: %s" %
                 STATUS_RESOURCE.PAYED)
     if resource.status != STATUS_RESOURCE.CHECKED:
         return self.failure(ERROR.res_pay_err)
     resource.status = STATUS_RESOURCE.PAYED
     self.db.add(resource)
     self.db.flush()
     mail_html = self.render_to_string(
         "admin/mail/pro_resource_apply_to_admin.html",
         resource_apply=resource,
         STATUS_RESOURCE=STATUS_RESOURCE)
     mail_title = mail_title_format % {
         "user_name": resource.user.email or resource.user.mobile,
         "pro_name": resource.project.name,
         "res_desc": resource.desc,
         "action": u"申请的",
         "todo_action": u"资源,已完成支付,请确认收款",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title,
                    mail_html)
     task_post_pro_res_apply_history.delay(status=resource.status,
                                           content=mail_title,
                                           pro_id=resource.project.id,
                                           res_apply_id=resource.id,
                                           user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=resource)
    def do_resource_action(self):
        """
            针对管理员修改资源申请状态
        """
        res_ids = self.params.get("res_ids", "")
        checker_id = self.params.get("checker_id", 0)
        action = self.params.get("action", "")
        reason = self.params.get("reason", "")
        logger.info("\t action: %s, STATUS_RESOURCE.refused.value_en: %s" %
                    (action, STATUS_RESOURCE.refused.value_en))
        logger.info("\t reason: %s" % reason)
        if action == STATUS_RESOURCE.refused.value_en:
            if not reason:
                return self.failure(ERROR.res_reason_empty_err)
        actions = [
            STATUS_RESOURCE.get(i).value_en for i in STATUS_RESOURCE.keys()
            if isinstance(i, int)
        ]
        logger.info("\t [actions] : %s" % actions)
        if action not in actions:
            return self.failure(ERROR.res_do_resource_action_err)
        res_id_list = [int(i) for i in res_ids.split(",") if i.isdigit()]
        email_list = []
        tip_messages = []
        logger.info("<" + "start for" + ">")
        logger.info("< %s >" % res_id_list)

        def _get_message(msg=u"", err=None, level="success"):
            if msg:
                return u"%s" % msg, "%s" % level
            elif err:
                return u"(%s)%s" % (err.errcode, err.errvalue), "%s" % level
            return u""

        for res_id in res_id_list:
            resource = self.db.query(Pro_Resource_Apply).filter(
                Pro_Resource_Apply.id == res_id).first()
            if not resource:
                tip_messages.append(
                    _get_message(err=ERROR.not_found_err, level="warning"))
                continue
            email = resource.user.email

            previous_status = STATUS_RESOURCE.get(action.upper()) - 1
            if action == STATUS_RESOURCE.refused.value_en:
                previous_status = STATUS_RESOURCE.APPLIED
            if action == STATUS_RESOURCE.applied.value_en:
                previous_status = STATUS_RESOURCE.CHECKED
            logger.info("<" + "#" * 60 + ">")
            logger.info(resource.status)
            if resource.status != previous_status:
                if action == STATUS_RESOURCE.checked.value_en:
                    tip_messages.append(
                        _get_message(err=ERROR.res_check_err, level="warning"))
                elif action == STATUS_RESOURCE.refused.value_en:
                    tip_messages.append(
                        _get_message(err=ERROR.res_refuse_err,
                                     level="warning"))
                elif action == STATUS_RESOURCE.confirmpayed.value_en:
                    tip_messages.append(
                        _get_message(err=ERROR.res_confirmpay_err,
                                     level="warning"))
                elif action == STATUS_RESOURCE.started.value_en:
                    tip_messages.append(
                        _get_message(err=ERROR.res_start_err, level="warning"))
                elif action == STATUS_RESOURCE.closed.value_en:
                    tip_messages.append(
                        _get_message(err=ERROR.res_close_err, level="warning"))
                else:
                    tip_messages.append(
                        _get_message(err=ERROR.res_do_resource_action_err,
                                     level="warning"))
                continue
            resource.status = STATUS_RESOURCE.get(action.upper())
            # if action == STATUS_RESOURCE.refused.value_en:
            #     reason = self.params.get("reason", "")
            resource.reason = reason
            resource.checker_id = checker_id
            # logger.info(resource.status)
            # logger.info("<"+"#"*60+">")
            self.db.add(resource)
            self.db.flush()
            mail_html = self.render_to_string(
                "admin/mail/pro_resource_apply.html",
                resource_apply=resource,
                STATUS_RESOURCE=STATUS_RESOURCE)
            mail_title = mail_title_format % {
                "user_name":
                resource.checker.email or resource.checker.mobile,
                "pro_name":
                resource.project.name,
                "res_desc":
                resource.desc,
                "action":
                u"审核的",
                "todo_action":
                u"资源,%s,%s" %
                (STATUS_RESOURCE.get(resource.status).value,
                 STATUS_RESOURCE.get(resource.status).todo_value),
            }
            email_list.append((email, mail_html))
            tip_messages.append(_get_message(msg=mail_title))
            task_post_pro_res_apply_history.delay(status=resource.status,
                                                  content=mail_title,
                                                  pro_id=resource.project.id,
                                                  res_apply_id=resource.id,
                                                  user_id=resource.user_id,
                                                  checker_id=checker_id)
            publish_notice_user.delay(resource.user_id)

        email_dict = {}
        for email, mail_content in email_list:
            if email_dict.get(email):
                email_dict[email].append(mail_content)
            else:
                email_dict[email] = [mail_content]
        logger.info("\t [email_dict]%s" % email_dict)

        for email, mail_content_list in email_dict.items():
            sendMail.delay("*****@*****.**", [email],
                           u"资源%s通知" % STATUS_RESOURCE.get(action).act_value,
                           "\n\n".join(mail_content_list))
        # from scloud.views.admin.ws.pubsub import r
        # r.publish("test_realtime", "notice_user")
        return self.success(data=tip_messages)
 def do_re_apply(self):
     logger.info("---------[do_re_apply]----------")
     param_schema = self.get_param_schema()
     form_valid_res = self.check_form_valid(param_schema)
     if form_valid_res.return_code < 0:
         return form_valid_res
     # form_empty_res = self.check_form_empty()
     # if form_empty_res.return_code < 0:
     #     return form_empty_res
     pro_id = self.params.get("pro_id")
     res_id = self.params.get("res_id")
     # user_id = self.params.get("user_id")
     user_id = self.handler.current_user.id
     if not pro_id:
         return self.failure(ERROR.not_found_err)
     if not res_id:
         return self.failure(ERROR.not_found_err)
     if not user_id:
         return self.failure(ERROR.user_empty_err)
     pro_info = self.db.query(Pro_Info).filter(
         Pro_Info.id == pro_id).first()
     if not pro_info:
         return self.failure(ERROR.not_found_err)
     resource = self.db.query(Pro_Resource_Apply).filter(
         Pro_Resource_Apply.id == res_id).first()
     if not resource:
         return self.failure(ERROR.not_found_err)
     generate_fee_res = self.generate_fee()
     if generate_fee_res.return_code == 0:
         fee_data = generate_fee_res.data
     else:
         fee_data = dict(unit_fee=0, total_fee=0)
         return generate_fee_res
     params = form_valid_res.data
     logger.info("\t form_valid_res data: %s" % params)
     resource.computer = params.get('computer')
     resource.cpu = params.get('cpu')
     resource.memory = params.get('memory')
     resource.disk = params.get('disk')
     resource.disk_amount = params.get('disk_amount')
     # resource.disk_backup = params.get('disk_backup')
     resource.disk_backup_amount = params.get('disk_backup_amount')
     resource.out_ip = params.get('out_ip')
     resource.snapshot = params.get('snapshot')
     resource.loadbalance = params.get('loadbalance')
     resource.internet_ip = params.get('internet_ip')
     resource.bandwidth = params.get('bandwidth')
     # resource.internet_ip_ssl = params.get('internet_ip_ssl')
     resource.start_date = params.get('start_date')
     resource.period = params.get('period')
     resource.unit_fee = fee_data['unit_fee']
     resource.total_fee = fee_data['total_fee']
     resource.fee_desc = u"单月费用 %s(元/月)×有效期 %s(月)=总费用 %s(元)" % (
         fee_data['unit_fee'], params.get('period'), fee_data['total_fee'])
     resource.status = STATUS_RESOURCE.APPLIED
     resource.reason = u''
     self.db.add(resource)
     self.db.flush()
     mail_html = self.render_to_string(
         "admin/mail/pro_resource_apply_to_admin.html",
         resource_apply=resource,
         STATUS_RESOURCE=STATUS_RESOURCE)
     mail_title = mail_title_format % {
         "user_name": resource.user.email or resource.user.mobile,
         "pro_name": resource.project.name,
         "res_desc": resource.desc,
         "action": u"申请的",
         "todo_action": u"资源,已重新提交,请审核",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title,
                    mail_html)
     task_post_pro_res_apply_history.delay(status=resource.status,
                                           content=mail_title,
                                           pro_id=resource.project.id,
                                           res_apply_id=resource.id,
                                           user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=resource)
 def do_apply(self):
     logger.info("---------[do_apply]---------")
     param_schema = self.get_param_schema()
     form_valid_res = self.check_form_valid(param_schema)
     if form_valid_res.return_code < 0:
         return form_valid_res
     # form_empty_res = self.check_form_empty()
     # if form_empty_res.return_code < 0:
     #     return form_empty_res
     pro_id = self.params.get("pro_id")
     # user_id = self.params.get("user_id")
     user_id = self.handler.current_user.id
     logger.info(self.params)
     if not pro_id:
         return self.failure(ERROR.not_found_err)
     if not user_id:
         return self.failure(ERROR.user_empty_err)
     pro_info = self.db.query(Pro_Info).filter(
         Pro_Info.id == pro_id).first()
     generate_fee_res = self.generate_fee()
     if generate_fee_res.return_code == 0:
         fee_data = generate_fee_res.data
     else:
         fee_data = dict(unit_fee=0, total_fee=0)
         return generate_fee_res
     applies = pro_info.pro_resource_applies
     if len(applies) > 0:
         first_apply = applies[0]
         last_apply = applies[-1]
         # 状态为0、1时不能申请新配额
         logger.info("\t [last_apply status] %s" % last_apply.status)
         if last_apply.status >= 0 and last_apply.status < 2:
             return self.failure(ERROR.res_new_apply_err)
     else:
         first_apply = None
         last_apply = None
     params = form_valid_res.data
     apply = Pro_Resource_Apply()
     apply.pro_id = pro_id
     apply.computer = params.get('computer')
     apply.cpu = params.get('cpu')
     apply.memory = params.get('memory')
     apply.disk = params.get('disk')
     apply.disk_amount = params.get('disk_amount')
     # apply.disk_backup = params.get('disk_backup')
     apply.disk_backup_amount = params.get('disk_backup_amount')
     apply.out_ip = params.get('out_ip')
     apply.snapshot = params.get('snapshot')
     apply.loadbalance = params.get('loadbalance')
     apply.internet_ip = params.get('internet_ip')
     apply.bandwidth = params.get('bandwidth')
     # apply.internet_ip_ssl = params.get('internet_ip_ssl')
     apply.start_date = params.get('start_date')
     apply.period = params.get('period')
     apply.unit_fee = fee_data["unit_fee"]
     apply.total_fee = fee_data["total_fee"]
     apply.fee_desc = u"单月费用 %s(元/月)×有效期 %s(月)=总费用 %s(元)" % (
         fee_data['unit_fee'], params.get('period'), fee_data['total_fee'])
     apply.user_id = user_id
     apply.status = STATUS_RESOURCE.APPLIED
     apply.reason = u''
     if first_apply:
         apply.desc = u'资源调整'
     self.db.add(apply)
     self.db.flush()
     mail_html = self.render_to_string(
         "admin/mail/pro_resource_apply_to_admin.html",
         resource_apply=apply,
         STATUS_RESOURCE=STATUS_RESOURCE)
     logger.info("<" + "=" * 60 + ">")
     logger.info(mail_html)
     # user_name = apply.user.email or apply.user.mobile
     mail_title = mail_title_format % {
         "user_name": apply.user.email or apply.user.mobile,
         "pro_name": apply.project.name,
         "res_desc": apply.desc,
         "action": u"申请的",
         "todo_action": u"资源,已提交,请审核",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title,
                    mail_html)
     task_post_pro_res_apply_history.delay(status=apply.status,
                                           content=mail_title,
                                           pro_id=pro_id,
                                           res_apply_id=apply.id,
                                           user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=apply)
    def do_resource_action(self):
        """
            针对管理员修改资源申请状态
        """
        res_ids = self.params.get("res_ids", "")
        checker_id = self.params.get("checker_id", 0)
        action = self.params.get("action", "")
        reason = self.params.get("reason", "")
        logger.info("\t action: %s, STATUS_RESOURCE.refused.value_en: %s" % (action, STATUS_RESOURCE.refused.value_en))
        logger.info("\t reason: %s" % reason)
        if action == STATUS_RESOURCE.refused.value_en:
            if not reason:
                return self.failure(ERROR.res_reason_empty_err)
        actions = [STATUS_RESOURCE.get(i).value_en for i in STATUS_RESOURCE.keys() if isinstance(i, int)]
        logger.info("\t [actions] : %s" % actions)
        if action not in actions:
            return self.failure(ERROR.res_do_resource_action_err)
        res_id_list = [int(i) for i in res_ids.split(",") if i.isdigit()]
        email_list = []
        tip_messages = []
        logger.info("<"+"start for"+">")
        logger.info("< %s >" % res_id_list)
        def _get_message(msg=u"", err=None, level="success"):
            if msg:
                return u"%s" % msg, "%s" % level
            elif err:
                return u"(%s)%s" % (err.errcode, err.errvalue), "%s" % level
            return u""
        for res_id in res_id_list:
            resource = self.db.query(Pro_Resource_Apply).filter(Pro_Resource_Apply.id == res_id).first()
            if not resource:
                tip_messages.append(_get_message(err=ERROR.not_found_err, level="warning"))
                continue
            email = resource.user.email

            previous_status = STATUS_RESOURCE.get(action.upper()) - 1
            if action == STATUS_RESOURCE.refused.value_en:
                previous_status = STATUS_RESOURCE.APPLIED
            if action == STATUS_RESOURCE.applied.value_en:
                previous_status = STATUS_RESOURCE.CHECKED
            logger.info("<" + "#" * 60 + ">")
            logger.info(resource.status)
            if resource.status != previous_status:
                if action == STATUS_RESOURCE.checked.value_en:
                    tip_messages.append(_get_message(err=ERROR.res_check_err, level="warning"))
                elif action == STATUS_RESOURCE.refused.value_en:
                    tip_messages.append(_get_message(err=ERROR.res_refuse_err, level="warning"))
                elif action == STATUS_RESOURCE.confirmpayed.value_en:
                    tip_messages.append(_get_message(err=ERROR.res_confirmpay_err, level="warning"))
                elif action == STATUS_RESOURCE.started.value_en:
                    tip_messages.append(_get_message(err=ERROR.res_start_err, level="warning"))
                elif action == STATUS_RESOURCE.closed.value_en:
                    tip_messages.append(_get_message(err=ERROR.res_close_err, level="warning"))
                else:
                    tip_messages.append(_get_message(err=ERROR.res_do_resource_action_err, level="warning"))
                continue
            resource.status = STATUS_RESOURCE.get(action.upper())
            # if action == STATUS_RESOURCE.refused.value_en:
            #     reason = self.params.get("reason", "")
            resource.reason = reason
            resource.checker_id = checker_id
            # logger.info(resource.status)
            # logger.info("<"+"#"*60+">")
            self.db.add(resource)
            self.db.flush()
            mail_html = self.render_to_string("admin/mail/pro_resource_apply.html", resource_apply=resource, STATUS_RESOURCE=STATUS_RESOURCE)
            mail_title = mail_title_format % {
                "user_name": resource.checker.email or resource.checker.mobile,
                "pro_name": resource.project.name,
                "res_desc": resource.desc,
                "action": u"审核的",
                "todo_action": u"资源,%s,%s" % (STATUS_RESOURCE.get(resource.status).value, STATUS_RESOURCE.get(resource.status).todo_value),
            }
            email_list.append((email, mail_html))
            tip_messages.append(_get_message(msg=mail_title))
            task_post_pro_res_apply_history.delay(status=resource.status, content=mail_title, pro_id=resource.project.id, res_apply_id=resource.id, user_id=resource.user_id, checker_id=checker_id)
            publish_notice_user.delay(resource.user_id)

        email_dict = {}
        for email, mail_content in email_list:
            if email_dict.get(email):
                email_dict[email].append(mail_content)
            else:
                email_dict[email] = [mail_content]
        logger.info("\t [email_dict]%s" % email_dict)

        for email, mail_content_list in email_dict.items():
            sendMail.delay("*****@*****.**", [email], u"资源%s通知" % STATUS_RESOURCE.get(action).act_value, "\n\n".join(mail_content_list))
        # from scloud.views.admin.ws.pubsub import r
        # r.publish("test_realtime", "notice_user")
        return self.success(data=tip_messages)
 def do_re_apply(self):
     logger.info("---------[do_re_apply]----------")
     param_schema = self.get_param_schema()
     form_valid_res = self.check_form_valid(param_schema)
     if form_valid_res.return_code < 0:
         return form_valid_res
     # form_empty_res = self.check_form_empty()
     # if form_empty_res.return_code < 0:
     #     return form_empty_res
     pro_id = self.params.get("pro_id")
     res_id = self.params.get("res_id")
     # user_id = self.params.get("user_id")
     user_id = self.handler.current_user.id
     if not pro_id:
         return self.failure(ERROR.not_found_err)
     if not res_id:
         return self.failure(ERROR.not_found_err)
     if not user_id:
         return self.failure(ERROR.user_empty_err)
     pro_info = self.db.query(
         Pro_Info
     ).filter(
         Pro_Info.id == pro_id
     ).first()
     if not pro_info:
         return self.failure(ERROR.not_found_err)
     resource = self.db.query(
         Pro_Resource_Apply
     ).filter(
         Pro_Resource_Apply.id == res_id
     ).first()
     if not resource:
         return self.failure(ERROR.not_found_err)
     generate_fee_res = self.generate_fee()
     if generate_fee_res.return_code == 0:
         fee_data = generate_fee_res.data
     else:
         fee_data = dict(unit_fee=0, total_fee=0)
         return generate_fee_res
     params = form_valid_res.data
     logger.info("\t form_valid_res data: %s" % params)
     resource.computer = params.get('computer')
     resource.cpu = params.get('cpu')
     resource.memory = params.get('memory')
     resource.disk = params.get('disk')
     resource.disk_amount = params.get('disk_amount')
     # resource.disk_backup = params.get('disk_backup')
     resource.disk_backup_amount = params.get('disk_backup_amount')
     resource.out_ip = params.get('out_ip')
     resource.snapshot = params.get('snapshot')
     resource.loadbalance = params.get('loadbalance')
     resource.internet_ip = params.get('internet_ip')
     resource.bandwidth = params.get('bandwidth')
     # resource.internet_ip_ssl = params.get('internet_ip_ssl')
     resource.start_date = params.get('start_date')
     resource.period = params.get('period')
     resource.unit_fee = fee_data['unit_fee']
     resource.total_fee = fee_data['total_fee']
     resource.fee_desc = u"单月费用 %s(元/月)×有效期 %s(月)=总费用 %s(元)" % (fee_data['unit_fee'], params.get('period'), fee_data['total_fee'])
     resource.status = STATUS_RESOURCE.APPLIED
     resource.reason = u''
     self.db.add(resource)
     self.db.flush()
     mail_html = self.render_to_string("admin/mail/pro_resource_apply_to_admin.html", resource_apply=resource, STATUS_RESOURCE=STATUS_RESOURCE)
     mail_title = mail_title_format % {
         "user_name": resource.user.email or resource.user.mobile,
         "pro_name": resource.project.name,
         "res_desc": resource.desc,
         "action": u"申请的",
         "todo_action": u"资源,已重新提交,请审核",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title, mail_html)
     task_post_pro_res_apply_history.delay(status=resource.status, content=mail_title, pro_id=resource.project.id, res_apply_id=resource.id, user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=resource)
 def do_apply(self):
     logger.info("---------[do_apply]---------")
     param_schema = self.get_param_schema()
     form_valid_res = self.check_form_valid(param_schema)
     if form_valid_res.return_code < 0:
         return form_valid_res
     # form_empty_res = self.check_form_empty()
     # if form_empty_res.return_code < 0:
     #     return form_empty_res
     pro_id = self.params.get("pro_id")
     # user_id = self.params.get("user_id")
     user_id = self.handler.current_user.id
     logger.info(self.params)
     if not pro_id:
         return self.failure(ERROR.not_found_err)
     if not user_id:
         return self.failure(ERROR.user_empty_err)
     pro_info = self.db.query(
         Pro_Info
     ).filter(
         Pro_Info.id == pro_id
     ).first()
     generate_fee_res = self.generate_fee()
     if generate_fee_res.return_code == 0:
         fee_data = generate_fee_res.data
     else:
         fee_data = dict(unit_fee=0, total_fee=0)
         return generate_fee_res
     applies = pro_info.pro_resource_applies
     if len(applies) > 0:
         first_apply = applies[0]
         last_apply = applies[-1]
         # 状态为0、1时不能申请新配额
         logger.info("\t [last_apply status] %s" % last_apply.status)
         if last_apply.status >= 0 and last_apply.status < 2:
             return self.failure(ERROR.res_new_apply_err)
     else:
         first_apply = None
         last_apply = None
     params = form_valid_res.data
     apply = Pro_Resource_Apply()
     apply.pro_id = pro_id
     apply.computer = params.get('computer')
     apply.cpu = params.get('cpu')
     apply.memory = params.get('memory')
     apply.disk = params.get('disk')
     apply.disk_amount = params.get('disk_amount')
     # apply.disk_backup = params.get('disk_backup')
     apply.disk_backup_amount = params.get('disk_backup_amount')
     apply.out_ip = params.get('out_ip')
     apply.snapshot = params.get('snapshot')
     apply.loadbalance = params.get('loadbalance')
     apply.internet_ip = params.get('internet_ip')
     apply.bandwidth = params.get('bandwidth')
     # apply.internet_ip_ssl = params.get('internet_ip_ssl')
     apply.start_date = params.get('start_date')
     apply.period = params.get('period')
     apply.unit_fee = fee_data["unit_fee"]
     apply.total_fee = fee_data["total_fee"]
     apply.fee_desc = u"单月费用 %s(元/月)×有效期 %s(月)=总费用 %s(元)" % (fee_data['unit_fee'], params.get('period'), fee_data['total_fee'])
     apply.user_id = user_id
     apply.status = STATUS_RESOURCE.APPLIED
     apply.reason = u''
     if first_apply:
         apply.desc = u'资源调整'
     self.db.add(apply)
     self.db.flush()
     mail_html = self.render_to_string("admin/mail/pro_resource_apply_to_admin.html", resource_apply=apply, STATUS_RESOURCE=STATUS_RESOURCE)
     logger.info("<" + "=" * 60 + ">")
     logger.info(mail_html)
     # user_name = apply.user.email or apply.user.mobile
     mail_title = mail_title_format % {
         "user_name": apply.user.email or apply.user.mobile,
         "pro_name": apply.project.name,
         "res_desc": apply.desc,
         "action": u"申请的",
         "todo_action": u"资源,已提交,请审核",
     }
     sendMail.delay("*****@*****.**", admin_emails, mail_title, mail_html)
     task_post_pro_res_apply_history.delay(status=apply.status, content=mail_title, pro_id=pro_id, res_apply_id=apply.id, user_id=user_id)
     # from scloud.views.admin.ws.pubsub import r
     # r.publish("test_realtime", "notice_checker")
     return self.success(data=apply)