<s> __author__ = '<STR_LIT>' <EOL> from django . http import HttpResponseRedirect <EOL> from django . template . response import TemplateResponse <EOL> from django . contrib . auth . decorators import login_required <EOL> from . models import Calendar , User , SchedulePolicy , SchedulePolicyRule <EOL> from django . http import Http404 <EOL> from django . views . decorators . http import require_http_methods <EOL> from django . db import IntegrityError <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib import messages <EOL> @ login_required ( ) <EOL> def list ( request ) : <EOL> policies = SchedulePolicy . objects . all ( ) <EOL> return TemplateResponse ( request , '<STR_LIT>' , { '<STR_LIT>' : policies } ) <EOL> @ login_required ( ) <EOL> def delete ( request , id ) : <EOL> try : <EOL> policy = SchedulePolicy . objects . get ( id = id ) <EOL> policy . delete ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> except Calendar . DoesNotExist : <EOL> raise Http404 <EOL> @ login_required ( ) <EOL> def new ( request ) : <EOL> try : <EOL> users = User . objects . all ( ) <EOL> except User . DoesNotExist : <EOL> users = None <EOL> try : <EOL> calendars = Calendar . objects . all ( ) <EOL> except Calendar . DoesNotExist : <EOL> calendars = None <EOL> return TemplateResponse ( request , '<STR_LIT>' , { '<STR_LIT>' : calendars , '<STR_LIT>' : users } ) <EOL> @ login_required ( ) <EOL> def edit ( request , id ) : <EOL> try : <EOL> policy = SchedulePolicy . objects . get ( id = id ) <EOL> try : <EOL> elements = SchedulePolicyRule . objects . filter ( schedule_policy = policy ) . order_by ( '<STR_LIT>' ) <EOL> except SchedulePolicyRule . DoesNotExist : <EOL> elements = None <EOL> try : <EOL> calendars = Calendar . objects . all ( ) <EOL> except Calendar . DoesNotExist : <EOL> calendars = None <EOL> try : <EOL> users = User . objects . all ( ) <EOL> except User . DoesNotExist : <EOL> users = None <EOL> return TemplateResponse ( request , '<STR_LIT>' , { '<STR_LIT>' : policy , '<STR_LIT>' : elements , <EOL> '<STR_LIT>' : calendars , '<STR_LIT>' : users } ) <EOL> except Calendar . DoesNotExist : <EOL> raise Http404 <EOL> @ login_required ( ) <EOL> @ require_http_methods ( [ "<STR_LIT:POST>" ] ) <EOL> def save ( request ) : <EOL> try : <EOL> policy = SchedulePolicy . objects . get ( id = request . POST [ '<STR_LIT:id>' ] ) <EOL> except SchedulePolicy . DoesNotExist : <EOL> policy = SchedulePolicy ( ) <EOL> policy . name = request . POST [ '<STR_LIT:name>' ] <EOL> policy . repeat_times = request . POST [ '<STR_LIT>' ] <EOL> try : <EOL> policy . save ( ) <EOL> except IntegrityError : <EOL> messages . error ( request , '<STR_LIT>' ) <EOL> if int ( request . POST [ '<STR_LIT:id>' ] ) > <NUM_LIT:0> : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , None , [ str ( request . POST [ '<STR_LIT:id>' ] ) ] ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> elements = request . POST . getlist ( '<STR_LIT>' ) <EOL> try : <EOL> SchedulePolicyRule . objects . filter ( schedule_policy = policy ) . delete ( ) <EOL> except SchedulePolicyRule . DoesNotExist : <EOL> pass <EOL> for idx , item in enumerate ( elements ) : <EOL> rule = SchedulePolicyRule ( ) <EOL> rule . schedule_policy = policy <EOL> parts = item . split ( "<STR_LIT:|>" ) <EOL> rule . escalate_after = <NUM_LIT:0> <EOL> rule . position = idx + <NUM_LIT:1> <EOL> if parts [ <NUM_LIT:0> ] == "<STR_LIT:user>" : <EOL> rule . user_id = User . objects . get ( id = parts [ <NUM_LIT:1> ] ) <EOL> rule . schedule = None <EOL> if parts [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> rule . schedule = Calendar . objects . get ( id = parts [ <NUM_LIT:1> ] ) <EOL> rule . user_id = None <EOL> try : <EOL> rule . save ( ) <EOL> except IntegrityError : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , None , [ str ( request . POST [ '<STR_LIT:id>' ] ) ] ) ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) </s>
