| 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.wsgi import webinterface_handler_config as apache |
|---|
| 32 | |
|---|
| 33 | # indico legacy imports |
|---|
| 34 | from MaKaC.common import DBMgr |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | class HTTPAPIError(Exception): |
|---|
| 38 | pass |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | def get_query_parameter(qdata, keys, default=None, integer=False): |
|---|
| 42 | if type(keys) != list: |
|---|
| 43 | keys = list(keys) |
|---|
| 44 | for k in keys: |
|---|
| 45 | paramlist = qdata.get(k) |
|---|
| 46 | if paramlist: |
|---|
| 47 | if len(paramlist) == 1: |
|---|
| 48 | val = paramlist[0] |
|---|
| 49 | if integer: |
|---|
| 50 | val = int(val) |
|---|
| 51 | return val |
|---|
| 52 | else: |
|---|
| 53 | raise Exception("duplicate argument' %s'!" % k) |
|---|
| 54 | return None |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | def handler(req, **params): |
|---|
| 58 | path, query = req.URLFields['PATH_INFO'], req.URLFields['QUERY_STRING'] |
|---|
| 59 | |
|---|
| 60 | m = re.match(r'/export/(event|categ)/(\w+(?:-\w+)*)\.(json|xml|rss|ical|html)/?', |
|---|
| 61 | path) |
|---|
| 62 | |
|---|
| 63 | if m: |
|---|
| 64 | dtype, idlist, dformat = m.groups() |
|---|
| 65 | idlist = idlist.split('-') |
|---|
| 66 | qdata = dict(parse_qs(query)) |
|---|
| 67 | orderBy = get_query_parameter(qdata, ['o', 'order']) |
|---|
| 68 | descending = get_query_parameter(qdata, ['c', 'descending'], False) |
|---|
| 69 | detail = get_query_parameter(qdata, ['d', 'detail'], 'events') |
|---|
| 70 | |
|---|
| 71 | expInt = ExportInterface(DBMgr.getInstance()) |
|---|
| 72 | |
|---|
| 73 | if dtype == 'categ': |
|---|
| 74 | fromDT = get_query_parameter(qdata, ['f', 'from']) |
|---|
| 75 | toDT = get_query_parameter(qdata, ['t', 'to']) |
|---|
| 76 | location = get_query_parameter(qdata, ['l', 'location']) |
|---|
| 77 | limit = get_query_parameter(qdata, ['n', 'limit'], integer=True) |
|---|
| 78 | |
|---|
| 79 | return expInt.category(idlist, dformat, fromDT, toDT, location, |
|---|
| 80 | limit, orderBy, descending, detail) |
|---|
| 81 | else: |
|---|
| 82 | # TODO: usage page |
|---|
| 83 | raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND |
|---|