| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | ## |
|---|
| 3 | ## |
|---|
| 4 | ## This file is part of CDS Indico. |
|---|
| 5 | ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 CERN. |
|---|
| 6 | ## |
|---|
| 7 | ## CDS Indico is free software; you can redistribute it and/or |
|---|
| 8 | ## modify it under the terms of the GNU General Public License as |
|---|
| 9 | ## published by the Free Software Foundation; either version 2 of the |
|---|
| 10 | ## License, or (at your option) any later version. |
|---|
| 11 | ## |
|---|
| 12 | ## CDS Indico is distributed in the hope that it will be useful, but |
|---|
| 13 | ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 | ## General Public License for more details. |
|---|
| 16 | ## |
|---|
| 17 | ## You should have received a copy of the GNU General Public License |
|---|
| 18 | ## along with CDS Indico; if not, write to the Free Software Foundation, Inc., |
|---|
| 19 | ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 20 | |
|---|
| 21 | """ |
|---|
| 22 | HTTP API - Handlers |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | # python stdlib imports |
|---|
| 26 | import re |
|---|
| 27 | from urlparse import parse_qs |
|---|
| 28 | |
|---|
| 29 | # indico imports |
|---|
| 30 | from indico.web.http_api import ExportInterface |
|---|
| 31 | from indico.web.http_api.cache import RequestCache |
|---|
| 32 | from indico.web.http_api.util import remove_lists |
|---|
| 33 | from indico.web.wsgi import webinterface_handler_config as apache |
|---|
| 34 | from indico.util.metadata.serializer import Serializer |
|---|
| 35 | |
|---|
| 36 | # indico legacy imports |
|---|
| 37 | from MaKaC.common import DBMgr |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | from indico.web.http_api.util import get_query_parameter |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | class HTTPAPIError(Exception): |
|---|
| 44 | pass |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | def handler(req, **params): |
|---|
| 48 | path, query = req.URLFields['PATH_INFO'], req.URLFields['QUERY_STRING'] |
|---|
| 49 | qdata = parse_qs(query) |
|---|
| 50 | qdata_copy = dict(qdata) |
|---|
| 51 | |
|---|
| 52 | cache = RequestCache() |
|---|
| 53 | obj = cache.loadObject(path, qdata_copy) |
|---|
| 54 | |
|---|
| 55 | resp = None |
|---|
| 56 | from_cache = False |
|---|
| 57 | if obj is not None: |
|---|
| 58 | resp = obj.getContent() |
|---|
| 59 | from_cache = True |
|---|
| 60 | else: |
|---|
| 61 | pretty = get_query_parameter(qdata, ['p', 'pretty'], 'no') == 'yes' |
|---|
| 62 | |
|---|
| 63 | results = None |
|---|
| 64 | m = re.match(r'/export/(event|categ)/(\w+(?:-\w+)*)\.(\w+)/?$', path) |
|---|
| 65 | if m and m.group(3) in ExportInterface.getAllowedFormats(): |
|---|
| 66 | dtype, idlist, dformat = m.groups() |
|---|
| 67 | idlist = idlist.split('-') |
|---|
| 68 | |
|---|
| 69 | expInt = ExportInterface(DBMgr.getInstance()) |
|---|
| 70 | |
|---|
| 71 | tzName = get_query_parameter(qdata, ['tz'], None) |
|---|
| 72 | |
|---|
| 73 | if dtype == 'categ': |
|---|
| 74 | results = expInt.category(idlist, tzName, qdata) |
|---|
| 75 | |
|---|
| 76 | if results: |
|---|
| 77 | serializer = Serializer.create(dformat, pretty=pretty, **remove_lists(qdata)) |
|---|
| 78 | resp = serializer.getMIMEType(), serializer(results) |
|---|
| 79 | |
|---|
| 80 | if resp: |
|---|
| 81 | if not from_cache: |
|---|
| 82 | cache.cacheObject(path, qdata_copy, resp) |
|---|
| 83 | mime, result = resp |
|---|
| 84 | req.headers_out['Content-Type'] = mime |
|---|
| 85 | return result |
|---|
| 86 | else: |
|---|
| 87 | # TODO: usage page |
|---|
| 88 | raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND |
|---|