Skip to content

Commit c1a0471

Browse files
committed
feat: templates
1 parent 712c254 commit c1a0471

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

server/api/template.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import database
2+
from typing import Annotated
3+
from database import Template
4+
from sqlalchemy.orm import Session
5+
from fastapi import APIRouter, Depends, Query, Header
6+
from pydantic import BaseModel
7+
from commons.utils import get_user_from_jwt, verify_user
8+
9+
template_router = APIRouter(prefix="", tags=["template"])
10+
11+
12+
class TemplateModel(BaseModel):
13+
name: str = None
14+
dag: dict = None
15+
description: str = None
16+
17+
18+
@template_router.get("/templates", status_code=200)
19+
def get_all_templates(token: Annotated[str, Header()], db: Session = Depends(database.db_session)):
20+
verify_user(get_user_from_jwt(token))
21+
templates = db.query(Template).all()
22+
response = {"msg": "success", "templates": [template.to_dict() for template in templates]}
23+
return response
24+
25+
26+
@template_router.get("/template/{id}", status_code=200)
27+
def get_template_by_id(id: int, token: Annotated[str, Header()], db: Session = Depends(database.db_session)):
28+
verify_user(get_user_from_jwt(token))
29+
template: Template = db.query(Template).filter(Template.id == id).first() # type: ignore
30+
response = {"msg": "success", "template": template.to_dict()}
31+
return response
32+
33+
34+
@template_router.post("/template", status_code=200)
35+
def create_template(inputs: TemplateModel, token: Annotated[str, Header()], db: Session = Depends(database.db_session)):
36+
verify_user(get_user_from_jwt(token))
37+
template = Template(name=inputs.name, description=inputs.description, dag=inputs.dag)
38+
db.add(template)
39+
db.commit()
40+
response = {"msg": "success", "template": template.to_dict()}
41+
return response

server/database.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ class IntermediateStep(Base):
7676

7777
def __repr__(self):
7878
return f"Prompt(id={self.id}, name={self.name}, created_by={self.created_by}, dag={self.dag}, meta={self.meta})"
79+
80+
81+
class Template(Base):
82+
__tablename__ = "template"
83+
id = Column(Integer, primary_key=True)
84+
name = Column(Text, nullable=False)
85+
dag = Column(JSON, nullable=False)
86+
description = Column(Text)
87+
meta = Column(JSON)
88+
89+
def __repr__(self):
90+
return f"Template(id={self.id}, name={self.name}, description={self.description}, dag={self.dag}, meta={self.meta})"

0 commit comments

Comments
 (0)