From 958d240411b245b1500a30cffa2f7a028991e21c Mon Sep 17 00:00:00 2001 From: Ben Lopatin Date: Thu, 15 May 2014 09:39:12 +0200 Subject: [PATCH] Add test custom models --- example/accounts/migrations/0001_initial.py | 115 +++++++++++++++++ example/accounts/migrations/__init__.py | 0 example/conf/settings.py | 5 +- example/vendors/migrations/0001_initial.py | 130 +++++++++++++++++++ example/vendors/migrations/__init__.py | 0 example/vendors/models.py | 5 +- organizations/base.py | 31 ++--- organizations/models.py | 18 +-- runtests.py | 6 + test_accounts/__init__.py | 0 test_accounts/migrations/0001_initial.py | 115 +++++++++++++++++ test_accounts/migrations/__init__.py | 0 test_accounts/models.py | 15 +++ test_vendors/__init__.py | 0 test_vendors/migrations/0001_initial.py | 131 ++++++++++++++++++++ test_vendors/migrations/__init__.py | 0 test_vendors/models.py | 18 +++ tests/test_models.py | 20 +++ 18 files changed, 570 insertions(+), 39 deletions(-) create mode 100644 example/accounts/migrations/0001_initial.py create mode 100644 example/accounts/migrations/__init__.py create mode 100644 example/vendors/migrations/0001_initial.py create mode 100644 example/vendors/migrations/__init__.py create mode 100644 test_accounts/__init__.py create mode 100644 test_accounts/migrations/0001_initial.py create mode 100644 test_accounts/migrations/__init__.py create mode 100644 test_accounts/models.py create mode 100644 test_vendors/__init__.py create mode 100644 test_vendors/migrations/0001_initial.py create mode 100644 test_vendors/migrations/__init__.py create mode 100644 test_vendors/models.py diff --git a/example/accounts/migrations/0001_initial.py b/example/accounts/migrations/0001_initial.py new file mode 100644 index 00000000..9b7f4a00 --- /dev/null +++ b/example/accounts/migrations/0001_initial.py @@ -0,0 +1,115 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Account' + db.create_table('accounts_account', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('name', self.gf('django.db.models.fields.CharField')(max_length=200)), + ('is_active', self.gf('django.db.models.fields.BooleanField')(default=True)), + ('monthly_subscription', self.gf('django.db.models.fields.IntegerField')(default=1000)), + )) + db.send_create_signal('accounts', ['Account']) + + # Adding model 'AccountUser' + db.create_table('accounts_accountuser', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('user_type', self.gf('django.db.models.fields.CharField')(default='', max_length=1)), + ('user', self.gf('django.db.models.fields.related.ForeignKey')(related_name='accounts_accountuser', to=orm['auth.User'])), + ('organization', self.gf('django.db.models.fields.related.ForeignKey')(related_name='organization_users', to=orm['accounts.Account'])), + )) + db.send_create_signal('accounts', ['AccountUser']) + + # Adding unique constraint on 'AccountUser', fields ['user', 'organization'] + db.create_unique('accounts_accountuser', ['user_id', 'organization_id']) + + # Adding model 'AccountOwner' + db.create_table('accounts_accountowner', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('organization_user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['accounts.AccountUser'], unique=True)), + ('organization', self.gf('django.db.models.fields.related.OneToOneField')(related_name='owner', unique=True, to=orm['accounts.Account'])), + )) + db.send_create_signal('accounts', ['AccountOwner']) + + + def backwards(self, orm): + # Removing unique constraint on 'AccountUser', fields ['user', 'organization'] + db.delete_unique('accounts_accountuser', ['user_id', 'organization_id']) + + # Deleting model 'Account' + db.delete_table('accounts_account') + + # Deleting model 'AccountUser' + db.delete_table('accounts_accountuser') + + # Deleting model 'AccountOwner' + db.delete_table('accounts_accountowner') + + + models = { + 'accounts.account': { + 'Meta': {'ordering': "['name']", 'object_name': 'Account'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'monthly_subscription': ('django.db.models.fields.IntegerField', [], {'default': '1000'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}), + 'users': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'accounts_account'", 'symmetrical': 'False', 'through': "orm['accounts.AccountUser']", 'to': "orm['auth.User']"}) + }, + 'accounts.accountowner': { + 'Meta': {'object_name': 'AccountOwner'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'owner'", 'unique': 'True', 'to': "orm['accounts.Account']"}), + 'organization_user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['accounts.AccountUser']", 'unique': 'True'}) + }, + 'accounts.accountuser': { + 'Meta': {'ordering': "['organization', 'user']", 'unique_together': "(('user', 'organization'),)", 'object_name': 'AccountUser'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'organization_users'", 'to': "orm['accounts.Account']"}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'accounts_accountuser'", 'to': "orm['auth.User']"}), + 'user_type': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1'}) + }, + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + } + } + + complete_apps = ['accounts'] \ No newline at end of file diff --git a/example/accounts/migrations/__init__.py b/example/accounts/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/example/conf/settings.py b/example/conf/settings.py index eff0b1a1..59654a59 100644 --- a/example/conf/settings.py +++ b/example/conf/settings.py @@ -128,11 +128,8 @@ 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', - # Uncomment the next line to enable the admin: 'django.contrib.admin', - # Uncomment the next line to enable admin documentation: - 'django.contrib.admindocs', - #'organizations', + 'south', 'accounts', 'vendors', ) diff --git a/example/vendors/migrations/0001_initial.py b/example/vendors/migrations/0001_initial.py new file mode 100644 index 00000000..445a9ceb --- /dev/null +++ b/example/vendors/migrations/0001_initial.py @@ -0,0 +1,130 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Vendor' + db.create_table('vendors_vendor', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('name', self.gf('django.db.models.fields.CharField')(max_length=200)), + ('is_active', self.gf('django.db.models.fields.BooleanField')(default=True)), + ('street_address', self.gf('django.db.models.fields.CharField')(default='', max_length=100)), + ('city', self.gf('django.db.models.fields.CharField')(default='', max_length=100)), + )) + db.send_create_signal('vendors', ['Vendor']) + + # Adding model 'VendorUser' + db.create_table('vendors_vendoruser', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('user_type', self.gf('django.db.models.fields.CharField')(default='', max_length=1)), + ('user', self.gf('django.db.models.fields.related.ForeignKey')(related_name='vendors_vendoruser', to=orm['auth.User'])), + ('organization', self.gf('django.db.models.fields.related.ForeignKey')(related_name='organization_users', to=orm['vendors.Vendor'])), + )) + db.send_create_signal('vendors', ['VendorUser']) + + # Adding unique constraint on 'VendorUser', fields ['user', 'organization'] + db.create_unique('vendors_vendoruser', ['user_id', 'organization_id']) + + # Adding M2M table for field permissions on 'VendorUser' + m2m_table_name = db.shorten_name('vendors_vendoruser_permissions') + db.create_table(m2m_table_name, ( + ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), + ('vendoruser', models.ForeignKey(orm['vendors.vendoruser'], null=False)), + ('permission', models.ForeignKey(orm['auth.permission'], null=False)) + )) + db.create_unique(m2m_table_name, ['vendoruser_id', 'permission_id']) + + # Adding model 'VendorOwner' + db.create_table('vendors_vendorowner', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('organization_user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['vendors.VendorUser'], unique=True)), + ('organization', self.gf('django.db.models.fields.related.OneToOneField')(related_name='owner', unique=True, to=orm['vendors.Vendor'])), + )) + db.send_create_signal('vendors', ['VendorOwner']) + + + def backwards(self, orm): + # Removing unique constraint on 'VendorUser', fields ['user', 'organization'] + db.delete_unique('vendors_vendoruser', ['user_id', 'organization_id']) + + # Deleting model 'Vendor' + db.delete_table('vendors_vendor') + + # Deleting model 'VendorUser' + db.delete_table('vendors_vendoruser') + + # Removing M2M table for field permissions on 'VendorUser' + db.delete_table(db.shorten_name('vendors_vendoruser_permissions')) + + # Deleting model 'VendorOwner' + db.delete_table('vendors_vendorowner') + + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + 'vendors.vendor': { + 'Meta': {'ordering': "['name']", 'object_name': 'Vendor'}, + 'city': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}), + 'street_address': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100'}), + 'users': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'vendors_vendor'", 'symmetrical': 'False', 'through': "orm['vendors.VendorUser']", 'to': "orm['auth.User']"}) + }, + 'vendors.vendorowner': { + 'Meta': {'object_name': 'VendorOwner'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'owner'", 'unique': 'True', 'to': "orm['vendors.Vendor']"}), + 'organization_user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['vendors.VendorUser']", 'unique': 'True'}) + }, + 'vendors.vendoruser': { + 'Meta': {'ordering': "['organization', 'user']", 'unique_together': "(('user', 'organization'),)", 'object_name': 'VendorUser'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'organization_users'", 'to': "orm['vendors.Vendor']"}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'vendors_vendoruser'", 'to': "orm['auth.User']"}), + 'user_type': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1'}) + } + } + + complete_apps = ['vendors'] \ No newline at end of file diff --git a/example/vendors/migrations/__init__.py b/example/vendors/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/example/vendors/models.py b/example/vendors/models.py index 9cc9a928..3a27d35d 100644 --- a/example/vendors/models.py +++ b/example/vendors/models.py @@ -1,14 +1,17 @@ +from django.contrib.auth.models import Permission from django.db import models from organizations.base import (OrganizationBase, OrganizationUserBase, OrganizationOwnerBase) class Vendor(OrganizationBase): - monthly_subscription = models.IntegerField(default=1000) + street_address = models.CharField(max_length=100, default='') + city = models.CharField(max_length=100, default='') class VendorUser(OrganizationUserBase): user_type = models.CharField(max_length=1, default='') + permissions = models.ManyToManyField(Permission, blank=True) class VendorOwner(OrganizationOwnerBase): diff --git a/organizations/base.py b/organizations/base.py index cf6e2cc8..704b7e3f 100644 --- a/organizations/base.py +++ b/organizations/base.py @@ -1,8 +1,6 @@ from django.conf import settings -from django.core.exceptions import ImproperlyConfigured from django.db import models from django.db.models.fields import FieldDoesNotExist -from django.db.models import get_model from django.db.models.base import ModelBase from django.utils.translation import ugettext_lazy as _ @@ -11,19 +9,6 @@ USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User') -def get_user_model(): - """ - Returns the chosen user model as a class. This functionality is not - available in Django 1.4.x. - """ - try: - klass = get_model(USER_MODEL.split('.')[0], USER_MODEL.split('.')[1]) - except: - raise ImproperlyConfigured("Your user class, {0}," - " is improperly defined".format(USER_MODEL)) - return klass - - class OrgMeta(ModelBase): """ Base metaclass for dynamically linking related organization models. @@ -139,9 +124,6 @@ def __unicode__(self): def is_member(self, user): return True if user in self.users.all() else False - def is_admin(self, user): - return True if self.organization_users.filter(user=user, is_admin=True) else False - class OrganizationUserBase(models.Model): """ @@ -166,6 +148,16 @@ def __unicode__(self): return u"{0} ({1})".format(self.user.get_full_name() if self.user.is_active else self.user.email, self.organization.name) + @property + def name(self): + """ + Returns the connected user's full name or string representation if the + full name method is unavailable (e.g. on a custom user class). + """ + if hasattr(self.user, 'get_full_name'): + return self.user.get_full_name() + return "{0}".format(self.user) + class OrganizationOwnerBase(models.Model): """Each organization must have one and only one organization owner.""" @@ -173,3 +165,6 @@ class OrganizationOwnerBase(models.Model): class Meta: abstract = True + + def __unicode__(self): + return u"{0}: {1}".format(self.organization, self.organization_user) diff --git a/organizations/models.py b/organizations/models.py index a95c63c8..602182ca 100644 --- a/organizations/models.py +++ b/organizations/models.py @@ -15,7 +15,7 @@ def get_user_model(): """ Returns the chosen user model as a class. This functionality won't be - built-in until Django 1.5. + builtin until Django 1.5. """ try: klass = get_model(USER_MODEL.split('.')[0], USER_MODEL.split('.')[1]) @@ -33,8 +33,7 @@ class Organization(OrganizationBase, TimeStampedModel): populate_from='name', unique=True, help_text=_("The name in all lowercase, suitable for URL identification")) - class Meta: - ordering = ['name'] + class Meta(OrganizationBase.Meta): verbose_name = _("organization") verbose_name_plural = _("organizations") @@ -88,9 +87,6 @@ def get_or_add_user(self, user, **kwargs): return org_user, created - def is_member(self, user): - return True if user in self.users.all() else False - def is_admin(self, user): return True if self.organization_users.filter(user=user, is_admin=True) else False @@ -99,7 +95,6 @@ class OrganizationUser(OrganizationUserBase, TimeStampedModel): is_admin = models.BooleanField(default=False) class Meta(OrganizationUserBase.Meta): - ordering = ['organization', 'user'] verbose_name = _("organization user") verbose_name_plural = _("organization users") @@ -129,12 +124,6 @@ def get_absolute_url(self): return ('organization_user_detail', (), {'organization_pk': self.organization.pk, 'user_pk': self.user.pk}) - @property - def name(self): - if hasattr(self.user, 'get_full_name'): - return self.user.get_full_name() - return "{0}".format(self.user) - class OrganizationOwner(OrganizationOwnerBase, TimeStampedModel): @@ -142,9 +131,6 @@ class Meta: verbose_name = _("organization owner") verbose_name_plural = _("organization owners") - def __unicode__(self): - return u"{0}: {1}".format(self.organization, self.organization_user) - def save(self, *args, **kwargs): """ Extends the default save method by verifying that the chosen diff --git a/runtests.py b/runtests.py index 72501939..a82ba97b 100755 --- a/runtests.py +++ b/runtests.py @@ -19,6 +19,12 @@ "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sites", + # The ordering here, the apps using the organization base models + # first and *then* the organizations app itself is an implicit test + # that the organizations app need not be installed in order to use + # its base models. + "test_accounts", + "test_vendors", "organizations", ], SITE_ID=1, diff --git a/test_accounts/__init__.py b/test_accounts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test_accounts/migrations/0001_initial.py b/test_accounts/migrations/0001_initial.py new file mode 100644 index 00000000..a342c05f --- /dev/null +++ b/test_accounts/migrations/0001_initial.py @@ -0,0 +1,115 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Account' + db.create_table(u'test_accounts_account', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('name', self.gf('django.db.models.fields.CharField')(max_length=200)), + ('is_active', self.gf('django.db.models.fields.BooleanField')(default=True)), + ('monthly_subscription', self.gf('django.db.models.fields.IntegerField')(default=1000)), + )) + db.send_create_signal('test_accounts', ['Account']) + + # Adding model 'AccountUser' + db.create_table(u'test_accounts_accountuser', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('user_type', self.gf('django.db.models.fields.CharField')(default='', max_length=1)), + ('user', self.gf('django.db.models.fields.related.ForeignKey')(related_name='test_accounts_accountuser', to=orm['auth.User'])), + ('organization', self.gf('django.db.models.fields.related.ForeignKey')(related_name='organization_users', to=orm['test_accounts.Account'])), + )) + db.send_create_signal('test_accounts', ['AccountUser']) + + # Adding unique constraint on 'AccountUser', fields ['user', 'organization'] + db.create_unique(u'test_accounts_accountuser', [u'user_id', u'organization_id']) + + # Adding model 'AccountOwner' + db.create_table(u'test_accounts_accountowner', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('organization_user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['test_accounts.AccountUser'], unique=True)), + ('organization', self.gf('django.db.models.fields.related.OneToOneField')(related_name='owner', unique=True, to=orm['test_accounts.Account'])), + )) + db.send_create_signal('test_accounts', ['AccountOwner']) + + + def backwards(self, orm): + # Removing unique constraint on 'AccountUser', fields ['user', 'organization'] + db.delete_unique(u'test_accounts_accountuser', [u'user_id', u'organization_id']) + + # Deleting model 'Account' + db.delete_table(u'test_accounts_account') + + # Deleting model 'AccountUser' + db.delete_table(u'test_accounts_accountuser') + + # Deleting model 'AccountOwner' + db.delete_table(u'test_accounts_accountowner') + + + models = { + 'test_accounts.account': { + 'Meta': {'ordering': "['name']", 'unique_together': '()', 'object_name': 'Account', 'index_together': '()'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'monthly_subscription': ('django.db.models.fields.IntegerField', [], {'default': '1000'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}), + 'users': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'test_accounts_account'", 'symmetrical': 'False', 'through': "orm['test_accounts.AccountUser']", 'to': "orm['auth.User']"}) + }, + 'test_accounts.accountowner': { + 'Meta': {'unique_together': '()', 'object_name': 'AccountOwner', 'index_together': '()'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'owner'", 'unique': 'True', 'to': "orm['test_accounts.Account']"}), + 'organization_user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['test_accounts.AccountUser']", 'unique': 'True'}) + }, + 'test_accounts.accountuser': { + 'Meta': {'ordering': "['organization', 'user']", 'unique_together': "(('user', 'organization'),)", 'object_name': 'AccountUser', 'index_together': '()'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'organization_users'", 'to': "orm['test_accounts.Account']"}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'test_accounts_accountuser'", 'to': "orm['auth.User']"}), + 'user_type': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1'}) + }, + 'auth.group': { + 'Meta': {'unique_together': '()', 'object_name': 'Group', 'index_together': '()'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission', 'index_together': '()'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'unique_together': '()', 'object_name': 'User', 'index_together': '()'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': "orm['auth.Group']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': "orm['auth.Permission']"}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'", 'index_together': '()'}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + } + } + + complete_apps = ['test_accounts'] diff --git a/test_accounts/migrations/__init__.py b/test_accounts/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test_accounts/models.py b/test_accounts/models.py new file mode 100644 index 00000000..36e11f72 --- /dev/null +++ b/test_accounts/models.py @@ -0,0 +1,15 @@ +from django.db import models +from organizations.base import (OrganizationBase, OrganizationUserBase, + OrganizationOwnerBase) + + +class Account(OrganizationBase): + monthly_subscription = models.IntegerField(default=1000) + + +class AccountUser(OrganizationUserBase): + user_type = models.CharField(max_length=1, default='') + + +class AccountOwner(OrganizationOwnerBase): + pass diff --git a/test_vendors/__init__.py b/test_vendors/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test_vendors/migrations/0001_initial.py b/test_vendors/migrations/0001_initial.py new file mode 100644 index 00000000..2070bd25 --- /dev/null +++ b/test_vendors/migrations/0001_initial.py @@ -0,0 +1,131 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Vendor' + db.create_table('test_vendors_vendor', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('name', self.gf('django.db.models.fields.CharField')(max_length=200)), + ('is_active', self.gf('django.db.models.fields.BooleanField')(default=True)), + ('street_address', self.gf('django.db.models.fields.CharField')(default='', max_length=100)), + ('city', self.gf('django.db.models.fields.CharField')(default='', max_length=100)), + )) + db.send_create_signal('test_vendors', ['Vendor']) + + # Adding model 'VendorUser' + db.create_table('test_vendors_vendoruser', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('user_type', self.gf('django.db.models.fields.CharField')(default='', max_length=1)), + ('user', self.gf('django.db.models.fields.related.ForeignKey')(related_name='test_vendors_vendoruser', to=orm['auth.User'])), + ('organization', self.gf('django.db.models.fields.related.ForeignKey')(related_name='organization_users', to=orm['test_vendors.Vendor'])), + )) + db.send_create_signal('test_vendors', ['VendorUser']) + + # Adding unique constraint on 'VendorUser', fields ['user', 'organization'] + db.create_unique('test_vendors_vendoruser', ['user_id', 'organization_id']) + + # Adding M2M table for field permissions on 'VendorUser' + m2m_table_name = db.shorten_name('test_vendors_vendoruser_permissions') + db.create_table(m2m_table_name, ( + ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), + ('vendoruser', models.ForeignKey(orm['test_vendors.vendoruser'], null=False)), + ('permission', models.ForeignKey(orm['auth.permission'], null=False)) + )) + db.create_unique(m2m_table_name, ['vendoruser_id', 'permission_id']) + + # Adding model 'VendorOwner' + db.create_table('test_vendors_vendorowner', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('organization_user', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['test_vendors.VendorUser'], unique=True)), + ('organization', self.gf('django.db.models.fields.related.OneToOneField')(related_name='owner', unique=True, to=orm['test_vendors.Vendor'])), + )) + db.send_create_signal('test_vendors', ['VendorOwner']) + + + def backwards(self, orm): + # Removing unique constraint on 'VendorUser', fields ['user', 'organization'] + db.delete_unique('test_vendors_vendoruser', ['user_id', 'organization_id']) + + # Deleting model 'Vendor' + db.delete_table('test_vendors_vendor') + + # Deleting model 'VendorUser' + db.delete_table('test_vendors_vendoruser') + + # Removing M2M table for field permissions on 'VendorUser' + db.delete_table(db.shorten_name('test_vendors_vendoruser_permissions')) + + # Deleting model 'VendorOwner' + db.delete_table('test_vendors_vendorowner') + + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + 'test_vendors.vendor': { + 'Meta': {'ordering': "['name']", 'object_name': 'Vendor'}, + 'city': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}), + 'street_address': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100'}), + 'users': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'test_vendors_vendor'", 'symmetrical': 'False', 'through': "orm['test_vendors.VendorUser']", 'to': "orm['auth.User']"}) + }, + 'test_vendors.vendorowner': { + 'Meta': {'object_name': 'VendorOwner'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'owner'", 'unique': 'True', 'to': "orm['test_vendors.Vendor']"}), + 'organization_user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['test_vendors.VendorUser']", 'unique': 'True'}) + }, + 'test_vendors.vendoruser': { + 'Meta': {'ordering': "['organization', 'user']", 'unique_together': "(('user', 'organization'),)", 'object_name': 'VendorUser'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'organization': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'organization_users'", 'to': "orm['test_vendors.Vendor']"}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'test_vendors_vendoruser'", 'to': "orm['auth.User']"}), + 'user_type': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '1'}) + } + } + + complete_apps = ['test_vendors'] + diff --git a/test_vendors/migrations/__init__.py b/test_vendors/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test_vendors/models.py b/test_vendors/models.py new file mode 100644 index 00000000..3a27d35d --- /dev/null +++ b/test_vendors/models.py @@ -0,0 +1,18 @@ +from django.contrib.auth.models import Permission +from django.db import models +from organizations.base import (OrganizationBase, OrganizationUserBase, + OrganizationOwnerBase) + + +class Vendor(OrganizationBase): + street_address = models.CharField(max_length=100, default='') + city = models.CharField(max_length=100, default='') + + +class VendorUser(OrganizationUserBase): + user_type = models.CharField(max_length=1, default='') + permissions = models.ManyToManyField(Permission, blank=True) + + +class VendorOwner(OrganizationOwnerBase): + pass diff --git a/tests/test_models.py b/tests/test_models.py index 5ec33801..8799deeb 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,6 +7,7 @@ from organizations.models import (Organization, OrganizationUser, OrganizationOwner) +from test_accounts.models import Account @override_settings(USE_TZ=True) @@ -118,3 +119,22 @@ def test_delete_orguser(self): organization__name="Nirvana", user=krist) org_user.delete() self.assertTrue(krist.pk) + + +class CustomModelTests(TestCase): + + # Load the world as we know it. + fixtures = ['users.json', 'orgs.json'] + + def setUp(self): + self.kurt = User.objects.get(username="kurt") + self.dave = User.objects.get(username="dave") + self.krist = User.objects.get(username="krist") + self.duder = User.objects.get(username="duder") + self.red_account = Account.objects.create( + name="Red Account", + monthly_subscription=1200, + ) + + def test_org_string(self): + self.assertEqual(self.red_account.__str__(), "Red Account")