import requests
import requests_cache
from core.utils import get_env_variable

from requests_futures.sessions import FuturesSession

from .models import Legislator, Organization, Rating, NPRStory

# retrieve the API keys from environment variables
OPEN_SECRETS_API = get_env_variable('OPEN_SECRETS_API')
VOTE_SMART_API = get_env_variable('VOTE_SMART_API')
NPR_API = get_env_variable('NPR_API')

# set up a requests-cache for requests that will last for half an hour.
# This may need to be commented out if using a network that adds redirects to the header.
requests_cache.install_cache(
    'legislators_cache',
    backend='sqlite',
    expire_after=1800)


def verify_api_response(request):
    """Checks for HTTPError from API calls"""
    # make sure we get a 200 response
    try:
        request.raise_for_status()
    except requests.exceptions.HTTPError as e:
        # this will catch anything that isn't a 2XX (4XX, 5XX)
        return "Error: {}".format(e)

    return request
Ejemplo n.º 2
0
from core.utils import get_env_variable

DATABASES = {
    'default': {
        'ENGINE': get_env_variable('DB_ENGINE'), #'django.db.backends.postgresql_psycopg2',
        'NAME': get_env_variable('DB_NAME'),
        'USER': get_env_variable('DB_USER'),
        'PASSWORD': get_env_variable('DB_PW'),
        'HOST': get_env_variable('DB_HOST'), #'localhost'
        'PORT': get_env_variable('DB_PORT'), #'', # Set to empty string for default.,
        'OPTIONS': {
            'autocommit': True,
        },
    }
}

Ejemplo n.º 3
0
import requests

from core.utils import get_env_variable

OPEN_SECRETS_API = get_env_variable("OPEN_SECRETS_API")


def get_legislators(state):
    """Returns a legislator list from the OpenSecrets API"""
    payload = {'id': 'state', 'apikey': OPEN_SECRETS_API}
    request = requests.get(
        'http://www.opensecrets.org/api/?method=getLegislators',
        params=payload)
    return request
Ejemplo n.º 4
0
from __future__ import absolute_import

from celery import Celery
import os
import logging
logger = logging.getLogger(__name__)

from core.utils import get_env_variable
from django.conf import settings

os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE', get_env_variable('DJANGO_SETTINGS_MODULE')
)

app = Celery('run')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda:  settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    logger.info('Request: {0!r}'.format(self.request))

Ejemplo n.º 5
0
import os

from core.utils import get_env_variable

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_env_variable('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
Ejemplo n.º 6
0
def test_get_env_variable_success():
    os.environ["_TEST_ENV_VARIABLE_"] = "_TEST_ENV_VARIABLE_VALUE_"
    env_variable = utils.get_env_variable("_TEST_ENV_VARIABLE_")
    assert env_variable == "_TEST_ENV_VARIABLE_VALUE_"
Ejemplo n.º 7
0
def test_get_env_variable_failure():
    with pytest.raises(ImproperlyConfigured):
        utils.get_env_variable("_NOT_EXISTED_VARIABLE_")
Ejemplo n.º 8
0
#########################################
#   Asyncronous Messaging Services      #
#########################################
from core.utils import get_env_variable
##
## CELERY SECTION
##
BROKER_URL = get_env_variable('AMQP_MESSAGE_BROKER_URL')
CELERY_DISABLE_RATE_LIMITS = True
CELERY_RESULT_BACKEND = 'amqp'
CELERY_TASK_RESULT_EXPIRES = 18000  # 5 hours.
CELERY_RESULT_PERSISTENT = True
CELERY_CHORD_PROPAGATES = True #propagate errors/exceptions in the chain