Beispiel #1
0
 def funds_target_usd(self):
     from funding.bin.utils import Summary, coin_to_usd
     prices = Summary.fetch_prices()
     if not prices:
         return
     return coin_to_usd(amt=self.funds_target,
                        btc_per_coin=prices['coin-btc'],
                        usd_per_btc=prices['btc-usd'])
Beispiel #2
0
    def balance(self):
        """This property retrieves the current funding status
        of this proposal. It uses Redis cache to not spam the
        daemon too much. Returns a nice dictionary containing
        all relevant proposal funding info"""
        from funding.bin.utils import Summary, coin_to_usd
        from funding.factory import cache, db_session
        rtn = {'sum': 0.0, 'txs': [], 'pct': 0.0, 'available': 0.0}

        cache_key = 'coin_balance_pid_%d' % self.id
        data = cache.get(cache_key)
        if not data:
            from funding.bin.daemon import Daemon
            try:
                data = Daemon().get_transfers_in(index=self.id,
                                                 proposal_id=self.id)
                if not isinstance(data, dict):
                    print('error; get_transfers_in; %d' % self.id)
                    return rtn
                cache.set(cache_key, data=data, expiry=300)
            except:
                print('error; get_transfers_in; %d' % self.id)
                return rtn

        prices = Summary.fetch_prices()
        for tx in data['txs']:
            if prices:
                tx['amount_usd'] = coin_to_usd(amt=tx['amount_human'],
                                               btc_per_coin=prices['coin-btc'],
                                               usd_per_btc=prices['btc-usd'])
            tx['datetime'] = datetime.fromtimestamp(tx['timestamp'])

        if data.get('sum', 0.0):
            data['pct'] = 100 / float(self.funds_target / data.get('sum', 0.0))
            data['available'] = data['sum']
        else:
            data['pct'] = 0.0
            data['available'] = 0.0

        if data['pct'] != self.funds_progress:
            self.funds_progress = data['pct']
            db_session.commit()
            db_session.flush()

        if data['available']:
            data['remaining_pct'] = 100 / float(
                data['sum'] / data['available'])
        else:
            data['remaining_pct'] = 0.0

        return data
Beispiel #3
0
def templating():
    from flask.ext.login import current_user
    recent_comments = db_session.query(Comment).filter(
        Comment.automated == False).order_by(
            Comment.date_added.desc()).limit(8).all()
    summary_data = Summary.fetch_stats()
    newest_users = db_session.query(User).filter(User.admin == False).order_by(
        User.registered_on.desc()).limit(5).all()
    return dict(logged_in=current_user.is_authenticated,
                current_user=current_user,
                funding_categories=settings.FUNDING_CATEGORIES,
                funding_statuses=settings.FUNDING_STATUSES,
                summary_data=summary_data,
                recent_comments=recent_comments,
                newest_users=newest_users)
Beispiel #4
0
    def spends(self):
        from funding.bin.utils import Summary, coin_to_usd
        from funding.factory import cache, db_session
        rtn = {'sum': 0.0, 'txs': [], 'pct': 0.0, 'spent': 0.0}

        cache_key = 'coin_spends_pid_%d' % self.id
        data = cache.get(cache_key)
        if not data:
            from funding.bin.daemon import Daemon
            try:
                data = Daemon().get_transfers_out(index=self.id,
                                                  proposal_id=self.id)
                if not isinstance(data, dict):
                    print('error; get_transfers_out; %d' % self.id)
                    return rtn
                cache.set(cache_key, data=data, expiry=300)
            except:
                print('error; get_transfers_out; %d' % self.id)
                return rtn

        prices = Summary.fetch_prices()
        for tx in data['txs']:
            if prices:
                tx['amount_usd'] = coin_to_usd(amt=tx['amount_human'],
                                               btc_per_coin=prices['coin-btc'],
                                               usd_per_btc=prices['btc-usd'])
            tx['datetime'] = datetime.fromtimestamp(tx['timestamp'])

        if data.get('sum', 0.0):
            data['pct'] = 100 / float(self.funds_target / data.get('sum', 0.0))
            data['spent'] = data['sum']
        else:
            data['pct'] = 0.0
            data['spent'] = 0.0

        if data['spent']:
            data['remaining_pct'] = 100 / float(data['sum'] / data['spent'])
        else:
            data['remaining_pct'] = 0.0

        return data
