from django.core.urlresolvers import reverse
from django.contrib.sitemaps import Sitemap
from django.contrib.auth.models import User
from django.utils import timezone
from judge.models import Problem, Organization, Contest, BlogPost, Solution


class ProblemSitemap(Sitemap):
    changefreq = 'daily'
    priority = 0.8

    def items(self):
        return Problem.objects.filter(is_public=True)

    def location(self, obj):
        return reverse('problem_detail', args=(obj.code,))


class UserSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.5

    def items(self):
        return User.objects.all()

    def location(self, obj):
        return reverse('user_page', args=(obj.username,))


class ContestSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.5

    def items(self):
        return Contest.objects.filter(is_public=True)

    def location(self, obj):
        return reverse('contest_view', args=(obj.key,))


class OrganizationSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.5

    def items(self):
        return Organization.objects.all()

    def location(self, obj):
        return reverse('organization_home', args=(obj.key,))


class BlogPostSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.7

    def items(self):
        return BlogPost.objects.filter(visible=True, publish_on__lte=timezone.now())

    def location(self, obj):
        return reverse('blog_post', args=(obj.id, obj.slug))


class SolutionSitemap(Sitemap):
    changefreq = 'hourly'
    priority = 0.8

    def items(self):
        return (Solution.objects.filter(is_public=True, publish_on__lte=timezone.now(), problem__isnull=False)
                .values_list('problem__code', flat=True))

    def location(self, obj):
        return reverse('problem_editorial', args=[obj])


class HomePageSitemap(Sitemap):
    priority = 1.0
    changefreq = 'daily'

    def items(self):
        return ['home']

    def location(self, obj):
        return reverse(obj)


class UrlSitemap(Sitemap):
    def __init__(self, pages):
        self.pages = pages

    def items(self):
        return self.pages

    def location(self, obj):
        return obj['location'] if isinstance(obj, dict) else obj

    def priority(self, obj):
        return obj.get('priority', 0.5) if isinstance(obj, dict) else 0.5

    def changefreq(self, obj):
        return obj.get('changefreq', 'daily') if isinstance(obj, dict) else 'daily'

@@ -380,32 +380,52 @@
 (is_public=True)
+.values_list('code')
 %0A%0A    def locati
@@ -489,19 +489,11 @@
 rgs=
-(
 obj
-.code,)
 )%0A%0A%0A
@@ -607,36 +607,54 @@
 rn User.objects.
+v
 al
-l(
+ues_list('username'
 )%0A%0A    def locat
@@ -714,23 +714,11 @@
 rgs=
-(
 obj
-.username,)
 )%0A%0A%0A
@@ -868,16 +868,35 @@
 ic=True)
+.values_list('key')
 %0A%0A    de
@@ -958,34 +958,27 @@
 view', args=
-(
 obj
-.key,)
 )%0A%0A%0Aclass Or
@@ -1112,12 +1112,25 @@
 cts.
+v
 al
-l(
+ues_list('key'
 )%0A%0A 
@@ -1210,18 +1210,11 @@
 rgs=
-(
 obj
-.key,)
 )%0A%0A%0A
@@ -1396,16 +1396,42 @@
 e.now())
+.values_list('id', 'slug')
 %0A%0A    de
@@ -1498,26 +1498,11 @@
 rgs=
-(
 obj
-.id, obj.slug)
 )%0A%0A%0A
@@ -1759,19 +1759,8 @@
 ode'
-, flat=True
 ))%0A%0A
@@ -1841,13 +1841,11 @@
 rgs=
-%5B
 obj
-%5D
 )%0A%0A%0A

Optimize sitemap