def __init__(self):
        self._db = TodoListDB()
        self.current_id = None

        self.filter_finished = 0
        self.filter_due = 0
        self.filter_category = -2
def cmd_add_todo(argv):
    db = TodoListDB()
    if (len(argv) == 2):
        db.add_todo(argv[0], argv[1])
    elif (len(argv) == 3):
        db.add_todo(argv[0], argv[1], argv[2])
    elif (len(argv) == 4):
        db.add_todo(argv[0], argv[1], argv[2], argv[3])
    elif (len(argv) == 5):
        db.add_todo(argv[0], argv[1], argv[2], argv[3], argv[4])
Ejemplo n.º 3
0
import todolist
from todolist.todolistdb import TodoListDB

db = TodoListDB()
db.add_category("생활")
db.add_category("개발")

columns = ("id", "할일", "기한", "카테고리", "완료")


def main_menu():
    while True:
        print("Choose an option.")
        ans = input(
            " 1. Add todo\n 2. List todo\n 3. Modify todo\n 4. Quit\n\n>>> ")

        if ans == "1":
            # Add todo
            add_todo()

        elif ans == "2":
            # List todo
            list_todo()

        elif ans == "3":
            # Modify todo
            modify_todo()

        elif ans == "4":
            # Quit
            break
def cmd_list_category():
    db = TodoListDB()
    rows = db.get_category()
    for row in rows:
        print("{0}. {1}".format(row[0], row[1]))
def cmd_list_todo():
    db = TodoListDB()
    rows = db.get_todo()
    for row in rows:
        print("{0}. {1}, {2}, {3}, {4}".format(row[0], row[1], row[2], row[3],
                                               row[4]))
def cmd_delete_category(argv):
    db = TodoListDB()
    if (len(argv) == 1):
        db.remove_category("id=" + argv[0])
def cmd_delete_todo(argv):
    db = TodoListDB()
    if (len(argv) == 1):
        db.remove_todo("id=" + argv[0])
def cmd_add_category(argv):
    db = TodoListDB()
    if (len(argv) == 1):
        db.add_category(argv[0])
class TodoListModel(object):
    def __init__(self):
        self._db = TodoListDB()
        self.current_id = None

        self.filter_finished = 0
        self.filter_due = 0
        self.filter_category = -2

    def add_todo(self, what, due, category, notes, finished):
        return self._db.add_todo(what, due, category, notes, finished)

    def add_category(self, category):
        return self._db.add_category(category)

    def get_summary(self):
        rows = self._db.get_todo()
        if self.filter_finished == 1:
            summary = [((x[1], x[2], x[3], 'Y' if x[5] else 'N'), x[0])
                       for x in rows if x[5]]
        elif self.filter_finished == 2:
            summary = [((x[1], x[2], x[3], 'Y' if x[5] else 'N'), x[0])
                       for x in rows if not x[5]]
        else:
            summary = [((x[1], x[2], x[3], 'Y' if x[5] else 'N'), x[0])
                       for x in rows]

        if self.filter_category == -1:
            summary = [x for x in summary if not x[0][2]]
        elif 0 <= self.filter_category:
            filter_category_str = self._db.get_category(
                "id=" + str(self.filter_category))
            if filter_category_str:
                filter_category_str = filter_category_str[0][1]
                summary = [
                    x for x in summary if x[0][2] == filter_category_str
                ]

        if self.filter_due == 1:
            summary = sorted(summary,
                             key=lambda x: datetime.datetime.strptime(
                                 x[0][1], "%Y-%m-%d %H:%M:%S"))
        elif self.filter_due == 2:
            summary = sorted(summary,
                             key=lambda x: datetime.datetime.strptime(
                                 x[0][1], "%Y-%m-%d %H:%M:%S"),
                             reverse=True)

        return summary

    def get_categorys(self):
        rows = self._db.get_category()
        categorys = [(x[1], x[0]) for x in rows]

        return categorys

    def get_todo(self, todo_id):
        row = self._db.get_todo("id=" + str(todo_id))
        if row:
            row = row[0]
        else:
            return None
        id = row[0]
        what = row[1]
        due = row[2]
        due_datetime = datetime.datetime.strptime(due, "%Y-%m-%d %H:%M:%S")
        due_datetime = due_datetime.replace(microsecond=0)
        due_date = due_datetime.date()
        due_time = due_datetime.time()
        category = row[3]
        category = self._db.get_category("name='" + category +
                                         "'") if category else None
        category = category[0][0] if category else -1
        notes = row[4]
        finished = row[5]
        todo = {
            "id": id,
            "what": what,
            "due_date": due_date,
            "due_time": due_time,
            "category": category,
            "notes": notes,
            "finished": bool(finished)
        }

        return todo

    def get_current_todo(self):
        if self.current_id is None:
            due_datetime = datetime.datetime.now()
            due_datetime = due_datetime.replace(microsecond=0)
            todo = {
                "id": -1,
                "what": "",
                "due_date": due_datetime.date(),
                "due_time": due_datetime.time(),
                "category": -1,
                "notes": "",
                "finished": False
            }
            return todo
        else:
            return self.get_todo(self.current_id)

    def get_current_categorys(self):
        rows = self._db.get_category()
        categorys = [("", -1)] + [(x[1], x[0]) for x in rows]
        return categorys

    def update_current_todo(self, details):
        id = details["id"]
        what = details["what"]
        what = what.replace("'", "''")
        due = str(details["due_date"]) + " " + str(details["due_time"])
        category = details["category"]
        category = self._db.get_category("id=" + str(category))
        category = category[0][1] if category else ""
        notes = details["notes"]
        notes = notes.replace("'", "''")
        finished = details["finished"]

        if id == -1 and self._db.get_todo("what='" + what + "'"):
            return False

        if self.current_id is None:
            self.add_todo(what, due, category, notes, str(int(finished)))
        else:
            modifys = (("what", what), ("due", due), ("category", category),
                       ("notes", notes), ("finished", str(int(finished))))
            self._db.modify_todo(modifys, "id=" + str(id))

        return True

    def delete_todo(self, todo_id):
        if todo_id is not None:
            self._db.remove_todo("id=" + str(todo_id))

    def delete_category(self, category_id):
        if category_id is not None:
            self._db.remove_category("id=" + str(category_id))

    @staticmethod
    def _valid_string_check(s):
        for c in s:
            c_ord = ord(c)
            if not ((ord('A') <= c_ord <= ord('Z')) or
                    (ord('a') <= c_ord <= ord('z')) or
                    (ord('0') <= c_ord <= ord('9')) or
                    (ord('ㄱ') <= c_ord <= ord('ㅣ')) or
                    (ord('가') <= c_ord <= ord('ퟻ')) or
                    (c in " `~!@#$%^&*()-_=+\\|[]{};:'\",<.>/?")):
                return False
        return True