From 7b32cfbf5b1e24ab00e558a82cfd88ad678cf190 Mon Sep 17 00:00:00 2001
From: j <0x006A@0x2620.org>
Date: Thu, 16 Jan 2014 11:01:31 +0000
Subject: [PATCH] include public and featured texts in sitemap and include
on-js view for texts
---
pandora/item/tasks.py | 19 ++++++++++++++++++-
pandora/templates/text.html | 38 +++++++++++++++++++++++++++++++++++++
pandora/text/models.py | 11 +++++++++--
pandora/text/views.py | 27 ++++++++++++++++++++++++++
pandora/urls.py | 1 +
5 files changed, 93 insertions(+), 3 deletions(-)
create mode 100644 pandora/templates/text.html
diff --git a/pandora/item/tasks.py b/pandora/item/tasks.py
index 62ec6c9a..91ee6ded 100644
--- a/pandora/item/tasks.py
+++ b/pandora/item/tasks.py
@@ -8,11 +8,12 @@ random
from django.conf import settings
from django.db import connection, transaction
+from django.db.models import Q
from ox.utils import ET
from celery.task import task, periodic_task
import models
-
+from text.models import Text
@periodic_task(run_every=timedelta(days=1), queue='encoding')
def cronjob(**kwargs):
@@ -161,6 +162,22 @@ def update_sitemap(base_url):
el = ET.SubElement(video, "video: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:
f.write('\n' + ET.tostring(urlset))
with gzip.open(sitemap, 'wb') as f:
diff --git a/pandora/templates/text.html b/pandora/templates/text.html
new file mode 100644
index 00000000..f7503c2f
--- /dev/null
+++ b/pandora/templates/text.html
@@ -0,0 +1,38 @@
+
+
+
+
+ {{head_title}}
+
+
+
+
+
+
+
+
+ {%if description %} {%endif%}
+
+
+
+
+
+ {%if description %}{%endif%}
+
+
+
+
+
+
diff --git a/pandora/text/models.py b/pandora/text/models.py
index de36af83..2ff188f6 100644
--- a/pandora/text/models.py
+++ b/pandora/text/models.py
@@ -53,12 +53,19 @@ class Text(models.Model):
def __unicode__(self):
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):
- 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):
- return '/texts/%s/text.pdf' % quote(self.get_id())
+ return '%s/text.pdf' % self.get_absolute_url()
def get_id(self):
return u'%s:%s' % (self.user.username, self.name)
diff --git a/pandora/text/views.py b/pandora/text/views.py
index 4e3abceb..ce97e83e 100644
--- a/pandora/text/views.py
+++ b/pandora/text/views.py
@@ -4,6 +4,7 @@ from __future__ import division
import os
import re
+import ox
from ox.utils import json
from ox.django.api import actions
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=400, text='this request requires POST')
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)
diff --git a/pandora/urls.py b/pandora/urls.py
index b5e21668..fa2cf346 100644
--- a/pandora/urls.py
+++ b/pandora/urls.py
@@ -37,6 +37,7 @@ urlpatterns = patterns('',
(r'^text/(?P.*?)/icon(?P\d*).jpg$', 'text.views.icon'),
(r'^texts/(?P.*?)/text.pdf$', 'text.views.pdf'),
(r'^texts/(?P.*?)/text.pdf.html$', 'text.views.pdf_viewer'),
+ (r'^texts/(?P.*?)$', 'text.views.text'),
(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'^opensearch.xml$', 'app.views.opensearch_xml'),