def get_user(user_id, with_proposals, with_comments, with_pending, with_work): user = User.get_by_id(user_id) if user: result = user_schema.dump(user) authed_user = auth.get_authed_user() is_self = authed_user and authed_user.id == user.id if with_proposals: proposals = Proposal.get_by_user(user) proposals_dump = user_proposals_schema.dump(proposals) result["proposals"] = proposals_dump if with_comments: comments = Comment.get_by_user(user) comments_dump = user_comments_schema.dump(comments) result["comments"] = comments_dump if with_pending and is_self: pending = Proposal.get_by_user(user, [ ProposalStatus.PENDING, ProposalStatus.APPROVED, ProposalStatus.REJECTED, ]) pending_dump = user_proposals_schema.dump(pending) result["pendingProposals"] = pending_dump if with_work: result["work"] = rfw_models.RFWWorker.get_work(user.id, is_self) return result else: message = "User with id matching {} not found".format(user_id) return {"message": message}, 404
def authed_claim(self): authed = get_authed_user() if not authed: return None for c in self.claims: if c.worker.user_id == authed.id: return c return None
def authed_liked(self): from grant.utils.auth import get_authed_user authed = get_authed_user() if not authed: return False res = (db.session.query(rfp_liker).filter_by(user_id=authed.id, rfp_id=self.id).count()) if res: return True return False
def authed_follows(self): from grant.utils.auth import get_authed_user authed = get_authed_user() if not authed: return False res = db.session.query(proposal_follower) \ .filter_by(user_id=authed.id, proposal_id=self.id) \ .count() if res: return True return False
def get_ccr(ccr_id): ccr = CCR.query.filter_by(id=ccr_id).first() if ccr: if ccr.status != CCRStatus.LIVE: if CCR.status == CCRStatus.DELETED: return {"message": "CCR was deleted"}, 404 authed_user = get_authed_user() if authed_user.id != ccr.author.id: return {"message": "User cannot view this CCR"}, 404 return ccr_schema.dump(ccr) else: return {"message": "No CCR matching id"}, 404
def get_proposal(proposal_id): proposal = Proposal.query.filter_by(id=proposal_id).first() if proposal: if proposal.status == ProposalStatus.ARCHIVED: return {"message": "Proposal has been archived"}, 401 if proposal.status not in [ ProposalStatus.LIVE, ProposalStatus.DISCUSSION ]: if proposal.status == ProposalStatus.DELETED: return {"message": "Proposal was deleted"}, 404 authed_user = get_authed_user() team_ids = list(x.id for x in proposal.team) if not authed_user or authed_user.id not in team_ids: return {"message": "User cannot view this proposal"}, 404 return proposal_schema.dump(proposal) else: return {"message": "No proposal matching id"}, 404
def get_proposal(proposal_id): proposal = Proposal.query.filter_by(id=proposal_id).first() if proposal: authed_user = get_authed_user() team_ids = list(x.id for x in proposal.team) authed_in_team = authed_user and authed_user.id in team_ids if proposal.status != ProposalStatus.LIVE: if proposal.status == ProposalStatus.DELETED: return {"message": "Proposal was deleted"}, 404 if not authed_in_team: return {"message": "User cannot view this proposal"}, 404 if proposal.private: if not authed_in_team: return {"message": "Proposal is private"}, 404 return proposal_schema.dump(proposal) else: return {"message": "No proposal matching id"}, 404
def get_by_user(user, statuses=[ProposalStatus.LIVE]): from grant.utils.auth import get_authed_user authed = get_authed_user() status_filter = or_(Proposal.status == v for v in statuses) res = Proposal.query \ .join(proposal_team) \ .filter(proposal_team.c.user_id == user.id) \ .filter(status_filter) \ .all() # only team members get to see private proposals without_priv = [] for p in res: if p.private: if authed and authed.id in [t.id for t in p.team]: without_priv.append(p) else: without_priv.append(p) return without_priv
def post_proposal_contribution(proposal_id, amount, private): proposal = Proposal.query.filter_by(id=proposal_id).first() if not proposal: return {"message": "No proposal matching id"}, 404 code = 200 user = get_authed_user() contribution = None if user: contribution = ProposalContribution \ .get_existing_contribution(user.id, proposal_id, amount, private) if not contribution: code = 201 contribution = proposal.create_contribution( amount=amount, private=private, user_id=user.id if user else None, ) dumped_contribution = proposal_contribution_schema.dump(contribution) return dumped_contribution, code
def authed_worker(self): authed = get_authed_user() if not authed: return None return get(self.workers, 'user_id', authed.id, None)
def is_self(self): authed = get_authed_user() if authed: return authed.id == self.user.id return False
def grantio_authed(response): response.headers["X-Grantio-Authed"] = 'yes' if get_authed_user( ) else 'no' return response
def get_user(user_id, with_proposals, with_comments, with_funded, with_pending, with_arbitrated, with_requests, with_rejected_permanently): user = User.get_by_id(user_id) if user: result = user_schema.dump(user) authed_user = auth.get_authed_user() is_self = authed_user and authed_user.id == user.id if with_requests: requests = CCR.get_by_user(user) requests_dump = ccrs_schema.dump(requests) result["requests"] = requests_dump if with_proposals: proposals = Proposal.get_by_user(user) proposals_dump = user_proposals_schema.dump(proposals) result["proposals"] = proposals_dump if with_funded: contributions = ProposalContribution.get_by_userid(user_id) if not authed_user or user.id != authed_user.id: contributions = [ c for c in contributions if c.status == ContributionStatus.CONFIRMED ] contributions = [c for c in contributions if not c.private] contributions = [ c for c in contributions if c.proposal.status == ProposalStatus.LIVE ] contributions_dump = user_proposal_contributions_schema.dump( contributions) result["contributions"] = contributions_dump if with_comments: comments = Comment.get_by_user(user) comments_dump = user_comments_schema.dump(comments) result["comments"] = comments_dump if with_pending and is_self: pending_proposals = Proposal.get_by_user(user, [ ProposalStatus.STAKING, ProposalStatus.PENDING, ProposalStatus.APPROVED, ProposalStatus.REJECTED, ]) pending_proposals_dump = user_proposals_schema.dump( pending_proposals) result["pendingProposals"] = pending_proposals_dump pending_ccrs = CCR.get_by_user(user, [ CCRStatus.PENDING, CCRStatus.APPROVED, CCRStatus.REJECTED, ]) pending_ccrs_dump = ccrs_schema.dump(pending_ccrs) result["pendingRequests"] = pending_ccrs_dump if with_arbitrated and is_self: result["arbitrated"] = user_proposal_arbiters_schema.dump( user.arbiter_proposals) if with_rejected_permanently and is_self: rejected_proposals = Proposal.get_by_user( user, [ProposalStatus.REJECTED_PERMANENTLY]) result[ "rejectedPermanentlyProposals"] = user_proposals_schema.dump( rejected_proposals) rejected_ccrs = CCR.get_by_user(user, [ CCRStatus.REJECTED_PERMANENTLY, ]) result["rejectedPermanentlyRequests"] = ccrs_schema.dump( rejected_ccrs) return result else: message = "User with id matching {} not found".format(user_id) return {"message": message}, 404