Exemplo n.º 1
0
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
Exemplo n.º 2
0
def get_user(id):
    user_db = User.query.filter(User.id == id).first()
    if user_db:
        user = admin_user_schema.dump(user_db)
        user_proposals = Proposal.query.filter(Proposal.team.any(id=user['id'])).all()
        user['proposals'] = proposals_schema.dump(user_proposals)
        user_comments = Comment.get_by_user(user_db)
        user['comments'] = user_comments_schema.dump(user_comments)
        return user
    return {"message": f"Could not find user with id {id}"}, 404
Exemplo n.º 3
0
def create_proposals(count, category, stage, with_comments=False):
    user = User.query.filter_by().first()
    for i in range(count):
        target = "123.456"
        p = Proposal.create(
            stage=stage,
            status=ProposalStatus.LIVE,
            title=f'Fake Proposal #{i} {category} {stage}',
            content=f'My fake proposal content, numero {i}',
            brief=f'This is proposal {i} generated by e2e testing',
            category=category,
            target=target,
            payout_address="fake123",
            deadline_duration=100)
        p.date_published = datetime.now()
        p.team.append(user)
        db.session.add(p)
        db.session.flush()
        num_ms = randint(1, 9)
        for j in range(num_ms):
            m = Milestone(
                title=f'Fake MS {j}',
                content=
                f'Fake milestone #{j} on fake proposal #{i} {category} {stage}!',
                date_estimated=datetime.now(),
                payout_amount=str(float(target) / num_ms),
                immediate_payout=j == 0,
                proposal_id=p.id,
                index=j)
            db.session.add(m)
        # limit comment creation as it is slow
        if i == 0 and with_comments:
            for j in range(31):
                c = Comment(
                    proposal_id=p.id,
                    user_id=user.id,
                    parent_comment_id=None,
                    content=
                    f'Fake comment #{j} on fake proposal #{i} {category} {stage}!'
                )
                db.session.add(c)
        if stage == ProposalStage.FUNDING_REQUIRED:
            stake = p.create_contribution('1', None, True)
            stake.confirm('fakestaketxid', '1')
            db.session.add(stake)
            db.session.flush()
            fund = p.create_contribution('100', None, False)
            fund.confirm('fakefundtxid0', '100')
            db.session.add(fund)
            db.session.flush()
            p.status = ProposalStatus.LIVE
            db.session.add(p)
            db.session.flush()
Exemplo n.º 4
0
def post_proposal_comments(proposal_id, user_id, content):
    proposal = Proposal.query.filter_by(id=proposal_id).first()
    if proposal:
        comment = Comment(
            proposal_id=proposal_id,
            user_id=g.current_user.id,
            content=content
        )
        db.session.add(comment)
        db.session.commit()
        dumped_comment = comment_schema.dump(comment)
        return dumped_comment, 201
    else:
        return {"message": "No proposal matching id"}, 404
Exemplo n.º 5
0
def create_proposals(count):
    user = User.query.filter_by().first()
    for i in range(count):
        if i < 5:
            stage = ProposalStage.WIP
        else:
            stage = ProposalStage.COMPLETED
        p = Proposal.create(
            stage=stage,
            status=ProposalStatus.LIVE,
            title=f'Fake Proposal #{i}',
            content=f'My fake proposal content, numero {i}',
            brief=f'This is proposal {i} generated by "flask create-proposals"',
            category=Category.ACCESSIBILITY,
            target="123.456",
            payout_address="fake123",
            deadline_duration=100)
        p.date_published = datetime.datetime.now()
        p.team.append(user)
        p.date_approved = datetime.datetime.now()
        p.accepted_with_funding = True
        p.version = '2'
        p.fully_fund_contibution_bounty()
        db.session.add(p)
        db.session.flush()
        num_ms = randint(1, 9)
        for j in range(num_ms):
            m = Milestone(
                title=f'Fake MS {j}',
                content=f'Fake milestone #{j} on fake proposal #{i}!',
                days_estimated='10',
                payout_percent=str(floor(1 / num_ms * 100)),
                immediate_payout=j == 0,
                proposal_id=p.id,
                index=j)
            db.session.add(m)
        for j in range(100):
            c = Comment(proposal_id=p.id,
                        user_id=user.id,
                        parent_comment_id=None,
                        content=f'Fake comment #{j} on fake proposal #{i}!')
            db.session.add(c)

        Milestone.set_v2_date_estimates(p)
        db.session.add(p)

    db.session.commit()
    print(f'Added {count} LIVE fake proposals')
Exemplo n.º 6
0
def create_proposals(count):
    user = User.query.filter_by().first()
    for i in range(count):
        if i < 5:
            stage = ProposalStageEnum.WIP
        else:
            stage = ProposalStageEnum.COMPLETED
        target = str(randint(1, 10))
        p = Proposal.create(
            stage=stage,
            status=ProposalStatus.LIVE,
            title=f'Fake Proposal #{i}',
            content=f'My fake proposal content, numero {i}',
            brief=
            f'This is proposal {i} generated by "flask create-proposals", a useful tool for generating test proposal data.',
            category=Category.random(),
            target=target,
        )
        p.date_published = datetime.datetime.now()
        p.team.append(user)
        db.session.add(p)
        db.session.flush()

        num_ms = randint(1, 9)
        for j in range(num_ms):
            m = Milestone(
                title=f'Fake MS {j}',
                content=f'Fake milestone #{j} on fake proposal #{i}!',
                date_estimated=datetime.datetime.now(),
                payout_amount=str(float(target) / num_ms),
                immediate_payout=j == 0,
                proposal_id=p.id,
                index=j)
            db.session.add(m)
        for j in range(100):
            c = Comment(proposal_id=p.id,
                        user_id=user.id,
                        parent_comment_id=None,
                        content=f'Fake comment #{j} on fake proposal #{i}!')
            db.session.add(c)

    db.session.commit()
    print(f'Added {count} LIVE fake proposals')
Exemplo n.º 7
0
def post_proposal_comments(proposal_id, comment, parent_comment_id):
    # Make sure proposal exists
    proposal = Proposal.query.filter_by(id=proposal_id).first()
    if not proposal:
        return {"message": "No proposal matching id"}, 404

    if proposal.status != ProposalStatus.LIVE and proposal.status != ProposalStatus.DISCUSSION:
        return {
            "message":
            "Proposal must be live or open for public review to comment"
        }, 400

    # Make sure the parent comment exists
    parent = None
    if parent_comment_id:
        parent = Comment.query.filter_by(id=parent_comment_id).first()
        if not parent:
            return {"message": "Parent comment doesn’t exist"}, 400

    # Make sure user has verified their email
    if not g.current_user.email_verification.has_verified:
        return {"message": "Please confirm your email before commenting"}, 401

    # Make sure user is not silenced
    if g.current_user.silenced:
        return {
            "message":
            "Your account has been silenced, commenting is disabled."
        }, 403

    # Make the comment
    comment = Comment(proposal_id=proposal_id,
                      user_id=g.current_user.id,
                      parent_comment_id=parent_comment_id,
                      content=comment)
    db.session.add(comment)
    db.session.commit()
    dumped_comment = comment_schema.dump(comment)

    # Email proposal team if top-level comment
    if not parent:
        for member in proposal.team:
            send_email(
                member.email_address, 'proposal_comment', {
                    'author':
                    g.current_user,
                    'proposal':
                    proposal,
                    'comment_url':
                    make_url(
                        f'/proposals/{proposal.id}?tab=discussions&comment={comment.id}'
                    ),
                    'author_url':
                    make_url(f'/profile/{comment.author.id}'),
                })
    # Email parent comment creator, if it's not themselves
    if parent and parent.author.id != comment.author.id:
        send_email(
            parent.author.email_address, 'comment_reply', {
                'author':
                g.current_user,
                'proposal':
                proposal,
                'comment_url':
                make_url(
                    f'/proposals/{proposal.id}?tab=discussions&comment={comment.id}'
                ),
                'author_url':
                make_url(f'/profile/{comment.author.id}'),
            })

    return dumped_comment, 201
Exemplo n.º 8
0
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