# Path: narrative/api.py
# class DatumResource(ModelResource):
#     class Meta:
#         queryset = Datum.objects.all()
#         urlconf_namespace = 'narrative'
#         resource_name = 'datum'
#         authorization = Authorization()
#         authentication = ApiKeyAuthentication()
#         allowed_methods = ['get', 'post']
#         always_return_data = True
# 
#     def build_filters(self, filters=None):
#         filters = filters or {}
# 
#         orm_filters = super(DatumResource, self).build_filters(filters)
# 
#         if 'level' in filters:
#             status = ''
#             try:
#                 status = DatumLogLevel.status_by_name(filters['level'].title())
#             except:
#                 pass
#             orm_filters['log_level'] = status
# 
#         return orm_filters
# 
#     def hydrate(self, bundle):
#         # Convert log level from string to int
#         if type(bundle.data.get('log_level')) in [six.binary_type, six.string_types]:
#             bundle.data['log_level'] = DatumLogLevel.status_by_name(bundle.data.get('log_level'))
# 
#         ttl = settings.DEFAULT_NARRATIVE_DATUM_TTL
# 
#         # Override the default ttl if one is provided
#         if bundle.data.get('ttl'):
#             ttl = timedelta(seconds=bundle.data.get('ttl'))
# 
#         bundle.data['datum_note_json'] = json.dumps(bundle.data.get('note', ''))
#         bundle.data['expiration_time'] = self.get_utc_now() + ttl
# 
#         return bundle
# 
#     def get_utc_now(self):
#         return datetime.datetime.utcnow()
# 
# Path: narrative/models.py
# class NarrativeConfig(models.Model):
#     # Determines what the minimum log_level allowed to be created by the log_datum function
#     minimum_datum_log_level = models.IntegerField(
#         choices=DatumLogLevel.types, default=DatumLogLevel.INFO)
# 
#     objects = NarrativeConfigManager()
# 
# class DatumLogLevel(StatusType):
#     TRACE = 0
#     DEBUG = 1
#     INFO = 2
#     WARN = 3
#     ERROR = 4
# 
#     types = (
#         (TRACE, 'Trace'),
#         (ERROR, 'Error'),
#         (WARN, 'Warn'),
#         (INFO, 'Info'),
#         (DEBUG, 'Debug'),
#     )
# 
#     @classmethod
#     def status_by_name(cls, name):
#         status = find_tuple(cls.types, name.title(), 1)
#         return status[0] if status is not None else NarrativeConfig.objects.get_minimum_log_level()


import json
import six
from django.http import HttpResponseBadRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from tastypie.exceptions import ImmediateHttpResponse
from narrative.api import DatumResource
from narrative.models import NarrativeConfig, DatumLogLevel


class LogView(View):
    """

    """

    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        """
        This method is extended simply to add the csrf_exempt decorator
        """
        return super(LogView, self).dispatch(request, *args, **kwargs)

    def post(self, request):
        """

        @return: A json response
        """
        # Get the minimum log level

# Path: narrative/api.py
# class DatumResource(ModelResource):
#     class Meta:
#         queryset = Datum.objects.all()
#         urlconf_namespace = 'narrative'
#         resource_name = 'datum'
#         authorization = Authorization()
#         authentication = ApiKeyAuthentication()
#         allowed_methods = ['get', 'post']
#         always_return_data = True
# 
#     def build_filters(self, filters=None):
#         filters = filters or {}
# 
#         orm_filters = super(DatumResource, self).build_filters(filters)
# 
#         if 'level' in filters:
#             status = ''
#             try:
#                 status = DatumLogLevel.status_by_name(filters['level'].title())
#             except:
#                 pass
#             orm_filters['log_level'] = status
# 
#         return orm_filters
# 
#     def hydrate(self, bundle):
#         # Convert log level from string to int
#         if type(bundle.data.get('log_level')) in [six.binary_type, six.string_types]:
#             bundle.data['log_level'] = DatumLogLevel.status_by_name(bundle.data.get('log_level'))
# 
#         ttl = settings.DEFAULT_NARRATIVE_DATUM_TTL
# 
#         # Override the default ttl if one is provided
#         if bundle.data.get('ttl'):
#             ttl = timedelta(seconds=bundle.data.get('ttl'))
# 
#         bundle.data['datum_note_json'] = json.dumps(bundle.data.get('note', ''))
#         bundle.data['expiration_time'] = self.get_utc_now() + ttl
# 
#         return bundle
# 
#     def get_utc_now(self):
#         return datetime.datetime.utcnow()
# 
# Path: narrative/models.py
# class NarrativeConfig(models.Model):
#     # Determines what the minimum log_level allowed to be created by the log_datum function
#     minimum_datum_log_level = models.IntegerField(
#         choices=DatumLogLevel.types, default=DatumLogLevel.INFO)
# 
#     objects = NarrativeConfigManager()
# 
# class DatumLogLevel(StatusType):
#     TRACE = 0
#     DEBUG = 1
#     INFO = 2
#     WARN = 3
#     ERROR = 4
# 
#     types = (
#         (TRACE, 'Trace'),
#         (ERROR, 'Error'),
#         (WARN, 'Warn'),
#         (INFO, 'Info'),
#         (DEBUG, 'Debug'),
#     )
# 
#     @classmethod
#     def status_by_name(cls, name):
#         status = find_tuple(cls.types, name.title(), 1)
#         return status[0] if status is not None else NarrativeConfig.objects.get_minimum_log_level()


# Path: narrative/views.py
import json
import six
from django.http import HttpResponseBadRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from tastypie.exceptions import ImmediateHttpResponse
from narrative.api import DatumResource
from narrative.models import NarrativeConfig, DatumLogLevel



class LogView(View):
    """

    """

    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        """
        This method is extended simply to add the csrf_exempt decorator
        """
        return super(LogView, self).dispatch(request, *args, **kwargs)

    def post(self, request):
        """

        @return: A json response
        """
        # Get the minimum log level

        minimum_log_level = NarrativeConfig.objects.get_minimum_log_level()