Beispiel #1
0
class AliSmsView(AdminView):
    """
    API Endpoints for managing ali sms.

    GET /api/admin/notification/alisms
        description: list (search) sms
        return: array of sms objects, with pagination
        status: 200, 400
        query string:
            * phone: filter phone_numbers with 'icontains'
            * content: filter content with 'icontains'
            * status: filter status with '='
            * sent_before: filter sms sent before the date, date format: '2019-03-21'
            * sent_after: filter sms sent after the date, date format: '2019-03-21'
            * pagination params (page=1, page_size=10)

    GET /api/admin/notification/alisms/{id}
        description: get sms model
        return: sms object
        status: 200, 404

    POST /api/admin/notification/alisms
        description: send sms, supposed to be used as testing purpose only.
        return: email object on success
        status: 200, 400
        body:
            {
                phone_numbers: '13912345678, 13812345678',
                signature_name: '大鱼测试',
                template_code: 'SMS_134310520',
                template_param: { code: '123456' },
            }
    """
    MODEL = AliSms
    ORDER_BY = ['-sent_at']
    USE_PAGINATION = True

    CREATE_METHOD = 'create_sms'
    UPDATE_METHOD = None
    PATCH_METHOD = None
    DELETE_METHOD = None

    SEARCH_FORM = Form([
        panels.TextPanel('phone_numbers',
                         form_field_name='phone',
                         search_op='icontains',
                         labels='手机号'),
        panels.TextPanel('content', search_op='icontains', labels='内容'),
        panels.SelectPanel('status', labels='状态'),
        panels.DateRangePanel(
            'sent_at', form_field_name='sent_range', label='发送日期'),
    ],
                       model=MODEL,
                       form_mode='search')

    def create_sms(self, request):
        phone_numbers = request.json.get('phone_numbers')
        if not phone_numbers:
            return api.bad_request(form_errors=dict(
                phone_numbers=['接收方手机号不能为空']))

        signature_name = request.json.get('signature_name')
        if not signature_name:
            return api.bad_request(form_errors=dict(signature_name=['签名不能为空']))
        if signature_name not in AliSms.SIGNATURES:
            return api.bad_request(form_errors=dict(
                signature_name=['不允许使用的签名']))

        template_code = request.json.get('template_code')
        if not template_code:
            return api.bad_request(form_errors=dict(
                template_code=['模板编号不能为空']))
        if template_code not in AliSms.TEMPLATES:
            return api.bad_request(form_errors=dict(template_code=['模板编号不存在']))

        template_param = reuqest.json.get('template_param', {})

        sms = send_sms(phone_numbers, signature_name, template_code,
                       template_param)

        return api.ok(data=sms.serialize())