<s> import calendar <EOL> import datetime <EOL> from django . conf import settings <EOL> from django . conf . urls . defaults import url , patterns , include <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . contenttypes . generic import GenericForeignKey <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . sites . models import Site <EOL> from django . core . exceptions import ValidationError , ObjectDoesNotExist <EOL> from django . core . validators import RegexValidator <EOL> from django . db import models <EOL> from django . db . models . query import QuerySet <EOL> from django . http import HttpResponse , Http404 <EOL> from django . utils . encoding import force_unicode <EOL> from taggit . managers import TaggableManager <EOL> from philo . contrib . julian . feedgenerator import ICalendarFeed <EOL> from philo . contrib . winer . models import FeedView <EOL> from philo . contrib . winer . feeds import registry <EOL> from philo . exceptions import ViewCanNotProvideSubpath <EOL> from philo . models import Tag , Entity , Page <EOL> from philo . models . fields import TemplateField <EOL> from philo . utils import ContentTypeRegistryLimiter <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> registry . register ( ICalendarFeed , verbose_name = "<STR_LIT>" ) <EOL> try : <EOL> DEFAULT_SITE = Site . objects . get_current ( ) <EOL> except : <EOL> DEFAULT_SITE = None <EOL> _languages = dict ( settings . LANGUAGES ) <EOL> try : <EOL> _languages [ settings . LANGUAGE_CODE ] <EOL> DEFAULT_LANGUAGE = settings . LANGUAGE_CODE <EOL> except KeyError : <EOL> try : <EOL> lang = settings . LANGUAGE_CODE . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] <EOL> _languages [ lang ] <EOL> DEFAULT_LANGUAGE = lang <EOL> except KeyError : <EOL> DEFAULT_LANGUAGE = None <EOL> location_content_type_limiter = ContentTypeRegistryLimiter ( ) <EOL> def register_location_model ( model ) : <EOL> location_content_type_limiter . register_class ( model ) <EOL> def unregister_location_model ( model ) : <EOL> location_content_type_limiter . unregister_class ( model ) <EOL> class Location ( Entity ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> slug = models . SlugField ( max_length = <NUM_LIT:255> , unique = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> register_location_model ( Location ) <EOL> class TimedModel ( models . Model ) : <EOL> start_date = models . DateField ( help_text = "<STR_LIT>" ) <EOL> start_time = models . TimeField ( blank = True , null = True , help_text = "<STR_LIT>" ) <EOL> end_date = models . DateField ( ) <EOL> end_time = models . TimeField ( blank = True , null = True ) <EOL> def is_all_day ( self ) : <EOL> return self . start_time is None and self . end_time is None <EOL> def clean ( self ) : <EOL> if bool ( self . start_time ) != bool ( self . end_time ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> if self . start_date > self . end_date or self . start_date == self . end_date and self . start_time > self . end_time : <EOL> raise ValidationError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> def get_start ( self ) : <EOL> return datetime . datetime . combine ( self . start_date , self . start_time ) if self . start_time else self . start_date <EOL> def get_end ( self ) : <EOL> return datetime . datetime . combine ( self . end_date , self . end_time ) if self . end_time else self . end_date <EOL> class Meta : <EOL> abstract = True <EOL> class EventManager ( models . Manager ) : <EOL> def get_query_set ( self ) : <EOL> return EventQuerySet ( self . model ) <EOL> class EventQuerySet ( QuerySet ) : <EOL> def upcoming ( self ) : <EOL> return self . filter ( start_date__gte = datetime . date . today ( ) ) <EOL> def current ( self ) : <EOL> return self . filter ( start_date__lte = datetime . date . today ( ) , end_date__gte = datetime . date . today ( ) ) <EOL> def single_day ( self ) : <EOL> return self . filter ( start_date__exact = models . F ( '<STR_LIT>' ) ) <EOL> def multiday ( self ) : <EOL> return self . exclude ( start_date__exact = models . F ( '<STR_LIT>' ) ) <EOL> class Event ( Entity , TimedModel ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> slug = models . SlugField ( max_length = <NUM_LIT:255> , unique_for_date = '<STR_LIT>' ) <EOL> location_content_type = models . ForeignKey ( ContentType , limit_choices_to = location_content_type_limiter , blank = True , null = True ) <EOL> location_pk = models . TextField ( blank = True ) <EOL> location = GenericForeignKey ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> description = TemplateField ( ) <EOL> tags = models . ManyToManyField ( Tag , related_name = '<STR_LIT>' , blank = True , null = True ) <EOL> parent_event = models . ForeignKey ( '<STR_LIT>' , blank = True , null = True ) <EOL> owner = models . ForeignKey ( User , related_name = '<STR_LIT>' ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> last_modified = models . DateTimeField ( auto_now = True ) <EOL> site = models . ForeignKey ( Site , default = DEFAULT_SITE ) <EOL> @ property <EOL> def uuid ( self ) : <EOL> return "<STR_LIT>" % ( self . created . isoformat ( ) , getattr ( self . site , '<STR_LIT>' , '<STR_LIT:None>' ) ) <EOL> objects = EventManager ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Calendar ( Entity ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> slug = models . SlugField ( max_length = <NUM_LIT:100> ) <EOL> description = models . TextField ( blank = True ) <EOL> events = models . ManyToManyField ( Event , related_name = '<STR_LIT>' , blank = True ) <EOL> site = models . ForeignKey ( Site , default = DEFAULT_SITE ) <EOL> language = models . CharField ( max_length = <NUM_LIT:5> , choices = settings . LANGUAGES , default = DEFAULT_LANGUAGE ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> @ property <EOL> def fpi ( self ) : <EOL> return "<STR_LIT>" % ( self . site . name , self . name , self . language . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] . upper ( ) ) <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class CalendarView ( FeedView ) : <EOL> calendar = models . ForeignKey ( Calendar ) <EOL> index_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" ) <EOL> event_detail_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" ) <EOL> timespan_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> tag_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> location_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> owner_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> tag_archive_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> location_archive_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> owner_archive_page = models . ForeignKey ( Page , related_name = "<STR_LIT>" , blank = True , null = True ) <EOL> tag_permalink_base = models . CharField ( max_length = <NUM_LIT:30> , default = '<STR_LIT>' ) <EOL> owner_permalink_base = models . CharField ( max_length = <NUM_LIT:30> , default = '<STR_LIT>' ) <EOL> location_permalink_base = models . CharField ( max_length = <NUM_LIT:30> , default = '<STR_LIT>' ) <EOL> events_per_page = models . PositiveIntegerField ( blank = True , null = True ) <EOL> item_context_var = "<STR_LIT>" <EOL> object_attr = "<STR_LIT>" <EOL> def get_reverse_params ( self , obj ) : <EOL> if isinstance ( obj , User ) : <EOL> return '<STR_LIT>' , [ ] , { '<STR_LIT:username>' : obj . username } <EOL> elif isinstance ( obj , Event ) : <EOL> return '<STR_LIT>' , [ ] , { <EOL> '<STR_LIT>' : str ( obj . start_date . year ) . zfill ( <NUM_LIT:4> ) , <EOL> '<STR_LIT>' : str ( obj . start_date . month ) . zfill ( <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : str ( obj . start_date . day ) . zfill ( <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : obj . slug <EOL> } <EOL> elif isinstance ( obj , Tag ) or isinstance ( obj , models . query . QuerySet ) and obj . model == Tag : <EOL> if isinstance ( obj , Tag ) : <EOL> obj = [ obj ] <EOL> return '<STR_LIT>' , [ ] , { '<STR_LIT>' : '<STR_LIT:/>' . join ( obj ) } <EOL> raise ViewCanNotProvideSubpath <EOL> def timespan_patterns ( self , pattern , timespan_name ) : <EOL> return self . feed_patterns ( pattern , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" % timespan_name ) <EOL> @ property <EOL> def urlpatterns ( self ) : <EOL> urlpatterns = self . feed_patterns ( r'<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:index>' ) + self . timespan_patterns ( r'<STR_LIT>' , '<STR_LIT>' ) + self . timespan_patterns ( r'<STR_LIT>' , '<STR_LIT>' ) + self . timespan_patterns ( r'<STR_LIT>' , '<STR_LIT>' ) + self . feed_patterns ( r'<STR_LIT>' % self . owner_permalink_base , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) + self . feed_patterns ( r'<STR_LIT>' % self . location_permalink_base , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) + self . feed_patterns ( r'<STR_LIT>' % self . tag_permalink_base , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) + patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , self . event_detail_view , name = "<STR_LIT>" ) , <EOL> ) <EOL> if self . tag_archive_page_id : <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % self . tag_permalink_base , self . tag_archive_view , name = '<STR_LIT>' ) <EOL> ) <EOL> if self . owner_archive_page_id : <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % self . owner_permalink_base , self . owner_archive_view , name = '<STR_LIT>' ) <EOL> ) <EOL> if self . location_archive_page_id : <EOL> urlpatterns += patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' % self . location_permalink_base , self . location_archive_view , name = '<STR_LIT>' ) <EOL> ) <EOL> return urlpatterns <EOL> def get_event_queryset ( self ) : <EOL> return self . calendar . events . all ( ) <EOL> def get_timespan_queryset ( self , year , month = None , day = None ) : <EOL> qs = self . get_event_queryset ( ) <EOL> if year and month and day : <EOL> year , month , day = int ( year ) , int ( month ) , int ( day ) <EOL> start_datetime = datetime . datetime ( year , month , day , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> end_datetime = datetime . datetime ( year , month , day , <NUM_LIT> , <NUM_LIT> ) <EOL> elif year and month : <EOL> year , month = int ( year ) , int ( month ) <EOL> start_datetime = datetime . datetime ( year , month , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> end_datetime = datetime . datetime ( year , month , calendar . monthrange ( year , month ) [ <NUM_LIT:1> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> else : <EOL> year = int ( year ) <EOL> start_datetime = datetime . datetime ( year , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> end_datetime = datetime . datetime ( year , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> return qs . exclude ( end_date__lt = start_datetime , end_time__lt = start_datetime ) . exclude ( start_date__gt = end_datetime , start_time__gt = end_datetime , start_time__isnull = False ) . exclude ( start_time__isnull = True , start_date__gt = end_datetime ) <EOL> def get_tag_queryset ( self ) : <EOL> return Tag . objects . filter ( events__calendars = self . calendar ) . distinct ( ) <EOL> def get_location_querysets ( self ) : <EOL> location_map = { } <EOL> locations = Event . objects . values_list ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for ct , pk in locations : <EOL> location_map . setdefault ( ct , [ ] ) . append ( pk ) <EOL> location_cts = ContentType . objects . in_bulk ( location_map . keys ( ) ) <EOL> location_querysets = { } <EOL> for ct_pk , pks in location_map . items ( ) : <EOL> ct = location_cts [ ct_pk ] <EOL> location_querysets [ ct ] = ct . model_class ( ) . _default_manager . filter ( pk__in = pks ) <EOL> return location_querysets <EOL> def get_owner_queryset ( self ) : <EOL> return User . objects . filter ( owned_events__calendars = self . calendar ) . distinct ( ) <EOL> def get_all_events ( self , request , extra_context = None ) : <EOL> return self . get_event_queryset ( ) , extra_context <EOL> def get_events_by_timespan ( self , request , year , month = None , day = None , extra_context = None ) : <EOL> context = extra_context or { } <EOL> context . update ( { <EOL> '<STR_LIT>' : year , <EOL> '<STR_LIT>' : month , <EOL> '<STR_LIT>' : day <EOL> } ) <EOL> return self . get_timespan_queryset ( year , month , day ) , context <EOL> def get_events_by_owner ( self , request , username , extra_context = None ) : <EOL> try : <EOL> owner = self . get_owner_queryset ( ) . get ( username = username ) <EOL> except User . DoesNotExist : <EOL> raise Http404 <EOL> qs = self . get_event_queryset ( ) . filter ( owner = owner ) <EOL> context = extra_context or { } <EOL> context . update ( { <EOL> '<STR_LIT>' : owner <EOL> } ) <EOL> return qs , context <EOL> def get_events_by_tag ( self , request , tag_slugs , extra_context = None ) : <EOL> tag_slugs = tag_slugs . replace ( '<STR_LIT:+>' , '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> tags = self . get_tag_queryset ( ) . filter ( slug__in = tag_slugs ) <EOL> if not tags : <EOL> raise Http404 <EOL> found_slugs = [ tag . slug for tag in tags ] <EOL> for slug in tag_slugs : <EOL> if slug and slug not in found_slugs : <EOL> raise Http404 <EOL> events = self . get_event_queryset ( ) <EOL> for tag in tags : <EOL> events = events . filter ( tags = tag ) <EOL> context = extra_context or { } <EOL> context . update ( { '<STR_LIT>' : tags } ) <EOL> return events , context <EOL> def get_events_by_location ( self , request , app_label , model , pk , extra_context = None ) : <EOL> try : <EOL> ct = ContentType . objects . get_by_natural_key ( app_label , model ) <EOL> location = ct . model_class ( ) . _default_manager . get ( pk = pk ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 <EOL> events = self . get_event_queryset ( ) . filter ( location_content_type = ct , location_pk = location . pk ) <EOL> context = extra_context or { } <EOL> context . update ( { <EOL> '<STR_LIT:location>' : location <EOL> } ) <EOL> return events , context <EOL> def event_detail_view ( self , request , year , month , day , slug , extra_context = None ) : <EOL> try : <EOL> event = Event . objects . select_related ( '<STR_LIT>' ) . get ( start_date__year = year , start_date__month = month , start_date__day = day , slug = slug ) <EOL> except Event . DoesNotExist : <EOL> raise Http404 <EOL> context = self . get_context ( ) <EOL> context . update ( extra_context or { } ) <EOL> context . update ( { <EOL> '<STR_LIT>' : event <EOL> } ) <EOL> return self . event_detail_page . render_to_response ( request , extra_context = context ) <EOL> def tag_archive_view ( self , request , extra_context = None ) : <EOL> tags = self . get_tag_queryset ( ) <EOL> context = self . get_context ( ) <EOL> context . update ( extra_context or { } ) <EOL> context . update ( { <EOL> '<STR_LIT>' : tags <EOL> } ) <EOL> return self . tag_archive_page . render_to_response ( request , extra_context = context ) <EOL> def location_archive_view ( self , request , extra_context = None ) : <EOL> locations = self . get_location_querysets ( ) <EOL> context = self . get_context ( ) <EOL> context . update ( extra_context or { } ) <EOL> context . update ( { <EOL> '<STR_LIT>' : locations <EOL> } ) <EOL> return self . location_archive_page . render_to_response ( request , extra_context = context ) <EOL> def owner_archive_view ( self , request , extra_context = None ) : <EOL> owners = self . get_owner_queryset ( ) <EOL> context = self . get_context ( ) <EOL> context . update ( extra_context or { } ) <EOL> context . update ( { <EOL> '<STR_LIT>' : owners <EOL> } ) <EOL> return self . owner_archive_page . render_to_response ( request , extra_context = context ) <EOL> def process_page_items ( self , request , items ) : <EOL> if self . events_per_page : <EOL> page_num = request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> paginator , paginated_page , items = paginate ( items , self . events_per_page , page_num ) <EOL> item_context = { <EOL> '<STR_LIT>' : paginator , <EOL> '<STR_LIT>' : paginated_page , <EOL> self . item_context_var : items <EOL> } <EOL> else : <EOL> item_context = { <EOL> self . item_context_var : items <EOL> } <EOL> return items , item_context <EOL> def title ( self , obj ) : <EOL> return obj . name <EOL> def link ( self , obj ) : <EOL> return "<STR_LIT>" <EOL> def feed_guid ( self , obj ) : <EOL> return obj . fpi <EOL> def description ( self , obj ) : <EOL> return obj . description <EOL> def feed_extra_kwargs ( self , obj ) : <EOL> return { '<STR_LIT:filename>' : "<STR_LIT>" % obj . slug } <EOL> def item_title ( self , item ) : <EOL> return item . name <EOL> def item_description ( self , item ) : <EOL> return item . description <EOL> def item_link ( self , item ) : <EOL> return self . reverse ( item ) <EOL> def item_guid ( self , item ) : <EOL> return item . uuid <EOL> def item_author_name ( self , item ) : <EOL> if item . owner : <EOL> return item . owner . get_full_name ( ) <EOL> def item_author_email ( self , item ) : <EOL> return getattr ( item . owner , '<STR_LIT:email>' , None ) or None <EOL> def item_pubdate ( self , item ) : <EOL> return item . created <EOL> def item_categories ( self , item ) : <EOL> return [ tag . name for tag in item . tags . all ( ) ] <EOL> def item_extra_kwargs ( self , item ) : <EOL> return { <EOL> '<STR_LIT:start>' : item . get_start ( ) , <EOL> '<STR_LIT:end>' : item . get_end ( ) , <EOL> '<STR_LIT>' : item . last_modified , <EOL> '<STR_LIT:location>' : force_unicode ( item . location ) , <EOL> } <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . __class__ . __name__ , self . calendar ) <EOL> field = CalendarView . _meta . get_field ( '<STR_LIT>' ) <EOL> field . default = registry . get_slug ( ICalendarFeed , field . default ) </s>
