Browse Source

Django fortune test

Patrick Falls 12 years ago
parent
commit
96b3920bda

+ 2 - 1
django/benchmark_config

@@ -6,8 +6,9 @@
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/db?queries=",
       "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
       "port": 8080,
       "port": 8080,
       "sort": 3
       "sort": 3
     }
     }
   }]
   }]
-}
+}

+ 3 - 3
django/hello/templates/fortunes/index.html → django/hello/templates/fortunes.html

@@ -1,4 +1,4 @@
-{% extend base.html %}
+{% extends "base.html" %}
 
 
 {% block content %}
 {% block content %}
 <table>
 <table>
@@ -8,8 +8,8 @@
 </tr>
 </tr>
 {% for fortune in fortunes %}
 {% for fortune in fortunes %}
 <tr>
 <tr>
-<td>{% fortune.id %}</td>
-<td>{% fortune.message %}</td>
+<td>{{ fortune.id }}</td>
+<td>{{ fortune.message }}</td>
 </tr>
 </tr>
 {% endfor %}
 {% endfor %}
 </table>
 </table>

+ 1 - 1
django/hello/world/models.py

@@ -8,6 +8,6 @@ class World(models.Model):
     db_table = 'world'
     db_table = 'world'
 
 
 class Fortune(models.Model):
 class Fortune(models.Model):
-  message = models.StringField()
+  message = models.CharField()
   class Meta:
   class Meta:
     db_table = 'fortune'
     db_table = 'fortune'

+ 5 - 4
django/hello/world/views.py

@@ -3,10 +3,11 @@
 from django.template import Context, loader
 from django.template import Context, loader
 from django.http import HttpResponse
 from django.http import HttpResponse
 from django.core import serializers
 from django.core import serializers
-from world.models import World
-from world.models import Fortune
+from world.models import World, Fortune
+from django.shortcuts import render
 import ujson
 import ujson
 import random
 import random
+from operator import attrgetter
 
 
 def json(request):
 def json(request):
   response = {
   response = {
@@ -25,10 +26,10 @@ def db(request):
   return HttpResponse(serializers.serialize("json", worlds), mimetype="application/json")
   return HttpResponse(serializers.serialize("json", worlds), mimetype="application/json")
 
 
 def fortunes(request):
 def fortunes(request):
-  fortunes = Fortune.objects.all()
+  fortunes = list(Fortune.objects.all())
   fortunes.append(Fortune(id=0, message="Additional message added at runtime."))
   fortunes.append(Fortune(id=0, message="Additional message added at runtime."))
 
 
   fortunes = sorted(fortunes, key=attrgetter('message'))
   fortunes = sorted(fortunes, key=attrgetter('message'))
 
 
   context = {'fortunes': fortunes}
   context = {'fortunes': fortunes}
-  return render(request, 'fortunes/index.html', context)
+  return render(request, 'fortunes.html', context)

+ 4 - 0
django/setup.py

@@ -2,9 +2,13 @@ import subprocess
 import sys
 import sys
 import setup_util
 import setup_util
 import os
 import os
+from os.path import expanduser
+
+home = expanduser("~")
 
 
 def start(args):
 def start(args):
   setup_util.replace_text("django/hello/hello/settings.py", "HOST': '.*'", "HOST': '" + args.database_host + "'")
   setup_util.replace_text("django/hello/hello/settings.py", "HOST': '.*'", "HOST': '" + args.database_host + "'")
+  setup_util.replace_text("django/hello/hello/settings.py", "\/home\/ubuntu",  home)
   subprocess.Popen("gunicorn hello.wsgi:application -k gevent  -b 0.0.0.0:8080 -w " + str((args.max_threads * 2)) + " --log-level=critical", shell=True, cwd="django/hello")
   subprocess.Popen("gunicorn hello.wsgi:application -k gevent  -b 0.0.0.0:8080 -w " + str((args.max_threads * 2)) + " --log-level=critical", shell=True, cwd="django/hello")
   return 0
   return 0
 def stop():
 def stop():