Skip to content

Commit

Permalink
added can_download and hybrid_property has_raw_quote
Browse files Browse the repository at this point in the history
  • Loading branch information
Leechael committed Nov 13, 2024
1 parent 0bb7bb9 commit 4b4327c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion dcap-attestation/src/dcap_attestation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class VerificationResponse(BaseModel):
success: bool
quote: Optional[Quote] = None
checksum: Optional[str] = None
can_download: bool


app = FastAPI(root_path='/api/attestations')
Expand Down Expand Up @@ -45,7 +46,9 @@ async def view(checksum: str, db: Session = Depends(get_db)):
row = crud.get_quote(db, checksum)
if not row:
raise HTTPException(status_code=404, detail="Not found")
return JSONResponse(content=row.to_instance().dict())
d = row.to_instance().dict()
d['can_download'] = row.has_raw_quote
return JSONResponse(content=d)


@app.get('/raw/{checksum}')
Expand Down
19 changes: 19 additions & 0 deletions dcap-attestation/src/dcap_attestation/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from sqlalchemy import Column, Integer, String, BLOB, DateTime, Enum, Text, Boolean, LargeBinary, Index
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from sqlalchemy.ext.hybrid import hybrid_property
import enum
from datetime import datetime

Expand Down Expand Up @@ -57,6 +59,23 @@ class QuoteModel(Base):
verified = Column(Boolean)
created_at = Column(DateTime(timezone=True), server_default=func.now())

raw_quote = relationship(
"RawQuoteModel",
primaryjoin="and_(foreign(QuoteModel.checksum) == RawQuoteModel.checksum)",
uselist=False,
viewonly=True,
)

@hybrid_property
def has_raw_quote(self):
# Instance-level check (Python side)
return self.raw_quote is not None

@has_raw_quote.expression
def has_raw_quote(cls):
# SQL-level check (Database side)
return exists().where(RawQuoteModel.checksum == cls.checksum)

def to_instance(self):
header = QuoteHeader(
version=self.version,
Expand Down

0 comments on commit 4b4327c

Please # to comment.