Convert Django project files in the root to example project.
Move pexp project to 'example' folder too.
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import os
|
||||
|
||||
DEBUG = True
|
||||
TEMPLATE_DEBUG = DEBUG
|
||||
|
||||
ADMINS = (
|
||||
# ('Your Name', 'your_email@example.com'),
|
||||
)
|
||||
|
||||
MANAGERS = ADMINS
|
||||
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(PROJECT_ROOT, 'example.db'),
|
||||
}
|
||||
}
|
||||
|
||||
SITE_ID = 1
|
||||
|
||||
# Make this unique, and don't share it with anybody.
|
||||
SECRET_KEY = '5$f%)&a4tc*bg(79+ku!7o$kri-duw99@hq_)va^_kaw9*l)!7'
|
||||
|
||||
|
||||
# Language
|
||||
# TIME_ZONE = 'America/Chicago'
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
USE_I18N = True
|
||||
USE_L10N = True
|
||||
USE_TZ = True
|
||||
|
||||
# Paths
|
||||
MEDIA_ROOT = ''
|
||||
MEDIA_URL = '/media/'
|
||||
STATIC_ROOT = ''
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
# Apps
|
||||
STATICFILES_FINDERS = (
|
||||
'django.contrib.staticfiles.finders.FileSystemFinder',
|
||||
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
|
||||
)
|
||||
|
||||
TEMPLATE_LOADERS = (
|
||||
'django.template.loaders.filesystem.Loader',
|
||||
'django.template.loaders.app_directories.Loader',
|
||||
)
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'example.urls'
|
||||
|
||||
WSGI_APPLICATION = 'example.wsgi.application'
|
||||
|
||||
TEMPLATE_DIRS = ()
|
||||
|
||||
INSTALLED_APPS = (
|
||||
'django.contrib.auth',
|
||||
'django.contrib.admin',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
#'django.contrib.sites',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
|
||||
'polymorphic', # needed if you want to use the polymorphic admin
|
||||
'pexp', # this Django app is for testing and experimentation; not needed otherwise
|
||||
)
|
||||
|
||||
# Logging configuration
|
||||
LOGGING = {
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'filters': {
|
||||
'require_debug_false': {
|
||||
'()': 'django.utils.log.RequireDebugFalse'
|
||||
}
|
||||
},
|
||||
'handlers': {
|
||||
'mail_admins': {
|
||||
'level': 'ERROR',
|
||||
'filters': ['require_debug_false'],
|
||||
'class': 'django.utils.log.AdminEmailHandler'
|
||||
}
|
||||
},
|
||||
'loggers': {
|
||||
'django.request': {
|
||||
'handlers': ['mail_admins'],
|
||||
'level': 'ERROR',
|
||||
'propagate': True,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
from django.conf.urls import patterns, include, url
|
||||
from django.contrib import admin
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.views.generic import RedirectView
|
||||
|
||||
admin.autodiscover()
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
url(r'^$', RedirectView.as_view(url=reverse_lazy('admin:index'), permanent=False)),
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
WSGI config for example project.
|
||||
|
||||
This module contains the WSGI application used by Django's development server
|
||||
and any production WSGI deployments. It should expose a module-level variable
|
||||
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
|
||||
this application via the ``WSGI_APPLICATION`` setting.
|
||||
|
||||
Usually you will have the standard Django WSGI application here, but it also
|
||||
might make sense to replace the whole Django WSGI application with a custom one
|
||||
that later delegates to the Django one. For example, you could introduce WSGI
|
||||
middleware here, or combine a Django application with an application of another
|
||||
framework.
|
||||
|
||||
"""
|
||||
import os
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")
|
||||
|
||||
# This application object is used by any WSGI server configured to use this
|
||||
# file. This includes Django's development server, if the WSGI_APPLICATION
|
||||
# setting points here.
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
application = get_wsgi_application()
|
||||
|
||||
# Apply WSGI middleware here.
|
||||
# from helloworld.wsgi import HelloWorldApplication
|
||||
# application = HelloWorldApplication(application)
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")
|
||||
|
||||
# Import polymorphic from this folder.
|
||||
SRC_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||
sys.path.insert(0, SRC_ROOT)
|
||||
|
||||
from django.core.management import execute_from_command_line
|
||||
|
||||
execute_from_command_line(sys.argv)
|
||||
@@ -0,0 +1,63 @@
|
||||
from django.contrib import admin
|
||||
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
|
||||
from pexp.models import *
|
||||
|
||||
|
||||
class ProjectChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = Project
|
||||
|
||||
class ProjectAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = Project
|
||||
child_models = (
|
||||
(Project, ProjectChildAdmin),
|
||||
(ArtProject, ProjectChildAdmin),
|
||||
(ResearchProject, ProjectChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(Project, ProjectAdmin)
|
||||
|
||||
|
||||
|
||||
class ModelAChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = ModelA
|
||||
|
||||
class ModelAAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = ModelA
|
||||
child_models = (
|
||||
(ModelA, ModelAChildAdmin),
|
||||
(ModelB, ModelAChildAdmin),
|
||||
(ModelC, ModelAChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(ModelA, ModelAAdmin)
|
||||
|
||||
|
||||
if 'Model2A' in globals():
|
||||
class Model2AChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = Model2A
|
||||
|
||||
class Model2AAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = Model2A
|
||||
child_models = (
|
||||
(Model2A, Model2AChildAdmin),
|
||||
(Model2B, Model2AChildAdmin),
|
||||
(Model2C, Model2AChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(Model2A, Model2AAdmin)
|
||||
|
||||
|
||||
if 'UUIDModelA' in globals():
|
||||
class UUIDModelAChildAdmin(PolymorphicChildModelAdmin):
|
||||
base_model = UUIDModelA
|
||||
|
||||
class UUIDModelAAdmin(PolymorphicParentModelAdmin):
|
||||
base_model = UUIDModelA
|
||||
child_models = (
|
||||
(UUIDModelA, UUIDModelAChildAdmin),
|
||||
(UUIDModelB, UUIDModelAChildAdmin),
|
||||
(UUIDModelC, UUIDModelAChildAdmin),
|
||||
)
|
||||
|
||||
admin.site.register(UUIDModelA, UUIDModelAAdmin)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
[
|
||||
{
|
||||
"pk": 1,
|
||||
"model": "pexp.project",
|
||||
"fields": {
|
||||
"topic": "John's gathering",
|
||||
"polymorphic_ctype": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "pexp.project",
|
||||
"fields": {
|
||||
"topic": "Sculpting with Tim",
|
||||
"polymorphic_ctype": 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "pexp.project",
|
||||
"fields": {
|
||||
"topic": "Swallow Aerodynamics",
|
||||
"polymorphic_ctype": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 2,
|
||||
"model": "pexp.artproject",
|
||||
"fields": {
|
||||
"artist": "T. Turner"
|
||||
}
|
||||
},
|
||||
{
|
||||
"pk": 3,
|
||||
"model": "pexp.researchproject",
|
||||
"fields": {
|
||||
"supervisor": "Dr. Winter"
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,106 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
Well, even more so than pcmd.py. You best ignore p2cmd.py.
|
||||
"""
|
||||
import uuid
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
import time,sys
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
|
||||
|
||||
def print_timing(func, message='', iterations=1):
|
||||
def wrapper(*arg):
|
||||
results=[]
|
||||
reset_queries()
|
||||
for i in xrange(iterations):
|
||||
t1 = time.time()
|
||||
x = func(*arg)
|
||||
t2 = time.time()
|
||||
results.append((t2-t1)*1000.0)
|
||||
res_sum=0
|
||||
for r in results: res_sum +=r
|
||||
median = res_sum / len(results)
|
||||
print '%s%-19s: %.4f ms, %i queries (%i times)' % (
|
||||
message,func.func_name,
|
||||
res_sum,
|
||||
len(connection.queries),
|
||||
iterations
|
||||
)
|
||||
sys.stdout.flush()
|
||||
return wrapper
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
if False:
|
||||
ModelA.objects.all().delete()
|
||||
a=ModelA.objects.create(field1='A1')
|
||||
b=ModelB.objects.create(field1='B1', field2='B2')
|
||||
c=ModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
reset_queries()
|
||||
print ModelC.base_objects.all();
|
||||
show_queries()
|
||||
|
||||
if False:
|
||||
ModelA.objects.all().delete()
|
||||
for i in xrange(1000):
|
||||
a=ModelA.objects.create(field1=str(i%100))
|
||||
b=ModelB.objects.create(field1=str(i%100), field2=str(i%200))
|
||||
c=ModelC.objects.create(field1=str(i%100), field2=str(i%200), field3=str(i%300))
|
||||
if i%100==0: print i
|
||||
|
||||
f=print_timing(poly_sql_query,iterations=1000)
|
||||
f()
|
||||
|
||||
f=print_timing(poly_sql_query2,iterations=1000)
|
||||
f()
|
||||
|
||||
return
|
||||
|
||||
nModelA.objects.all().delete()
|
||||
a=nModelA.objects.create(field1='A1')
|
||||
b=nModelB.objects.create(field1='B1', field2='B2')
|
||||
c=nModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
qs=ModelA.objects.raw("SELECT * from pexp_modela")
|
||||
for o in list(qs): print o
|
||||
|
||||
from django.db import connection, transaction
|
||||
from random import Random
|
||||
rnd=Random()
|
||||
|
||||
def poly_sql_query():
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("""
|
||||
SELECT id, pexp_modela.field1, pexp_modelb.field2, pexp_modelc.field3
|
||||
FROM pexp_modela
|
||||
LEFT OUTER JOIN pexp_modelb
|
||||
ON pexp_modela.id = pexp_modelb.modela_ptr_id
|
||||
LEFT OUTER JOIN pexp_modelc
|
||||
ON pexp_modelb.modela_ptr_id = pexp_modelc.modelb_ptr_id
|
||||
WHERE pexp_modela.field1=%i
|
||||
ORDER BY pexp_modela.id
|
||||
""" % rnd.randint(0,100) )
|
||||
#row=cursor.fetchone()
|
||||
return
|
||||
|
||||
def poly_sql_query2():
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("""
|
||||
SELECT id, pexp_modela.field1
|
||||
FROM pexp_modela
|
||||
WHERE pexp_modela.field1=%i
|
||||
ORDER BY pexp_modela.id
|
||||
""" % rnd.randint(0,100) )
|
||||
#row=cursor.fetchone()
|
||||
return
|
||||
@@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging.
|
||||
"""
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
Project.objects.all().delete()
|
||||
a=Project.objects.create(topic="John's gathering")
|
||||
b=ArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
|
||||
c=ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
||||
print Project.objects.all()
|
||||
print
|
||||
|
||||
ModelA.objects.all().delete()
|
||||
a=ModelA.objects.create(field1='A1')
|
||||
b=ModelB.objects.create(field1='B1', field2='B2')
|
||||
c=ModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
print ModelA.objects.all()
|
||||
print
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
"""
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
import sys
|
||||
from pexp.models import *
|
||||
|
||||
num_objects=1000
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; reset_queries()
|
||||
|
||||
import time
|
||||
|
||||
###################################################################################
|
||||
### benchmark wrappers
|
||||
|
||||
def print_timing(func, message='', iterations=1):
|
||||
def wrapper(*arg):
|
||||
results=[]
|
||||
reset_queries()
|
||||
for i in xrange(iterations):
|
||||
t1 = time.time()
|
||||
x = func(*arg)
|
||||
t2 = time.time()
|
||||
results.append((t2-t1)*1000.0)
|
||||
res_sum=0
|
||||
for r in results: res_sum +=r
|
||||
median = res_sum / len(results)
|
||||
print '%s%-19s: %.0f ms, %i queries' % (
|
||||
message,func.func_name,
|
||||
median,
|
||||
len(connection.queries)/len(results)
|
||||
)
|
||||
sys.stdout.flush()
|
||||
return wrapper
|
||||
|
||||
def run_vanilla_any_poly(func, iterations=1):
|
||||
f=print_timing(func,' ', iterations)
|
||||
f(nModelC)
|
||||
f=print_timing(func,'poly ', iterations)
|
||||
f(ModelC)
|
||||
|
||||
|
||||
###################################################################################
|
||||
### benchmarks
|
||||
|
||||
def bench_create(model):
|
||||
for i in xrange(num_objects):
|
||||
model.objects.create(field1='abc'+str(i), field2='abcd'+str(i), field3='abcde'+str(i))
|
||||
#print 'count:',model.objects.count()
|
||||
|
||||
def bench_load1(model):
|
||||
for o in model.objects.all():
|
||||
pass
|
||||
|
||||
def bench_load1_short(model):
|
||||
for i in xrange(num_objects/100):
|
||||
for o in model.objects.all()[:100]:
|
||||
pass
|
||||
|
||||
def bench_load2(model):
|
||||
for o in model.objects.all():
|
||||
f1=o.field1
|
||||
f2=o.field2
|
||||
f3=o.field3
|
||||
|
||||
def bench_load2_short(model):
|
||||
for i in xrange(num_objects/100):
|
||||
for o in model.objects.all()[:100]:
|
||||
f1=o.field1
|
||||
f2=o.field2
|
||||
f3=o.field3
|
||||
|
||||
def bench_delete(model):
|
||||
model.objects.all().delete()
|
||||
|
||||
###################################################################################
|
||||
### Command
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
func_list = [
|
||||
( bench_delete, 1 ),
|
||||
( bench_create, 1 ),
|
||||
( bench_load1, 5 ),
|
||||
( bench_load1_short, 5 ),
|
||||
( bench_load2, 5 ),
|
||||
( bench_load2_short, 5 )
|
||||
]
|
||||
for f,iterations in func_list:
|
||||
run_vanilla_any_poly(f,iterations=iterations)
|
||||
|
||||
print
|
||||
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
"""
|
||||
|
||||
from django.core.management.base import NoArgsCommand
|
||||
from django.db.models import connection
|
||||
from pprint import pprint
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
def reset_queries():
|
||||
connection.queries=[]
|
||||
|
||||
def show_queries():
|
||||
print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
help = ""
|
||||
|
||||
def handle_noargs(self, **options):
|
||||
Project.objects.all().delete()
|
||||
o=Project.objects.create(topic="John's gathering")
|
||||
o=ArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
|
||||
o=ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
||||
print Project.objects.all()
|
||||
print
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models
|
||||
|
||||
from polymorphic import PolymorphicModel, PolymorphicManager, PolymorphicQuerySet
|
||||
from polymorphic.showfields import ShowFieldContent, ShowFieldType, ShowFieldTypeAndContent
|
||||
|
||||
class Project(ShowFieldContent, PolymorphicModel):
|
||||
topic = models.CharField(max_length=30)
|
||||
class ArtProject(Project):
|
||||
artist = models.CharField(max_length=30)
|
||||
class ResearchProject(Project):
|
||||
supervisor = models.CharField(max_length=30)
|
||||
|
||||
class ModelA(ShowFieldTypeAndContent, PolymorphicModel):
|
||||
field1 = models.CharField(max_length=10)
|
||||
class ModelB(ModelA):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class ModelC(ModelB):
|
||||
field3 = models.CharField(max_length=10)
|
||||
|
||||
class nModelA(models.Model):
|
||||
field1 = models.CharField(max_length=10)
|
||||
class nModelB(nModelA):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class nModelC(nModelB):
|
||||
field3 = models.CharField(max_length=10)
|
||||
|
||||
# for Django 1.2+, test models with same names in different apps
|
||||
# (the other models with identical names are in polymorphic/tests.py)
|
||||
from django import VERSION as django_VERSION
|
||||
if not (django_VERSION[0]<=1 and django_VERSION[1]<=1):
|
||||
class Model2A(PolymorphicModel):
|
||||
field1 = models.CharField(max_length=10)
|
||||
class Model2B(Model2A):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class Model2C(Model2B):
|
||||
field3 = models.CharField(max_length=10)
|
||||
|
||||
try: from polymorphic.test_tools import UUIDField
|
||||
except: pass
|
||||
if 'UUIDField' in globals():
|
||||
class UUIDModelA(ShowFieldTypeAndContent, PolymorphicModel):
|
||||
uuid_primary_key = UUIDField(primary_key = True)
|
||||
field1 = models.CharField(max_length=10)
|
||||
class UUIDModelB(UUIDModelA):
|
||||
field2 = models.CharField(max_length=10)
|
||||
class UUIDModelC(UUIDModelB):
|
||||
field3 = models.CharField(max_length=10)
|
||||
Reference in New Issue
Block a user