include public and featured texts in sitemap and include on-js view for texts

This commit is contained in:
j 2014-01-16 11:01:31 +00:00
parent 3fb59e6a99
commit 7b32cfbf5b
5 changed files with 93 additions and 3 deletions

View File

@ -8,11 +8,12 @@ random
from django.conf import settings from django.conf import settings
from django.db import connection, transaction from django.db import connection, transaction
from django.db.models import Q
from ox.utils import ET from ox.utils import ET
from celery.task import task, periodic_task from celery.task import task, periodic_task
import models import models
from text.models import Text
@periodic_task(run_every=timedelta(days=1), queue='encoding') @periodic_task(run_every=timedelta(days=1), queue='encoding')
def cronjob(**kwargs): def cronjob(**kwargs):
@ -161,6 +162,22 @@ def update_sitemap(base_url):
el = ET.SubElement(video, "video:duration") el = ET.SubElement(video, "video:duration")
el.text = "%s" % int(duration) el.text = "%s" % int(duration)
for t in Text.objects.filter(Q(status='featured')|Q(status='public')):
url = ET.SubElement(urlset, "url")
# URL of the page. This URL must begin with the protocol (such as http)
loc = ET.SubElement(url, "loc")
loc.text = absolute_url(t.get_absolute_url()[1:])
# This date should be in W3C Datetime format, can be %Y-%m-%d
lastmod = ET.SubElement(url, "lastmod")
lastmod.text = t.modified.strftime("%Y-%m-%d")
# always, hourly, daily, weekly, monthly, yearly, never
changefreq = ET.SubElement(url, "changefreq")
changefreq.text = 'monthly'
# priority of page on site values 0.1 - 1.0
priority = ET.SubElement(url, "priority")
priority.text = '1.0' if t.status == 'featured' else '0.75'
with open(sitemap[:-3], 'wb') as f: with open(sitemap[:-3], 'wb') as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset)) f.write('<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset))
with gzip.open(sitemap, 'wb') as f: with gzip.open(sitemap, 'wb') as f:

View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{{head_title}}</title>
<link rel="icon" type="image/png" href="/static/png/icon.png" />
<meta name="application-name" content="{{settings.SITENAME}}"/>
<meta name="application-url" content="{{base_url}}"/>
<link rel="alternate" type="application/json+oembed" href="{{base_url}}oembed?url={{current_url|urlencode}}" title="oEmbed Profile" />
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="{{settings.SITENAME}}" />
<script>
try {
if (localStorage && !localStorage['Ox.theme']) {
localStorage['Ox.theme'] = '"{{settings.CONFIG.user.ui.theme}}"';
}
} catch(e) {}
</script>
<script type="text/javascript" src="/static/js/pandora.js?{{settings.CONFIG.site.version}}"></script>
<meta name="title" content="{{title}}" />
{%if description %}<meta name="description" content="{{description|safe}}"/> {%endif%}
<meta property="og:title" content="{{title}}"/>
<meta property="og:type" content="article"/>
<meta property="og:url" content="{{url}}"/>
<meta property="og:image" content="{{icon}}"/>
<meta property="og:site_name" content="{{settings.SITENAME}}"/>
{%if description %}<meta property="og:description" content="{{description}}"/>{%endif%}
<meta name="google" value="notranslate"/>
</head>
<body>
<noscript>
<div style="position: fixed; left: 0; top: 0; right: 0; bottom: 0; background-color: rgb(144, 144, 144);"></div>
<div style="position: absolute; left: 8px; top: 8px; right: 8px; bottom: 0; font-family: Lucida Grande, Segoe UI, DejaVu Sans, Lucida Sans Unicode, Helvetica, Arial, sans-serif; font-size: 11px; color: rgb(0, 0, 0); -moz-user-select: none; -o-user-select: none; -webkit-user-select: none">
<h1>{{text.name}}</h1>
<div>{{text.text|safe}}</div>
</div>
</noscript>
</body>
</html>

View File

@ -53,12 +53,19 @@ class Text(models.Model):
def __unicode__(self): def __unicode__(self):
return self.get_id() return self.get_id()
@classmethod
def get(cls, id):
id = id.split(':')
username = id[0]
name = ":".join(id[1:])
return cls.objects.get(user__username=username, name=name)
def get_absolute_url(self): def get_absolute_url(self):
return '/texts/%s' % quote(self.get_id()) return '/texts/%s' % quote(self.get_id().replace('_', '\t').replace(' ', '_')).replace('/', '%2F')
def get_absolute_pdf_url(self): def get_absolute_pdf_url(self):
return '/texts/%s/text.pdf' % quote(self.get_id()) return '%s/text.pdf' % self.get_absolute_url()
def get_id(self): def get_id(self):
return u'%s:%s' % (self.user.username, self.name) return u'%s:%s' % (self.user.username, self.name)

View File

@ -4,6 +4,7 @@ from __future__ import division
import os import os
import re import re
import ox
from ox.utils import json from ox.utils import json
from ox.django.api import actions from ox.django.api import actions
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
@ -434,3 +435,29 @@ def upload(request):
response = json_response(status=404, text='permission denied') response = json_response(status=404, text='permission denied')
response = json_response(status=400, text='this request requires POST') response = json_response(status=400, text='this request requires POST')
return render_to_json_response(response) return render_to_json_response(response)
def text(request, id):
id = id.replace('_', ' ').replace('\t', '_')
try:
text = models.Text.get(id)
if not text.accessible(request.user):
raise
template = 'text.html'
context = RequestContext(request, {
'base_url': request.build_absolute_uri('/'),
'description': ox.strip_tags(text.description),
'icon': request.build_absolute_uri('/text/%s/icon256.jpg' % text.get_id()),
'settings': settings,
'text': text,
'title': ox.strip_tags(text.name),
'url': request.build_absolute_uri(text.get_absolute_url()),
})
except models.Text.DoesNotExist:
template = 'index.html'
context = RequestContext(request, {
'base_url': request.build_absolute_uri('/'),
'settings': settings,
'title': settings.SITENAME
})
return render_to_response(template, context)

View File

@ -37,6 +37,7 @@ urlpatterns = patterns('',
(r'^text/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'text.views.icon'), (r'^text/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'text.views.icon'),
(r'^texts/(?P<id>.*?)/text.pdf$', 'text.views.pdf'), (r'^texts/(?P<id>.*?)/text.pdf$', 'text.views.pdf'),
(r'^texts/(?P<id>.*?)/text.pdf.html$', 'text.views.pdf_viewer'), (r'^texts/(?P<id>.*?)/text.pdf.html$', 'text.views.pdf_viewer'),
(r'^texts/(?P<id>.*?)$', 'text.views.text'),
(r'^robots.txt$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'robots.txt'), 'content_type': 'text/plain'}), (r'^robots.txt$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'robots.txt'), 'content_type': 'text/plain'}),
(r'^favicon.ico$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'png/icon.16.png'), 'content_type': 'image/x-icon'}), (r'^favicon.ico$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'png/icon.16.png'), 'content_type': 'image/x-icon'}),
(r'^opensearch.xml$', 'app.views.opensearch_xml'), (r'^opensearch.xml$', 'app.views.opensearch_xml'),