add attachment model and task order relation to it

This commit is contained in:
dandds
2018-08-24 13:29:19 -04:00
committed by Montana
parent ef2e97713a
commit 54d1e7235b
9 changed files with 145 additions and 10 deletions

View File

@@ -13,3 +13,4 @@ from .task_order import TaskOrder
from .workspace import Workspace
from .project import Project
from .environment import Environment
from .attachment import Attachment

32
atst/models/attachment.py Normal file
View File

@@ -0,0 +1,32 @@
from sqlalchemy import Column, Integer, String
from flask import current_app as app
from atst.models import Base
from atst.database import db
from atst.uploader import UploadError
class AttachmentError(Exception):
pass
class Attachment(Base):
__tablename__ = "attachments"
id = Column(Integer, primary_key=True)
filename = Column(String)
object_name = Column(String, unique=True)
@classmethod
def attach(cls, fyle):
try:
filename, object_name = app.uploader.upload(fyle)
except UploadError as e:
raise AttachmentError("Could not add attachment. " + str(e))
attachment = Attachment(filename=filename, object_name=object_name)
db.session.add(attachment)
db.session.commit()
return attachment

View File

@@ -1,6 +1,7 @@
from enum import Enum
from sqlalchemy import Column, Integer, String, Enum as SQLAEnum
from sqlalchemy import Column, Integer, String, ForeignKey, Enum as SQLAEnum
from sqlalchemy.orm import relationship
from atst.models import Base
@@ -31,3 +32,6 @@ class TaskOrder(Base):
clin_1003 = Column(Integer)
clin_2001 = Column(Integer)
clin_2003 = Column(Integer)
attachment_id = Column(ForeignKey("attachments.id"))
pdf = relationship("Attachment")