浏览代码

improve django coding (#5625)

* improve django coding

update

* update latest requirements

* update to python 3.7 for django

* update
TonyYang 5 年之前
父节点
当前提交
c47a134897

+ 1 - 3
frameworks/Python/django/django-postgresql.dockerfile

@@ -1,4 +1,4 @@
-FROM python:3.6.9-stretch
+FROM python:3.7-stretch
 
 ADD ./ /django
 
@@ -6,6 +6,4 @@ WORKDIR /django
 
 RUN pip install -r /django/requirements.txt
 
-WORKDIR /django
-
 CMD gunicorn --pid=gunicorn.pid hello.wsgi:application -c gunicorn_conf.py --env DJANGO_DB=postgresql_psycopg2

+ 1 - 3
frameworks/Python/django/django.dockerfile

@@ -1,4 +1,4 @@
-FROM python:3.6.9-stretch
+FROM python:3.7-stretch
 
 ADD ./ /django
 
@@ -6,6 +6,4 @@ WORKDIR /django
 
 RUN pip install -r /django/requirements.txt
 
-WORKDIR /django
-
 CMD gunicorn --pid=gunicorn.pid hello.wsgi:application -c gunicorn_conf.py --env DJANGO_DB=mysql

+ 27 - 36
frameworks/Python/django/hello/world/views.py

@@ -1,5 +1,5 @@
 import random
-from operator import attrgetter
+from operator import itemgetter
 from functools import partial
 from ujson import dumps as uj_dumps
 
@@ -9,6 +9,9 @@ from django.shortcuts import render
 from world.models import World, Fortune
 
 
+_random_int = partial(random.randint, 1, 10000)
+
+
 def _get_queries(request):
     try:
         queries = int(request.GET.get('queries', 1))
@@ -26,60 +29,48 @@ def plaintext(request):
 
 
 def json(request):
-    response = {
-        "message": "Hello, World!"
-    }
-    return HttpResponse(uj_dumps(response), content_type="application/json")
+    return HttpResponse(
+            uj_dumps({"message": "Hello, World!"}),
+            content_type="application/json"
+        )
 
 
 def db(request):
-    r = random.randint(1, 10000)
-    world = uj_dumps({'id' : r, 'randomNumber' : World.objects.get(id=r).randomnumber})
+    r = _random_int()
+    world = uj_dumps({
+        'id' : r,
+        'randomNumber' : World.objects.get(id=r).randomnumber
+    })
     return HttpResponse(world, content_type="application/json")
 
 
 def dbs(request):
     queries = _get_queries(request)
 
-    # fun fact:    every dot-notation lookup calls some python magic under the hood.    Like every other code,
-    # one can eliminate dereferences by storing the end dereferenced thing in an identifier
-    g = World.objects.get
-
-    # but wait!    there's more!    if we're calling a function over and over with the same parameters,
-    # we can use even more function magic.
-    #r = random.randint
-    rp = partial(random.randint, 1, 10000)
+    def caller(input_):
+        int_ = _random_int()
+        return {'id' : int_, 'randomNumber' : World.objects.get(id=int_).randomnumber}
+    worlds = tuple(map(caller, range(queries)))
 
-    # now we're ready to write our awesome query iterator thingy
-    # first of all, we know the id's correspond to the random number we're picking, so we can create
-    # dictionaries on the fly instead of serializing later
-    # by creating dicts, we don't need to user the model serializer, which is probably slow and only appropriate
-    # for complicated serializations of joins and crazy query sets etc
-    # test range vs range if the query number is gigantic
-    worlds = uj_dumps([{'id' : r, 'randomNumber' : g(id=r).randomnumber} for r in [rp() for q in range(queries)]])
-    return HttpResponse(worlds, content_type="application/json")
+    return HttpResponse(uj_dumps(worlds), content_type="application/json")
 
 
 def fortunes(request):
-    fortunes = list(Fortune.objects.all())
-    fortunes.append(Fortune(id=0, message="Additional fortune added at request time."))
-
-    fortunes = sorted(fortunes, key=attrgetter('message'))
+    fortunes = list(Fortune.objects.values('id', 'message'))
+    fortunes.append({"id": 0, 'message': "Additional fortune added at request time."})
+    fortunes.sort(key=itemgetter('message'))
 
-    context = {'fortunes': fortunes}
-    return render(request, 'fortunes.html', context)
+    return render(request, 'fortunes.html', {'fortunes': fortunes})
 
 
 def update(request):
     queries = _get_queries(request)
-    g = World.objects.get
-    rp = partial(random.randint, 1, 10000)
 
-    worlds = []
-    for r in [rp() for q in range(queries)]:
-        w = g(id=r)
-        w.randomnumber = rp()
+    def caller(input_):
+        w = World.objects.get(id= _random_int())
+        w.randomnumber = _random_int()
         w.save()
-        worlds.append({'id' : r, 'randomNumber' : w.randomnumber})
+        return {'id' : w.id, 'randomNumber' : w.randomnumber}
+    worlds = tuple(map(caller, range(queries)))
 
     return HttpResponse(uj_dumps(worlds), content_type="application/json")

+ 8 - 8
frameworks/Python/django/requirements.txt

@@ -1,8 +1,8 @@
-Django==2.2.3
-greenlet==0.4.14
-gunicorn==19.9.0
-meinheld==0.6.1
-mysqlclient==1.3.13
-psycopg2==2.7.5
-pytz==2017.3
-ujson==1.35
+Django==3.0.5
+greenlet==0.4.15
+gunicorn==20.0.4
+meinheld==1.0.1
+mysqlclient==1.4.6
+psycopg2==2.8.5
+pytz==2019.3
+ujson==2.0.3