add msp strongbox module

This commit is contained in:
Benjamin Dauvergne 2013-10-22 12:33:25 +02:00
parent b7279f8185
commit 64c1c6ef1e
6 changed files with 198 additions and 3 deletions

View File

@ -11,4 +11,14 @@ class FileWithPreviewAndStrongboxWidget(FileWithPreviewWidget):
data-url="%smyspace/strongbox/pick"
rel="popup">%s</span></p>''') % (
root_url, _('Use file from strongbox'))
if get_publisher().has_site_option('msp') and not self.preview:
get_response().add_javascript(['../../aq/js/msp.js'])
root_url = get_publisher().get_root_url()
t += htmltext('''<p class="use-file-from-msp"><span
data-src="%smsp/pick"
data-width="500"
data-height="400"
data-title="Pick file on MSP"
rel="popup">%s</span></p>''') % (
root_url, _('Use file from mon.Service-Public.fr'))
return t

145
extra/modules/msp_ui.ptl Normal file
View File

@ -0,0 +1,145 @@
import urllib
import urllib2
import urlparse
import json
import base64
import datetime
from quixote import get_publisher, get_request, redirect, get_response, get_session_manager, get_session
from quixote.directory import AccessControlled, Directory
import qommon.form
authorize_url = 'https://portail-citoyen.dev.au-quotidien.com/msp/authorize/'
documents_url = 'https://portail-citoyen.dev.au-quotidien.com/msp/documents/'
document_url = 'https://portail-citoyen.dev.au-quotidien.com/msp/documents/%s/'
class MSPDirectory(Directory):
_q_exports = ['', 'pick', 'download']
def fix_document(self, document, site_charset):
for key, value in document.iteritems():
if key.endswith('Date') and value is not None:
document[key] = unicode(datetime.date.fromtimestamp(value // 1000))
value = document[key] = document[key].replace(u'-', u'\u2011')
if isinstance(value, unicode) and key != 'content':
document[key] = value.encode(site_charset)
def get_documents(self, code):
params = {
'code': code,
'redirect_uri': authorize_url,
}
url = '%s?%s' % (documents_url, urllib.urlencode(params))
content = urllib2.urlopen(url).read()
documents = json.loads(content)
'<ul>'
site_charset = get_publisher().site_charset
for document in documents:
self.fix_document(document, site_charset)
return documents
def get_document(self, code, doc_id):
params = {
'code': code,
'redirect_uri': authorize_url,
}
url = '%s?%s' % (document_url % doc_id, urllib.urlencode(params))
content = urllib2.urlopen(url).read()
document = json.loads(content)
site_charset = get_publisher().site_charset
self.fix_document(document, site_charset)
return document
def authorize(self, self_url, scope):
params = {
'redirect_uri': self_url,
'response_type': 'code',
'scope': scope,
}
return redirect('%s?%s' % (
authorize_url, urllib.urlencode(params)))
def pick (self):
request = get_request()
frontoffice_url = get_publisher().get_frontoffice_url()
self_url = frontoffice_url
self_url += '/msp/pick'
self_url = self_url.replace('//localhost', '//iframe-localhost')
if 'code' not in request.form and 'error' not in request.form:
return self.authorize(self_url, 'LIST_DOCS')
return self.pick_display(request.form['code'])
def pick_display [html] (self, code):
request = get_request()
get_response().add_javascript(['jquery.js',
'tablesorter/jquery.tablesorter.min.js'])
get_response().add_javascript_code(
str('''$(function() { $("table.sortable").tablesorter(); });'''))
get_response().add_css_include('../js/tablesorter/themes/blue/style.css')
frontoffice_url = get_publisher().get_frontoffice_url()
'<h2>%s</h2>' % _('Pick a file')
if 'error' not in request.form:
documents = self.get_documents(code)
'<table id="msp-pick-file-table" class="sortable tablesorter">'
'<thead>'
'<tr>'
'<th>%s</th>' % _('Filename')
'<th>%s</th>' % _('Title')
'<th>%s</th>' % _('Creation date')
'<th>%s</th>' % _('Expiration date')
'<th>%s</th>' % _('Description')
'</tr>'
'</thead>'
'<tbody>'
for document in documents:
'<tr>'
for key in ('name', 'title', 'createdDate', 'expirationDate',
'description'):
'<td class="msp-pick-file-table-%s">' % key
if key == 'name':
'<a href="%s/msp/download?doc_id=%s">' % \
(frontoffice_url, document['id'])
'%s' % (document[key] or '')
if key == 'name':
'</a>'
'</td>'
'</tr>'
'</tbody>'
'</table>'
else:
'<p>%s</p>' % _('Unable to access your mon.Service-Public.fr documents')
def set_token [html] (self, token, document):
get_response().add_javascript(['jquery.js'])
get_response().page_template_key = 'iframe'
'<html><body>'
'<pre>Token: %s</pre>' % token
'<script>window.top.document.set_token("%s", "%s");</script>' % (token, document['name'])
'</body></html>'
def download(self):
request = get_request()
assert 'doc_id' in request.form
doc_id = request.form['doc_id']
frontoffice_url = get_publisher().get_frontoffice_url()
self_url = frontoffice_url
self_url += '/msp/download?%s' % urllib.urlencode({'doc_id': doc_id})
if 'code' not in request.form and 'error' not in request.form:
return self.authorize(self_url, 'GET_DOC')
if 'error' in request.form:
return self.download_error()
else:
document = self.get_document(request.form['code'], doc_id)
download = qommon.form.PicklableUpload(document['name'],
content_type='application/pdf')
download.__setstate__({
'data': base64.b64decode(document['content']),
})
token = get_session().add_tempfile(download)
return self.set_token(token, document)

View File

@ -26,7 +26,7 @@ import root
from announces import AnnounceSubscription
from strongbox import StrongboxItem, StrongboxType
from payments import Invoice, Regie, is_payment_supported
import msp_ui
class MyInvoicesDirectory(Directory):
_q_exports = ['']
@ -408,8 +408,9 @@ class JsonDirectory(Directory):
class MyspaceDirectory(Directory):
_q_exports = ['', 'profile', 'new', 'password', 'remove', 'announces',
'strongbox', 'invoices', 'json']
'strongbox', 'invoices', 'json', 'msp']
msp = msp_ui.MSPDirectory()
strongbox = StrongboxDirectory()
invoices = MyInvoicesDirectory()
json = JsonDirectory()

View File

@ -53,6 +53,7 @@ import qommon.ident.idp
import drupal
import ezldap_ui
import msp_ui
def category_get_homepage_position(self):
if hasattr(self, 'homepage_position') and self.homepage_position:
@ -727,7 +728,8 @@ class AlternateRootDirectory(OldRootDirectory):
'accessibility', 'contact', 'help',
'myspace', 'services', 'agenda', 'categories', 'user',
('tmp-upload', 'tmp_upload'), 'json', '__version__',
'themes', 'pages', 'payment', 'invoices', 'accesscode', 'roles']
'themes', 'pages', 'payment', 'invoices', 'accesscode', 'roles',
'msp']
admin = admin.AdminRootDirectory()
announces_dir = AnnouncesDirectory()
@ -739,6 +741,7 @@ class AlternateRootDirectory(OldRootDirectory):
saml = Saml2Directory()
payment = PublicPaymentDirectory()
invoices = InvoicesDirectory()
msp = msp_ui.MSPDirectory()
def get_substitution_variables(self):
d = {}

36
static/js/msp.js Normal file
View File

@ -0,0 +1,36 @@
$(function() {
var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>');
var dialog = $("<div></div>").append(iframe).appendTo("body").dialog({
autoOpen: false,
modal: true,
resizable: false,
width: "auto",
height: "auto",
close: function () {
iframe.attr("src", "");
}
});
$('p.use-file-from-msp span').click(function(e) {
e.preventDefault();
var base_widget = $(this).parents('.file-upload-widget');
document.set_token = function (token, title) {
$(base_widget).find('.filename').text(title);
$(base_widget).find('.fileinfo').show();
$(base_widget).find('input[type=hidden]').val(token);
$(base_widget).find('input[type=file]').hide();
document.set_token = undefined;
dialog.dialog('close');
}
var src = $(this).data('src');
var title = $(this).data("title");
var width = $(this).data("width");
var height = $(this).data("height");
iframe.attr({
width: +width,
height: +height,
src: src
});
dialog.dialog("option", "title", title);
dialog.dialog("open");
});
});

0
static/js/msp2.js Normal file
View File