<s> import datetime <EOL> from django . conf import settings <EOL> try : <EOL> from threadlocals . threadlocals import get_current_request <EOL> except ImportError : <EOL> get_current_request = None <EOL> from openinghours . models import OpeningHours , ClosingRules , PREMISES_MODEL <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from compat import get_model <EOL> def get_premises_model ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> app_label , model_name = PREMISES_MODEL . split ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> premises_model = get_model ( app_label = app_label , model_name = model_name ) <EOL> if premises_model is None : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % PREMISES_MODEL ) <EOL> return premises_model <EOL> Company = get_premises_model ( ) <EOL> def get_now ( ) : <EOL> """<STR_LIT>""" <EOL> if not get_current_request : <EOL> return datetime . datetime . now ( ) <EOL> request = get_current_request ( ) <EOL> if request : <EOL> openinghours_now = request . GET . get ( '<STR_LIT>' ) <EOL> if openinghours_now : <EOL> return datetime . datetime . strptime ( openinghours_now , '<STR_LIT>' ) <EOL> return datetime . datetime . now ( ) <EOL> def get_closing_rule_for_now ( location ) : <EOL> """<STR_LIT>""" <EOL> now = get_now ( ) <EOL> if location : <EOL> return ClosingRules . objects . filter ( company = location , <EOL> start__lte = now , end__gte = now ) <EOL> return Company . objects . first ( ) . closingrules_set . filter ( start__lte = now , <EOL> end__gte = now ) <EOL> def has_closing_rule_for_now ( location ) : <EOL> """<STR_LIT>""" <EOL> cr = get_closing_rule_for_now ( location ) <EOL> return cr . count ( ) <EOL> def is_open ( location , now = None ) : <EOL> """<STR_LIT>""" <EOL> if now is None : <EOL> now = get_now ( ) <EOL> if has_closing_rule_for_now ( location ) : <EOL> return False <EOL> now_time = datetime . time ( now . hour , now . minute , now . second ) <EOL> if location : <EOL> ohs = OpeningHours . objects . filter ( company = location ) <EOL> else : <EOL> ohs = Company . objects . first ( ) . openinghours_set . all ( ) <EOL> for oh in ohs : <EOL> is_open = False <EOL> if ( oh . weekday == now . isoweekday ( ) and <EOL> oh . from_hour <= now_time and <EOL> now_time <= oh . to_hour ) : <EOL> is_open = oh <EOL> if ( oh . weekday == now . isoweekday ( ) and <EOL> oh . from_hour <= now_time and <EOL> ( ( oh . to_hour < oh . from_hour ) and <EOL> ( now_time < datetime . time ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ) ) : <EOL> is_open = oh <EOL> if ( oh . weekday == ( now . isoweekday ( ) - <NUM_LIT:1> ) % <NUM_LIT:7> and <EOL> oh . from_hour >= now_time and <EOL> oh . to_hour >= now_time and <EOL> oh . to_hour < oh . from_hour ) : <EOL> is_open = oh <EOL> if is_open is not False : <EOL> return oh <EOL> return False <EOL> def next_time_open ( location ) : <EOL> """<STR_LIT>""" <EOL> if not is_open ( location ) : <EOL> now = get_now ( ) <EOL> now_time = datetime . time ( now . hour , now . minute , now . second ) <EOL> found_opening_hours = False <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> l_weekday = ( now . isoweekday ( ) + i ) % <NUM_LIT:8> <EOL> ohs = OpeningHours . objects . filter ( company = location , <EOL> weekday = l_weekday <EOL> ) . order_by ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if ohs . count ( ) : <EOL> for oh in ohs : <EOL> future_now = now + datetime . timedelta ( days = i ) <EOL> tmp_now = datetime . datetime ( future_now . year , <EOL> future_now . month , <EOL> future_now . day , <EOL> oh . from_hour . hour , <EOL> oh . from_hour . minute , <EOL> oh . from_hour . second ) <EOL> if tmp_now < now : <EOL> tmp_now = now <EOL> if is_open ( location , now = tmp_now ) : <EOL> found_opening_hours = oh <EOL> break <EOL> if found_opening_hours is not False : <EOL> return found_opening_hours , tmp_now <EOL> return False , None </s>
