Exemplo n.º 1
0
def snapshots_refresh(limit: int = 10, days: int = 7):
    symbols = all_nyse_symbols()
    random.shuffle(symbols)
    subset = []
    for symbol in symbols:
        # Find or Populate Companies from list
        company = Company.query.filter_by(symbol=symbol).first()
        if not company:
            company = Company.make(symbol)
            db.session.add(company)
            db.session.flush()
        # Check for latest snapshot
        latest_snapshot = (Snapshot.query.filter_by(
            company_id=company.id).order_by(
                Snapshot.creation_time.desc()).first())
        if not latest_snapshot or latest_snapshot.stale(days):
            subset.append(company)
        if len(subset) >= limit:
            break

    successes = []
    errors = []
    for company in subset:
        logger.info(f"Refreshing snapshot for {company.symbol}.")
        try:
            company.refresh_latest_snapshot()
            previous_failure = (SnapshotFailure.query.filter_by(
                symbol=company.symbol).first())
            if previous_failure:
                previous_failure.delete()
                db.session.commit()

            successes.append(company.symbol)

        except Exception as e:
            errors.append(company.symbol)
            failure = SnapshotFailure.make(company.symbol)
            db.session.add(failure)
            db.session.commit()

    out = {
        "name": "referrals:process_targets",
        "completed-at": str(datetime.now()),
        "args": args,
        "errors": errors,
        "successes": successes,
    }
    return out
Exemplo n.º 2
0
def api_company(symbol):
    auth_header = request.headers.get("Authorization", "").split(" ")
    if len(auth_header) != 2:
        abort(403)
    bearer = auth_header[0]
    token = auth_header[1]

    if not bearer == "Bearer":
        abort(403)

    if token not in app.config["API_BEARER_TOKENS"]:
        abort(403)

    if request.method == "GET":
        company = Company.query.filter_by(symbol=symbol).first()
        if not company:
            company = Company.make(symbol)
            db.session.add(company)
            db.session.flush()
        if not company:
            return json.dumps({})

        company = company.refresh_latest_snapshot()
        return json.dumps({"company": company.repr_dict()})
Exemplo n.º 3
0
def dashboard():
    if request.method == "POST":
        _list = current_user.lists.first()
        if not _list:
            _list = List.make(current_user)
            db.session.add(_list)
            db.session.flush()

        symbol = request.form.to_dict().get("symbol")
        # TODO: check if valid symbol here, capitalization too
        company = Company.query.filter_by(symbol=symbol).first()
        if not company:
            company = Company.make(symbol)
            db.session.add(company)
            db.session.flush()

        _list.add_company(company)

        snapshot = (Snapshot.query.filter_by(company_id=company.id).order_by(
            Snapshot.creation_time.desc()).first())
        if not snapshot:
            snapshot = Snapshot.make(symbol, company)
            if snapshot:
                db.session.add(snapshot)
                db.session.commit()
            else:
                db.session.rollback()

        db.session.commit()
        companies = []
        if _list:
            companies = _list.companies()
        return Company.repr_card_grid(_list.companies())

    if request.method == "DELETE":
        target = request.form.get("target")

        _list = current_user.lists.first()
        if not _list:
            return ""  # should not happen

        company = Company.query.filter_by(id=target).first()
        _list.remove_company(company)
        db.session.commit()
        return ""

    if request.method == "PUT":
        target = request.form.get("target")
        company = Company.query.filter_by(id=target).first()
        if not company:
            return ""

        latest_snapshot = (Snapshot.query.filter_by(
            company_id=company.id).order_by(
                Snapshot.creation_time.desc()).first())
        if not latest_snapshot.stale():
            return company.repr_card()

        snapshot = Snapshot.make(company.symbol, company)
        if snapshot:
            db.session.add(snapshot)
            db.session.commit()
        else:
            db.session.rollback()

        return company.repr_card()

    _list = current_user.lists.first()
    if not _list:
        _list = List.make(current_user)
        db.session.add(_list)
        db.session.commit()
    card_grid = Company.repr_card_grid(_list.companies())
    return render_template("dashboard.html", card_grid=card_grid)