# Path: eulcommon/djangoextras/taskresult/models.py
# class TaskResult(models.Model):
#     label = models.CharField(max_length=100)
#     object_id = models.CharField(max_length=50)
#     url = models.URLField()
#     created = models.DateTimeField(auto_now_add=True)
#     task_id = models.CharField(max_length=100)
#     task_start = models.DateTimeField(blank=True, null=True)
#     task_end = models.DateTimeField(blank=True, null=True)
# 
#     @property
#     def task(self):
#         return AsyncResult(self.task_id)
# 
#     def __unicode__(self):
#         return self.label
# 
#     @property
#     def duration(self):
#         if self.task_end and self.task_start:
#             return self.task_end - self.task_start
#         else:
#             return '-'
# 
#     @property
#     def status(self):
#         return self.task.status
# 
#     @property
#     def result(self):
#         return self.task.result or ''
# 
#     # use font-awesome icons for short-hand status display?
#     unknown_icon = 'glyphicon-question-sign'
#     status_icon_map = {
#         'pending': 'glyphicon-time',
#         'success': 'glyphicon-ok',
#         'failure': 'glyphicon-remove',
#         # 'failure': 'glyphicon-alert',  # doesn't display in keep; version?
#         '': unknown_icon
#     }
#     status_style = {
#         'pending': 'text-muted',
#         'success': 'text-success',
#         'failure': 'text-danger',
#         '': 'text-warning',
#     }
# 
#     def status_icon(self):
#         'glyphicon for task status; requires bootstrap'
#         icon = self.status_icon_map.get(self.status.lower(),
#                                         self.unknown_icon)
#         style = self.status_style.get(self.status.lower(), '')
#         return mark_safe(
#             '<span class="glyphicon %s %s" aria-hidden="true"></span>' %
#             (icon, style))
#     status_icon.short_description = 'Status'


from time import sleep
from celery.signals import task_prerun, task_postrun
from django import VERSION as django_version
from django.core.urlresolvers import reverse
from django.test import TestCase
from .models import TaskResult
import pytest
import test_setup
            'task end time should be set based on celery task_postrun signal')
        self.assertEqual(task_time, tr.duration.seconds)

    # NOTE: as of August 2016 with django-celery==3.1.17,
    # django-celery is attempting to import get_cache, which has been removed
    # from django as of Django 1.9
    # (parts of the test work, but displaying the task details fails)
    @pytest.mark.skipif(django_version >= (1, 9),
                        reason="django-celery not yet Django 1.9 compatible")
    def test_view_recent(self):
        # no tasks to display
        response = self.client.get(reverse('tasks:recent'))
        self.assertContains(response, 'No recent tasks')

        # create some tasks
        task1 = TaskResult(label='test1', object_id='a', url='/foo-a', task_id=id)
        task1.save()
        # sleep for a couple of seconds to ensure tasks have different creation times
        sleep(2)
        task2 = TaskResult(label='test2', object_id='b', url='/foo-b', task_id=id)
        task2.save()

        queryset = TaskResult.objects.order_by('-created')[:25]
        response = self.client.get(reverse('tasks:recent'))
        self.assertEqual(response.context['task_results'][0], task2,
            'newest task should be listed first')
        template_names = [templ.name for templ in response.templates]
        self.assert_('taskresult/recent.html' in template_names)
        self.assert_('taskresult/snippets/display_task.html' in template_names)
        self.assertContains(response, task1.label)

# Path: eulcommon/djangoextras/taskresult/models.py
# class TaskResult(models.Model):
#     label = models.CharField(max_length=100)
#     object_id = models.CharField(max_length=50)
#     url = models.URLField()
#     created = models.DateTimeField(auto_now_add=True)
#     task_id = models.CharField(max_length=100)
#     task_start = models.DateTimeField(blank=True, null=True)
#     task_end = models.DateTimeField(blank=True, null=True)
# 
#     @property
#     def task(self):
#         return AsyncResult(self.task_id)
# 
#     def __unicode__(self):
#         return self.label
# 
#     @property
#     def duration(self):
#         if self.task_end and self.task_start:
#             return self.task_end - self.task_start
#         else:
#             return '-'
# 
#     @property
#     def status(self):
#         return self.task.status
# 
#     @property
#     def result(self):
#         return self.task.result or ''
# 
#     # use font-awesome icons for short-hand status display?
#     unknown_icon = 'glyphicon-question-sign'
#     status_icon_map = {
#         'pending': 'glyphicon-time',
#         'success': 'glyphicon-ok',
#         'failure': 'glyphicon-remove',
#         # 'failure': 'glyphicon-alert',  # doesn't display in keep; version?
#         '': unknown_icon
#     }
#     status_style = {
#         'pending': 'text-muted',
#         'success': 'text-success',
#         'failure': 'text-danger',
#         '': 'text-warning',
#     }
# 
#     def status_icon(self):
#         'glyphicon for task status; requires bootstrap'
#         icon = self.status_icon_map.get(self.status.lower(),
#                                         self.unknown_icon)
#         style = self.status_style.get(self.status.lower(), '')
#         return mark_safe(
#             '<span class="glyphicon %s %s" aria-hidden="true"></span>' %
#             (icon, style))
#     status_icon.short_description = 'Status'


# Path: eulcommon/djangoextras/taskresult/tests.py
from time import sleep
from celery.signals import task_prerun, task_postrun
from django import VERSION as django_version
from django.core.urlresolvers import reverse
from django.test import TestCase
from .models import TaskResult
import pytest
import test_setup

            'task end time should be set based on celery task_postrun signal')
        self.assertEqual(task_time, tr.duration.seconds)

    # NOTE: as of August 2016 with django-celery==3.1.17,
    # django-celery is attempting to import get_cache, which has been removed
    # from django as of Django 1.9
    # (parts of the test work, but displaying the task details fails)
    @pytest.mark.skipif(django_version >= (1, 9),
                        reason="django-celery not yet Django 1.9 compatible")
    def test_view_recent(self):
        # no tasks to display
        response = self.client.get(reverse('tasks:recent'))
        self.assertContains(response, 'No recent tasks')

        # create some tasks
        task1 = TaskResult(label='test1', object_id='a', url='/foo-a', task_id=id)
        task1.save()
        # sleep for a couple of seconds to ensure tasks have different creation times
        sleep(2)
        task2 = TaskResult(label='test2', object_id='b', url='/foo-b', task_id=id)
        task2.save()

        queryset = TaskResult.objects.order_by('-created')[:25]
        response = self.client.get(reverse('tasks:recent'))
        self.assertEqual(response.context['task_results'][0], task2,
            'newest task should be listed first')
        template_names = [templ.name for templ in response.templates]
        self.assert_('taskresult/recent.html' in template_names)
        self.assert_('taskresult/snippets/display_task.html' in template_names)
        self.assertContains(response, task1.label)

        self.assertContains(response, task2.label)