# Path: wham/apis/twitter/twitter_bearer_auth.py
# class BearerAuth(requests.auth.AuthBase):
#     """Request bearer access token for oAuth2 authentication.
# 
#     :param consumer_key: Twitter application consumer key
#     :param consumer_secret: Twitter application consumer secret
#     :param proxies: Dictionary of proxy URLs (see documentation for python-requests).
#     """
# 
#     def __init__(self, consumer_key, consumer_secret, proxies=None):
#         self._consumer_key = consumer_key
#         self._consumer_secret = consumer_secret
#         self.proxies = proxies
#         self._bearer_token = self._get_access_token()
# 
#     def _get_access_token(self):
#         token_url = '%s://%s.%s/%s' % (PROTOCOL,
#                                        REST_SUBDOMAIN,
#                                        DOMAIN,
#                                        OAUTH2_TOKEN_ENDPOINT)
#         auth = self._consumer_key + ':' + self._consumer_secret
#         b64_bearer_token_creds = base64.b64encode(auth.encode('utf8'))
#         params = {'grant_type': 'client_credentials'}
#         headers = {}
#         headers['User-Agent'] = USER_AGENT
#         headers['Authorization'] = 'Basic ' + \
#                                    b64_bearer_token_creds.decode('utf8')
#         headers[
#             'Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
#         try:
#             response = requests.post(
#                 token_url,
#                 params=params,
#                 headers=headers,
#                 proxies=self.proxies)
# 
#             data = response.json()
#             return data['access_token']
#         except Exception as e:
#             raise Exception(
#                 'Error while requesting bearer access token: %s' %
#                 e)
# 
#     def __call__(self, r):
#         auth_list = [
#             self._consumer_key,
#             self._consumer_secret,
#             self._bearer_token]
#         if all(auth_list):
#             r.headers['Authorization'] = "Bearer %s" % self._bearer_token
#             return r
#         else:
#             raise Exception('Not enough keys passed to Bearer token manager.')
# 
# Path: wham/fields.py
# class WhamDateTimeField(WhamFieldMixin, models.DateTimeField):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_format = kwargs.pop('wham_format', None)
#         return super(WhamDateTimeField, self).__init__(*args, **kwargs)
# 
# class WhamForeignKey(models.ForeignKey):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_result_path = kwargs.pop('wham_result_path', None)
#         self.wham_endpoint = kwargs.pop('wham_endpoint', None)
#         self.wham_results_path = kwargs.pop('wham_results_path', ())
#         self.wham_pk_param = kwargs.pop('wham_pk_param', None)
#         self.wham_params = kwargs.pop('wham_params', {})
#         return super(WhamForeignKey, self).__init__(*args, **kwargs)
# 
#     def get_result_path(self):
#         result_path = self.wham_result_path
#         if not result_path:
#             return (self.name,)
#         else:
#             return result_path
# 
#     @property
#     def type_repr(self):
#         return 'foreign key'


from copy import deepcopy
from urllib import urlencode
from django.conf import settings
from django.db.models import ForeignKey
from django.template import Template, Context
from django.template.loader import render_to_string
from datetime import datetime
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from wham.apis.twitter.twitter_bearer_auth import BearerAuth as TwitterBearerAuth
from wham.fields import WhamDateTimeField, WhamForeignKey
import requests
import time
import string
    def get_field(self, field_name):
        return self.model._meta.get_field_by_name(field_name)[0]

    def dict_to_model_instance(self, dict):
        pass


    def get_from_dict(self, data, pk_dict_key='id'):

        field_names = self.get_field_names()

        kwargs = {}

        for field_name in field_names:
            field = self.get_field(field_name)
            result_path = field.get_result_path()
            try:
                value = dpath(data, result_path)
            except KeyError:
                pass
            else:
                if isinstance(field, WhamForeignKey):
                    fk_model_class = field.rel.to
                    if value is not None:
                        fk_instance = fk_model_class.objects.get_from_dict(value)
                    else:
                        fk_instance = None
                    kwargs[field_name] = fk_instance

                else:

