<s> """<STR_LIT>""" <EOL> from django . db import models <EOL> from django . db import transaction <EOL> class BuildableModel ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> detail_views = [ ] <EOL> def _get_view ( self , name ) : <EOL> from django . core . urlresolvers import get_callable <EOL> return get_callable ( name ) <EOL> def _build_related ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _build_extra ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _unbuild_extra ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def build ( self ) : <EOL> """<STR_LIT>""" <EOL> for detail_view in self . detail_views : <EOL> view = self . _get_view ( detail_view ) <EOL> view ( ) . build_object ( self ) <EOL> self . _build_extra ( ) <EOL> self . _build_related ( ) <EOL> def unbuild ( self ) : <EOL> """<STR_LIT>""" <EOL> for detail_view in self . detail_views : <EOL> view = self . _get_view ( detail_view ) <EOL> view ( ) . unbuild_object ( self ) <EOL> self . _unbuild_extra ( ) <EOL> self . _build_related ( ) <EOL> def get_absolute_url ( self ) : <EOL> pass <EOL> class Meta : <EOL> abstract = True <EOL> class AutoPublishingBuildableModel ( BuildableModel ) : <EOL> """<STR_LIT>""" <EOL> publication_status_field = '<STR_LIT>' <EOL> def get_publication_status ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , self . publication_status_field ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from bakery import tasks <EOL> from django . contrib . contenttypes . models import ContentType <EOL> if not kwargs . pop ( '<STR_LIT>' , True ) : <EOL> super ( AutoPublishingBuildableModel , self ) . save ( * args , ** kwargs ) <EOL> else : <EOL> try : <EOL> preexisting = self . __class__ . objects . get ( pk = self . pk ) <EOL> except self . __class__ . DoesNotExist : <EOL> preexisting = None <EOL> if not preexisting : <EOL> if self . get_publication_status ( ) : <EOL> action = '<STR_LIT>' <EOL> else : <EOL> action = None <EOL> else : <EOL> if not self . get_publication_status ( ) and preexisting . get_publication_status ( ) : <EOL> action = '<STR_LIT>' <EOL> elif self . get_publication_status ( ) : <EOL> action = '<STR_LIT>' <EOL> else : <EOL> action = None <EOL> with transaction . atomic ( ) : <EOL> super ( AutoPublishingBuildableModel , self ) . save ( * args , ** kwargs ) <EOL> ct = ContentType . objects . get_for_model ( self . __class__ ) <EOL> if action == '<STR_LIT>' : <EOL> tasks . publish_object . delay ( ct . pk , self . pk ) <EOL> elif action == '<STR_LIT>' : <EOL> tasks . unpublish_object . delay ( ct . pk , self . pk ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from bakery import tasks <EOL> from django . contrib . contenttypes . models import ContentType <EOL> unpublish = kwargs . pop ( '<STR_LIT>' , True ) <EOL> super ( AutoPublishingBuildableModel , self ) . delete ( * args , ** kwargs ) <EOL> if unpublish : <EOL> ct = ContentType . objects . get_for_model ( self . __class__ ) <EOL> tasks . unpublish_object . delay ( ct . pk , self . pk ) <EOL> class Meta : <EOL> abstract = True </s>
