def render(self): widget_id = self.request.get("widget_id") if widget_id: setHeader = self.request.response.setHeader setHeader("Content-Type", "text/javascript") upload_utility = getUtility(IUpload) url = upload_utility.upload_url() # XXX: Workaround for translating the JS strings # XXX: We need to get the lang from the request, instead of like this. upload_error = _(u"Error uploading file, please try again or use a diferent file") upload_error = translate(upload_error, domain="openmultimedia.reporter", target_language="es") upload_success = _(u"File uploaded correctly") upload_success = translate(upload_success, domain="openmultimedia.reporter", target_language="es") already_uploaded = _(u"Your file was already uploaded, no need to do it again.") already_uploaded = translate(already_uploaded, domain="openmultimedia.reporter", target_language="es") return self.js_template_input % dict( id=widget_id, id_uploader=widget_id + "-uploader", upload_url=url, upload_error=upload_error, upload_success=upload_success, already_uploaded=already_uploaded, )
def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return obj = self.createAndAdd(data) if obj is None: raise ActionExecutionError(Invalid(_(u"Error creating the report, please try again"))) body = {} if "file_type" in data and data["file_type"]: file_type = data["file_type"] else: self.context.manage_delObjects(obj.id) raise ActionExecutionError(Invalid(_(u"Error creating the " "report, please try again"))) if "title" in data: body["titulo"] = data["title"].encode("utf-8", "ignore") if "report" in data: body["report"] = data["report"].encode("utf-8", "ignore") if "country" in data: body["country"] = obj.get_country_code() if "date" in data: body["date"] = data["date"].strftime("%Y-%m-%d %H:%M") if "name" in data: body["name"] = data["name"].encode("utf-8", "ignore") if "file_id" in data and data["file_id"]: body["archivo"] = data["file_id"] body["tipo"] = "soy-reportero" callback_url = obj.generate_callback_url(self.context) body["callback"] = callback_url upload_utility = getUtility(IUpload) response, content = upload_utility.create_structure(body, file_type) if "status" not in response.keys() or response["status"] != "200": logger.info("Invalid response from server: %s" % response) self.context.manage_delObjects(obj.id) raise ActionExecutionError(Invalid(_(u"Error creating the " "report, please try again"))) if content and "slug" in content: slug = content["slug"] else: logger.info("No slug provided: %s" % content) self.context.manage_delObjects(obj.id) raise ActionExecutionError(Invalid(_(u"Error creating the " "report, please try again"))) obj.file_slug = slug obj.reindexObject() self._finishedAdd = True self.request.RESPONSE.redirect(self.context.absolute_url()) return obj