Author: andrej
Date: Fri May  3 11:07:03 2013
New Revision: 1478724

URL: http://svn.apache.org/r1478724
Log:
adding bhrelations cleanup on a ticket deletion - toward BEP-0006

Modified:
    bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py
    bloodhound/trunk/bloodhound_relations/bhrelations/api.py
    bloodhound/trunk/bloodhound_relations/bhrelations/tests/api.py

Modified: bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py
URL: 
http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py?rev=1478724&r1=1478723&r2=1478724&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py (original)
+++ bloodhound/trunk/bloodhound_dashboard/bhdashboard/model.py Fri May  3 
11:07:03 2013
@@ -106,7 +106,10 @@ class ModelBase(object):
             self._data[name] = value
         else:
             dict.__setattr__(self, name, value)
-            
+
+    @classmethod
+    def get_table_name(cls):
+        return cls._meta["table_name"]
     
     def _update_from_row(self, row = None):
         """uses a provided database row to update the model"""

Modified: bloodhound/trunk/bloodhound_relations/bhrelations/api.py
URL: 
http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_relations/bhrelations/api.py?rev=1478724&r1=1478723&r2=1478724&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_relations/bhrelations/api.py (original)
+++ bloodhound/trunk/bloodhound_relations/bhrelations/api.py Fri May  3 
11:07:03 2013
@@ -25,9 +25,8 @@ from trac.core import Component, impleme
 from trac.env import IEnvironmentSetupParticipant
 from trac.db import DatabaseManager
 from trac.resource import (manager_for_neighborhood, ResourceSystem, Resource,
-                           get_resource_shortname, get_resource_description,
-                           get_resource_summary, get_relative_resource, 
Neighborhood)
-from trac.ticket import Ticket, ITicketManipulator
+                           get_resource_shortname, Neighborhood)
+from trac.ticket import Ticket, ITicketManipulator, ITicketChangeListener
 
 PLUGIN_NAME = 'Bloodhound Relations Plugin'
 
@@ -155,12 +154,9 @@ class RelationsSystem(Component):
             self,
             relation_id,
         ):
-        #TODO: some optimization can be made here by not loading relations
+        #TODO: some optimization may be added here by not loading relations
         #before actual DELETE SQL
         relation = Relation.load_by_relation_id(self.env, relation_id)
-        self._delete_relation(relation)
-
-    def _delete_relation(self, relation):
         source = relation.source
         destination = relation.destination
         relation_type = relation.type
@@ -175,6 +171,14 @@ class RelationsSystem(Component):
                 ))
                 reverted_relation.delete()
 
+    def delete_resource_relations(self, resource_instance):
+        sql = "DELETE FROM " + Relation.get_table_name() + \
+              " WHERE source=%s OR destination=%s"
+        full_resource_id = ResourceIdSerializer.get_resource_id_from_instance(
+            self.env, resource_instance)
+        with self.env.db_transaction as db:
+            db(sql, (full_resource_id, full_resource_id))
+
     def _debug_select(self):
         """The method is used for debug purposes"""
         sql = "SELECT id, source, destination, type FROM bloodhound_relations"
@@ -420,7 +424,20 @@ class ResourceIdSerializer(object):
 
 
 class TicketRelationsSpecifics(Component):
-    implements(ITicketManipulator)
+    implements(ITicketManipulator, ITicketChangeListener)
+
+    #ITicketChangeListener methods
+
+    def ticket_created(self, ticket):
+        pass
+
+    def ticket_changed(self, ticket, comment, author, old_values):
+        pass
+
+    def ticket_deleted(self, ticket):
+        RelationsSystem(self.env).delete_resource_relations(ticket)
+
+    #ITicketMnimpulator methods
 
     def prepare_ticket(self, req, ticket, fields, actions):
         pass

Modified: bloodhound/trunk/bloodhound_relations/bhrelations/tests/api.py
URL: 
http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_relations/bhrelations/tests/api.py?rev=1478724&r1=1478723&r2=1478724&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_relations/bhrelations/tests/api.py (original)
+++ bloodhound/trunk/bloodhound_relations/bhrelations/tests/api.py Fri May  3 
11:07:03 2013
@@ -345,7 +345,23 @@ class ApiTestCase(unittest.TestCase):
         #assert
         self.assertEqual(0, len(list(warnings)))
 
-    #todo: add tests that relations are deleted when ticket was deleted
+    def test_that_relations_are_deleted_when_ticket_is_deleted(self):
+        #arrange
+        ticket1 = self._insert_and_load_ticket("A1")
+        ticket2 = self._insert_and_load_ticket("A2")
+        relations_system = self.relations_system
+        relations_system.add(ticket1, ticket2, "dependent")
+        self.assertEqual(1, len(relations_system.get_relations(ticket2)))
+        #act
+        ticket1.delete()
+        #assert
+        self.assertEqual(0, len(relations_system.get_relations(ticket2)))
+
+    def test_that_no_error_when_deleting_ticket_without_relations(self):
+        #arrange
+        ticket1 = self._insert_and_load_ticket("A1")
+        #act
+        ticket1.delete()
 
     #todo: add multi-product ticket relations test
 


Reply via email to