def test_serialized_name(self): '''testing serialized_name''' activity = Fact("case@cat") test1 = activity.serialized_name() test2 = "case@cat" self.assertEquals(test1, test2) activity = Fact("case@cat, description #tag #bag") test3 = activity.serialized_name() test4 = "case@cat,description #tag #bag" self.assertEquals(test3, test4)
def test_str(self): '''testing str''' #first, testing start_time only activity = Fact("12:35 with start time") t1 = "%s %s" % (activity.start_time.strftime( activity.STRINGFORMAT), activity.serialized_name()) t2 = str(activity) self.assertEquals(t1, t2) #now, testing both start_time and end_time activity = Fact("12:35-14:25 with start-end time") t1 = "%s - %s %s" % ( activity.start_time.strftime(activity.STRINGFORMAT), activity.end_time.strftime( activity.TIMESTRINGFORMAT), activity.serialized_name()) t2 = str(activity)
def add_fact(self, fact, start_time, end_time, temporary = False): fact = Fact(fact, start_time = start_time, end_time = end_time) start_time = fact.start_time or dt.datetime.now().replace(second = 0, microsecond = 0) self.start_transaction() result = self.__add_fact(fact.serialized_name(), start_time, end_time, temporary) self.end_transaction() if result: self.facts_changed() return result
def test_str(self): '''testing str''' #first, testing start_time only activity = Fact("12:35 with start time") t1 = "%s %s" % (activity.start_time.strftime(activity.STRINGFORMAT), activity.serialized_name()) t2 = str(activity) self.assertEquals(t1, t2) #now, testing both start_time and end_time activity = Fact("12:35-14:25 with start-end time") t1 = "%s - %s %s" % (activity.start_time.strftime(activity.STRINGFORMAT), activity.end_time.strftime(activity.TIMESTRINGFORMAT), activity.serialized_name()) t2 = str(activity)
def __solve_overlaps(self, start_time, end_time): """finds facts that happen in given interval and shifts them to make room for new fact """ if end_time is None or start_time is None: return # possible combinations and the OR clauses that catch them # (the side of the number marks if it catches the end or start time) # |----------------- NEW -----------------| # |--- old --- 1| |2 --- old --- 1| |2 --- old ---| # |3 ----------------------- big old ------------------------ 3| query = """ SELECT a.*, b.name, c.name as category FROM facts a LEFT JOIN activities b on b.id = a.activity_id LEFT JOIN categories c on b.category_id = c.id WHERE (end_time > ? and end_time < ?) OR (start_time > ? and start_time < ?) OR (start_time < ? and end_time > ?) ORDER BY start_time """ conflicts = self.fetchall( query, (start_time, end_time, start_time, end_time, start_time, end_time)) for fact in conflicts: # won't eliminate as it is better to have overlapping entries than loosing data if start_time < fact["start_time"] and end_time > fact["end_time"]: continue # split - truncate until beginning of new entry and create new activity for end if fact["start_time"] < start_time < fact["end_time"] and \ fact["start_time"] < end_time < fact["end_time"]: logging.info("splitting %s" % fact["name"]) # truncate until beginning of the new entry self.execute( """UPDATE facts SET end_time = ? WHERE id = ?""", (start_time, fact["id"])) fact_name = fact["name"] # create new fact for the end new_fact = Fact(fact["name"], category=fact["category"], description=fact["description"]) new_fact_id = self.__add_fact(new_fact.serialized_name(), end_time, fact["end_time"]) # copy tags tag_update = """INSERT INTO fact_tags(fact_id, tag_id) SELECT ?, tag_id FROM fact_tags WHERE fact_id = ?""" self.execute(tag_update, (new_fact_id, fact["id"])) #clone tags if trophies: trophies.unlock("split") # overlap start elif start_time < fact["start_time"] < end_time: logging.info("Overlapping start of %s" % fact["name"]) self.execute("UPDATE facts SET start_time=? WHERE id=?", (end_time, fact["id"])) # overlap end elif start_time < fact["end_time"] < end_time: logging.info("Overlapping end of %s" % fact["name"]) self.execute("UPDATE facts SET end_time=? WHERE id=?", (start_time, fact["id"]))
def __solve_overlaps(self, start_time, end_time): """finds facts that happen in given interval and shifts them to make room for new fact """ if end_time is None or start_time is None: return # possible combinations and the OR clauses that catch them # (the side of the number marks if it catches the end or start time) # |----------------- NEW -----------------| # |--- old --- 1| |2 --- old --- 1| |2 --- old ---| # |3 ----------------------- big old ------------------------ 3| query = """ SELECT a.*, b.name, c.name as category FROM facts a LEFT JOIN activities b on b.id = a.activity_id LEFT JOIN categories c on b.category_id = c.id WHERE (end_time > ? and end_time < ?) OR (start_time > ? and start_time < ?) OR (start_time < ? and end_time > ?) ORDER BY start_time """ conflicts = self.fetchall(query, (start_time, end_time, start_time, end_time, start_time, end_time)) for fact in conflicts: # won't eliminate as it is better to have overlapping entries than loosing data if start_time < fact["start_time"] and end_time > fact["end_time"]: continue # split - truncate until beginning of new entry and create new activity for end if fact["start_time"] < start_time < fact["end_time"] and \ fact["start_time"] < end_time < fact["end_time"]: logging.info("splitting %s" % fact["name"]) # truncate until beginning of the new entry self.execute("""UPDATE facts SET end_time = ? WHERE id = ?""", (start_time, fact["id"])) fact_name = fact["name"] # create new fact for the end new_fact = Fact(fact["name"], category = fact["category"], description = fact["description"]) new_fact_id = self.__add_fact(new_fact.serialized_name(), end_time, fact["end_time"]) # copy tags tag_update = """INSERT INTO fact_tags(fact_id, tag_id) SELECT ?, tag_id FROM fact_tags WHERE fact_id = ?""" self.execute(tag_update, (new_fact_id, fact["id"])) #clone tags if trophies: trophies.unlock("split") # overlap start elif start_time < fact["start_time"] < end_time: logging.info("Overlapping start of %s" % fact["name"]) self.execute("UPDATE facts SET start_time=? WHERE id=?", (end_time, fact["id"])) # overlap end elif start_time < fact["end_time"] < end_time: logging.info("Overlapping end of %s" % fact["name"]) self.execute("UPDATE facts SET end_time=? WHERE id=?", (start_time, fact["id"]))