<s> from __future__ import unicode_literals <EOL> from datetime import timedelta , date , datetime <EOL> from django . utils . timezone import make_aware , get_default_timezone <EOL> from django . utils . six . moves import xrange <EOL> from happenings . utils . common import inc_month <EOL> class UpcomingEvents ( object ) : <EOL> def __init__ ( self , event , now , finish , num = <NUM_LIT:5> ) : <EOL> self . event = event <EOL> self . finish = finish <EOL> self . now = now <EOL> self . num = num <EOL> self . events = [ ] <EOL> def get_upcoming_events ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . event . repeats ( '<STR_LIT>' ) : <EOL> has_ended = False <EOL> now_gt_start = self . now > self . event . l_start_date <EOL> now_gt_end = self . now > self . event . end_date <EOL> if now_gt_end or now_gt_start : <EOL> has_ended = True <EOL> has_not_started = self . event . l_start_date > self . finish <EOL> if has_ended or has_not_started : <EOL> return self . events <EOL> self . events . append ( ( self . event . l_start_date , self . event ) ) <EOL> return self . events <EOL> if self . event . repeats ( '<STR_LIT>' ) : <EOL> self . _weekday ( ) <EOL> elif self . event . repeats ( '<STR_LIT>' ) : <EOL> self . _monthly ( ) <EOL> elif self . event . repeats ( '<STR_LIT>' ) : <EOL> self . _yearly ( ) <EOL> else : <EOL> self . _others ( ) <EOL> return self . events <EOL> def we_should_stop ( self , start , start_ ) : <EOL> """<STR_LIT>""" <EOL> if start > self . finish or self . event . end_repeat is not None and start_ > self . event . end_repeat : <EOL> return True <EOL> else : <EOL> return False <EOL> def _yearly ( self ) : <EOL> num = self . num <EOL> year = self . now . year <EOL> if self . event . l_start_date > self . now : <EOL> year = self . event . l_start_date . year <EOL> elif self . now . month > self . event . l_start_date . month : <EOL> year += <NUM_LIT:1> <EOL> elif self . now . month == self . event . l_start_date . month : <EOL> if self . now . day > self . event . l_start_date . day or self . now . day == self . event . l_start_date . day and self . now . time ( ) > self . event . l_start_date . time ( ) : <EOL> year += <NUM_LIT:1> <EOL> month = self . event . l_start_date . month <EOL> day = self . event . l_start_date . day <EOL> while num : <EOL> try : <EOL> start = make_aware ( <EOL> datetime ( year , month , day ) , get_default_timezone ( ) <EOL> ) <EOL> start_ = date ( year , month , day ) <EOL> except ValueError : <EOL> year += <NUM_LIT:1> <EOL> continue <EOL> if self . we_should_stop ( start , start_ ) : <EOL> return <EOL> self . events . append ( ( start , self . event ) ) <EOL> year += <NUM_LIT:1> <EOL> num -= <NUM_LIT:1> <EOL> def _monthly ( self ) : <EOL> num = self . num <EOL> start = self . event . l_start_date <EOL> year = self . now . year <EOL> month = self . now . month <EOL> day = self . event . l_start_date . day <EOL> if self . event . l_start_date > self . now : <EOL> year = self . event . l_start_date . year <EOL> month = self . event . l_start_date . month <EOL> while num : <EOL> try : <EOL> start = start . replace ( year = year , month = month ) <EOL> if self . now > start : <EOL> month , year = inc_month ( month , year ) <EOL> continue <EOL> start_ = date ( year , month , day ) <EOL> except ValueError : <EOL> month , year = inc_month ( month , year ) <EOL> continue <EOL> if self . we_should_stop ( start , start_ ) : <EOL> return <EOL> self . events . append ( ( start , self . event ) ) <EOL> month , year = inc_month ( month , year ) <EOL> num -= <NUM_LIT:1> <EOL> def _weekday ( self ) : <EOL> start = self . event . l_start_date <EOL> if not self . event . l_start_date > self . now : <EOL> start = start . replace ( <EOL> year = self . now . year , month = self . now . month , day = self . now . day <EOL> ) <EOL> while start . weekday ( ) > <NUM_LIT:4> : <EOL> start += timedelta ( days = <NUM_LIT:1> ) <EOL> if start < self . now : <EOL> start += timedelta ( days = <NUM_LIT:1> ) <EOL> while start . weekday ( ) > <NUM_LIT:4> : <EOL> start += timedelta ( days = <NUM_LIT:1> ) <EOL> for i in xrange ( self . num ) : <EOL> start_ = date ( start . year , start . month , start . day ) <EOL> if self . we_should_stop ( start , start_ ) : <EOL> return <EOL> while start . weekday ( ) > <NUM_LIT:4> : <EOL> start += timedelta ( days = <NUM_LIT:1> ) <EOL> self . events . append ( ( start , self . event ) ) <EOL> start += timedelta ( days = <NUM_LIT:1> ) <EOL> def _others ( self ) : <EOL> repeat = { '<STR_LIT>' : <NUM_LIT:7> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> if self . event . repeats ( '<STR_LIT>' ) : <EOL> if self . event . l_start_date > self . now : <EOL> start = self . event . l_start_date <EOL> elif self . event . l_start_date . time ( ) < self . now . time ( ) : <EOL> start = self . now <EOL> start += timedelta ( days = <NUM_LIT:1> ) <EOL> else : <EOL> start = self . now <EOL> else : <EOL> start = self . event . l_start_date <EOL> end = self . event . l_end_date <EOL> while start < self . now or end <= self . now : <EOL> start += timedelta ( days = repeat [ self . event . repeat ] ) <EOL> end += timedelta ( days = repeat [ self . event . repeat ] ) <EOL> for i in xrange ( self . num ) : <EOL> start_ = date ( start . year , start . month , start . day ) <EOL> if self . we_should_stop ( start , start_ ) : <EOL> return <EOL> self . events . append ( ( start , self . event ) ) <EOL> start += timedelta ( days = repeat [ self . event . repeat ] ) </s>
