# Path: narrative/models.py
# class Datum(models.Model):
#     # When was the datum created
#     timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
#     expiration_time = models.DateTimeField(null=True, blank=True, default=None)
#     # Origin of this datum; ie, what piece of software created it
#     origin = models.CharField(max_length=64)
#     datum_name = models.CharField(max_length=64)
# 
#     # Additional information about the datum; this is very datum specific
#     # This approach, storing json in a textfield and having an
#     # explicit accessor and mutator, is very ugly.  Really this is because
#     # we are storing non-structured/sparse data in a relational format.
#     datum_note_json = models.TextField(null=True, blank=True, default=None)
# 
#     # An ID to tie particular datums together
#     thread_id = models.CharField(max_length=36, null=True, blank=True)
# 
#     log_level = models.IntegerField(choices=DatumLogLevel.types, default=DatumLogLevel.INFO)
# 
#     objects = DatumManager()
# 
#     def __init__(self, *args, **kwargs):
#         if 'ttl' in kwargs:
#             kwargs['expiration_time'] = self.get_utc_now() + kwargs.pop('ttl')
# 
#         super(Datum, self).__init__(*args, **kwargs)
# 
#     def __str__(self):
#         note_snippet = self.datum_note_json[:50] if self.datum_note_json else ''
#         return 'origin:{0} datum_name:{1} note:{2}'.format(
#             self.origin, self.datum_name, note_snippet
#         )
# 
#     def get_note(self):
#         if self.datum_note_json:
#             return json.loads(self.datum_note_json)
#         else:
#             return []
# 
#     def set_note(self, note):
#         self.datum_note_json = json.dumps(note)
# 
#     def get_utc_now(self):
#         return utc_tz.localize(datetime.datetime.utcnow())
# 
#     @property
#     def timestamp_with_tz(self):
#         return utc_tz.localize(self.timestamp)
# 
#     def save(self, *args, **kwargs):
#         if None == self.thread_id:
#             self.thread_id = str(uuid.uuid4())
# 
#         super(Datum, self).save(*args, **kwargs)
# 
# 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 datetime
import json
import six
from datetime import timedelta
from django.conf import settings
from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
from tastypie.authentication import ApiKeyAuthentication
from .models import Datum, DatumLogLevel





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:

# Path: narrative/models.py
# class Datum(models.Model):
#     # When was the datum created
#     timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
#     expiration_time = models.DateTimeField(null=True, blank=True, default=None)
#     # Origin of this datum; ie, what piece of software created it
#     origin = models.CharField(max_length=64)
#     datum_name = models.CharField(max_length=64)
# 
#     # Additional information about the datum; this is very datum specific
#     # This approach, storing json in a textfield and having an
#     # explicit accessor and mutator, is very ugly.  Really this is because
#     # we are storing non-structured/sparse data in a relational format.
#     datum_note_json = models.TextField(null=True, blank=True, default=None)
# 
#     # An ID to tie particular datums together
#     thread_id = models.CharField(max_length=36, null=True, blank=True)
# 
#     log_level = models.IntegerField(choices=DatumLogLevel.types, default=DatumLogLevel.INFO)
# 
#     objects = DatumManager()
# 
#     def __init__(self, *args, **kwargs):
#         if 'ttl' in kwargs:
#             kwargs['expiration_time'] = self.get_utc_now() + kwargs.pop('ttl')
# 
#         super(Datum, self).__init__(*args, **kwargs)
# 
#     def __str__(self):
#         note_snippet = self.datum_note_json[:50] if self.datum_note_json else ''
#         return 'origin:{0} datum_name:{1} note:{2}'.format(
#             self.origin, self.datum_name, note_snippet
#         )
# 
#     def get_note(self):
#         if self.datum_note_json:
#             return json.loads(self.datum_note_json)
#         else:
#             return []
# 
#     def set_note(self, note):
#         self.datum_note_json = json.dumps(note)
# 
#     def get_utc_now(self):
#         return utc_tz.localize(datetime.datetime.utcnow())
# 
#     @property
#     def timestamp_with_tz(self):
#         return utc_tz.localize(self.timestamp)
# 
#     def save(self, *args, **kwargs):
#         if None == self.thread_id:
#             self.thread_id = str(uuid.uuid4())
# 
#         super(Datum, self).save(*args, **kwargs)
# 
# 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/api.py
import datetime
import json
import six
from datetime import timedelta
from django.conf import settings
from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
from tastypie.authentication import ApiKeyAuthentication
from .models import Datum, DatumLogLevel






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())