Beispiel #5
0
def proposal_api_add(title, content, pid, funds_target, addr_receiving, category, status):
    import markdown2

    if current_user.is_anonymous:
        return make_response(jsonify('err'), 500)

    if len(title) <= 8:
        return make_response(jsonify('title too short'), 500)

    if len(content) <= 20:
        return make_response(jsonify('content too short'), 500)

    if category and category not in settings.FUNDING_CATEGORIES:
        return make_response(jsonify('unknown category'), 500)

    if status not in settings.FUNDING_STATUSES.keys():
        make_response(jsonify('unknown status'), 500)

    if status != 1 and not current_user.admin:
        return make_response(jsonify('no rights to change status'), 500)

    try:
        from funding.bin.anti_xss import such_xss
        content_escaped = such_xss(content)
        html = markdown2.markdown(content_escaped, safe_mode=True)
    except Exception as ex:
        return make_response(jsonify('markdown error'), 500)



    if pid:
        p = Proposal.find_by_id(pid=pid)
        if not p:
            return make_response(jsonify('proposal not found'), 500)

        if p.user.id != current_user.id and not current_user.admin:
            return make_response(jsonify('no rights to edit this proposal'), 500)

        p.headline = title
        p.content = content
        p.html = html
        if addr_receiving:
            p.addr_receiving = addr_receiving
        if category:
            p.category = category

        # detect if an admin moved a proposal to a new status and auto-comment
        if p.status != status and current_user.admin:
            msg = "Moved to status \"%s\"." % settings.FUNDING_STATUSES[status].capitalize()
            try:
                Comment.add_comment(user_id=current_user.id, message=msg, pid=pid, automated=True)
            except:
                pass

        p.status = status
        p.last_edited = datetime.now()


    else:
        try: 
            funds_target = float(funds_target) 
        except Exception as ex:
            return make_response(jsonify('letters detected'),500)
        if funds_target < 1:
                return make_response(jsonify('Proposal asking less than 1 error :)'), 500)
        if len(addr_receiving) != 95:
            return make_response(jsonify('Faulty address, should be of length 95'), 500)

        p = Proposal(headline=title, content=content, category='misc', user=current_user)
        proposalID = current_user
        addr_donation = Proposal.generate_proposal_subaccount(proposalID)
        p.addr_donation = addr_donation
        p.html = html
        p.last_edited = datetime.now()
        p.funds_target = funds_target
        p.addr_receiving = addr_receiving
        p.category = category
        p.status = status
        db_session.add(p)
    


    db_session.commit()
    db_session.flush()

    # reset cached statistics
    from funding.bin.utils import Summary
    Summary.fetch_stats(purge=True)

    return make_response(jsonify({'url': url_for('proposal', pid=p.id)}))
Beispiel #6
0
def api_coin_usd(amount):
    from funding.bin.utils import Summary, coin_to_usd
    prices = Summary.fetch_prices()
    return jsonify(usd=coin_to_usd(amt=amount,
                                   btc_per_coin=prices['coin-btc'],
                                   usd_per_btc=prices['btc-usd']))
Beispiel #7
0
    def balance(self):
        """This property retrieves the current funding status
        of this proposal. It uses Redis cache to not spam the
        daemon too much. Returns a nice dictionary containing
        all relevant proposal funding info"""
        from funding.bin.utils import Summary, coin_to_usd
        from funding.factory import db
        rtn = {'sum': 0.0, 'txs': [], 'pct': 0.0, 'available': 0}

        if self.archived:
            return rtn

        try:
            r = requests.get(
                f'http://{settings.RPC_HOST}:{settings.RPC_PORT}/json_rpc',
                json={
                    "jsonrpc": "2.0",
                    "id": "0",
                    "method": "get_payments",
                    "params": {
                        "payment_id": self.payment_id
                    }
                })
            r.raise_for_status()
            blob = r.json()

            assert 'result' in blob
            assert 'payments' in blob['result']
            assert isinstance(blob['result']['payments'], list)
        except Exception as ex:
            return rtn

        txs = blob['result']['payments']
        for tx in txs:
            tx['amount_human'] = float(tx['amount']) / 1e11
            tx['txid'] = tx['tx_hash']
            tx['type'] = 'in'

        data = {
            'sum': sum([float(z['amount']) / 1e11 for z in txs]),
            'txs': txs
        }

        if not isinstance(data, dict):
            print('error; get_transfers_in; %d' % self.id)
            return rtn

        prices = Summary.fetch_prices()
        for tx in data['txs']:
            if prices:
                tx['amount_usd'] = coin_to_usd(amt=tx['amount_human'],
                                               btc_per_coin=prices['coin-btc'],
                                               usd_per_btc=prices['btc-usd'])

        if data.get('sum', 0.0):
            data['pct'] = 100 / float(self.funds_target / data.get('sum', 0.0))
            data['available'] = data['sum']
        else:
            data['pct'] = 0.0
            data['available'] = 0.0

        if data['pct'] != self.funds_progress:
            self.funds_progress = data['pct']
            db.session.commit()
            db.session.flush()

        if data['available']:
            data['remaining_pct'] = 100 / float(
                data['sum'] / data['available'])
        else:
            data['remaining_pct'] = 0.0

        return data