Exemplo n.º 1
0
    def get_or_create_cups(self, date: str, cups: int) -> None:
        """Get existing record or create a new record of cups drank.

        :param date:
        :param cups:
        :return:
        """
        self.query(date=date)
        if self.id:
            with db.atomic():
                WaterDB.update(cups_drank=WaterDB.cups_drank + cups).where(
                    WaterDB.date_stamp.contains(date)).execute()
        else:
            with db.atomic():
                WaterDB.insert(cups_drank=cups, date_stamp=date).execute()

        self.query(date=date)
        console.print(
            f"[bold]{escape('[perry-bot]:')}[/bold] Water log added to "
            f"database.\n"
            f"[bold]{escape('[perry-bot]:')}[/bold] You've drunk "
            f"{self.cups_drank[-1]} cups of water today, "
            f"keep up the good work!",
            style="default",
        )
Exemplo n.º 2
0
    def edit_mood(self, edit_date: str) -> None:
        """Edit a specific mood rating.

        Based on a date by inputting the number of the rating.

        :param edit_date:
        :return:
        """
        self.query(date=edit_date)
        table = self.make_table(title="Edit a Mood Rating")
        if self.id:
            table.add_row(
                str(self.id[-1] + 1),
                "[b]----------[/b]",
                "[b]Cancel Edit[/b]",
                "[b]----------[/b]",
                style="cancel",
            )
            setup_edit = EditExistingEntry(
                table=table,
                id=self.id,
                column_choices=["rating", "comment"],
                new_value="",
            )
            edit_target = EditExistingEntry.edit_table(setup_edit)
            if not edit_target:
                return
            if edit_target[0] in ["rating"]:
                with db.atomic():
                    MoodDB.update(rating=edit_target[1]).where(
                        MoodDB.id == edit_target[2]).execute()
                console.print(
                    f"[bold]{escape('[perry-bot]:')}[/bold] Your rating has "
                    f"been updated.",
                    style="default",
                )
            elif edit_target[0] in ["comment"]:
                with db.atomic():
                    MoodDB.update(comment=edit_target[1]).where(
                        MoodDB.id == edit_target[2]).execute()
                console.print(
                    f"[bold]{escape('[perry-bot]:')}[/bold] Your comment has "
                    f"been updated.",
                    style="default",
                )

        else:
            console.print(
                f"[bold]{escape('[perry-bot]:')}[/bold] Sorry, there are no "
                f"records matching {edit_date}. "
                f"Try another date.",
                style="default",
            )
Exemplo n.º 3
0
    def delete_cups(self, date: str, cups: int) -> None:
        """Delete num of cups from today's record.

        :param date:
        :param cups:
        :return:
        """
        self.query(date=date)
        if self.id:
            check_cups = self.check_cups_less_than_zero(date=date, cups=cups)
            if not check_cups:
                return
            with db.atomic():
                WaterDB.update(cups_drank=WaterDB.cups_drank - cups).where(
                    WaterDB.date_stamp.contains(date)).execute()
            console.print(
                f"[bold]{escape('[perry-bot]:')}[/bold] {cups} cups deleted "
                f"from today's log.",
                style="default",
            )
        else:
            console.print(
                f"[bold]{escape('[perry-bot]:')}[/bold] You haven't recorded "
                f"any cups drank today yet, "
                f"there's nothing to delete!",
                style="default",
            )
Exemplo n.º 4
0
    def query(self, date: str) -> None:
        """Query database and append data to self.

        :param date:
        :return:
        """
        with db.atomic():
            query = WaterDB.select().where(WaterDB.date_stamp.contains(date))
        for record in query:
            self.id.append(record.id)
            self.cups_drank.append(record.cups_drank)
            self.date_stamp.append(record.date_stamp)
Exemplo n.º 5
0
    def query(self, date: str) -> None:
        """Query database and append data to self.

        :param date:
        :return:
        """
        with db.atomic():
            query = MoodDB.select().where(MoodDB.datetime_stamp.contains(date))
            for record in query:
                self.id.append(record.id)
                self.rating.append(record.rating)
                self.datetime_stamp.append(record.datetime_stamp)
                self.comment.append(record.comment)
Exemplo n.º 6
0
    def query(self, selection: collections.Iterable) -> None:
        """Query database and append data to self.

        :return:
        """
        with db.atomic():
            query = selection
            for record in query:
                self.id.append(record.id)
                self.habit_name.append(record.habit_name)
                self.completion.append(record.completion)
                self.start_date.append(record.start_date)
                self.completed_on.append(record.completed_on)
                self.frequency.append(record.frequency)
                self.next_due.append(record.next_due)
