piwigo: Add PwgError class.
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Mon, 20 May 2013 17:28:35 +0000 (19:28 +0200)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Mon, 20 May 2013 17:35:16 +0000 (19:35 +0200)
This allows to return piwigo xml errors.
Those can also be matched into html error codes.

mediagoblin/plugins/piwigo/tools.py

index f060e9f8c54dd2f91f4e912c67ee4b6486adb0ef..30bb4b977447ed471ec50a6be4900d42744b374a 100644 (file)
@@ -14,6 +14,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from collections import namedtuple
 import logging
 
 import six
@@ -27,6 +28,9 @@ from mediagoblin.tools.response import Response
 _log = logging.getLogger(__name__)
 
 
+PwgError = namedtuple("PwgError", ["code", "msg"])
+
+
 class PwgNamedArray(list):
     def __init__(self, l, item_name, as_attrib=()):
         self.item_name = item_name
@@ -74,9 +78,17 @@ def _fill_element(el, data):
 def response_xml(result):
     r = ET.Element("rsp")
     r.set("stat", "ok")
-    _fill_element(r, result)
+    status = None
+    if isinstance(result, PwgError):
+        r.set("stat", "fail")
+        err = ET.SubElement(r, "err")
+        err.set("code", str(result.code))
+        err.set("msg", result.msg)
+        status = result.code
+    else:
+        _fill_element(r, result)
     return Response(ET.tostring(r, encoding="utf-8", xml_declaration=True),
-                    mimetype='text/xml')
+                    mimetype='text/xml', status=status)
 
 
 class CmdTable(object):