Changeset 1612 for trunk/dev_scripts/local_tests/models_test.py
- Timestamp:
- 05/31/08 22:01:20 (6 months ago)
- Files:
-
- 1 modified
-
trunk/dev_scripts/local_tests/models_test.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/dev_scripts/local_tests/models_test.py
r1607 r1612 19 19 from django.contrib.auth.models import User 20 20 21 # Use the models from the test plugin 22 from tests.unittest_plugin.unittest_plugin import TestArtist, TestAlbum 21 23 22 24 23 class Musician(models.Model): 24 first_name = models.CharField(max_length=50) 25 last_name = models.CharField(max_length=50) 26 instrument = models.CharField(max_length=100) 27 class Meta: 28 # db_table is optional 29 # db_table = 'PyLucid_test' 30 app_label = 'PyLucid' # must be set to "PyLucid" 25 print "-----------------------------------------------------------------------" 31 26 32 class Album(models.Model): 33 artist = models.ForeignKey("Musician") 34 name = models.CharField(max_length=100) 35 num_stars = models.IntegerField() 36 37 createtime = models.DateTimeField(auto_now_add=True) 38 lastupdatetime = models.DateTimeField(auto_now=True) 39 40 createby = models.ForeignKey(User, related_name="test_createby", 41 null=True, blank=True 42 ) 43 lastupdateby = models.ForeignKey(User, related_name="test_lastupdateby", 44 null=True, blank=True 45 ) 46 class Meta: 47 # db_table is optional 48 # db_table = 'PyLucid_test' 49 app_label = 'PyLucid' # must be set to "PyLucid" 27 print "XXX" 28 print TestAlbum._meta.app_label 29 #t = TestAlbum._meta 30 #for i in dir(t): 31 # print i, getattr(t, i) 50 32 51 33 52 print "----------------------------------------------------------------------- -------------------------------"34 print "-----------------------------------------------------------------------" 53 35 try: 54 36 # Here the table doesn't exist 55 m = Musician(first_name = "foo", last_name = "bar", instrument= "foobar")37 m = TestArtist(name = "foobar") 56 38 m.save() 57 39 except Exception, err: 58 40 print err 59 41 60 print "----------------------------------------------------------------------- -------------------------------"42 print "-----------------------------------------------------------------------" 61 43 62 44 def get_create_table(models): … … 72 54 return statements 73 55 74 statements = get_create_table(models = (Musician, Album)) 56 def get_create_table2(*plugin_models): 57 from django.conf import settings 58 from django.core.management.color import no_style 59 style = no_style() 60 from django.core.management import sql 61 from django.db import models 62 63 models.loading.register_models("PyLucidPlugins", *plugin_models) 64 65 # get all delete statements for the given App 66 app = models.get_app("PyLucidPlugins") 67 statements = sql.sql_create(app, style) 68 69 #cleanup 70 app_models = models.loading.cache.app_models 71 del(app_models["PyLucidPlugins"]) 72 # settings.INSTALLED_APPS = old_inst_apps 73 74 return statements 75 76 statements = get_create_table(models = (TestArtist, TestAlbum)) 77 #statements = get_create_table2(TestArtist, TestAlbum) 75 78 76 79 from django.db import connection … … 80 83 cursor.execute(statement) 81 84 82 print "----------------------------------------------------------------------- -------------------------------"85 print "-----------------------------------------------------------------------" 83 86 84 87 # Now we can use the created tables 85 m = Musician(first_name = "foo", last_name = "bar", instrument= "foobar")88 m = TestArtist(name = "foobar") 86 89 m.save() 87 a = Album(90 a = TestAlbum( 88 91 artist = m, 89 92 name = "jup", … … 91 94 ) 92 95 a.save() 93 print Album.objects.all().values()96 print TestAlbum.objects.all().values() 94 97 95 print "----------------------------------------------------------------------- -------------------------------"98 print "-----------------------------------------------------------------------" 96 99 97 100 # Not needed to "insert" the models: 98 101 #from PyLucid.system.PyLucidPlugins import models as PluginModels 99 #PluginModels. Musician = Musician100 #PluginModels. Album =Album102 #PluginModels.TestArtist = TestArtist 103 #PluginModels.TestAlbum = TestAlbum 101 104 102 105 def get_delete_sql(*plugin_models): … … 107 110 from django.db import models 108 111 109 # Insert app in installed apps110 old_inst_apps = settings.INSTALLED_APPS111 inst_apps = list(old_inst_apps)112 inst_apps.append("PyLucid.system.PyLucidPlugins")113 settings.INSTALLED_APPS = inst_apps112 # # Insert app in installed apps 113 # old_inst_apps = settings.INSTALLED_APPS 114 # inst_apps = list(old_inst_apps) 115 # inst_apps.append("PyLucid.system.PyLucidPlugins") 116 # settings.INSTALLED_APPS = inst_apps 114 117 115 118 models.loading.register_models("PyLucidPlugins", *plugin_models) … … 122 125 app_models = models.loading.cache.app_models 123 126 del(app_models["PyLucidPlugins"]) 124 settings.INSTALLED_APPS = old_inst_apps127 # settings.INSTALLED_APPS = old_inst_apps 125 128 126 129 return statements 127 130 128 statements = get_delete_sql( Musician)131 statements = get_delete_sql(TestArtist) 129 132 print "1:", statements 130 133 131 statements = get_delete_sql( Musician,Album)134 statements = get_delete_sql(TestArtist, TestAlbum) 132 135 print "2:", statements 133 136 … … 139 142 140 143 # After this no tables left: 141 statements = get_delete_sql( Musician,Album)144 statements = get_delete_sql(TestArtist, TestAlbum) 142 145 print statements 143 146 144 statements = get_delete_sql( Album)147 statements = get_delete_sql(TestAlbum) 145 148 print statements 146 149 147 print "----------------------------------------------------------------------- -------------------------------"150 print "-----------------------------------------------------------------------" 148 151 149 152 # No tables error: 150 153 try: 151 m = Musician(first_name = "foo", last_name = "bar", instrument= "foobar")154 m = TestArtist(name = "foobar") 152 155 m.save() 153 156 except Exception, err:
