Esempio n. 1
0
def err_report(request):
    if not (not hasattr(settings, 'DJANGO_ONERROR_ACCEPT_REPORT')
            or settings.DJANGO_ONERROR_ACCEPT_REPORT):
        return response()

    errmsg = request.body

    if not errmsg:
        return response()

    try:
        payload = json.loads(errmsg)
    except ValueError:
        return response()

    errormessage = payload.get('errorMessage', '')

    if hasattr(
            settings, 'DJANGO_ONERROR_IGNORE_ERROR_MESSAGES'
    ) and errormessage in settings.DJANGO_ONERROR_IGNORE_ERROR_MESSAGES:
        return response()

    OnerrorReportInfo.objects.create(
        href=payload.get('href', ''),
        ua=payload.get('ua', ''),
        lineNo=payload.get('lineNo', -1) or 0,
        columnNo=payload.get('columnNo', -1) or 0,
        scriptURI=payload.get('scriptURI', ''),
        errorMessage=errormessage,
        stack=payload.get('stack', ''),
        extra=payload.get('extra', ''),
    )

    return response()
Esempio n. 2
0
def file_download(request):
    url = request.POST.get('url', '')

    # URL Not Found
    if not url:
        return response(settings.DJANGO_FILE_DOWNLOAD_URL_NOT_FOUND if hasattr(
            settings, 'DJANGO_FILE_DOWNLOAD_URL_NOT_FOUND') else 999998)

    file_path, file_url = djfile_download(url)

    if hasattr(settings, 'DJANGO_FILE_DOWNLOAD_CALLBACK_FUNC') and hasattr(
            settings.DJANGO_FILE_DOWNLOAD_CALLBACK_FUNC, '__call__'):
        settings.DJANGO_FILE_DOWNLOAD_CALLBACK_FUNC(request, file_path,
                                                    file_url)

    return response(200,
                    'File Download Success',
                    u'文件下载成功',
                    data={
                        'file_path': file_path,
                        'file_url': file_url,
                    })
Esempio n. 3
0
def del_cookie(request):
    cookie_key = request.GET.get('k', '')

    cookie_domain = bool(int(request.GET.get('d', 1)))
    if cookie_domain and hasattr(
            settings, 'DJANGO_UNIAPI_COOKIE_DOMAIN_FUNC') and hasattr(
                settings.DJANGO_UNIAPI_COOKIE_DOMAIN_FUNC, '__call__'):
        cookie_domain = settings.DJANGO_UNIAPI_COOKIE_DOMAIN_FUNC(request)
    else:
        cookie_domain = None

    resp = response()

    resp.delete_cookie(cookie_key, domain=cookie_domain)

    return resp
Esempio n. 4
0
def set_cookie(request):
    cookie_key = request.GET.get('k', '')
    cookie_value = request.GET.get('v', '')

    signed_cookie = bool(int(request.GET.get('s', 1)))

    cookie_domain = bool(int(request.GET.get('d', 1)))
    if cookie_domain and hasattr(
            settings, 'DJANGO_UNIAPI_COOKIE_DOMAIN_FUNC') and hasattr(
                settings.DJANGO_UNIAPI_COOKIE_DOMAIN_FUNC, '__call__'):
        cookie_domain = settings.DJANGO_UNIAPI_COOKIE_DOMAIN_FUNC(request)
    else:
        cookie_domain = None

    max_age = hasattr(settings, 'DJANGO_WE_COOKIE_MAX_AGE') and getattr(
        settings, 'DJANGO_WE_COOKIE_MAX_AGE') or 30 * 24 * 60 * 60  # 30d
    cookie_salt = hasattr(settings, 'DJANGO_WE_COOKIE_SALT') and getattr(
        settings,
        'DJANGO_WE_COOKIE_SALT') or 'djwe'  # Salt for ``set_signed_cookie``

    resp = response()

    if signed_cookie:
        resp.set_signed_cookie(cookie_key,
                               cookie_value,
                               domain=cookie_domain,
                               salt=cookie_salt,
                               **{
                                   'max_age': max_age,
                                   'httponly': True,
                               })
    else:
        resp.set_cookie(cookie_key,
                        cookie_value,
                        domain=cookie_domain,
                        max_age=max_age)

    return resp
