<s> from openinghours . models import OpeningHours , WEEKDAYS <EOL> from openinghours . forms import Slot , time_to_str , str_to_time <EOL> from openinghours . utils import get_premises_model <EOL> from django . shortcuts import render , redirect , get_object_or_404 <EOL> from django . views . generic import DetailView <EOL> from collections import OrderedDict <EOL> class OpeningHoursEditView ( DetailView ) : <EOL> """<STR_LIT>""" <EOL> model = get_premises_model ( ) <EOL> template_name = "<STR_LIT>" <EOL> def form_prefix ( self , day_n , slot_n ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( day_n , slot_n ) <EOL> def post ( self , request , pk ) : <EOL> """<STR_LIT>""" <EOL> location = self . get_object ( ) <EOL> present_prefixes = [ x . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] for x in request . POST . keys ( ) ] <EOL> day_forms = OrderedDict ( ) <EOL> for day_no , day_name in WEEKDAYS : <EOL> for slot_no in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> prefix = self . form_prefix ( day_no , slot_no ) <EOL> if prefix not in present_prefixes : <EOL> continue <EOL> day_forms [ prefix ] = ( day_no , Slot ( request . POST , prefix = prefix ) ) <EOL> if all ( [ day_form [ <NUM_LIT:1> ] . is_valid ( ) for pre , day_form in day_forms . items ( ) ] ) : <EOL> OpeningHours . objects . filter ( company = location ) . delete ( ) <EOL> for prefix , day_form in day_forms . items ( ) : <EOL> day , form = day_form <EOL> opens , shuts = [ str_to_time ( form . cleaned_data [ x ] ) <EOL> for x in ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> if opens != shuts : <EOL> OpeningHours ( from_hour = opens , to_hour = shuts , <EOL> company = location , weekday = day ) . save ( ) <EOL> return redirect ( request . path_info ) <EOL> def get ( self , request , pk ) : <EOL> """<STR_LIT>""" <EOL> location = self . get_object ( ) <EOL> two_sets = False <EOL> closed = None <EOL> opening_hours = { } <EOL> for o in OpeningHours . objects . filter ( company = location ) : <EOL> opening_hours . setdefault ( o . weekday , [ ] ) . append ( o ) <EOL> days = [ ] <EOL> for day_no , day_name in WEEKDAYS : <EOL> if day_no not in opening_hours . keys ( ) : <EOL> if opening_hours : <EOL> closed = True <EOL> ini1 , ini2 = [ None , None ] <EOL> else : <EOL> closed = False <EOL> ini = [ { '<STR_LIT>' : time_to_str ( oh . from_hour ) , <EOL> '<STR_LIT>' : time_to_str ( oh . to_hour ) } <EOL> for oh in opening_hours [ day_no ] ] <EOL> ini += [ None ] * ( <NUM_LIT:2> - len ( ini [ : <NUM_LIT:2> ] ) ) <EOL> ini1 , ini2 = ini [ : <NUM_LIT:2> ] <EOL> if ini2 : <EOL> two_sets = True <EOL> days . append ( { <EOL> '<STR_LIT:name>' : day_name , <EOL> '<STR_LIT>' : day_no , <EOL> '<STR_LIT>' : Slot ( prefix = self . form_prefix ( day_no , <NUM_LIT:1> ) , initial = ini1 ) , <EOL> '<STR_LIT>' : Slot ( prefix = self . form_prefix ( day_no , <NUM_LIT:2> ) , initial = ini2 ) , <EOL> '<STR_LIT>' : closed <EOL> } ) <EOL> return render ( request , self . template_name , { <EOL> '<STR_LIT>' : days , <EOL> '<STR_LIT>' : two_sets , <EOL> '<STR_LIT:location>' : location , <EOL> } ) </s>
