# 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 DatumManager(models.Manager):
#     def clear_expired(self):
#         expired_set = self.filter(expiration_time__isnull=False).filter(expiration_time__lte=self.get_utc_now())
# 
#         expired_count = expired_set.count()
# 
#         expired_set.delete()
# 
#         return expired_count
# 
#     def get_utc_now(self):
#         return utc_tz.localize(datetime.datetime.utcnow())
# 
# 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()
# 
# 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()
# 
# def log_datum(*args, **kwargs):
#     """
#     Handle logging a datum.  It is better to use this method than to manually create datums,
#     because it provides a central place for controlling which datums should or should not be
#     created based the logging level set in the NarrativeConfig.
#     """
#     minimum_datum_log_level = NarrativeConfig.objects.filter()[0].minimum_datum_log_level
# 
#     note = None
#     datum = None
# 
#     if kwargs['log_level'] >= minimum_datum_log_level:
#         note = kwargs.pop('note') if 'note' in kwargs else None
# 
#         datum = Datum(*args, **kwargs)
# 
#         if note:
#             datum.set_note(note)
# 
#         datum.save()
# 
#     return datum


import datetime
import json
import six
from unittest import skip
    from urllib import urlencode
    from urllib.parse import urlencode
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from tastypie.models import ApiKey
from ..models import Datum, DatumManager, DatumLogLevel, NarrativeConfig, log_datum

if six.PY2:  # pragma: no cover
else:  # pragma: no cover




class TestDatumTTLField(TestCase):
    def setUp(self):
        self.mock_utc_now = datetime.datetime(2013, 7, 6, 12, 0, 0)

        def mock_get_utc_now(self_):
            return self.mock_utc_now


# 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 DatumManager(models.Manager):
#     def clear_expired(self):
#         expired_set = self.filter(expiration_time__isnull=False).filter(expiration_time__lte=self.get_utc_now())
# 
#         expired_count = expired_set.count()
# 
#         expired_set.delete()
# 
#         return expired_count
# 
#     def get_utc_now(self):
#         return utc_tz.localize(datetime.datetime.utcnow())
# 
# 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()
# 
# 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()
# 
# def log_datum(*args, **kwargs):
#     """
#     Handle logging a datum.  It is better to use this method than to manually create datums,
#     because it provides a central place for controlling which datums should or should not be
#     created based the logging level set in the NarrativeConfig.
#     """
#     minimum_datum_log_level = NarrativeConfig.objects.filter()[0].minimum_datum_log_level
# 
#     note = None
#     datum = None
# 
#     if kwargs['log_level'] >= minimum_datum_log_level:
#         note = kwargs.pop('note') if 'note' in kwargs else None
# 
#         datum = Datum(*args, **kwargs)
# 
#         if note:
#             datum.set_note(note)
# 
#         datum.save()
# 
#     return datum


# Path: narrative/tests/datum_tests.py
import datetime
import json
import six
from unittest import skip
    from urllib import urlencode
    from urllib.parse import urlencode
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from tastypie.models import ApiKey
from ..models import Datum, DatumManager, DatumLogLevel, NarrativeConfig, log_datum


if six.PY2:  # pragma: no cover
else:  # pragma: no cover




class TestDatumTTLField(TestCase):
    def setUp(self):
        self.mock_utc_now = datetime.datetime(2013, 7, 6, 12, 0, 0)

        def mock_get_utc_now(self_):
            return self.mock_utc_now


        self.original_get_utc_now = Datum.get_utc_now