-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add project constraint check when creating relationships
- Loading branch information
Showing
8 changed files
with
183 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ProjectRelationshipError(Exception): | ||
"""Exception raised for illegal project on relationships. | ||
""" | ||
def __init__(self, msg): | ||
super().__init__(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Custom managers for project relationships.""" | ||
|
||
from django.db import models | ||
|
||
from . import exceptions | ||
|
||
|
||
class BaseRelationshipManager(models.Manager): | ||
""" | ||
Manager to provide project relationship checks. | ||
Checks that all entities belong to the same project. | ||
""" | ||
|
||
def check_project_constraints(self, project=None, left=None, right=None): | ||
"""Related entities must be in the same project.""" | ||
if (project.id != left.project.id or | ||
project.id != right.project.id or | ||
left.project.id != right.project.id): | ||
raise exceptions.ProjectRelationshipError( | ||
'Related entities are not in the same project.') | ||
|
||
|
||
class PartyRelationshipManager(BaseRelationshipManager): | ||
"""Manages PartyRelationships.""" | ||
|
||
def create(self, *args, **kwargs): | ||
"""Check that related entities are in the same project.""" | ||
project = kwargs['project'] | ||
party1 = kwargs['party1'] | ||
party2 = kwargs['party2'] | ||
self.check_project_constraints( | ||
project=project, left=party1, right=party2) | ||
return super().create(**kwargs) | ||
|
||
|
||
class TenureRelationshipManager(BaseRelationshipManager): | ||
"""Manages TenureRelationships.""" | ||
|
||
def create(self, *args, **kwargs): | ||
"""Check that related entities are in the same project.""" | ||
project = kwargs['project'] | ||
party = kwargs['party'] | ||
spatial_unit = kwargs['spatial_unit'] | ||
assert project is not None, 'Project must be set.' | ||
self.check_project_constraints( | ||
project=project, left=party, right=spatial_unit) | ||
return super().create(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.