def gbt_schedule(request, *args, **kws): """ Serves up a GBT schedule page tailored for Operations. Note: This view is in either ET or UTC, database is in UTC. """ def cleanSD(startDate): try: return datetime.strptime(startDate, '%m/%d/%Y') if startDate else datetime.now() except: # Bad input? return datetime.now() timezones = ['ET', 'UTC'] # Note: we probably should have better error handling here, # but since the forms are Date Pickers and drop downs, it seems # difficult for the user to send us malformed params. # Default date, days, and timezone. Loaded from the values # saved below, or from defaults if no values were saved. if request.method == 'POST': startDate, days, timezone = (None, 5, 'ET') else: startDate, days, timezone, _ = _get_calendar_defaults(request) data = request.POST if request.method == 'POST' else request.GET timezone = data.get('tz', timezone) days = int(data.get('days', days)) startDate = cleanSD(data.get('start', startDate)) start = TimeAgent.truncateDt(startDate) end = start + timedelta(days = days) # save these values for use in 'GET' above. _save_calendar_defaults(request, start, days, timezone) requestor = get_requestor(request) supervisor_mode = True if (requestor in get_rescal_supervisors()) else False schedule = get_gbt_schedule_events(start, end, timezone) try: tzutc = Schedule_Notification.objects.latest('date').date.replace(tzinfo=UTC) pubdate = tzutc.astimezone(EST) except: pubdate = None printerFriendly = data.get('printerFriendly', None) template = 'users/schedules/schedule_friendly.html' if printerFriendly == '1' \ else 'users/schedule.html' return render_to_response( template, {'calendar' : schedule, 'day_list' : range(1, 32), 'tz_list' : timezones, 'timezone' : timezone, 'today' : datetime.now(EST), 'start' : start, 'startFmt' : start.strftime('%m/%d/%Y'), 'days' : days, 'rschedule' : Receiver_Schedule.extract_schedule(start, days), 'requestor' : requestor, 'supervisor_mode' : supervisor_mode, 'pubdate' : pubdate, })
def summary(request, *args, **kws): """ Serves up a page that allows Operations to run reconcilation reports. There are two basic reports - schedule and project. Even though it is specifically for Operations, any logged in user may view it. """ now = datetime.now() psummary = [] if request.method == 'POST': summary = request.POST.get('summary', 'schedule') project = project_search(request.POST.get('project', '')) if isinstance(project, list) and len(project) == 1: project = project[0].pcode else: project = '' month = request.POST.get('month', None) year = request.POST.get('year', None) year = int(year) if year else None if month and year: start = datetime(int(year) , [m for m in calendar.month_name].index(month) , 1) else: # Default to this month start = datetime(now.year, now.month, 1) month = calendar.month_name[start.month] year = start.year else: # Default to this month summary = 'schedule' project = '' start = datetime(now.year, now.month, 1) month = calendar.month_name[start.month] year = start.year end = datetime(start.year , start.month , calendar.monthrange(start.year, start.month)[1]) + \ timedelta(days = 1) # View is in ET, database is in UTC. Only use scheduled periods. periods = Period.in_time_range(TimeAgent.est2utc(start) , TimeAgent.est2utc(end)) if project: periods = [p for p in periods if p.isScheduled() and p.session.project.pcode == project] # Handle either schedule or project summaries. if summary == "schedule": schedule = get_gbt_schedule_events(start, end, "ET", True) url = 'users/schedule_summary.html' projects = [] receivers = {} days = {} hours = {} summary = {} else: url = 'users/project_summary.html' projects = list(set([p.session.project for p in periods])) projects.sort(lambda x, y: cmp(x.pcode, y.pcode)) receivers = {} for p in periods: rxs = receivers.get(p.session.project.pcode, []) rxs.extend([r.abbreviation for r in p.receivers.all()]) receivers[p.session.project.pcode] = rxs schedule = {} days = dict([(p.pcode, []) for p in projects]) hours = dict([(p.pcode, 0) for p in projects]) summary = dict([(c, 0) for c in Sesshun.getCategories()]) for p in periods: pstart = TimeAgent.utc2est(p.start) pend = TimeAgent.utc2est(p.end()) # Find the days this period ran within the month. day = pstart.day if pstart >= start else pend.day days[p.session.project.pcode].append(str(day)) if day != pend.day: # For multi-day periods days[p.session.project.pcode].append(str(pend.day)) # Find the duration of this period within the month. duration = min(pend, end) - max(pstart, start) hrs = duration.seconds / 3600. + duration.days * 24. hours[p.session.project.pcode] += hrs # Tally hours for various categories important to Operations. summary[p.session.getCategory()] += hrs # If just for one project, create a more detailed summary. if project: psummary.append((pstart, hrs, p.receiver_list)) return render_to_response( url , {'calendar' : schedule , 'projects' : [(p , sorted(list(set(receivers[p.pcode]))) , sorted(list(set(days[p.pcode])) , lambda x, y: cmp(int(x), int(y))) , hours[p.pcode]) for p in projects] , 'start' : start , 'months' : calendar.month_name , 'month' : month , 'years' : [y for y in xrange(2009, now.year + 2, 1)] , 'year' : year , 'summary' : [(t, summary[t]) for t in sorted(summary)] , 'project' : project , 'psummary' : psummary , 'is_logged_in': request.user.is_authenticated()})