6198ec6e498b8ff34fca9e70f19d143542348fe7
[mediagoblin.git] / mediagoblin / plugins / piwigo / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2013 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import logging
18
19 from werkzeug.exceptions import MethodNotAllowed
20 from werkzeug.wrappers import BaseResponse
21
22 from mediagoblin.meddleware.csrf import csrf_exempt
23 from mediagoblin.tools.response import render_404
24 from .tools import CmdTable, PwgNamedArray, response_xml
25
26
27 _log = logging.getLogger(__name__)
28
29
30 @CmdTable("pwg.session.login", True)
31 def pwg_login(request):
32 username = request.form.get("username")
33 password = request.form.get("password")
34 _log.info("Login for %r/%r...", username, password)
35 return True
36
37
38 @CmdTable("pwg.session.logout")
39 def pwg_logout(request):
40 _log.info("Logout")
41 return True
42
43
44 @CmdTable("pwg.getVersion")
45 def pwg_getversion(request):
46 return "piwigo 2.5.0 (MediaGoblin)"
47
48
49 @CmdTable("pwg.session.getStatus")
50 def pwg_session_getStatus(request):
51 return {'username': "fake_user"}
52
53
54 @CmdTable("pwg.categories.getList")
55 def pwg_categories_getList(request):
56 catlist = ({'id': -29711,
57 'uppercats': "-29711",
58 'name': "All my images"},)
59 return {
60 'categories': PwgNamedArray(
61 catlist,
62 'category',
63 (
64 'id',
65 'url',
66 'nb_images',
67 'total_nb_images',
68 'nb_categories',
69 'date_last',
70 'max_date_last',
71 )
72 )
73 }
74
75
76 def possibly_add_cookie(request, response):
77 # TODO: We should only add a *real* cookie, if
78 # authenticated. And if there is no cookie already.
79 if True:
80 response.set_cookie(
81 'pwg_id',
82 "some_fake_for_now",
83 path=request.environ['SCRIPT_NAME'],
84 domain=mg_globals.app_config.get('csrf_cookie_domain'),
85 secure=(request.scheme.lower() == 'https'),
86 httponly=True)
87
88
89 @csrf_exempt
90 def ws_php(request):
91 if request.method not in ("GET", "POST"):
92 _log.error("Method %r not supported", request.method)
93 raise MethodNotAllowed()
94
95 func = CmdTable.find_func(request)
96 if not func:
97 _log.warn("wsphp: Unhandled %s %r %r", request.method,
98 request.args, request.form)
99 return render_404(request)
100
101 result = func(request)
102
103 if isinstance(result, BaseResponse):
104 return result
105
106 response = response_xml(result)
107
108 possibly_add_cookie(request, response)
109
110 return response