Esempio n. 5
0
def server_status(request):
    return response()
Esempio n. 6
0
def raise_error(request):
    # Raise ZeroDivisionError
    zero_division_error = 1 / 0
    return response()
Esempio n. 7
0
def server_time(request):
    ms = ('ms' in request.GET) or ('ms' in request.POST)
    return response(data={'time': tc.utc_datetime(ms=ms)})
Esempio n. 8
0
def send_tplmsg(request):
    """ push_id + title/ip/type/descr/detail/time/remark/color/openids + sign """
    text = request.POST.get('text', '') or request.GET.get('text', '')

    if not text:
        return response()

    data = json.loads(text)

    # Key for back compatibility
    wepush_id = data.get('push_id', '') or data.get('key', '')

    reqlog = WeChatTemplateMessageRequestLogInfo.objects.create(
        wepush_id=wepush_id,
        request_ip=client_ip(request),
        request_text=text,
    )

    try:
        tpl = WeChatTemplateInfo.objects.get(wepush_id=wepush_id, status=True)
    except WeChatTemplateInfo.DoesNotExist:
        return response(WeChatTemplateStatusCode.TEMPLATE_NOT_FOUND)

    if not check_signature(data, api_key=tpl.wepush_secret):
        reqlog.request_sign_status = False
        reqlog.save()
        return response(SignatureStatusCode.SIGNATURE_ERROR)

    # 调用方指定接收入
    openids = json.loads(data.get('openids', '[]'))
    if not openids:
        # Pushman 配置接收入
        receivers = WeChatTemplateReceiverInfo.objects.filter(
            wepush_id=tpl.wepush_id)
        openids = [receiver.openid for receiver in receivers]
        if not openids:
            return response(WeChatTemplateStatusCode.RECEIVER_NOT_FOUND)

    color = data.get('color', u'#173177')
    tpl_data = {
        'first': {
            'value': data.get('first') or data.get('title', u'服务器故障'),
            'color': color,
        },
        'keyword1': {
            'value': data.get('keyword1') or data.get('ip', '127.0.0.1'),
            'color': color,
        },
        'keyword2': {
            'value': data.get('keyword2') or data.get('type', u'服务器故障'),
            'color': color,
        },
        'keyword3': {
            'value': data.get('keyword3') or data.get('descr', u'服务器故障'),
            'color': color,
        },
        'keyword4': {
            'value': data.get('keyword4') or data.get('detail', u'服务器故障'),
            'color': color,
        },
        'keyword5': {
            'value':
            data.get('keyword5') or data.get('time', tc.local_string())
            or tc.local_string(),
            'color':
            color,
        },
        'remark': {
            'value': data.get('remark', u'请尽快处理!'),
            'color': color,
        },
    }

    tplmsg = TemplateMessage(appid=tpl.app_id,
                             secret=tpl.app_secret,
                             token=fetch_access_token(tpl.token_url,
                                                      tpl.token_key),
                             storage=RedisStorage(r))

    success = failure = 0
    for openid in openids:
        msgres = tplmsg.send_template_message(user_id=openid,
                                              template_id=tpl.template_id,
                                              data=tpl_data,
                                              url=data.get('url', None))
        # Success Or Not
        send_status = msgres['errcode'] == 0
        # TPL Send Log
        WeChatTemplateMessageSendLogInfo.objects.create(
            wepush_id=wepush_id,
            request_id=reqlog.request_id,
            openid=openid,
            send_msgres=msgres,
            send_status=send_status,
        )
        # Success/Failure Num Incr
        if send_status:
            success += 1
        else:
            failure += 1

    return response(200, data={
        'success': success,
        'failure': failure,
    })