def setUp(self): super(PeriodsTestCase, self).setUp() # setup some periods self.start = datetime(2000, 1, 1, 0) self.end = self.start + timedelta(hours = 12) times = [(datetime(2000, 1, 1, 0), 5.0, "one", "S") , (datetime(2000, 1, 1, 5), 3.0, "two", "P") , (datetime(2000, 1, 1, 8), 4.0, "three", "S") ] self.ps = [] for start, dur, name, st in times: s = create_sesshun() s.name = name s.save() scheduled = dur if st == "S" else 0.0 pa = Period_Accounting(scheduled = scheduled) pa.save() state = Period_State.objects.get(abbreviation = st) p = Period( session = s , start = start , duration = dur , state = state , accounting = pa ) p.save() self.ps.append(p)
def setUp(self): super(TestProposal, self).setUp() # this project has no allotments! self.project = DSSProject.objects.order_by('pcode').all()[0] # setup some periods self.start = datetime(2000, 1, 1, 0) self.end = self.start + timedelta(hours = 12) times = [(datetime(2000, 1, 1, 0), 5.0, "one") , (datetime(2000, 1, 1, 5), 3.0, "two") , (datetime(2000, 1, 1, 8), 4.0, "three") ] self.ps = [] state = DSSPeriod_State.objects.get(abbreviation = 'P') for start, dur, name in times: # each session has grade 4, time = 3 s = create_sesshun() s.name = name s.save() pa = DSSPeriod_Accounting(scheduled = dur) pa.save() p = DSSPeriod( session = s , start = start , duration = dur , state = state , accounting = pa ) p.save() self.ps.append(p) # Okay, now set up the corresponding proposal now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") sqlResult = { 'PROP_ID' : self.project.pcode , 'PROPOSAL_TYPE' : 'Regular' , 'STATUS' : 'Draft' , 'SUBMITTED_DATE' : now , 'CREATED_DATE' : now , 'MODIFIED_DATE' : now , 'TITLE' : 'Lynrd Sknyrd' , 'ABSTRACT' : 'What song do you wanna hear?' , 'proposal_id' : 0 , 'JOINT_PROPOSAL_TYPE' : 'Not a joint Proposal' } proposal = Proposal.createFromSqlResult(sqlResult) proposal.dss_project = self.project proposal.setSemester(self.project.semester.semester) proposal.save() self.proposal = proposal self.ta = TimeAccounting()
def read(self, request, *args, **kws): if len(args) == 1: tz, = args startPeriods = request.GET.get("startPeriods", datetime.now().strftime("%Y-%m-%d")) daysPeriods = request.GET.get("daysPeriods", '14') dt = TimeAgent.str2dt(startPeriods) start = dt if tz == 'UTC' else TimeAgent.est2utc(dt) duration = int(daysPeriods) * 24 * 60 periods = Period.get_periods(start, duration) pjson = [DssPeriodHttpAdapter(p).jsondict(tz) for p in periods] return HttpResponse( json.dumps(dict(total = len(periods) , periods = pjson , success = 'ok')) , content_type = "application/json") else: tz, id = args p = Period.objects.get(id = id) return HttpResponse( json.dumps(dict(DssPeriodHttpAdapter(p).jsondict(tz) , success = 'ok')) , content_type = "application/json")
def get_maintenance_activity_set2(self): """ Returns a set of maintenance activities occuring during this group's duration, in time order. """ unscheduled_maintenance = "Unscheduled Maintenance" if not self.period or self.period.session.name == unscheduled_maintenance: mas = self.maintenance_activity_set.all() else: # To handle repeat maintenance activity objects: period = self.period # Get the templates: repeatQ = models.Q(deleted = False) \ & (models.Q(repeat_interval = 1) \ | models.Q(repeat_interval = 7) \ | models.Q(repeat_interval = 30)) \ & (models.Q(_start__lte = period.end()) \ & models.Q(repeat_end__gte = period.end())) # Get the time period start_endQ = models.Q(start__gte = period.start) \ & models.Q(start__lte = period.end()) today = TimeAgent.truncateDt(period.start) # Get other groups today. They will be used below to see # if any of them is a better fit. We must exclude any # possible emergency maintenance periods: other_groups_today = Maintenance_Activity_Group.objects\ .filter(period__start__gte = today)\ .filter(period__start__lt = today + timedelta(1))\ .exclude(id = self.id) \ .exclude(period__session__name = unscheduled_maintenance) groupQ = models.Q(group = self) dbmas = Maintenance_Activity.objects.filter(groupQ) dbrmas = Maintenance_Activity.objects.filter(repeatQ) mas = [i for i in dbmas if not i.is_repeat_template()] rmas = [i for i in dbrmas] # rmas is the list repeating activity templates that may # apply for this period. We need clones of these to # include in mas. If however there are already clones in # mas, we'll want to skip that template. We will also # skip the template if there is a better candidate # maintenance activity group on this day (by better, a # better match in time, defined by the activity's start # time being within the maintenance activity group's time # span). x = [] for i in rmas: for j in mas: if j.repeat_template == i: x.append(i) for g in other_groups_today: if i.get_start().time() >= g.get_start().time() \ and i.get_start().time() < g.get_end().time(): x.append(i) # Weekly repeats have a problem: what if the repeat falls on a # day that is not a maintenance day? Where should we put it? # One strategy is to examine the maintenance periods from 3 # days in the past to 3 days into the future. if none of # those is more suitable, we keep the weekly activity here. If # there is a tie, we favor the earlier date. This is done by # taking the modulo 7 of start - maintenance_activity.start # and mapping it to the values in 'dm'. Lowest value wins. dm = {4: 30, 5: 20, 6: 10, 0: 0, 1: 15, 2: 25, 3: 35} delta = timedelta(days = 3) today = TimeAgent.truncateDt(period.start) p = Period.get_periods_by_observing_type(today - delta, today + delta, "maintenance") for i in rmas: if i.repeat_interval > 1: start_date = TimeAgent.truncateDt(i.get_start()) diff = (today - start_date).days % i.repeat_interval if diff: # doesn't fall on this date. Is this the closest # period though? if diff > 6: # monthly not due x.append(i) else: # weekly or monthly that is due this week for j in p: if j != period: # check only other periods mod = (j.start.date() \ - start_date.date()).days \ % i.repeat_interval # Test to see if it's a better fit in # another period. and if so, don't # use here. if mod < 7 and dm[mod] < dm[diff]: x.append(i) break # Now that we have a list of templates that are not suitable, # cull the template list: for i in x: if i in rmas: rmas.remove(i) # The remaining templates may be used: for i in rmas: ma = i.clone(self) mas.append(ma) # remove all activities marked deleted. This must be done # after all the above to prevent a replacement being generated # for a deleted activity, for repeat activities. mas = [i for i in mas if not i.deleted] mas.sort(cmp = lambda x, y: cmp(x.get_start(), y.get_start())) return mas
def read(self, request, *args, **kws): tz = args[0] # one or many? if len(args) == 1: # we are getting periods from within a range of dates sortField = jsonMap.get(request.GET.get("sortField", "start"), "start") order = "-" if request.GET.get("sortDir", "ASC") == "DESC" else "" # Either filter by date, or by something else. filterWnd = request.GET.get("filterWnd", None) # make sure we have defaults for dates defStart = datetime.now().strftime("%Y-%m-%d") if filterWnd is None else None defDays = "1" if filterWnd is None else None # Filtering by date involves a pair of keywords filterWnd = request.GET.get("filterWnd", None) filterElc = request.GET.get("filterElc", None) # make sure we have defaults for dates defStart = datetime.now().strftime("%Y-%m-%d") if filterWnd is None and filterElc is None else None defDays = "1" if filterWnd is None and filterElc is None else None startPeriods = request.GET.get("startPeriods", defStart) daysPeriods = request.GET.get("daysPeriods", defDays) if startPeriods is not None and daysPeriods is not None: if startPeriods is None: startPeriods = datetime.now().strftime("%Y-%m-%d") if daysPeriods is None: daysPeriods = "1" dt = TimeAgent.str2dt(startPeriods) start = dt if tz == "UTC" else TimeAgent.est2utc(dt) duration = int(daysPeriods) * 24 * 60 periods = Period.get_periods(start, duration) else: # filter by something else query_set = Period.objects # window id # filterWnd = request.GET.get("filterWnd", None) if filterWnd is not None: wId = int(filterWnd) query_set = query_set.filter(window__id=wId) # elective id # filterElc = request.GET.get("filterElc", None) if filterElc is not None: eId = int(filterElc) query_set = query_set.filter(elective__id=eId) periods = query_set.order_by(order + sortField) return HttpResponse( json.dumps( dict(total=len(periods), periods=[PeriodHttpAdapter(p).jsondict(tz) for p in periods], success="ok") ), content_type="application/json", ) else: # we're getting a single period as specified by ID p_id = int(args[1]) # p = Period.objects.get(id = p_id) p = get_object_or_404(Period, id=p_id) adapter = PeriodHttpAdapter(p) return HttpResponse( json.dumps(dict(period=adapter.jsondict(tz), success="ok")), content_type="application/json" )