Exemplo n.º 1
0
def index():
  catid = request.vars.get("catid")
  if catid == "None": catid = None
  start = int(request.vars.get("start", 0))
  num = min(1000, max(1, int(request.vars.get("num", 10))))
  is_root = catid is None
  
  sector = orm.session.query(orm.GoogleSector).filter(orm.GoogleSector.catid==catid).first()
  subsectors = sector and sector.children or []
  subsectors.sort(key=lambda x: x.name)

  ancestors = recursive_google_sector_ancestors(sector)
  ancestors.reverse()
  ancestors = filter(lambda ancestor: ancestor.catid is not None, ancestors)

  if is_root:
    company_q = orm.session.query(orm.GoogleCompany)
  else:
    company_q = recursive_google_sector_company_query(sector, orm)
  company_count = company_q.count()
  companies = company_q.order_by(orm.GoogleCompany.name).offset(start).limit(num)

  # Get list of page numbers to link to.
  href_fmt = u"?catid={catid}&start={offset}&num={num}".format(catid=catid, num=num, offset=u"{}")
  current_page, pages = get_pages(start, company_count, num)
  pagination = get_pagination(href_fmt, pages, current_page, num)

  # Construct left sidebar menu.
  navitems = response.sidebar_navitems.copy()
  navitems.enabled_items.insert(0, "sector")
  navitems.sector.expand = True
  navitems.sector.summary.active = True
  sidebar_menu = DIV(MENU(get_menuitems(navitems)), _class="nav-menu")

  return dict(
    message="hello from sector.py",
    sidebar_menu=sidebar_menu,
    sector=sector,
    subsectors=subsectors,
    ancestors=ancestors,
    companies=companies,
    current_company=start+1,
    through_company=min(start+num, company_count),
    company_count=company_count,
    is_root=is_root,
    pagination=pagination,
    current_page=current_page,
  )
Exemplo n.º 2
0
def financials():
  # Uniquely identify requested company by stock symbol.
  stock_symbol = request.vars.get("stock_symbol")
  # Offset for financial records to view.
  start = int(request.vars.get("start", 0))
  # Number of financial records to view.
  num = min(1000, max(1, int(request.vars.get("num", 4))))
  # Whether to view quarterly- or annual-period records.
  period = request.vars.get("period", "quarterly")
  if period != "quarterly": period = "annual"
  # Dict for constructing URL with request arguments.
  url_vars = dict(
    stock_symbol=stock_symbol,
    start=start,
    num=num,
    period=period,
  )

  # Query database for unique company with exact match for requested stock symbol.
  company = orm.session.query(orm.GoogleCompany).filter(orm.GoogleCompany.stock_symbol==stock_symbol).one()
  if company:
    # Query database for financials for this company.
    financials_q = orm.session.query(orm.NasdaqCompanyFinancials).filter(orm.NasdaqCompanyFinancials.company == company)
    # Filter and reverse-order financials by either annual or quarterly period ending.
    financials_q = filter_and_order_query_by_field_descending(
      financials_q,
        orm.NasdaqCompanyFinancials.quarter_ending
      if period == "quarterly" else
        orm.NasdaqCompanyFinancials.period_ending
    )
    # How many financials records did we find?
    financials_count = financials_q.count()
    # Limit and offset number of records to view.
    financials_q = financials_q.offset(start).limit(num)
    # Extract rows of financial data from financial objects.
    financials = [[getattr(financial, key) for key in financial_fields] for financial in financials_q]
    # Insert corresponding field names at top row.
    financials.insert(0, financial_fields)
    # Convert rows to columns.
    financials = itertools.izip_longest(*financials, fillvalue=None)
    # Filter-out empty rows.
    financials = [row for row in financials if filter(None, row[1:])]
  else:
    # No company found; return empty data.
    financials = tuple()
    financials_count = 0
    data = tuple()

  # Get list of page numbers to link to for more financials data..
  pagination_href_fmt = u"?stock_symbol={stock_symbol}&period={period}&start={offset}&num={num}".format(
    stock_symbol=stock_symbol,
    period=period,
    num=num,
    offset=u"{}"
  )
  current_page, pages = get_pages(start, financials_count, num)
  pagination = get_pagination(pagination_href_fmt, pages, current_page, num)

  # Setup a link to switch from quarterly to annual period view (and v.v.).
  switch_view_period_vars = url_vars.copy()
  switch_view_period_vars["period"] = "annual" if period == "quarterly" else "quarterly"
  switch_view_period_vars["start"]=0
  switch_view_period_link = A("{period} view".format(period=switch_view_period_vars["period"]), _href=URL("company", "financials", vars=switch_view_period_vars))

  # Construct left sidebar menu.
  navitems = response.sidebar_navitems.copy()
  navitems.enabled_items.insert(0, "company")
  navitems.company.expand = True
  navitems.company.financials.active = True
  navitems.company.summary.helper = URL("company", "index", vars=url_vars)
  navitems.company.financials.helper = URL("company", "financials", vars=url_vars)
  sidebar_menu = DIV(MENU(get_menuitems(navitems)), _class="nav-menu")

  return dict(
    message="hello from company.py",
    sidebar_menu=sidebar_menu,
    company=company,
    financials=financials,
    current_financial_stmt=start+1,
    through_financial_stmt=min(start+num, financials_count),
    financials_count=financials_count,
    pagination=pagination,
    current_page=current_page,
    period=period.capitalize(),
    switch_view_period_link=switch_view_period_link,
  )