Permalink
Browse files

adapt ManyToMany actions to flushing the complete session

  • Loading branch information...
erikjanss committed Sep 5, 2012
1 parent 0bcab9a commit b539e2fe4ac28165728e7e0152151da7dc184147
Showing with 17 additions and 16 deletions.
  1. +2 −2 camelot/admin/action/list_action.py
  2. +13 −10 camelot/view/proxy/collection_proxy.py
  3. +2 −4 camelot_example/model.py
@@ -708,7 +708,7 @@ def model_run( self, model_context ):
for obj in model_context.get_collection():
if obj_to_add == obj:
raise StopIteration()
- model_context._model.append_object( obj_to_add )
+ model_context._model.append_object( obj_to_add, flush = False )
yield action_steps.FlushSession( object_session( obj_to_add ) )
class AddNewObject( OpenNewView ):
@@ -740,6 +740,6 @@ def model_run( self, model_context ):
objects_to_remove = list( model_context.get_selection() )
if len( objects_to_remove ):
session = object_session( objects_to_remove[0] )
- model_context._model.remove_objects( objects_to_remove, delete = False )
+ model_context._model.remove_objects( objects_to_remove, delete = False, flush = False )
yield action_steps.FlushSession( session )
@@ -1065,11 +1065,12 @@ def append( self, o ):
collection.append( o )
@model_function
- def remove_objects( self, objects_to_remove, delete = True ):
+ def remove_objects( self, objects_to_remove, delete = True, flush = True ):
"""
:param objects_to_remove: a list of objects that need to be removed
from the collection
:param delete: True if the objects need to be deleted
+ :param fulsh: True if the flush needs to occur in this method
"""
#
# it might be impossible to determine the depending objects once
@@ -1098,7 +1099,7 @@ def remove_objects( self, objects_to_remove, delete = True ):
# even if the object is not deleted, it needs to be flushed to make
# sure the persisted object is out of the collection as well
self.remove( obj )
- if self.admin.is_persistent( obj ):
+ if self.admin.is_persistent( obj ) and flush:
self.admin.flush( obj )
#
# remove the entity from the cache, only if the delete and remove
@@ -1169,11 +1170,12 @@ def _rows_inserted( self, _first, _last ):
self.endInsertRows()
@model_function
- def append_object( self, obj ):
+ def append_object( self, obj, flush = True ):
"""Append an object to this collection, set the possible defaults and flush
the object if possible/needed
:param obj: the object to be added to the collection
+ :param flush: if this object should be flushed or not
:return: the new number of rows in the collection
"""
rows = self._rows
@@ -1182,13 +1184,14 @@ def append_object( self, obj ):
self.append( obj )
# defaults might depend on object being part of a collection
self.admin.set_defaults( obj )
- self.unflushed_rows.add( row )
- if self.flush_changes and not len( self.validator.objectValidity( obj ) ):
- self.admin.flush( obj )
- try:
- self.unflushed_rows.remove( row )
- except KeyError:
- pass
+ if flush:
+ self.unflushed_rows.add( row )
+ if self.flush_changes and not len( self.validator.objectValidity( obj ) ):
+ self.admin.flush( obj )
+ try:
+ self.unflushed_rows.remove( row )
+ except KeyError:
+ pass
for depending_obj in self.admin.get_depending_objects( obj ):
self.rsh.sendEntityUpdate( self, depending_obj )
self._rows = rows + 1
View
@@ -94,10 +94,7 @@ class Movie( Entity ):
# replaced by backref on Cast class
#cast = relationship( 'Cast' )
visitor_reports = relationship( 'VisitorReport', )
- tags = ManyToMany( 'Tag',
- tablename = 'tags_movies__movies_tags',
- local_colname = 'movies_id',
- remote_colname = 'tags_id' )
+ tags = ManyToMany( 'Tag' )
# end short movie definition
#
# Camelot includes custom sqlalchemy types, like Image, which stores an
@@ -223,6 +220,7 @@ class Tag( Entity ):
__tablename__ = 'tags'
name = Column( sqlalchemy.types.Unicode(60), nullable = False )
+ movies = ManyToMany( 'Movie' )
def __unicode__( self ):
return self.name

0 comments on commit b539e2f

Please sign in to comment.