-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathviews.py
More file actions
133 lines (88 loc) · 3.73 KB
/
views.py
File metadata and controls
133 lines (88 loc) · 3.73 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import asyncio
from asgiref.sync import sync_to_async
from django.contrib.auth.models import User
from django.core.cache import cache
from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import render
from django.template.response import TemplateResponse
from django.views.decorators.cache import cache_page
from debug_toolbar.utils import get_csp_nonce
from tests.models import PostgresJSON
def execute_sql(request):
list(User.objects.all())
return render(request, "base.html")
def execute_json_sql(request):
list(PostgresJSON.objects.filter(field__contains={"foo": "bar"}))
return render(request, "base.html")
async def async_execute_json_sql(request):
list_store = []
# make async query with filter, which is compatible with async for.
async for obj in PostgresJSON.objects.filter(field__contains={"foo": "bar"}):
list_store.append(obj)
return render(request, "base.html")
def execute_union_sql(request):
list(User.objects.all().union(User.objects.all(), all=True))
return render(request, "base.html")
async def async_execute_union_sql(request):
list_store = []
# make async query with filter, which is compatible with async for.
users = User.objects.all().union(User.objects.all(), all=True)
async for user in users:
list_store.append(user)
return render(request, "base.html")
async def async_execute_sql(request):
"""
Some query API can be executed asynchronously but some requires
async version of itself.
https://docs.djangoproject.com/en/5.1/topics/db/queries/#asynchronous-queries
"""
list_store = []
# make async query with filter, which is compatible with async for.
async for user in User.objects.filter(username="test"):
list_store.append(user)
# make async query with afirst
async_fetched_user = await User.objects.filter(username="test").afirst()
list_store.append(async_fetched_user)
return render(request, "base.html")
async def async_execute_sql_concurrently(request):
await asyncio.gather(sync_to_async(list)(User.objects.all()), User.objects.acount())
return render(request, "base.html")
def regular_view(request, title):
return render(request, "basic.html", {"title": title})
def csp_view(request):
"""Use request.csp_nonce to inject it into the headers"""
nonce = get_csp_nonce(request)
return render(request, "basic.html", {"title": f"CSP {nonce}"})
def template_response_view(request, title):
return TemplateResponse(request, "basic.html", {"title": title})
def new_user(request, username="joe"):
User.objects.create_user(username=username)
return render(request, "basic.html", {"title": "new user"})
def resolving_view(request, arg1, arg2):
# see test_url_resolving in tests.py
return render(request, "base.html")
@cache_page(60)
def cached_view(request):
return render(request, "base.html")
def cached_low_level_view(request):
key = "spam"
value = cache.get(key)
if not value:
value = "eggs"
cache.set(key, value, 60)
return render(request, "base.html")
def json_view(request):
return JsonResponse({"foo": "bar"})
def regular_jinjia_view(request, title):
return render(request, "basic.jinja", {"title": title}, using="jinja2")
def listcomp_view(request):
lst = [i for i in range(50000) if i % 2 == 0]
return render(request, "basic.html", {"title": "List comprehension", "lst": lst})
def redirect_view(request):
return HttpResponseRedirect("/regular/redirect/")
def ajax_view(request):
return render(request, "ajax/ajax.html")
def server_timing(request):
response = execute_sql(request)
response.headers["Server-Timing"] = 'existing_key;dur=100;desc="Details"'
return response