This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 681
/
cron.py
109 lines (96 loc) · 3.7 KB
/
cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import time
from xml.dom.minidom import parseString
from django.db import connection, transaction
from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.sitemaps import GenericSitemap
from django.template import loader
from django.utils.encoding import smart_str
import cronjobs
from wiki import tasks
from wiki.models import Document
@cronjobs.register
def calculate_related_documents():
"""Calculates all related documents based on common tags."""
cursor = connection.cursor()
cursor.execute('DELETE FROM wiki_relateddocument')
cursor.execute("""
INSERT INTO
wiki_relateddocument (document_id, related_id, in_common)
SELECT
t1.object_id,
t2.object_id,
COUNT(*) AS common_tags
FROM
wiki_document d1 JOIN
taggit_taggeditem t1 JOIN
taggit_taggeditem t2 JOIN
wiki_document d2
WHERE
d1.id = t1.object_id AND
t1.tag_id = t2.tag_id AND
t1.object_id <> t2.object_id AND
t1.content_type_id = (
SELECT
id
FROM
django_content_type
WHERE
app_label = 'wiki' AND
model = 'document'
) AND
t2.content_type_id = (
SELECT
id
FROM
django_content_type
WHERE
app_label = 'wiki' AND
model = 'document'
) AND
d2.id = t2.object_id AND
d2.locale = d1.locale AND
d2.category = d1.category AND
d1.current_revision_id IS NOT NULL AND
d2.current_revision_id IS NOT NULL
GROUP BY
t1.object_id,
t2.object_id""")
transaction.commit_unless_managed()
@cronjobs.register
def rebuild_kb():
tasks.rebuild_kb()
@cronjobs.register
def build_sitemaps():
sitemap_element = "<sitemap><loc>%s</loc><lastmod>%s</lastmod></sitemap>"
sitemap_index = "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
for locale in settings.MDN_LANGUAGES:
queryset = (Document.objects
.filter(is_template=False, locale=locale)
.exclude(title__startswith='User:')
.exclude(title__iregex=r'Redirect [0-9]+$')
.exclude(html__iregex=r'^(<p>)?(#)?REDIRECT')
.exclude(slug__icontains='Talk:')
)
if len(queryset) > 0:
info = {'queryset': queryset, 'date_field': 'modified'}
sitemap = GenericSitemap(info, priority=0.5)
urls = sitemap.get_urls(page=1)
xml = smart_str(loader.render_to_string('sitemap.xml',
{'urlset': urls}))
xml = xml.replace('http://', 'https://')
directory = '%s/sitemaps/%s' % (settings.MEDIA_ROOT, locale)
if not os.path.exists(directory):
os.makedirs(directory)
f = open('%s/sitemap.xml' % directory, 'w')
f.write(xml)
f.close()
sitemap_url = ("https://%s/sitemaps/%s/sitemap.xml" % (
Site.objects.get_current().domain, locale))
sitemap_index = sitemap_index + sitemap_element % (sitemap_url,
time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime()))
sitemap_index = sitemap_index + "</sitemapindex>"
index_file = open('%s/sitemap.xml' % settings.MEDIA_ROOT, 'w')
index_file.write(parseString(sitemap_index).toxml())
index_file.close()