File: views.py

package info (click to toggle)
python-django-structlog 9.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,004 kB
  • sloc: python: 3,509; sh: 206; javascript: 79; makefile: 19
file content (92 lines) | stat: -rw-r--r-- 2,301 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import asyncio
import logging
import time

import structlog
from django.http import HttpResponse, StreamingHttpResponse

from django_structlog_demo_project.taskapp.celery import (
    failing_task,
    nesting_task,
    rejected_task,
    successful_task,
)

logger = structlog.get_logger(__name__)


def enqueue_successful_task(request):
    logger.info("Enqueuing successful task")
    successful_task.apply_async(foo="bar", priority=5)
    return HttpResponse(status=201)


def enqueue_failing_task(request):
    logger.info("Enqueuing failing task")
    failing_task.delay(foo="bar")
    return HttpResponse(status=201)


def enqueue_nesting_task(request):
    logger.info("Enqueuing nesting task")
    nesting_task.delay()
    return HttpResponse(status=201)


def log_with_standard_logger(request):
    logging.getLogger("foreign_logger").info("This is a standard logger")
    return HttpResponse(status=200)


def revoke_task(request):
    async_result = successful_task.apply_async(countdown=1)
    async_result.revoke()
    return HttpResponse(status=201)


def enqueue_unknown_task(request):
    from django_structlog_demo_project.taskapp.celery import unknown_task

    logger.info("Enqueuing unknown task")
    unknown_task.delay()
    return HttpResponse(status=201)


def enqueue_rejected_task(request):
    rejected_task.delay()
    return HttpResponse(status=201)


async def async_view(request):
    for num in range(1, 2):
        await asyncio.sleep(1)
        logger.info(f"This this is an async view {num}")
    return HttpResponse(status=200)


async def async_streaming_response():
    for chunk in range(0, 5):
        await asyncio.sleep(0.5)
        logger.info("streaming_chunk", chunk=chunk)
        yield chunk


def sync_streaming_response():
    for chunk in range(0, 5):
        time.sleep(0.5)
        logger.info("streaming_chunk", chunk=chunk)
        yield chunk


def sync_streaming_view(request):
    logger.info("This this is a sync streaming view")
    return StreamingHttpResponse(sync_streaming_response())


async def async_streaming_view(request):
    logger.info("This this is an async streaming view")
    return StreamingHttpResponse(async_streaming_response())


def raise_exception(request):
    raise Exception("This is a view raising an exception.")