from django.contrib.sitemaps import Sitemap
from django.utils import timezone


class GenericSitemap(Sitemap):
    changefreq = "never"
    default_priority = 0.5
    lastmod_field = 'date_modified'
    min_priority = 0.1
    priority_field = 'date_modified'
    protocol = 'https'

    def lastmod(self, entry):
        """return the last modification date."""
        return getattr(entry, self.lastmod_field)

    def priority(self, entry):
        """
        Set a priority hint for crawlers.
        hightes priority = 1, lowest priority = 0.1, normal priority = 0.5
        updatet entries in the next/past 30 days (a month) should have a normal 
        priority. The nearer to the present day the higher the priority should 
        be.
        """
        delta = timezone.now() - getattr(entry, self.priority_field)
        priority = round(1 - abs(self.default_priority / 30 * delta.days), 2)
        return max(self.min_priority, priority)