# Path: wham/apis/twitter/twitter_bearer_auth.py
# class BearerAuth(requests.auth.AuthBase):
#     """Request bearer access token for oAuth2 authentication.
# 
#     :param consumer_key: Twitter application consumer key
#     :param consumer_secret: Twitter application consumer secret
#     :param proxies: Dictionary of proxy URLs (see documentation for python-requests).
#     """
# 
#     def __init__(self, consumer_key, consumer_secret, proxies=None):
#         self._consumer_key = consumer_key
#         self._consumer_secret = consumer_secret
#         self.proxies = proxies
#         self._bearer_token = self._get_access_token()
# 
#     def _get_access_token(self):
#         token_url = '%s://%s.%s/%s' % (PROTOCOL,
#                                        REST_SUBDOMAIN,
#                                        DOMAIN,
#                                        OAUTH2_TOKEN_ENDPOINT)
#         auth = self._consumer_key + ':' + self._consumer_secret
#         b64_bearer_token_creds = base64.b64encode(auth.encode('utf8'))
#         params = {'grant_type': 'client_credentials'}
#         headers = {}
#         headers['User-Agent'] = USER_AGENT
#         headers['Authorization'] = 'Basic ' + \
#                                    b64_bearer_token_creds.decode('utf8')
#         headers[
#             'Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
#         try:
#             response = requests.post(
#                 token_url,
#                 params=params,
#                 headers=headers,
#                 proxies=self.proxies)
# 
#             data = response.json()
#             return data['access_token']
#         except Exception as e:
#             raise Exception(
#                 'Error while requesting bearer access token: %s' %
#                 e)
# 
#     def __call__(self, r):
#         auth_list = [
#             self._consumer_key,
#             self._consumer_secret,
#             self._bearer_token]
#         if all(auth_list):
#             r.headers['Authorization'] = "Bearer %s" % self._bearer_token
#             return r
#         else:
#             raise Exception('Not enough keys passed to Bearer token manager.')
# 
# Path: wham/fields.py
# class WhamDateTimeField(WhamFieldMixin, models.DateTimeField):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_format = kwargs.pop('wham_format', None)
#         return super(WhamDateTimeField, self).__init__(*args, **kwargs)
# 
# class WhamForeignKey(models.ForeignKey):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_result_path = kwargs.pop('wham_result_path', None)
#         self.wham_endpoint = kwargs.pop('wham_endpoint', None)
#         self.wham_results_path = kwargs.pop('wham_results_path', ())
#         self.wham_pk_param = kwargs.pop('wham_pk_param', None)
#         self.wham_params = kwargs.pop('wham_params', {})
#         return super(WhamForeignKey, self).__init__(*args, **kwargs)
# 
#     def get_result_path(self):
#         result_path = self.wham_result_path
#         if not result_path:
#             return (self.name,)
#         else:
#             return result_path
# 
#     @property
#     def type_repr(self):
#         return 'foreign key'


# Path: wham/models.py
from copy import deepcopy
from urllib import urlencode
from django.conf import settings
from django.db.models import ForeignKey
from django.template import Template, Context
from django.template.loader import render_to_string
from datetime import datetime
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from wham.apis.twitter.twitter_bearer_auth import BearerAuth as TwitterBearerAuth
from wham.fields import WhamDateTimeField, WhamForeignKey
import requests
import time
import string

    def get_field(self, field_name):
        return self.model._meta.get_field_by_name(field_name)[0]

    def dict_to_model_instance(self, dict):
        pass


    def get_from_dict(self, data, pk_dict_key='id'):

        field_names = self.get_field_names()

        kwargs = {}

        for field_name in field_names:
            field = self.get_field(field_name)
            result_path = field.get_result_path()
            try:
                value = dpath(data, result_path)
            except KeyError:
                pass
            else:
                if isinstance(field, WhamForeignKey):
                    fk_model_class = field.rel.to
                    if value is not None:
                        fk_instance = fk_model_class.objects.get_from_dict(value)
                    else:
                        fk_instance = None
                    kwargs[field_name] = fk_instance

                else:

                    if isinstance(field, WhamDateTimeField):