Exemplo n.º 7
0
    def insert_new_habit(self) -> bool:
        """Insert a new habit into the database.

        :return:
        """
        try:
            with db.atomic():
                HabitDB.insert(
                    habit_name=self.habit_name,
                    start_date=self.start_date[0],
                    frequency=self.frequency[0],
                    next_due=self.next_due[0],
                ).execute()
            return True
        except peewee.IntegrityError:
            return False  # habit name already exists
Exemplo n.º 8
0
    def new_entry(self) -> None:
        """Create a new entry in the database.

        :return:
        """
        with db.atomic():
            MoodDB.insert(
                rating=self.rating,
                datetime_stamp=self.datetime_stamp,
                comment=self.comment,
            ).execute()
        console.print(
            f"[bold]{escape('[perry-bot]:')}[/bold] Mood entry added to "
            f"database.",
            style="default",
        )
Exemplo n.º 9
0
    def edit_cups(self, edit_date: str) -> None:
        """Edit number of cups drank in database.

        :param edit_date:
        :return:
        """
        self.query(date=edit_date)
        table = Table(title="Edit Water Records")
        table.add_column("#", justify="center")
        table.add_column("Date")
        table.add_column("Cups drank", justify="center")

        for (number, date, cups) in zip(self.id, self.date_stamp,
                                        self.cups_drank):
            table.add_row(str(number), str(date), str(cups))
        if self.id:
            table.add_row(
                str(self.id[-1] + 1),
                "[b]Cancel Edit[/b]",
                "[b]----------[/b]",
                style="cancel",
            )
            setup_edit = EditExistingEntry(
                table=table,
                id=self.id,
                column_choices=["cups drank"],
                new_value="",
            )
            edit_target = EditExistingEntry.edit_table(setup_edit)
            if edit_target is False:
                return
            with db.atomic():
                WaterDB.update(cups_drank=edit_target[1]).where(
                    WaterDB.id == edit_target[2]).execute()
            console.print(
                f"[bold]{escape('[perry-bot]:')}[/bold] Your number of cups "
                f"drank has been updated.",
                style="default",
            )
        else:
            console.print(
                f"[bold]{escape('[perry-bot]:')}[/bold] Sorry, there are no "
                f"records "
                f"matching {edit_date}. Try another date.",
                style="default",
            )
Exemplo n.º 10
0
    def mark_complete_or_incomplete(self, str_name_or_id: str) -> None:
        """Main function for marking a habit as complete or incomplete.

        :param str_name_or_id: Either 'id' or 'name'
        :return: None
        """
        exists = self.check_if_exists(str_name_or_id=str_name_or_id)
        if not exists:
            self.doesnt_exist_response(str_name_or_id=str_name_or_id)
            return
        with db.atomic():
            if str_name_or_id == "id":
                HabitDB.update(completion=self.completion[0]).where(
                    HabitDB.id == self.id).execute()
            elif str_name_or_id == "name":
                HabitDB.update(completion=self.completion[0]).where(
                    HabitDB.habit_name.contains(self.habit_name)).execute()
        self.complete_incomplete_response()
Exemplo n.º 11
0
    def check_cups_less_than_zero(self, date: str, cups: int) -> bool:
        """Check total number of cups.

        :param date:
        :param cups:
        :return:
        """
        total_cups = sum(self.cups_drank) - cups
        if total_cups < 0:
            console.print(
                f"[bold]{escape('[perry-bot]:')}[/bold] Deleting {cups} cups "
                f"will cause the total cups to be "
                f"less than zero ({sum(self.cups_drank) - cups})."
                f"\n[bold]{escape('[perry-bot]:')}[/bold] Reset today's cups "
                f"drank to 0?",
                style="default",
            )
            c = Prompt.ask(choices=["y", "n"], default="y")
            if c in ["y"]:
                with db.atomic():
                    WaterDB.update(cups_drank=0).where(
                        WaterDB.date_stamp.contains(date)).execute()
                console.print(
                    f"[bold]{escape('[perry-bot]:')}[/bold] Cups drank today "
                    f"reset to 0.",
                    style="default",
                )
                return False
            if c in ["n"]:
                console.print(
                    f"[bold]{escape('[perry-bot]:')}[/bold] Deleting cups "
                    f"cancelled.",
                    style="default",
                )
                return False
        return True