Skip to content

Commit

Permalink
Move org user uniqueness constraint to base model
Browse files Browse the repository at this point in the history
The unique constraint between a user and organization should be
maintained in every organization.
  • Loading branch information
bennylope committed May 13, 2014
1 parent 31a0125 commit abd10e6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions organizations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class OrganizationBase(models.Model):

class Meta:
abstract = True
ordering = ['name']

def __unicode__(self):
return self.name
Expand Down Expand Up @@ -158,6 +159,8 @@ class relates the OrganizationUser to the User model using a ForeignKey

class Meta:
abstract = True
ordering = ['organization', 'user']
unique_together = ('user', 'organization')

def __unicode__(self):
return u"{0} ({1})".format(self.user.get_full_name() if self.user.is_active else
Expand Down
7 changes: 3 additions & 4 deletions organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Organization(OrganizationBase, TimeStampedModel):

class Meta:
ordering = ['name']
verbose_name = _("organization") # TODO make this dynamic
verbose_name_plural = _("organizations") # TODO make this dynamic
verbose_name = _("organization")
verbose_name_plural = _("organizations")

def __unicode__(self):
return self.name
Expand Down Expand Up @@ -98,9 +98,8 @@ def is_admin(self, user):
class OrganizationUser(OrganizationUserBase, TimeStampedModel):
is_admin = models.BooleanField(default=False)

class Meta:
class Meta(OrganizationUserBase.Meta):
ordering = ['organization', 'user']
unique_together = ('user', 'organization')
verbose_name = _("organization user")
verbose_name_plural = _("organization users")

Expand Down
3 changes: 3 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-

from django.db import IntegrityError
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings
from django.contrib.auth.models import User
from django.db import IntegrityError

from organizations.models import (Organization, OrganizationUser,
OrganizationOwner)
Expand Down

0 comments on commit abd10e6

Please sign in to comment.