<s> import calendar <EOL> import itertools <EOL> import logging <EOL> from datetime import datetime , timedelta , time <EOL> from django import http <EOL> from django . db import models <EOL> from django . template . context import RequestContext <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from swingtime . models import Event , Occurrence <EOL> from swingtime import utils , forms <EOL> from swingtime . conf import settings as swingtime_settings <EOL> from dateutil import parser <EOL> if swingtime_settings . CALENDAR_FIRST_WEEKDAY is not None : <EOL> calendar . setfirstweekday ( swingtime_settings . CALENDAR_FIRST_WEEKDAY ) <EOL> def event_listing ( <EOL> request , <EOL> template = '<STR_LIT>' , <EOL> events = None , <EOL> ** extra_context <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> if events is None : <EOL> events = Event . objects . all ( ) <EOL> extra_context [ '<STR_LIT>' ] = events <EOL> return render ( request , template , extra_context ) <EOL> def event_view ( <EOL> request , <EOL> pk , <EOL> template = '<STR_LIT>' , <EOL> event_form_class = forms . EventForm , <EOL> recurrence_form_class = forms . MultipleOccurrenceForm <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> event = get_object_or_404 ( Event , pk = pk ) <EOL> event_form = recurrence_form = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if '<STR_LIT>' in request . POST : <EOL> event_form = event_form_class ( request . POST , instance = event ) <EOL> if event_form . is_valid ( ) : <EOL> event_form . save ( event ) <EOL> return http . HttpResponseRedirect ( request . path ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> recurrence_form = recurrence_form_class ( request . POST ) <EOL> if recurrence_form . is_valid ( ) : <EOL> recurrence_form . save ( event ) <EOL> return http . HttpResponseRedirect ( request . path ) <EOL> else : <EOL> return http . HttpResponseBadRequest ( '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : event , <EOL> '<STR_LIT>' : event_form or event_form_class ( instance = event ) , <EOL> '<STR_LIT>' : recurrence_form or recurrence_form_class ( initial = { '<STR_LIT>' : datetime . now ( ) } ) <EOL> } <EOL> return render ( request , template , data ) <EOL> def occurrence_view ( <EOL> request , <EOL> event_pk , <EOL> pk , <EOL> template = '<STR_LIT>' , <EOL> form_class = forms . SingleOccurrenceForm <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> occurrence = get_object_or_404 ( Occurrence , pk = pk , event__pk = event_pk ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( request . POST , instance = occurrence ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return http . HttpResponseRedirect ( request . path ) <EOL> else : <EOL> form = form_class ( instance = occurrence ) <EOL> return render ( request , template , { '<STR_LIT>' : occurrence , '<STR_LIT>' : form } ) <EOL> def add_event ( <EOL> request , <EOL> template = '<STR_LIT>' , <EOL> event_form_class = forms . EventForm , <EOL> recurrence_form_class = forms . MultipleOccurrenceForm <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> dtstart = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> event_form = event_form_class ( request . POST ) <EOL> recurrence_form = recurrence_form_class ( request . POST ) <EOL> if event_form . is_valid ( ) and recurrence_form . is_valid ( ) : <EOL> event = event_form . save ( ) <EOL> recurrence_form . save ( event ) <EOL> return http . HttpResponseRedirect ( event . get_absolute_url ( ) ) <EOL> else : <EOL> if '<STR_LIT>' in request . GET : <EOL> try : <EOL> dtstart = parser . parse ( request . GET [ '<STR_LIT>' ] ) <EOL> except ( TypeError , ValueError ) as exc : <EOL> logging . warning ( exc ) <EOL> dtstart = dtstart or datetime . now ( ) <EOL> event_form = event_form_class ( ) <EOL> recurrence_form = recurrence_form_class ( initial = { '<STR_LIT>' : dtstart } ) <EOL> return render ( <EOL> request , <EOL> template , <EOL> { '<STR_LIT>' : dtstart , '<STR_LIT>' : event_form , '<STR_LIT>' : recurrence_form } <EOL> ) <EOL> def _datetime_view ( <EOL> request , <EOL> template , <EOL> dt , <EOL> timeslot_factory = None , <EOL> items = None , <EOL> params = None <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> timeslot_factory = timeslot_factory or utils . create_timeslot_table <EOL> params = params or { } <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : dt , <EOL> '<STR_LIT>' : dt + timedelta ( days = + <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : dt + timedelta ( days = - <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : timeslot_factory ( dt , items , ** params ) <EOL> } ) <EOL> def day_view ( request , year , month , day , template = '<STR_LIT>' , ** params ) : <EOL> '''<STR_LIT>''' <EOL> dt = datetime ( int ( year ) , int ( month ) , int ( day ) ) <EOL> return _datetime_view ( request , template , dt , ** params ) <EOL> def today_view ( request , template = '<STR_LIT>' , ** params ) : <EOL> '''<STR_LIT>''' <EOL> return _datetime_view ( request , template , datetime . now ( ) , ** params ) <EOL> def year_view ( request , year , template = '<STR_LIT>' , queryset = None ) : <EOL> '''<STR_LIT>''' <EOL> year = int ( year ) <EOL> queryset = queryset . _clone ( ) if queryset is not None else Occurrence . objects . select_related ( ) <EOL> occurrences = queryset . filter ( <EOL> models . Q ( start_time__year = year ) | <EOL> models . Q ( end_time__year = year ) <EOL> ) <EOL> def group_key ( o ) : <EOL> return datetime ( <EOL> year , <EOL> o . start_time . month if o . start_time . year == year else o . end_time . month , <EOL> <NUM_LIT:1> <EOL> ) <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : year , <EOL> '<STR_LIT>' : [ ( dt , list ( o ) ) for dt , o in itertools . groupby ( occurrences , group_key ) ] , <EOL> '<STR_LIT>' : year + <NUM_LIT:1> , <EOL> '<STR_LIT>' : year - <NUM_LIT:1> <EOL> } ) <EOL> def month_view ( <EOL> request , <EOL> year , <EOL> month , <EOL> template = '<STR_LIT>' , <EOL> queryset = None <EOL> ) : <EOL> '''<STR_LIT>''' <EOL> year , month = int ( year ) , int ( month ) <EOL> cal = calendar . monthcalendar ( year , month ) <EOL> dtstart = datetime ( year , month , <NUM_LIT:1> ) <EOL> last_day = max ( cal [ - <NUM_LIT:1> ] ) <EOL> dtend = datetime ( year , month , last_day ) <EOL> queryset = queryset . _clone ( ) if queryset is not None else Occurrence . objects . select_related ( ) <EOL> occurrences = queryset . filter ( start_time__year = year , start_time__month = month ) <EOL> def start_day ( o ) : <EOL> return o . start_time . day <EOL> by_day = dict ( [ ( dt , list ( o ) ) for dt , o in itertools . groupby ( occurrences , start_day ) ] ) <EOL> data = { <EOL> '<STR_LIT>' : datetime . now ( ) , <EOL> '<STR_LIT>' : [ [ ( d , by_day . get ( d , [ ] ) ) for d in row ] for row in cal ] , <EOL> '<STR_LIT>' : dtstart , <EOL> '<STR_LIT>' : dtstart + timedelta ( days = + last_day ) , <EOL> '<STR_LIT>' : dtstart + timedelta ( days = - <NUM_LIT:1> ) , <EOL> } <EOL> return render ( request , template , data ) </s>
