codekasten/tycker/tycker.py

194 lines
5.7 KiB
Python
Raw Normal View History

2012-04-20 03:49:00 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2012-04-20 14:34:12 +02:00
#
# Copyright 2012 Lars Kruse <devel@sumpfralle.de>
#
# tycker is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# tycker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with tycker. If not, see <http://www.gnu.org/licenses/>.
#
2012-04-20 03:49:00 +02:00
import os
# the basedir is the parent dir of the location of this script
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Somehow "/usr/lib/python2.X/dist-packages" is missing from sys.path - thus bobo is not found.
# This applies to libapache2-mod-wsgi (3.3-2).
import sys
for path in list(sys.path):
options = []
parent_dir, last_dir = os.path.split(path)
options.append(os.path.join(path, "dist-packages"))
options.append(os.path.join(parent_dir, "pymodules", last_dir))
for option in options:
if os.path.isdir(option):
sys.path.append(option)
import datetime
import bobo
import sqlobject
import genshi.template
2015-05-01 11:49:30 +02:00
import subprocess
2012-04-20 03:49:00 +02:00
2015-04-25 23:58:21 +02:00
from settings import *
2012-04-20 03:49:00 +02:00
2015-04-25 23:58:21 +02:00
db_uri = "sqlite://%s" % DB_FILE
2012-04-20 03:49:00 +02:00
sqlobject.sqlhub.processConnection = sqlobject.connectionForURI(db_uri)
loader = genshi.template.TemplateLoader(os.path.join(BASE_DIR, 'templates'), auto_reload=True)
class Entry(sqlobject.SQLObject):
timestamp = sqlobject.DateTimeCol()
title = sqlobject.UnicodeCol()
content = sqlobject.UnicodeCol()
2016-04-26 22:01:41 +02:00
class Sidebar(sqlobject.SQLObject):
sidebar = sqlobject.UnicodeCol()
2012-04-20 03:49:00 +02:00
def render(filename, **values):
stream = loader.load(filename).generate(**values)
return stream.render("html", doctype="html")
def get_filename(index):
2015-04-25 23:58:21 +02:00
return os.path.join(OUTPUT_DIR, FILENAME_TEMPLATE % index)
2012-04-20 03:49:00 +02:00
def get_link(index):
return FILENAME_TEMPLATE % index
def update_static_html():
entries = list(Entry.select().orderBy("-timestamp"))
2016-04-26 22:01:41 +02:00
try:
sidebar_text = Sidebar.select()[0].sidebar
2016-04-27 00:38:50 +02:00
except (AttributeError, IndexError) as exc:
2016-04-26 22:01:41 +02:00
sidebar_text = ""
2012-04-20 03:49:00 +02:00
file_index = 1
2016-04-26 22:01:41 +02:00
remaining_entries = entries
while remaining_entries:
current = remaining_entries[:PAGE_SIZE]
remaining_entries = remaining_entries[PAGE_SIZE:]
2012-04-20 03:49:00 +02:00
current_filename = get_filename(file_index)
if file_index > 1:
previous_link = get_link(file_index - 1)
else:
previous_link = None
2016-04-26 22:01:41 +02:00
if remaining_entries:
2012-04-20 03:49:00 +02:00
next_link = get_link(file_index + 1)
else:
next_link = None
2016-04-26 22:01:41 +02:00
values = {"entries": current,
"prev_link": previous_link,
"next_link": next_link,
"sidebar_text": sidebar_text,
2012-04-20 03:49:00 +02:00
}
2016-04-26 22:01:41 +02:00
# WAP unterstuetzt wohl nur Latin-1
rendered = render("display.html", **values).encode("latin-1")
2012-04-20 03:49:00 +02:00
open(current_filename, "w").write(rendered)
2016-04-26 22:01:41 +02:00
subprocess.call(["html2wml", "-n", current_filename, "-o", "ticker%d.wml" % file_index], cwd=OUTPUT_DIR)
2012-04-20 03:49:00 +02:00
file_index += 1
2016-04-26 22:01:41 +02:00
2012-04-20 03:49:00 +02:00
@bobo.query('/submit')
def submit_entry(entry_id=None, title=None, content=None, date=None):
title = title.strip()
content = content.strip()
2015-04-27 22:40:01 +02:00
if not all((content, date)):
2012-04-20 03:49:00 +02:00
return bobo.redirect(BASE_URL)
try:
2015-04-25 23:58:21 +02:00
date = datetime.datetime.strptime(date, DATE_FORMAT_FULL)
2012-04-20 03:49:00 +02:00
except ValueError:
bobo.redirect(BASE_URL)
if entry_id is None:
Entry(title=title, content=content, timestamp=date)
else:
try:
entry = Entry.get(entry_id)
except sqlobject.SQLObjectNotFound:
bobo.redirect(BASE_URL)
entry.title = title
entry.content = content
entry.date = date
update_static_html()
return bobo.redirect(BASE_URL)
2016-04-26 22:01:41 +02:00
@bobo.query('/submit_sidebar')
def submit_sb(sidebar=None):
sidebar = sidebar.strip()
try:
entry = Sidebar.select()[0]
entry.sidebar = sidebar
except IndexError:
Sidebar(sidebar=sidebar)
update_static_html()
return bobo.redirect(BASE_URL)
2012-04-20 03:49:00 +02:00
@bobo.query('/delete')
def delete_entry(entry_id):
try:
Entry.delete(entry_id)
except sqlobject.SQLObjectNotFound:
bobo.redirect(BASE_URL)
update_static_html()
return bobo.redirect(BASE_URL)
@bobo.query('/generate')
def generate_static():
update_static_html()
return bobo.redirect(BASE_URL)
@bobo.query('/')
2015-04-27 22:07:18 +02:00
def create_entry():
2012-04-20 03:49:00 +02:00
values = {}
2015-04-25 23:58:21 +02:00
values["date_format"] = DATE_FORMAT_FULL
2012-04-20 03:49:00 +02:00
values["static_url"] = get_link(1)
2016-04-26 22:01:41 +02:00
return render("create.html", **values)
2015-04-27 22:07:18 +02:00
@bobo.query('/edit')
def edit_entries():
values = {}
values["entries"] = Entry.select().orderBy("-timestamp")
values["date_format"] = DATE_FORMAT_FULL
values["static_url"] = get_link(1)
2016-04-26 22:01:41 +02:00
return render("edit.html", **values)
@bobo.query('/sidebar')
def edit_sidebar():
entries_sb = list(Sidebar.select())
if len(entries_sb) == 0:
sidebar = Sidebar(sidebar="")
else:
sidebar = entries_sb[0]
values = {}
values["sidebar"] = sidebar
values["static_url"] = get_link(1)
return render("sidebar.html", **values)
2012-04-20 03:49:00 +02:00
@bobo.query('')
def redirect_base():
return bobo.redirect(os.path.split(__file__)[1] + "/")
for table in (Entry, ):
#table.dropTable()
if not table.tableExists():
table.createTable()
2016-04-26 22:01:41 +02:00
for table in (Sidebar, ):
#table.dropTable()
if not table.tableExists():
table.createTable()
#Sidebar(sidebar='Test')
2012-04-20 03:49:00 +02:00
application = bobo.Application(bobo_resources=__name__)
2016-04-26 22:01:41 +02:00