def view_problema_yaml(self, request): """Obtener versión Yaml del problema, como opción de exportación""" headers = { "Content-type": "application/x-yaml", "Content-disposition": 'attachment; filename="problema-{}.{}"'.format( self.id, "-".join(self.tags.name)) } orig_data = request.view(self, name="full") data = collections.OrderedDict() data['resumen'] = orig_data["resumen"] data["enunciado"] = orig_data["enunciado"] data["cuestiones"] = [ collections.OrderedDict( (("enunciado", q["enunciado"]), ("respuesta", q["respuesta"]), ("puntos", q["puntos"]))) for q in orig_data["cuestiones"] ] data["tags"] = orig_data["tags"] data["puntos"] = orig_data["puntos"] data["fecha_creacion"] = orig_data["fecha_creacion"] data["fecha_modificacion"] = orig_data["fecha_modificacion"] data["creador"] = orig_data["creador"]["nombre"] data["problema_origen"] = orig_data["problema_origen"] data["problemas_derivados"] = orig_data["problemas_derivados"] data["examenes"] = [{ "asignatura": e["asignatura"], "fecha": e["fecha"], "estado": e["estado"] } for e in orig_data["examenes"]] content = yaml.dump(data, allow_unicode=True, default_flow_style=False) return morepath.Response(body=content, status=200, headers=headers)
def download_task_result(self, request): content = self.get_result(request) if type(content) == tuple: content = content[-1] if self.tipo == "zip": mimetype = "application/zip" elif self.tipo == "pdf": mimetype = "application/pdf" elif self.tipo == "tgz" or self.tipo == "tar.gz": mimetype = "application/gzip" else: mimetype = "application/octect-stream" headers = { "Content-type": mimetype, "Content-disposition": 'attachment; filename="examen.{}"'.format(self.tipo) } return morepath.Response(body=content, status=200, headers=headers)
def serve_static(context, request): path = context.resource_path() if not path: raise HTTPNotFound() settings = request.app.settings.__dict__ max_age = int(settings.get('caching', {}).get('max-age', (60 * 60))) etag = request.headers.get('If-None-Match', '') def add_headers(response): response.headers.add('Cache-Control', 'public, max-age=%s' % max_age) response.headers.add('Expires', ( datetime.utcnow() + timedelta(seconds=max_age)).strftime(r'%a, %d %b %Y %H:%M:%S GMT')) response.headers.add('ETag', ETAG) if etag and etag == ETAG: @request.after def add_notmodified_headers(response): add_headers(response) return morepath.Response(status=304) resp = request.get_response(static.FileApp(path)) if resp.status_code == 404: raise HTTPNotFound() if resp.status_code == 403: raise HTTPUnauthorized() @request.after def add_caching_headers(response): add_headers(response) return resp
def delete_circulo(self, request): """Eliminar un círculo""" circulos = coll.Circulos() if circulos.delete_object(self.id) is None: return None return morepath.Response(status=204)
def download_examen(self, request): examen_json = request.view(self, name="data") formato = request.GET.get("formato", "json") resuelto = request.GET.get("resuelto", "noresuelto").lower() sync = request.GET.get("sync", False) if sync == "true" or sync == "1": sync = True else: sync = False if resuelto in ["false", "no"]: resuelto = "noresuelto" elif resuelto in ["true", "si", "sí"]: resuelto = "resuelto" elif resuelto == "explicado": pass else: resuelto = "noresuelto" examen_json["resuelto"] = resuelto examen_json = json.dumps(examen_json) if formato == "zip": tarea = self.creador.lanzar_tarea(request, "json2latex", data=examen_json, formato="zip") elif formato == "tgz": tarea = self.creador.lanzar_tarea(request, "json2latex", data=examen_json, formato="tgz") elif formato == "pdf": tarea = self.creador.lanzar_tarea(request, "json2pdf", data=examen_json, formato="pdf") else: # Retornamos la versión JSON headers = { "Content-type": "application/json", "Content-disposition": 'attachment; filename="examen.json"' } return morepath.Response(body=examen_json, status=200, headers=headers) if tarea is None: raise HTTPNotFound("Tipo de descarga no disponible") if not sync: return {"status": "Procesando", "link": request.link(tarea)} # esperar a que acabe la tarea y retornar el resultado wait = 0.1 while wait < 30: estado = request.view(tarea) if estado["status"] == "Completada": break time.sleep(wait) wait *= 2 if estado["status"] != "Completada": raise HTTPInternalServerError( "No se pudo completar la tarea de compilación") resultado = request.view(tarea, name="download") tarea.delete() return resultado
def delete_examen(self, request): """Eliminar un examen""" self.delete_object() return morepath.Response(status=204)
def delete_problema(self, request): """Eliminar un problema""" problemas = coll.Problemas() if self.delete_object() is None: return None return morepath.Response(status=204)
def delete_profesor(self, request): "Eliminar un profesor" profesores = coll.Profesores() if profesores.delete_object(self.id) is None: return None return morepath.Response(status=204)
def render_plain(content, request): response = morepath.Response(content) response.content_type = 'text/plain' return response
def render_css(content, request): response = morepath.Response(content) response.content_type = 'text/css' return response