2009-10-04 22:00:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2012-01-03 20:18:47 +00:00
|
|
|
import copy
|
2012-10-02 08:36:43 +00:00
|
|
|
from datetime import datetime
|
2012-01-03 20:18:47 +00:00
|
|
|
|
2011-09-29 15:16:48 +00:00
|
|
|
from django.shortcuts import render_to_response, redirect
|
2009-10-04 22:00:08 +00:00
|
|
|
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
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2011-09-29 15:16:48 +00:00
|
|
|
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
|
|
|
|
|
2012-01-09 09:06:35 +00:00
|
|
|
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
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2012-01-03 20:18:47 +00:00
|
|
|
from user.models import init_user
|
2010-12-22 07:45:37 +00:00
|
|
|
|
2012-01-03 20:18:47 +00:00
|
|
|
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)
|
|
|
|
|
2009-10-04 22:00:08 +00:00
|
|
|
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
|
2011-11-02 22:29:20 +00:00
|
|
|
context = RequestContext(request, {
|
|
|
|
'base_url': request.build_absolute_uri('/'),
|
2012-01-09 09:06:35 +00:00
|
|
|
'settings': settings,
|
2012-01-15 15:44:41 +00:00
|
|
|
'text': text,
|
|
|
|
'title': title,
|
2011-11-02 22:29:20 +00:00
|
|
|
})
|
2009-10-04 22:00:08 +00:00
|
|
|
return render_to_response('index.html', context)
|
|
|
|
|
2011-10-22 11:24:26 +00:00
|
|
|
def embed(request, id):
|
2011-12-05 13:48:59 +00:00
|
|
|
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'
|
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(
|
2012-01-11 07:51:26 +00:00
|
|
|
'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
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def getPage(request):
|
2011-01-21 15:44:46 +00:00
|
|
|
'''
|
|
|
|
param data {
|
|
|
|
name: pagename
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: ...
|
|
|
|
data: {
|
|
|
|
name:
|
2012-02-18 11:59:50 +00:00
|
|
|
text:
|
2011-01-21 15:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
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:
|
2012-06-13 15:59:10 +00:00
|
|
|
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)
|
2010-12-22 07:45:37 +00:00
|
|
|
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
|
2012-02-18 11:59:50 +00:00
|
|
|
text: text
|
2011-01-21 15:44:46 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: ...
|
|
|
|
data: {
|
|
|
|
name:
|
2012-02-18 11:59:50 +00:00
|
|
|
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
|
|
|
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-05-27 11:52:12 +00:00
|
|
|
page.text = ox.sanitize_html(data['text'])
|
2012-01-07 10:50:33 +00:00
|
|
|
page.save()
|
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
|
|
|
|
2012-01-03 20:18:47 +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)
|
2012-10-02 08:36:43 +00:00
|
|
|
request.session['last_init'] = str(datetime.now())
|
2012-01-03 20:18:47 +00:00
|
|
|
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)
|