<s> from django . db import models <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext_lazy as _ , ugettext <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . redirects . models import Redirect <EOL> from django . core . validators import validate_slug <EOL> from django . core . exceptions import ValidationError <EOL> from app_data import AppDataField <EOL> from ella . core . box import Box <EOL> from ella . core . cache import CachedGenericForeignKey , CachedForeignKey , ContentTypeForeignKey , CategoryForeignKey <EOL> from ella . core . conf import core_settings <EOL> from ella . core . managers import ListingManager , RelatedManager , PublishableManager <EOL> from ella . core . models . main import Author , Source <EOL> from ella . core . signals import content_published , content_unpublished <EOL> from ella . utils . timezone import now , localize <EOL> def PublishableBox ( publishable , box_type , nodelist , model = None ) : <EOL> "<STR_LIT>" <EOL> if not model : <EOL> model = publishable . content_type . model_class ( ) <EOL> box_class = model . box_class <EOL> if box_class == PublishableBox : <EOL> box_class = Box <EOL> return box_class ( publishable , box_type , nodelist , model = model ) <EOL> class Publishable ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> box_class = staticmethod ( PublishableBox ) <EOL> content_type = ContentTypeForeignKey ( editable = False ) <EOL> target = CachedGenericForeignKey ( '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> category = CategoryForeignKey ( verbose_name = _ ( '<STR_LIT>' ) ) <EOL> title = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> ) <EOL> slug = models . SlugField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> , validators = [ validate_slug ] ) <EOL> authors = models . ManyToManyField ( Author , verbose_name = _ ( '<STR_LIT>' ) ) <EOL> source = CachedForeignKey ( Source , blank = True , null = True , <EOL> verbose_name = _ ( '<STR_LIT>' ) , on_delete = models . SET_NULL ) <EOL> photo = CachedForeignKey ( '<STR_LIT>' , blank = True , null = True , on_delete = models . SET_NULL , <EOL> verbose_name = _ ( '<STR_LIT>' ) ) <EOL> description = models . TextField ( _ ( '<STR_LIT>' ) , blank = True ) <EOL> published = models . BooleanField ( _ ( '<STR_LIT>' ) ) <EOL> publish_from = models . DateTimeField ( _ ( '<STR_LIT>' ) , <EOL> default = core_settings . PUBLISH_FROM_WHEN_EMPTY , db_index = True ) <EOL> publish_to = models . DateTimeField ( _ ( "<STR_LIT>" ) , null = True , blank = True ) <EOL> static = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False ) <EOL> last_updated = models . DateTimeField ( _ ( '<STR_LIT>' ) , blank = True ) <EOL> app_data = AppDataField ( default = '<STR_LIT:{}>' , editable = False ) <EOL> announced = models . BooleanField ( help_text = '<STR_LIT>' , default = False , editable = False ) <EOL> objects = PublishableManager ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , Publishable ) and self . pk == other . pk <EOL> def get_absolute_url ( self , domain = False ) : <EOL> "<STR_LIT>" <EOL> category = self . category <EOL> kwargs = { <EOL> '<STR_LIT>' : self . slug , <EOL> } <EOL> if self . static : <EOL> kwargs [ '<STR_LIT:id>' ] = self . pk <EOL> if category . tree_parent_id : <EOL> kwargs [ '<STR_LIT>' ] = category . tree_path <EOL> url = reverse ( '<STR_LIT>' , kwargs = kwargs ) <EOL> else : <EOL> url = reverse ( '<STR_LIT>' , kwargs = kwargs ) <EOL> else : <EOL> publish_from = localize ( self . publish_from ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : publish_from . year , <EOL> '<STR_LIT>' : publish_from . month , <EOL> '<STR_LIT>' : publish_from . day , <EOL> } ) <EOL> if category . tree_parent_id : <EOL> kwargs [ '<STR_LIT>' ] = category . tree_path <EOL> url = reverse ( '<STR_LIT>' , kwargs = kwargs ) <EOL> else : <EOL> url = reverse ( '<STR_LIT>' , kwargs = kwargs ) <EOL> if category . site_id != settings . SITE_ID or domain : <EOL> return '<STR_LIT>' + category . site . domain + url <EOL> return url <EOL> def get_domain_url ( self ) : <EOL> return self . get_absolute_url ( domain = True ) <EOL> def clean ( self ) : <EOL> if self . static or not self . published : <EOL> return <EOL> if not self . category_id or not self . publish_from or not self . slug : <EOL> return <EOL> qset = self . __class__ . objects . filter ( <EOL> category = self . category , <EOL> published = True , <EOL> publish_from__day = self . publish_from . day , <EOL> publish_from__month = self . publish_from . month , <EOL> publish_from__year = self . publish_from . year , <EOL> slug = self . slug <EOL> ) <EOL> if self . pk : <EOL> qset = qset . exclude ( pk = self . pk ) <EOL> if qset : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) % self . _meta . verbose_name ) <EOL> def save ( self , ** kwargs ) : <EOL> if not self . content_type_id : <EOL> self . content_type = ContentType . objects . get_for_model ( self ) <EOL> send_signal = None <EOL> old_self = None <EOL> if self . pk : <EOL> try : <EOL> old_self = self . __class__ . objects . get ( pk = self . pk ) <EOL> except Publishable . DoesNotExist : <EOL> pass <EOL> if old_self : <EOL> old_path = old_self . get_absolute_url ( ) <EOL> new_path = self . get_absolute_url ( ) <EOL> if old_path != new_path and new_path and not old_self . static : <EOL> redirect = Redirect . objects . get_or_create ( old_path = old_path , <EOL> site = self . category . site ) [ <NUM_LIT:0> ] <EOL> redirect . new_path = new_path <EOL> redirect . save ( force_update = True ) <EOL> Redirect . objects . filter ( new_path = old_path ) . exclude ( <EOL> pk = redirect . pk ) . update ( new_path = new_path ) <EOL> if old_self . is_published ( ) != self . is_published ( ) : <EOL> if self . is_published ( ) : <EOL> send_signal = content_published <EOL> self . announced = True <EOL> else : <EOL> send_signal = content_unpublished <EOL> self . announced = False <EOL> if old_self . published != self . published and self . published is False : <EOL> send_signal = content_unpublished <EOL> self . announced = False <EOL> if old_self . last_updated == old_self . publish_from and self . last_updated == old_self . last_updated : <EOL> self . last_updated = self . publish_from <EOL> elif self . is_published ( ) : <EOL> send_signal = content_published <EOL> self . announced = True <EOL> if not self . last_updated : <EOL> self . last_updated = self . publish_from <EOL> super ( Publishable , self ) . save ( ** kwargs ) <EOL> if send_signal : <EOL> send_signal . send ( sender = self . __class__ , publishable = self ) <EOL> def delete ( self ) : <EOL> url = self . get_absolute_url ( ) <EOL> Redirect . objects . filter ( new_path = url ) . delete ( ) <EOL> if self . announced : <EOL> content_unpublished . send ( sender = self . __class__ , publishable = self ) <EOL> return super ( Publishable , self ) . delete ( ) <EOL> def is_published ( self ) : <EOL> "<STR_LIT>" <EOL> cur_time = now ( ) <EOL> return self . published and cur_time > self . publish_from and ( self . publish_to is None or cur_time < self . publish_to ) <EOL> def ListingBox ( listing , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> obj = listing . publishable <EOL> return obj . box_class ( obj , * args , ** kwargs ) <EOL> class Listing ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> box_class = staticmethod ( ListingBox ) <EOL> publishable = CachedForeignKey ( Publishable , verbose_name = _ ( '<STR_LIT>' ) ) <EOL> category = CategoryForeignKey ( verbose_name = _ ( '<STR_LIT>' ) , db_index = True ) <EOL> publish_from = models . DateTimeField ( _ ( "<STR_LIT>" ) , db_index = True ) <EOL> publish_to = models . DateTimeField ( _ ( "<STR_LIT>" ) , null = True , blank = True ) <EOL> commercial = models . BooleanField ( _ ( "<STR_LIT>" ) , default = False , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> objects = ListingManager ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> try : <EOL> return ugettext ( u'<STR_LIT>' ) % { '<STR_LIT>' : self . publishable , '<STR_LIT>' : self . category } <EOL> except : <EOL> return ugettext ( '<STR_LIT>' ) <EOL> def clean ( self ) : <EOL> if not self . publishable : <EOL> return <EOL> if self . publish_from and self . publish_from < self . publishable . publish_from : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if self . publishable . publish_to : <EOL> if not self . publish_to or self . publish_to > self . publishable . publish_to : <EOL> raise ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> def get_absolute_url ( self , domain = False ) : <EOL> return self . publishable . get_absolute_url ( domain ) <EOL> def get_domain_url ( self ) : <EOL> return self . get_absolute_url ( domain = True ) <EOL> class Related ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> publishable = models . ForeignKey ( Publishable , verbose_name = _ ( '<STR_LIT>' ) ) <EOL> related_ct = ContentTypeForeignKey ( verbose_name = _ ( '<STR_LIT>' ) ) <EOL> related_id = models . IntegerField ( _ ( '<STR_LIT>' ) ) <EOL> related = CachedGenericForeignKey ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> objects = RelatedManager ( ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return _ ( u'<STR_LIT>' ) % { '<STR_LIT>' : self . publishable , '<STR_LIT>' : self . related } </s>
