Commit c7d36568 authored by eric's avatar eric
Browse files

New view `/publish/` calls the index command and constructs the index

parent 6eb58417
......@@ -3,9 +3,11 @@
# Python imports
import os
import sys
import codecs
import json
from urllib2 import HTTPError
from time import clock
# PyPi imports
......@@ -121,38 +123,45 @@ def query_results_to_template_articles(query_results):
return sorted(template_articles, key=lambda a: a['date'] if 'date' in a else 0, reverse=True)
def snif():
start = clock()
g = rdflib.Graph()
host = 'http://f-u-t-u-r-e.org'
# host = 'http://127.0.0.1:8000'
for pad in Pad.objects.all():
# We only want to index the articles—
# For now we can distinguish them because they have url’s
# ending in ‘.md’
if not pad.display_slug.endswith('.md'):
continue
try:
result = g.parse(host + pad.get_absolute_url())
except HTTPError, e:
if e.code == 403:
# Some of the pads will not be public yet—
# They gives a ‘403 FORBIDDEN’ response
# this is expected, and we don’t need to scrape them
continue
else:
raise
d = query_results_to_template_articles(g.query(sparql_query))
with open(os.path.join(BACKUP_DIR, "index.json"), 'w') as f:
f.write(json.dumps(d, indent=2, ensure_ascii=False).encode('utf-8'))
# with open(os.path.join(BACKUP_DIR, "index.html"), 'w') as f:
# f.write(render_to_string("home.html", {"articles" : d}).encode('utf-8'))
duration = clock() - start
return "sniffed %s articles in %s seconds" % ( Pad.objects.count(), duration )
class Command(BaseCommand):
args = ''
help = 'Create an index of all the articles’ metadata'
def handle(self, *args, **options):
g = rdflib.Graph()
host = 'http://f-u-t-u-r-e.org'
# 'http://127.0.0.1:8000'
for pad in Pad.objects.all():
# We only want to index the articles—
# For now we can distinguish them because they have url’s
# ending in ‘.md’
if not pad.display_slug.endswith('.md'):
continue
try:
result = g.parse(host + pad.get_absolute_url())
except HTTPError, e:
if e.code == 403:
# Some of the pads will not be public yet—
# They gives a ‘403 FORBIDDEN’ response
# this is expected, and we don’t need to scrape them
continue
else:
raise
d = query_results_to_template_articles(g.query(sparql_query))
with open(os.path.join(BACKUP_DIR, "index.json"), 'w') as f:
f.write(json.dumps(d, indent=2, ensure_ascii=False).encode('utf-8'))
# with open(os.path.join(BACKUP_DIR, "index.html"), 'w') as f:
# f.write(render_to_string("home.html", {"articles" : d}).encode('utf-8'))
return snif()
......@@ -38,6 +38,7 @@
<a class="button screen-only" href="{% url 'logout' %}{% if pad %}?next={% url 'pad-read' pad.display_slug %}{% endif %}">Log out</a>
<a class="button screen-only" href="{% url 'pad-create' author.group.all.0.id %}">Create</a>
<a class="button screen-only" href="{% url 'all' %}">List</a>
<a class="button screen-only" href="{% url 'publish' %}">Publish</a>
<a class="button screen-only" href="/w/TODOLIST">TODO</a>
<a class="button screen-only" href="/w/style.css">styles</a>
<a class="button screen-only" href="/w/typo">typo</a>
......
{% extends "base.html" %}
{% block content %}
{% if published %}
<p style="background-color: white">Succesfully published</p>
<p style="background-color: white">{{ message }}</p>
{% else %}
<form action="" method="post">
{% csrf_token %}
<input style="background-color: white" type="submit" class="submit" value="Publish"/>
</form>
{% endif %}
{% endblock %}
......@@ -14,6 +14,7 @@ urlpatterns = patterns(
url(r'^admin/', include(admin.site.urls)),
url(r'^all/$', TemplateView.as_view(template_name = 'all.html'), name='all'),
url(r'^css/$', 'relearn.views.css', name='css'),
url(r'^publish/$', 'relearn.views.publish', name='publish'),
url(r'^css-print/$', 'relearn.views.cssprint', name='css-print'),
url(r'^$', 'relearn.views.home', name='home'),
url(r'^accounts/login$', 'django.contrib.auth.views.login',
......
......@@ -38,6 +38,8 @@ from django.shortcuts import render
from django.core.mail import send_mail
from relearn.forms import ContactForm
from relearn.management.commands.index import snif
from relearn.templatetags.wikify import dewikify
# By default, the homepage is the pad called ‘start’ (props to DokuWiki!)
......@@ -415,7 +417,16 @@ def home(request):
except Pad.DoesNotExist:
return HttpResponseRedirect(reverse('login'))
@login_required(login_url='/accounts/login')
def publish(request):
tpl_params = {}
if request.method == 'POST':
tpl_params['published'] = True
tpl_params['message'] = snif()
else:
tpl_params['published'] = False
tpl_params['message'] = ""
return render_to_response("publish.html", tpl_params, context_instance = RequestContext(request))
def css(request):
try:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment