piwigo: Add .images.add including form handling.
[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 import re
19
20 from werkzeug.exceptions import MethodNotAllowed, BadRequest, NotImplemented
21 from werkzeug.wrappers import BaseResponse
22
23 from mediagoblin import mg_globals
24 from mediagoblin.meddleware.csrf import csrf_exempt
25 from mediagoblin.submit.lib import check_file_field
26 from .tools import CmdTable, PwgNamedArray, response_xml, check_form
27 from .forms import AddSimpleForm, AddForm
28
29
30 _log = logging.getLogger(__name__)
31
32
33 @CmdTable("pwg.session.login", True)
34 def pwg_login(request):
35 username = request.form.get("username")
36 password = request.form.get("password")
37 _log.info("Login for %r/%r...", username, password)
38 return True
39
40
41 @CmdTable("pwg.session.logout")
42 def pwg_logout(request):
43 _log.info("Logout")
44 return True
45
46
47 @CmdTable("pwg.getVersion")
48 def pwg_getversion(request):
49 return "2.5.0 (MediaGoblin)"
50
51
52 @CmdTable("pwg.session.getStatus")
53 def pwg_session_getStatus(request):
54 return {'username': "fake_user"}
55
56
57 @CmdTable("pwg.categories.getList")
58 def pwg_categories_getList(request):
59 catlist = ({'id': -29711,
60 'uppercats': "-29711",
61 'name': "All my images"},)
62 return {
63 'categories': PwgNamedArray(
64 catlist,
65 'category',
66 (
67 'id',
68 'url',
69 'nb_images',
70 'total_nb_images',
71 'nb_categories',
72 'date_last',
73 'max_date_last',
74 )
75 )
76 }
77
78
79 @CmdTable("pwg.images.exist")
80 def pwg_images_exist(request):
81 return {}
82
83
84 @CmdTable("pwg.images.addSimple", True)
85 def pwg_images_addSimple(request):
86 form = AddSimpleForm(request.form)
87 if not form.validate():
88 _log.error("addSimple: form failed")
89 raise BadRequest()
90 dump = []
91 for f in form:
92 dump.append("%s=%r" % (f.name, f.data))
93 _log.info("addimple: %r %s %r", request.form, " ".join(dump), request.files)
94
95 if not check_file_field(request, 'image'):
96 raise BadRequest()
97
98 return {'image_id': 123456, 'url': ''}
99
100
101 md5sum_matcher = re.compile(r"^[0-9a-fA-F]{32}$")
102
103 def fetch_md5(request, parm_name, optional_parm=False):
104 val = request.form.get(parm_name)
105 if (val is None) and (not optional_parm):
106 _log.error("Parameter %s missing", parm_name)
107 raise BadRequest("Parameter %s missing" % parm_name)
108 if not md5sum_matcher.match(val):
109 _log.error("Parameter %s=%r has no valid md5 value", parm_name, val)
110 raise BadRequest("Parameter %s is not md5" % parm_name)
111 return val
112
113
114 @CmdTable("pwg.images.addChunk", True)
115 def pwg_images_addChunk(request):
116 o_sum = fetch_md5(request, 'original_sum')
117 typ = request.form.get('type')
118 pos = request.form.get('position')
119 data = request.form.get('data')
120
121 # Validate params:
122 pos = int(pos)
123 if not typ in ("file", "thumb"):
124 _log.error("type %r not allowed for now", typ)
125 return False
126
127 _log.info("addChunk for %r, type %r, position %d, len: %d",
128 o_sum, typ, pos, len(data))
129 if typ == "thumb":
130 _log.info("addChunk: Ignoring thumb, because we create our own")
131 return True
132
133 return True
134
135
136 @CmdTable("pwg.images.add", True)
137 def pwg_images_add(request):
138 _log.info("add: %r", request.form)
139 form = AddForm(request.form)
140 check_form(form)
141
142 return {'image_id': 123456, 'url': ''}
143
144
145 @csrf_exempt
146 def ws_php(request):
147 if request.method not in ("GET", "POST"):
148 _log.error("Method %r not supported", request.method)
149 raise MethodNotAllowed()
150
151 func = CmdTable.find_func(request)
152 if not func:
153 _log.warn("wsphp: Unhandled %s %r %r", request.method,
154 request.args, request.form)
155 raise NotImplemented()
156
157 result = func(request)
158
159 if isinstance(result, BaseResponse):
160 return result
161
162 response = response_xml(result)
163
164 return response