2018-09-10 09:55:26 -07:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from grant.comment.models import Comment
|
|
|
|
from grant.extensions import ma, db
|
|
|
|
from grant.utils.misc import dt_to_unix
|
|
|
|
|
|
|
|
FUNDING_REQUIRED = 'FUNDING_REQUIRED'
|
|
|
|
COMPLETED = 'COMPLETED'
|
|
|
|
PROPOSAL_STAGES = [FUNDING_REQUIRED, COMPLETED]
|
|
|
|
|
|
|
|
DAPP = "DAPP"
|
|
|
|
DEV_TOOL = "DEV_TOOL"
|
|
|
|
CORE_DEV = "CORE_DEV"
|
|
|
|
COMMUNITY = "COMMUNITY"
|
|
|
|
DOCUMENTATION = "DOCUMENTATION"
|
|
|
|
ACCESSIBILITY = "ACCESSIBILITY"
|
|
|
|
CATEGORIES = [DAPP, DEV_TOOL, CORE_DEV, COMMUNITY, DOCUMENTATION, ACCESSIBILITY]
|
|
|
|
|
|
|
|
|
|
|
|
class ValidationException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-25 13:09:25 -07:00
|
|
|
proposal_team = db.Table(
|
|
|
|
'proposal_team', db.Model.metadata,
|
|
|
|
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
|
|
|
|
db.Column('proposal_id', db.Integer, db.ForeignKey('proposal.id'))
|
|
|
|
)
|
|
|
|
|
2018-11-07 09:33:19 -08:00
|
|
|
|
2018-11-02 09:24:28 -07:00
|
|
|
class ProposalUpdate(db.Model):
|
|
|
|
__tablename__ = "proposal_update"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer(), primary_key=True)
|
|
|
|
date_created = db.Column(db.DateTime)
|
|
|
|
|
|
|
|
proposal_id = db.Column(db.Integer, db.ForeignKey("proposal.id"), nullable=False)
|
|
|
|
title = db.Column(db.String(255), nullable=False)
|
|
|
|
content = db.Column(db.Text, nullable=False)
|
|
|
|
|
|
|
|
def __init__(self, proposal_id: int, title: str, content: str):
|
|
|
|
self.proposal_id = proposal_id
|
|
|
|
self.title = title
|
|
|
|
self.content = content
|
|
|
|
self.date_created = datetime.datetime.now()
|
|
|
|
|
2018-09-25 13:09:25 -07:00
|
|
|
|
2018-09-10 09:55:26 -07:00
|
|
|
class Proposal(db.Model):
|
|
|
|
__tablename__ = "proposal"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer(), primary_key=True)
|
|
|
|
date_created = db.Column(db.DateTime)
|
|
|
|
|
|
|
|
title = db.Column(db.String(255), nullable=False)
|
2018-11-07 09:33:19 -08:00
|
|
|
proposal_address = db.Column(db.String(255), unique=True, nullable=False)
|
2018-09-10 09:55:26 -07:00
|
|
|
stage = db.Column(db.String(255), nullable=False)
|
|
|
|
content = db.Column(db.Text, nullable=False)
|
|
|
|
category = db.Column(db.String(255), nullable=False)
|
|
|
|
|
2018-09-25 13:09:25 -07:00
|
|
|
team = db.relationship("User", secondary=proposal_team)
|
2018-09-10 09:55:26 -07:00
|
|
|
comments = db.relationship(Comment, backref="proposal", lazy=True)
|
2018-11-02 09:24:28 -07:00
|
|
|
updates = db.relationship(ProposalUpdate, backref="proposal", lazy=True)
|
2018-09-10 09:55:26 -07:00
|
|
|
milestones = db.relationship("Milestone", backref="proposal", lazy=True)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
stage: str,
|
2018-11-07 09:33:19 -08:00
|
|
|
proposal_address: str,
|
2018-09-10 09:55:26 -07:00
|
|
|
title: str,
|
|
|
|
content: str,
|
|
|
|
category: str
|
|
|
|
):
|
|
|
|
self.stage = stage
|
2018-11-07 09:33:19 -08:00
|
|
|
self.proposal_address = proposal_address
|
2018-09-10 09:55:26 -07:00
|
|
|
self.title = title
|
|
|
|
self.content = content
|
|
|
|
self.category = category
|
|
|
|
self.date_created = datetime.datetime.now()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate(
|
|
|
|
stage: str,
|
2018-11-07 09:33:19 -08:00
|
|
|
proposal_address: str,
|
2018-09-10 09:55:26 -07:00
|
|
|
title: str,
|
|
|
|
content: str,
|
|
|
|
category: str):
|
|
|
|
if stage not in PROPOSAL_STAGES:
|
|
|
|
raise ValidationException("{} not in {}".format(stage, PROPOSAL_STAGES))
|
|
|
|
if category not in CATEGORIES:
|
|
|
|
raise ValidationException("{} not in {}".format(category, CATEGORIES))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create(**kwargs):
|
|
|
|
Proposal.validate(**kwargs)
|
|
|
|
return Proposal(
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ProposalSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
model = Proposal
|
|
|
|
# Fields to expose
|
|
|
|
fields = (
|
|
|
|
"stage",
|
|
|
|
"date_created",
|
|
|
|
"title",
|
|
|
|
"proposal_id",
|
2018-11-07 09:33:19 -08:00
|
|
|
"proposal_address",
|
2018-09-10 09:55:26 -07:00
|
|
|
"body",
|
|
|
|
"comments",
|
2018-11-02 09:24:28 -07:00
|
|
|
"updates",
|
2018-09-10 09:55:26 -07:00
|
|
|
"milestones",
|
2018-09-25 13:09:25 -07:00
|
|
|
"category",
|
|
|
|
"team"
|
2018-09-10 09:55:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
date_created = ma.Method("get_date_created")
|
|
|
|
proposal_id = ma.Method("get_proposal_id")
|
|
|
|
body = ma.Method("get_body")
|
|
|
|
|
|
|
|
comments = ma.Nested("CommentSchema", many=True)
|
2018-11-02 09:24:28 -07:00
|
|
|
updates = ma.Nested("ProposalUpdateSchema", many=True)
|
2018-09-25 13:09:25 -07:00
|
|
|
team = ma.Nested("UserSchema", many=True)
|
2018-09-10 09:55:26 -07:00
|
|
|
milestones = ma.Nested("MilestoneSchema", many=True)
|
|
|
|
|
|
|
|
def get_body(self, obj):
|
|
|
|
return obj.content
|
|
|
|
|
|
|
|
def get_proposal_id(self, obj):
|
2018-11-07 09:33:19 -08:00
|
|
|
return obj.id
|
2018-09-10 09:55:26 -07:00
|
|
|
|
|
|
|
def get_date_created(self, obj):
|
|
|
|
return dt_to_unix(obj.date_created)
|
|
|
|
|
|
|
|
|
|
|
|
proposal_schema = ProposalSchema()
|
|
|
|
proposals_schema = ProposalSchema(many=True)
|
2018-11-02 09:24:28 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ProposalUpdateSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
model = ProposalUpdate
|
|
|
|
# Fields to expose
|
|
|
|
fields = (
|
2018-11-07 09:33:19 -08:00
|
|
|
"update_id",
|
2018-11-02 09:24:28 -07:00
|
|
|
"date_created",
|
|
|
|
"proposal_id",
|
|
|
|
"title",
|
|
|
|
"content"
|
|
|
|
)
|
|
|
|
|
|
|
|
date_created = ma.Method("get_date_created")
|
|
|
|
proposal_id = ma.Method("get_proposal_id")
|
2018-11-07 09:33:19 -08:00
|
|
|
update_id = ma.Method("get_update_id")
|
|
|
|
|
|
|
|
def get_update_id(self, obj):
|
|
|
|
return obj.id
|
2018-11-02 09:24:28 -07:00
|
|
|
|
|
|
|
def get_proposal_id(self, obj):
|
|
|
|
return obj.proposal_id
|
|
|
|
|
|
|
|
def get_date_created(self, obj):
|
|
|
|
return dt_to_unix(obj.date_created)
|
|
|
|
|
|
|
|
|
|
|
|
proposal_update_schema = ProposalUpdateSchema()
|
2018-11-07 09:33:19 -08:00
|
|
|
proposals_update_schema = ProposalUpdateSchema(many=True)
|