NDOJ/judge/sitemap.py

104 lines
2.6 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
from django.contrib.auth.models import User
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from django.utils import timezone
from judge.models import BlogPost, Contest, Organization, Problem, Solution
class ProblemSitemap(Sitemap):
changefreq = 'daily'
priority = 0.8
def items(self):
2021-05-24 20:00:36 +00:00
return Problem.get_public_problems().values_list('code')
2020-01-21 06:35:58 +00:00
def location(self, obj):
return reverse('problem_detail', args=obj)
class UserSitemap(Sitemap):
changefreq = 'hourly'
priority = 0.5
def items(self):
return User.objects.values_list('username')
def location(self, obj):
return reverse('user_page', args=obj)
class ContestSitemap(Sitemap):
changefreq = 'hourly'
priority = 0.5
def items(self):
return Contest.objects.filter(is_visible=True, is_private=False,
is_organization_private=False).values_list('key')
def location(self, obj):
return reverse('contest_view', args=obj)
class OrganizationSitemap(Sitemap):
changefreq = 'hourly'
priority = 0.5
def items(self):
return Organization.objects.values_list('id', 'slug')
def location(self, obj):
return reverse('organization_home', args=obj)
class BlogPostSitemap(Sitemap):
changefreq = 'hourly'
priority = 0.7
def items(self):
2020-12-28 05:45:58 +00:00
return (BlogPost.objects.filter(visible=True, is_organization_private=False, publish_on__lte=timezone.now())
.values_list('id', 'slug'))
2020-01-21 06:35:58 +00:00
def location(self, obj):
return reverse('blog_post', args=obj)
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'))
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'