# Path: www/maposmatic/models.py
# class MapRenderingJobManager(models.Manager):
# class MapRenderingJob(models.Model):
#     def to_render(self):
#     def queue_size(self):
#     def get_random_with_thumbnail(self):
#     def get_by_filename(self, name):
#     def __str__(self):
#     def maptitle_computized(self):
#     def files_prefix(self):
#     def start_rendering(self):
#     def end_rendering(self, resultmsg):
#     def rendering_time_gt_1min(self):
#     def __is_ok(self):              return self.resultmsg == 'ok'
#     def is_waiting(self):           return self.status == 0
#     def is_rendering(self):         return self.status == 1
#     def needs_waiting(self):        return self.status  < 2
#     def is_done(self):              return self.status == 2
#     def is_done_ok(self):           return self.is_done() and self.__is_ok()
#     def is_done_failed(self):       return self.is_done() and not self.__is_ok()
#     def is_obsolete(self):          return self.status == 3
#     def is_obsolete_ok(self):       return self.is_obsolete() and self.__is_ok()
#     def is_obsolete_failed(self):   return self.is_obsolete() and not self.__is_ok()
#     def is_cancelled(self):         return self.status == 4
#     def can_recreate(self):
#     def get_map_fileurl(self, format):
#     def get_map_filepath(self, format):
#     def output_files(self):
#     def has_output_files(self):
#     def remove_all_files(self):
#     def cancel(self):
#     def get_thumbnail(self):
#     def current_position_in_queue(self):
#     def rendering_estimated_start_time(self):
#     def get_absolute_url(self):
# SPACE_REDUCE = re.compile(r"\s+")
# NONASCII_REMOVE = re.compile(r"[^A-Za-z0-9]+")
#     STATUS_LIST = (
#         (0, 'Submitted'),
#         (1, 'In progress'),
#         (2, 'Done'),
#         (3, 'Done w/o files'),
#         (4, 'Cancelled'),
#         )
#     NONCE_SIZE = 16


import datetime
import www.settings
from django.contrib.gis.feeds import Feed
from django.utils.translation import ugettext_lazy as _
from www.maposmatic import models


class MapsFeed(Feed):
    """
    This feeds syndicates the latest successfully rendered maps in MapOSMatic,
    with their thumbnail, and links to the rendered files.
    """

    title = www.settings.DEBUG and _('MapOSMatic maps') or _('MapOSMatic maps [DEV]')
    link = '/maps/' # We can't use reverse here as the urlpatterns aren't
                    # defined yet at this point.
    description = _('The latest rendered maps on MapOSMatic.')

    description_template = 'maposmatic/map-feed.html'

    def items(self):
        """Returns the successfully rendered maps from the last 24 hours, or
        the last 10 maps/jobs if nothing happened recently."""

        one_day_before = datetime.datetime.now() - datetime.timedelta(1)
        items = (models.MapRenderingJob.objects
                 .filter(status=2)
                 .filter(resultmsg='ok')
                 .filter(endofrendering_time__gte=one_day_before)
                 .order_by('-endofrendering_time'))

        if items.count():
            return items

        # Fall back to the last 10 entries, regardless of time

# Path: www/maposmatic/models.py
# class MapRenderingJobManager(models.Manager):
# class MapRenderingJob(models.Model):
#     def to_render(self):
#     def queue_size(self):
#     def get_random_with_thumbnail(self):
#     def get_by_filename(self, name):
#     def __str__(self):
#     def maptitle_computized(self):
#     def files_prefix(self):
#     def start_rendering(self):
#     def end_rendering(self, resultmsg):
#     def rendering_time_gt_1min(self):
#     def __is_ok(self):              return self.resultmsg == 'ok'
#     def is_waiting(self):           return self.status == 0
#     def is_rendering(self):         return self.status == 1
#     def needs_waiting(self):        return self.status  < 2
#     def is_done(self):              return self.status == 2
#     def is_done_ok(self):           return self.is_done() and self.__is_ok()
#     def is_done_failed(self):       return self.is_done() and not self.__is_ok()
#     def is_obsolete(self):          return self.status == 3
#     def is_obsolete_ok(self):       return self.is_obsolete() and self.__is_ok()
#     def is_obsolete_failed(self):   return self.is_obsolete() and not self.__is_ok()
#     def is_cancelled(self):         return self.status == 4
#     def can_recreate(self):
#     def get_map_fileurl(self, format):
#     def get_map_filepath(self, format):
#     def output_files(self):
#     def has_output_files(self):
#     def remove_all_files(self):
#     def cancel(self):
#     def get_thumbnail(self):
#     def current_position_in_queue(self):
#     def rendering_estimated_start_time(self):
#     def get_absolute_url(self):
# SPACE_REDUCE = re.compile(r"\s+")
# NONASCII_REMOVE = re.compile(r"[^A-Za-z0-9]+")
#     STATUS_LIST = (
#         (0, 'Submitted'),
#         (1, 'In progress'),
#         (2, 'Done'),
#         (3, 'Done w/o files'),
#         (4, 'Cancelled'),
#         )
#     NONCE_SIZE = 16


# Path: www/maposmatic/feeds.py
import datetime
import www.settings
from django.contrib.gis.feeds import Feed
from django.utils.translation import ugettext_lazy as _
from www.maposmatic import models



class MapsFeed(Feed):
    """
    This feeds syndicates the latest successfully rendered maps in MapOSMatic,
    with their thumbnail, and links to the rendered files.
    """

    title = www.settings.DEBUG and _('MapOSMatic maps') or _('MapOSMatic maps [DEV]')
    link = '/maps/' # We can't use reverse here as the urlpatterns aren't
                    # defined yet at this point.
    description = _('The latest rendered maps on MapOSMatic.')

    description_template = 'maposmatic/map-feed.html'

    def items(self):
        """Returns the successfully rendered maps from the last 24 hours, or
        the last 10 maps/jobs if nothing happened recently."""

        one_day_before = datetime.datetime.now() - datetime.timedelta(1)
        items = (models.MapRenderingJob.objects
                 .filter(status=2)
                 .filter(resultmsg='ok')
                 .filter(endofrendering_time__gte=one_day_before)
                 .order_by('-endofrendering_time'))

        if items.count():
            return items

        # Fall back to the last 10 entries, regardless of time

        return (models.MapRenderingJob.objects