prompts.py 2.77 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from pydantic import BaseModel
from peewee import *
from playhouse.shortcuts import model_to_dict
from typing import List, Union, Optional
import time

from utils.utils import decode_token
from utils.misc import get_gravatar_url

from apps.web.internal.db import DB

import json

####################
# Prompts DB Schema
####################


class Prompt(Model):
    command = CharField(unique=True)
    user_id = CharField()
    title = CharField()
    content = TextField()
    timestamp = DateField()

    class Meta:
        database = DB


class PromptModel(BaseModel):
    command: str
    user_id: str
    title: str
    content: str
    timestamp: int  # timestamp in epoch


####################
# Forms
####################


class PromptForm(BaseModel):
    command: str
    title: str
    content: str


class PromptsTable:
50

Timothy J. Baek's avatar
Timothy J. Baek committed
51
52
53
54
    def __init__(self, db):
        self.db = db
        self.db.create_tables([Prompt])

55
56
    def insert_new_prompt(self, user_id: str,
                          form_data: PromptForm) -> Optional[PromptModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
57
58
59
60
61
62
63
        prompt = PromptModel(
            **{
                "user_id": user_id,
                "command": form_data.command,
                "title": form_data.title,
                "content": form_data.content,
                "timestamp": int(time.time()),
64
            })
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

        try:
            result = Prompt.create(**prompt.model_dump())
            if result:
                return prompt
            else:
                return None
        except:
            return None

    def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
        try:
            prompt = Prompt.get(Prompt.command == command)
            return PromptModel(**model_to_dict(prompt))
        except:
            return None

    def get_prompts(self) -> List[PromptModel]:
        return [
84
            PromptModel(**model_to_dict(prompt)) for prompt in Prompt.select()
Timothy J. Baek's avatar
Timothy J. Baek committed
85
86
87
88
            # .limit(limit).offset(skip)
        ]

    def update_prompt_by_command(
89
90
            self, command: str,
            form_data: PromptForm) -> Optional[PromptModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        try:
            query = Prompt.update(
                title=form_data.title,
                content=form_data.content,
                timestamp=int(time.time()),
            ).where(Prompt.command == command)

            query.execute()

            prompt = Prompt.get(Prompt.command == command)
            return PromptModel(**model_to_dict(prompt))
        except:
            return None

    def delete_prompt_by_command(self, command: str) -> bool:
        try:
            query = Prompt.delete().where((Prompt.command == command))
            query.execute()  # Remove the rows, return number of rows removed.

            return True
        except:
            return False


Prompts = PromptsTable(DB)