def post(self): data = request.get_json() if Indicator.query.filter(Indicator.object == data['object']).first(): return { 'error': 'indicator {} already exists'.format(data['object']) }, 409 elif not helpers.valid_type(data['type']): return { 'error': 'indicator {} is not of valid type'.format(data['object']) }, 400 elif not helpers.valid_diamond_model(data['diamondmodel']): return { 'error': 'indicator {} has invalid dimond model {}'.format( data['object'], data['diamondmodel']) }, 400 else: indicator = Indicator(data['object'], data['type'], data['firstseen'], data['lastseen'], data['diamondmodel'], data['campaign'], data['confidence'], data['comments'], data['tags'], None) db_session.add(indicator) db_session.commit() indicators = Indicator.query.filter( Indicator.object == data['object']).first() return {'indicator': helpers.row_to_dict(indicators)}, 201
def update_quotes(self, ticker, check_all=True): """ Get all missing quotes through current day for the given stock """ ticker = ticker.lower() stockquotes = None session = self.db.Session() last = session.query(Quote).filter_by(Ticker=ticker).order_by( desc(Quote.Date)).first().Date start_date = last + timedelta(days=1) # Ignore missing quotes for today unless it's after 7, this keeps # us from hitting the yahoo API when we know the data isn't there yet end_date = (date.today() if datetime.datetime.now().time() > datetime.time(19) else date.today() - timedelta(days=1)) if end_date > start_date: stockquotes = self._download_quotes(ticker, start_date, end_date) # Appease the API rate limit gods???? time.sleep(10) if stockquotes is not None: for quote in stockquotes: quote.Features = Indicator(quote.Id) session.add_all(stockquotes) #indicators.update_all(ticker, session, False, check_all) indicators.update_all(ticker, session, True, check_all) session.commit() session.close()
def add_stock(self, ticker, name=None, exchange=None, sector=None, industry=None): """ Add a stock to the stock database Add the stock to the symbols table and populate quotes table with all available historical quotes. If any of the optional parameters are left out, the corresponding information will be obtained from Yahoo! Finance. :param ticker: Stock ticker symbol :param name: (optional) Company/security name :param exchange: (optional) Exchange on which the security is traded :param sector: (optional) Company/security sector :param Industry (optional) Company/security industry """ ticker = ticker.lower() session = self.db.Session() if self.check_stock_exists(ticker, session): print "Stock %s already exists!" % (ticker.upper()) return if name is None: name = quotes.get_name(ticker) if exchange is None: exchange = quotes.get_stock_exchange(ticker) if sector is None: sector = quotes.get_sector(ticker) if industry is None: industry = quotes.get_industry(ticker) stock = Symbol(ticker, name, exchange, sector, industry) session.add(stock) q = self._download_quotes(ticker, date(1900, 01, 01), date.today()) for quote in q: quote.Features = Indicator(quote.Id) session.add_all(q) session.commit() session.close() self.update_quotes(ticker)
def indicator_create(request, id=0): """ CREATE AN INDICATOR USING A TEMPLATE FIRST """ getIndicatorTypes = IndicatorType.objects.all() getCountries = Country.objects.all() countries = getCountry(request.user) country_id = Country.objects.get(country=countries[0]).id getPrograms = Program.objects.all().filter( funding_status="Funded", country__in=countries).distinct() getServices = ExternalService.objects.all() program_id = id if request.method == 'POST': #set vars from form and get values from user type = IndicatorType.objects.get(indicator_type="custom") country = Country.objects.get(id=request.POST['country']) program = Program.objects.get(id=request.POST['program']) service = request.POST['services'] level = Level.objects.all()[0] node_id = request.POST['service_indicator'] owner = request.user sector = None name = None source = None definition = None external_service_record = None #import recursive library for substitution import re print node_id #checkfor service indicator and update based on values if node_id != None and int(node_id) != 0: getImportedIndicators = import_indicator(service) for item in getImportedIndicators: if item['nid'] == node_id: getSector, created = Sector.objects.get_or_create( sector=item['sector']) sector = getSector getLevel, created = Level.objects.get_or_create( name=item['level'].title()) level = getLevel name = item['title'] source = item['source'] definition = item['definition'] #replace HTML tags if they are in the string definition = re.sub("<.*?>", "", definition) getService = ExternalService.objects.get(id=service) full_url = getService.url + "/" + item['nid'] external_service_record = ExternalServiceRecord( record_id=item['nid'], external_service=getService, full_url=full_url) external_service_record.save() getType, created = IndicatorType.objects.get_or_create( indicator_type=item['type'].title()) type = getType #save form new_indicator = Indicator( country=country, owner=owner, sector=sector, name=name, source=source, definition=definition, external_service_record=external_service_record) new_indicator.save() new_indicator.program.add(program) new_indicator.indicator_type.add(type) new_indicator.level.add(level) latest = new_indicator.id #redirect to update page messages.success(request, 'Success, Basic Indicator Created!') redirect_url = '/indicators/indicator_update/' + str(latest) + '/' return HttpResponseRedirect(redirect_url) # send the keys and vars from the json data to the template along with submitted feed info and silos for new form return render( request, "indicators/indicator_create.html", { 'country_id': country_id, 'program_id': int(program_id), 'getCountries': getCountries, 'getPrograms': getPrograms, 'getIndicatorTypes': getIndicatorTypes, 'getServices': getServices })