pandora/pandora/app/views.py

227 lines
7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import copy
from datetime import datetime
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 changelog.models import add_changelog
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)
2012-02-18 11:59:50 +00:00
text = text.text
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['site'].get('sendReferrer', False):
2012-01-10 16:00:41 +00:00
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'
2012-03-03 12:36:24 +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
2014-10-06 08:26:43 +00:00
def getPage(request, data):
2011-01-21 15:44:46 +00:00
'''
2013-03-04 19:35:06 +00:00
takes {
2011-01-21 15:44:46 +00:00
name: pagename
}
2013-03-04 19:35:06 +00:00
returns {
name:
text:
2011-01-21 15:44:46 +00:00
}
'''
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.text= ''
2011-01-28 08:48:38 +00:00
page.save()
2012-02-18 11:59:50 +00:00
response = json_response({'name': page.name, 'text': page.text})
2010-02-10 13:10:14 +00:00
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
2014-10-06 08:26:43 +00:00
def editPage(request, data):
2011-01-21 15:44:46 +00:00
'''
2013-03-04 19:35:06 +00:00
takes {
2011-01-21 15:44:46 +00:00
name: pagename
2012-02-18 11:59:50 +00:00
text: text
2011-01-21 15:44:46 +00:00
}
2013-03-04 19:35:06 +00:00
returns {
name:
text:
2011-01-21 15:44:46 +00:00
}
'''
2012-01-07 10:53:36 +00:00
if request.user.get_profile().capability('canEditSitePages'):
2012-01-07 10:50:33 +00:00
page, created = models.Page.objects.get_or_create(name=data['name'])
2012-05-27 11:52:12 +00:00
page.text = ox.sanitize_html(data['text'])
2012-01-07 10:50:33 +00:00
page.save()
add_changelog(request, data, page.name)
2012-02-18 11:59:50 +00:00
response = json_response({'name': page.name, 'text': page.text})
2012-01-07 10:50:33 +00:00
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
2014-10-06 08:26:43 +00:00
def init(request, data):
'''
2013-03-04 19:35:06 +00:00
takes {}
returns {
user: object
}
'''
response = json_response({})
config = copy.deepcopy(settings.CONFIG)
del config['keys']
if 'HTTP_ACCEPT_LANGUAGE' in request.META:
response['data']['locale'] = request.META['HTTP_ACCEPT_LANGUAGE'].split(';')[0].split('-')[0]
response['data']['site'] = config
response['data']['user'] = init_user(request.user, request)
request.session['last_init'] = str(datetime.now())
return render_to_json_response(response)
actions.register(init)
2012-01-10 16:00:41 +00:00
2014-10-06 08:26:43 +00:00
def embedURL(request, data):
2012-01-10 16:00:41 +00:00
'''
2013-03-04 19:35:06 +00:00
takes {
2012-01-10 16:00:41 +00:00
url
maxwidth
maxheight
}
2013-03-04 19:35:06 +00:00
returns {
html
...
2012-01-10 16:00:41 +00:00
}
'''
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)
2014-02-16 10:16:31 +00:00
2014-10-06 08:26:43 +00:00
def getEmbedDefaults(request, data):
2014-02-16 10:16:31 +00:00
'''
takes {}
returns {
document: str // first document, sorted by id
edit: str // first edit, sorted by name
editDuration: float // duration of that edit
item: str // first item, sorted by id
itemDuration: float // duration of that item
itemRatio: float // video ratio of that item
list: str // first list, sorted by name
text: str // first text, sorted by name
2014-02-16 10:31:37 +00:00
videoRatio: float // pandora.site.video.previewRatio
2014-02-16 10:16:31 +00:00
videoResolution: int // largest value in pandora.site.video.resolutions
}
'''
from document.models import Document
from item.models import Item
from itemlist.models import List
from edit.models import Edit
from text.models import Text
response = json_response({})
qs = Document.objects.filter(uploading=False).order_by('id')
if qs.exists():
response['data']['document'] = qs[0].get_id()
qs = Edit.objects.exclude(status='private').order_by('name')
if qs.exists():
e = qs[0].json(keys=['id', 'duration'])
response['data']['edit'] = e['id']
response['data']['editDuration'] = e['duration']
level = settings.CONFIG['capabilities']['canSeeItem']['guest']
2014-09-19 12:26:46 +00:00
qs = Item.objects.filter(level__lte=level, rendered=True).order_by('sort__public_id')
2014-02-16 10:16:31 +00:00
if qs.exists():
i = qs[0].json
response['data']['item'] = i['id']
response['data']['itemDuration'] = i['duration']
response['data']['itemRatio'] = i['videoRatio']
qs = List.objects.exclude(status='private').order_by('name')
if qs.exists():
i = qs[0].json()
response['data']['list'] = i['id']
2014-02-16 10:31:37 +00:00
response['data']['videoRatio'] = settings.CONFIG['video']['previewRatio']
2014-02-16 10:16:31 +00:00
response['data']['videoResolution'] = max(settings.CONFIG['video']['resolutions'])
return render_to_json_response(response)
actions.register(getEmbedDefaults)