def load(self, year, month): """load calendar with data from database""" #temp = models.EventCalendar.objects.filter(owner=self.owner, # year=year, month=month) temp = models.EventCalendar.objects.all() if temp: # either 1 record or no record , check models.py if len(temp) == 1: self.db_cal = temp[0] else: for cale in temp: if cale.owner == self.owner: self.db_cal = cale self.db_events = models.RunningNight.objects.filter(cal=self.db_cal, date__year=year, date__month=month) self.curr = eventCalBase.monthCalendar(self.db_cal.id, self.owner, year, month) for db_e in self.db_events: e = eventCalBase.event(id=db_e.id, name=db_e.name, date=db_e.date, end=db_e.end, cars=db_e.cars, descr=db_e.descr, evtype=db_e.evtype) self.curr.addEvent(e, db_e.date.day) else: self.curr = eventCalBase.monthCalendar(None, self.owner, year, month)
def testDelEvent(self): """Test delEvent()""" daylist = [1, 2, 20] timelist = [ datetime.time(12, 30, 0), datetime.time(13, 0, 0), datetime.time(14, 45, 0), ] # add event id = 1 for d in daylist: for t in timelist: date = datetime.date(self.now.year, self.now.month, d) dt = datetime.datetime.combine(date, t) event = eventCalBase.event(id, 'test', dt) self.cal.addEvent(event, d) id += 1 # get event (before delete) before = [] for d in daylist: before.append(self.cal.getDailyEvents(d)) # del event and compare output for d, b in zip(daylist, before): b.pop(1) self.cal.delEvent(d, timelist[1]) expect = b got = self.cal.getDailyEvents(d) self.failUnlessEqual(expect, got, self.emsg % (expect, got))
def testDelEvent(self): """Test delEvent()""" daylist = [1,2,20] timelist = [datetime.time(12,30,0), datetime.time(13, 0,0), datetime.time(14,45,0),] # add event id = 1 for d in daylist: for t in timelist: date = datetime.date(self.now.year, self.now.month, d) dt = datetime.datetime.combine(date, t) event = eventCalBase.event(id, 'test', dt) self.cal.addEvent(event, d) id += 1 # get event (before delete) before = [] for d in daylist: before.append(self.cal.getDailyEvents(d)) # del event and compare output for d, b in zip(daylist, before): b.pop(1) self.cal.delEvent(d, timelist[1]) expect = b got = self.cal.getDailyEvents(d) self.failUnlessEqual(expect, got, self.emsg % (expect, got))
def testGreaterThanEqual(self): """Test >=operator""" _dt = self.eDatetime - datetime.timedelta(1) _other = eventCalBase.event(2, 'event', _dt) self.failUnless(operator.ge(self.e, _other), 'a1 => a2 failed. a1=%s, a2=%s' % (self.e, _other)) self.failUnless(operator.ge(_other, _other), 'a1 => a2 failed. a1=%s, a2=%s' % (_other, _other))
def testLessThan(self): """Test <operator""" _dt = self.eDatetime + datetime.timedelta(1) _other = eventCalBase.event(2, 'event', _dt) self.failUnless(operator.lt(self.e, _other), 'a1 < a2 failed. a1=%s, a2=%s' % (self.e, _other)) self.failIf(operator.lt(_other, _other), 'a1 < a2 failed. a1=%s, a2=%s' % (_other, _other))
def testLessThanEqual(self): """Test <=operator""" _dt = self.eDatetime + datetime.timedelta(1) _other = eventCalBase.event(2, 'event', _dt) self.failUnless(operator.le(self.e, _other), 'a1 <= a2 failed. a1=%s, a2=%s' % (self.e, _other)) self.failUnless(operator.le(_other, _other), 'a1 <= a2 failed. a1=%s, a2=%s' % (_other, _other))
def testNotEqual(self): """Test !=operator""" _dt = self.eDatetime - datetime.timedelta(1) _other = eventCalBase.event(2, 'event', _dt) self.failUnless(operator.ne(self.e, _other), 'a1 != a2 failed. a1=%s, a2=%s' % (self.e, _other)) self.failIf(operator.ne(_other, _other), 'a1 != a2 failed. a1=%s, a2=%s' % (_other, _other))
def testGetEvents_multipleEvents(self): """Test getEvents()""" now = datetime.datetime.now() event = eventCalBase.event(1, 'test', now) for c in xrange(3): self.cal.addEvent(event, now.day) expect = self.cal.events[now.day] got = self.cal.getEvents(now.day, now.time().replace(microsecond=0)) self.failUnlessEqual(expect, got, self.emsg % (expect, got))
def testEqual(self): """Test ==operator""" _other = eventCalBase.event( 1, 'event', self.eDatetime, ) self.failUnlessEqual( self.e, _other, 'a1 == a2 failed. a1=%s, a2=%s' % (self.e, _other))
def testAddEvent(self): """Test addEvent()""" fixdate = datetime.datetime(2007,6,20,12,30,0) event = eventCalBase.event(1, 'test', fixdate) self.cal.addEvent(event, fixdate.day) expectdate = self.cal.getDate(fixdate.day) event.setStart(datetime.datetime.combine(expectdate, fixdate.time())) expect = [event] got = self.cal.getDailyEvents(fixdate.day) self.failUnlessEqual(expect, got, self.emsg % (expect, got))
def testAddEvent(self): """Test addEvent()""" fixdate = datetime.datetime(2007, 6, 20, 12, 30, 0) event = eventCalBase.event(1, 'test', fixdate) self.cal.addEvent(event, fixdate.day) expectdate = self.cal.getDate(fixdate.day) event.setStart(datetime.datetime.combine(expectdate, fixdate.time())) expect = [event] got = self.cal.getDailyEvents(fixdate.day) self.failUnlessEqual(expect, got, self.emsg % (expect, got))
def addEvent(self, day, name, when, desc): """Add event to the calendar. For now change is sync to db at once.""" if not self.db_cal: # we are adding event to a month where no event exist, hence # finally we need a EventCalendar record for the month in database self.db_cal = models.EventCalendar(owner=self.curr.owner, year=self.curr.year, month=self.curr.month) self.db_cal.save() db_e = models.Event(name=name, when=when, desc=desc, cal=self.db_cal) db_e.save() # should have check the return above e = eventCalBase.event(db_e.id, db_e.name, db_e.when, db_e.desc) self.curr.addEvent(e, db_e.when.day)
def load(self, year, month): """load calendar with data from database""" temp = models.EventCalendar.objects.filter(owner=self.owner, year=year, month=month) if temp: # either 1 record or no record , check models.py self.db_cal = temp[0] self.db_events = models.Event.objects.filter(cal=self.db_cal, when__year=year, when__month=month) self.curr = eventCalBase.monthCalendar(self.db_cal.id, self.owner, year, month) for db_e in self.db_events: e = eventCalBase.event(db_e.id, db_e.name, db_e.when, db_e.desc) self.curr.addEvent(e, db_e.when.day) else: self.curr = eventCalBase.monthCalendar(None, self.owner, year, month)
def load(self, year, month): """load calendar with data from database""" temp = filter_samples(0,month,year,0) if temp: # either 1 record or no record , check models.py self.db_cal = temp[0] if self.area == 0: self.db_events = filter_samples(0,month,year,self.area) else: self.db_events = filter_samples(0,month,year,self.area) self.curr = eventCalBase.monthCalendar(self, year, month) #put events to map a month for db_e in self.db_events: e = eventCalBase.event(db_e.id, db_e.taken_by,db_e.date_taken, db_e.sampling_point) self.curr.addEvent(e, db_e.date_taken.day) else: self.curr = eventCalBase.monthCalendar(None, year, month)
def load(self, year, month): """load calendar with data from database""" temp = Sample.objects.filter(date_taken__year=year, date_taken__month=month) if temp: # either 1 record or no record , check models.py self.db_cal = temp[0] if self.area == 0: self.db_events = Sample.objects.filter(date_taken__year=year, date_taken__month=month) else: a = Sample.objects.filter(date_taken__year=year, date_taken__month=month) self.db_events = a.filter(sampling_point__wqmarea = self.area) self.curr = eventCalBase.monthCalendar(self, year, month) #put events to map a month for db_e in self.db_events: e = eventCalBase.event(db_e.id, db_e.taken_by,db_e.date_taken, db_e.sampling_point) self.curr.addEvent(e, db_e.date_taken.day) else: self.curr = eventCalBase.monthCalendar(None, year, month)
def setUp(self): self.eDatetime = datetime.datetime.now() self.e = eventCalBase.event(1, 'event', self.eDatetime, 'some event')
def testEqual(self): """Test ==operator""" _other = eventCalBase.event(1, 'event', self.eDatetime,) self.failUnlessEqual(self.e, _other, 'a1 == a2 failed. a1=%s, a2=%s' % (self.e, _other))