pandora/pandora/app/views.py

189 lines
5.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import copy
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
2009-10-04 23:48:18 +00:00
from django.conf import settings
2011-10-09 09:43:05 +00:00
from django.http import HttpResponse
from ox.django.shortcuts import json_response, render_to_json_response
2011-01-21 15:44:46 +00:00
from ox.django.decorators import login_required_json
import ox
2012-01-10 16:00:41 +00:00
from ox.utils import json, ET
2010-02-10 13:10:14 +00:00
import models
from user.models import init_user
from ox.django.api import actions
2011-01-01 11:44:42 +00:00
2010-08-24 17:16:33 +00:00
def intro(request):
2011-01-01 11:44:42 +00:00
context = RequestContext(request, {'settings': settings})
2010-08-24 17:16:33 +00:00
return render_to_response('intro.html', context)
def index(request):
2012-01-15 15:44:41 +00:00
title = settings.SITENAME
text = settings.CONFIG['site']['description']
page = request.path.split('/')
if len(page) == 2:
page = page[1]
else:
page = ''
for p in settings.CONFIG['sitePages']:
if p['id'] == page:
title += ' - ' + p['title']
text, created = models.Page.objects.get_or_create(name=page)
text = text.body
context = RequestContext(request, {
'base_url': request.build_absolute_uri('/'),
'settings': settings,
2012-01-15 15:44:41 +00:00
'text': text,
'title': title,
})
return render_to_response('index.html', context)
2011-10-22 11:24:26 +00:00
def embed(request, id):
context = RequestContext(request, {
'settings': settings
})
2011-01-04 06:03:00 +00:00
return render_to_response('embed.html', context)
2012-01-10 16:00:41 +00:00
def redirect_url(request, url):
if request.META['QUERY_STRING']:
url += "?" + request.META['QUERY_STRING']
if settings.CONFIG.get('sendReferrer', False):
return redirect(url)
else:
return HttpResponse('<script>document.location.href=%s;</script>'%json.dumps(url))
2011-12-30 10:30:49 +00:00
def opensearch_xml(request):
osd = ET.Element('OpenSearchDescription')
osd.attrib['xmlns']="http://a9.com/-/spec/opensearch/1.1/"
e = ET.SubElement(osd, 'ShortName')
e.text = settings.SITENAME
e = ET.SubElement(osd, 'Description')
e.text = settings.SITENAME
e = ET.SubElement(osd, 'Image')
e.attrib['height'] = '16'
e.attrib['width'] = '16'
e.attrib['type'] = 'image/x-icon'
e.text = request.build_absolute_uri('/favicon.ico')
e = ET.SubElement(osd, 'Url')
e.attrib['type'] = 'text/html'
e.attrib['method'] = 'GET'
2011-12-30 21:14:09 +00:00
e.attrib['template'] = "%s={searchTerms}" % request.build_absolute_uri('/')
2011-12-30 10:30:49 +00:00
'''
e = ET.SubElement(osd, 'Url')
e.attrib['type'] = 'application/x-suggestions+json'
e.attrib['method'] = 'GET'
e.attrib['template'] = "%s?q={searchTerms}" % request.build_absolute_uri('/opensearch_suggest')
'''
return HttpResponse(
'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(osd),
'application/xml'
)
2012-01-11 07:42:32 +00:00
def robots_txt(request, url):
return HttpResponse(
'User-agent: *\nDisallow:\nSitemap: %s\n' % request.build_absolute_uri('/sitemap.xml'),
2012-01-11 07:42:32 +00:00
'text/plain'
)
2011-01-04 06:03:00 +00:00
def getPage(request):
2011-01-21 15:44:46 +00:00
'''
param data {
name: pagename
}
return {
status: ...
data: {
name:
body:
}
}
'''
2010-02-10 13:10:14 +00:00
data = json.loads(request.POST['data'])
2011-01-22 17:38:53 +00:00
if isinstance(data, basestring):
name = data
else:
name = data['name']
2011-01-28 08:48:38 +00:00
page, created = models.Page.objects.get_or_create(name=name)
if created:
page.body = 'Insert text here'
page.save()
2010-02-10 13:10:14 +00:00
response = json_response({'name': page.name, 'body': page.body})
return render_to_json_response(response)
actions.register(getPage)
2010-02-10 13:10:14 +00:00
2011-01-01 11:44:42 +00:00
2011-01-21 15:44:46 +00:00
@login_required_json
def editPage(request):
'''
param data {
name: pagename
body: text
}
return {
status: ...
data: {
name:
body:
}
}
'''
2012-01-07 10:53:36 +00:00
if request.user.get_profile().capability('canEditSitePages'):
2012-01-07 10:50:33 +00:00
data = json.loads(request.POST['data'])
page, created = models.Page.objects.get_or_create(name=data['name'])
2012-01-31 17:12:03 +00:00
if not created:
page.log()
2012-01-07 10:50:33 +00:00
page.body = data['body']
page.save()
response = json_response({'name': page.name, 'page': page.body})
else:
2011-01-21 15:44:46 +00:00
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
2011-10-05 17:35:16 +00:00
actions.register(editPage)
2011-01-21 15:44:46 +00:00
2011-10-23 01:30:09 +00:00
def init(request):
'''
return {'status': {'code': int, 'text': string},
'data': {user: object}}
'''
response = json_response({})
config = copy.deepcopy(settings.CONFIG)
del config['keys']
response['data']['site'] = config
response['data']['user'] = init_user(request.user, request)
return render_to_json_response(response)
actions.register(init)
2012-01-10 16:00:41 +00:00
def embedURL(request):
'''
param data {
url
maxwidth
maxheight
}
return {
status: ...
data: {
html
...
}
}
return {'status': {'code': int, 'text': string},
'data': {user: object}}
'''
data = json.loads(request.POST['data'])
response = json_response({})
response['data'] = ox.get_embed_code(data['url'], data.get('maxwidth'), data.get('maxheight'))
return render_to_json_response(response)
actions.register(embedURL)