Skip to content

Commit

Permalink
added pre_delete to detach resources from records
Browse files Browse the repository at this point in the history
  • Loading branch information
linzjax committed Oct 7, 2016
1 parent bc24cfc commit c4e03c0
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
9 changes: 8 additions & 1 deletion cadasta/party/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from organization.validators import validate_contact
from simple_history.models import HistoricalRecords

from resources.mixins import ResourceModelMixin
from resources.mixins import ResourceModelMixin, detach_object_resources
from spatial.models import SpatialUnit
from tutelary.decorators import permissioned_model

Expand Down Expand Up @@ -133,6 +133,9 @@ def ui_detail_url(self):
)


models.signals.pre_delete.connect(detach_object_resources, sender=Party)


@fix_model_for_attributes
@permissioned_model
class PartyRelationship(RandomIDModel):
Expand Down Expand Up @@ -314,6 +317,10 @@ def ui_detail_url(self):
)


models.signals.pre_delete.connect(
detach_object_resources, sender=TenureRelationship)


class TenureRelationshipType(models.Model):
"""Defines allowable tenure types."""

Expand Down
45 changes: 44 additions & 1 deletion cadasta/party/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from jsonattrs.models import Attribute, AttributeType, Schema
from core.tests.utils.cases import UserTestCase
from organization.tests.factories import ProjectFactory
from resources.tests.factories import ResourceFactory
from resources.models import ContentObject
from party.models import Party, TenureRelationshipType
from party.tests.factories import (PartyFactory, PartyRelationshipFactory,
TenureRelationshipFactory)
Expand Down Expand Up @@ -64,11 +66,26 @@ def test_ui_detail_url(self):
prj=party.project.slug,
id=party.id))

def test_detach_party_resources(self):
project = ProjectFactory.create()
party = PartyFactory.create(project=project)
resource = ResourceFactory.create(project=project)
resource.content_objects.create(
content_object=party)

assert ContentObject.objects.filter(
object_id=party.id, resource=resource).exists()
assert resource in party.resources

party.delete()
assert not ContentObject.objects.filter(
object_id=party.id, resource=resource).exists()


class PartyRelationshipTest(UserTestCase, TestCase):

def test_str(self):
project = ProjectFactory(name='TestProject')
project = ProjectFactory.create(name='TestProject')
relationship = PartyRelationshipFactory(
project=project,
party1__project=project,
Expand Down Expand Up @@ -134,6 +151,17 @@ def test_left_and_right_project_ids(self):
party2__project=project2
)

# def test_detach_party_relationship_resources(self):
# relationship = PartyRelationshipFactory()
# resource = ResourceFactory.create()
# ContentObject.objects.create(
# object_id=relationship.id,
# resource=resource,)

# relationship.delete()
# assert not ContentObject.objects.filter(
# object_id=relationship.id, resource=resource).exists()


class TenureRelationshipTest(UserTestCase, TestCase):

Expand Down Expand Up @@ -209,6 +237,21 @@ def test_ui_detail_url(self):
prj=tenurerel.project.slug,
id=tenurerel.id))

def test_detach_tenure_relationship_resources(self):
project = ProjectFactory.create()
tenure = TenureRelationshipFactory.create(project=project)
resource = ResourceFactory.create(project=project)
resource.content_objects.create(
content_object=tenure)
assert ContentObject.objects.filter(
object_id=tenure.id,
resource=resource,).exists()
assert resource in tenure.resources

tenure.delete()
assert not ContentObject.objects.filter(
object_id=tenure.id, resource=resource).exists()


class TenureRelationshipTypeTest(UserTestCase, TestCase):
"""Test TenureRelationshipType."""
Expand Down
8 changes: 8 additions & 0 deletions cadasta/resources/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ def resources(self):
def reload_resources(self):
if hasattr(self, '_resources'):
del self._resources


def detach_object_resources(sender, instance, **kwargs):
for resource in instance.resources:
content_object = resource.content_objects.get(
object_id=instance.id,
resource__project__slug=instance.project.slug)
content_object.delete()
4 changes: 3 additions & 1 deletion cadasta/spatial/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from . import messages
from .choices import TYPE_CHOICES
from .exceptions import SpatialRelationshipError
from resources.mixins import ResourceModelMixin
from resources.mixins import ResourceModelMixin, detach_object_resources
from jsonattrs.fields import JSONAttributeField
from jsonattrs.decorators import fix_model_for_attributes

Expand Down Expand Up @@ -145,6 +145,8 @@ def check_extent(sender, instance, **kwargs):
if instance.geometry:
reassign_spatial_geometry(instance)

models.signals.pre_delete.connect(detach_object_resources, sender=SpatialUnit)


class SpatialRelationshipManager(managers.BaseRelationshipManager):
"""Check conditions based on spatial unit type before creating
Expand Down
17 changes: 17 additions & 0 deletions cadasta/spatial/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from jsonattrs.models import Attribute, AttributeType, Schema
from core.tests.utils.cases import UserTestCase
from organization.tests.factories import ProjectFactory
from resources.tests.factories import ResourceFactory
from resources.models import ContentObject
from party import exceptions
from spatial.tests.factories import (SpatialUnitFactory,
SpatialRelationshipFactory)
Expand Down Expand Up @@ -96,6 +98,21 @@ def test_ui_detail_url(self):
prj=su.project.slug,
id=su.id))

def test_detach_spatial_unit_resources(self):
project = ProjectFactory.create()
su = SpatialUnitFactory.create(project=project)
resource = ResourceFactory.create(project=project)
resource.content_objects.create(
content_object=su)
assert ContentObject.objects.filter(
object_id=su.id,
resource=resource,).exists()
assert resource in su.resources

su.delete()
assert not ContentObject.objects.filter(
object_id=su.id, resource=resource).exists()


class SpatialRelationshipTest(UserTestCase, TestCase):

Expand Down
2 changes: 1 addition & 1 deletion functional_tests/pages/Project.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def click_on_add_button(self, location, success=True):
self.click_through(button, self.test.BY_ALERT)
self.click_on_close_alert_button()

def click_on_detatch_resource_button(self, location, success=True):
def click_on_detach_resource_button(self, location, success=True):
button = self.test.page_content(
"//div[contains(@class, '{}')]".format(location) +
"//button[contains(@class, 'btn-danger')]")
Expand Down
8 changes: 4 additions & 4 deletions functional_tests/projects/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_project_archived_view(self):
page.click_on_location_resource_tab()
page.click_on_add_button('active', success=False)
page.click_on_location_resource_tab()
page.click_on_detatch_resource_button('detail', success=False)
page.click_on_detach_resource_button('detail', success=False)

# Test spatial unit relationship tab
page.click_on_location_relationship_tab()
Expand All @@ -103,19 +103,19 @@ def test_project_archived_view(self):
page.click_on_edit_button(success=False)
page.click_on_delete_button(success=False)
page.click_on_add_button('detail', success=False)
page.click_on_detatch_resource_button('detail', success=False)
page.click_on_detach_resource_button('detail', success=False)

# Test party page
page.click_on_party_in_table()
page.click_on_edit_button(success=False)
page.click_on_delete_button(success=False)
page.click_on_add_button('panel-body', success=False)
page.click_on_detatch_resource_button('panel-body', success=False)
page.click_on_detach_resource_button('panel-body', success=False)

# Test project resource page
page.click_on_resources_tab()
page.click_on_add_button('panel-body', success=False)
page.click_on_detatch_resource_button('panel-body', success=False)
page.click_on_detach_resource_button('panel-body', success=False)

page.click_on_resource_in_table()
page.click_on_edit_resource_button(success=False)
Expand Down

0 comments on commit c4e03c0

Please sign in to comment.