Beispiel #1
0
def add_stylesheet(template_id: int):
    template: Template = get_or_404(
        db.session.query(Template).filter_by(id=template_id))
    template_schema = TemplateSchema(include=("styles", ))
    data = TemplateSchema.all_request_data()
    if isinstance(data, list):
        for entry in data:
            style: Stylesheet = get_or_404(
                db.session.query(Stylesheet).filter_by(**entry))
            template.styles.append(style)
    else:
        style: Stylesheet = get_or_404(
            db.session.query(Stylesheet).filter_by(**data))
        template.styles.append(style)
    db.session.commit()
    return jsonify(template_schema.dump(template))
Beispiel #2
0
def remove_stylesheet(template_id: int):
    template: Template = get_or_404(
        db.session.query(Template).filter_by(id=template_id))
    data = TemplateSchema.all_request_data()
    if isinstance(data, list):
        for entry in data:
            if not hasattr(entry, "id"):
                raise BadRequest("Required property: id")
            delete_relation = template_stylesheet_relation.delete().where(
                and_(
                    template_stylesheet_relation.c.template_id == template.id,
                    template_stylesheet_relation.c.style_id == entry.get("id"),
                ))
            db.session.execute(delete_relation)
    elif isinstance(data, dict):
        delete_relation = template_stylesheet_relation.delete().where(
            and_(
                template_stylesheet_relation.c.template_id == template.id,
                template_stylesheet_relation.c.style_id == data.get("id"),
            ))
        db.session.execute(delete_relation)
    db.session.commit()
    template_schema = TemplateSchema(include=("styles", ),
                                     instance=template,
                                     session=db.session)
    return jsonify(template_schema.dump(template))
Beispiel #3
0
 def delete(cls, instance_id: int, commit=True):
     instance = get_or_404(
         db.session.query(cls.Meta.model).filter_by(id=instance_id))
     db.session.delete(instance)
     if commit:
         db.session.commit()
     return jsonify({"success": True})
Beispiel #4
0
def upload(stylesheet_id: int):
    style: Stylesheet = get_or_404(
        db.session.query(Stylesheet).filter_by(id=stylesheet_id))
    schema = StylesheetSchema(unknown=EXCLUDE,
                              session=db.session,
                              instance=style,
                              partial=True)

    data = dict(request.form.copy())
    if request.json is not None:
        data.update(request.json)

    if request.files.get("source", None) is not None:
        file: FileStorage = request.files.get("source")
        data["source"] = file.read()
        file.close()

    try:
        stylesheet = schema.load(data)
    except ValidationError as e:
        return jsonify(e.messages), BadRequest.code

    db.session.commit()

    result = schema.dump(stylesheet)
    return jsonify(result)
Beispiel #5
0
def index(template_id: int):
    template: Template = get_or_404(
        db.session.query(Template).filter_by(id=template_id))
    template_schema = TemplateSchema(session=db.session)
    stylesheet_schemas = StylesheetSchema(session=db.session, many=True)
    return render_template(
        "sites/edit_template.html",
        template_json=template_schema.dumps(template),
        stylesheets_json=stylesheet_schemas.dumps(template.styles),
    )
Beispiel #6
0
    def delete(cls, instance_id: int, commit=True):
        instance = get_or_404(
            db.session.query(cls.Meta.model).filter_by(id=instance_id))
        filename = instance.filename
        os.remove(os.path.join(current_app.config["UPLOAD_FOLDER"], filename))
        db.session.delete(instance)
        if commit:
            db.session.commit()

        return jsonify({"success": True})
Beispiel #7
0
def render(document_id: int):
    document: Document = get_or_404(
        db.session.query(Document).filter_by(id=document_id)
    )
    output = document.render()
    file = BytesIO(output)
    return send_file(
        file,
        mimetype="application/pdf",
        attachment_filename=f"{document.filename}.pdf",
        cache_timeout=0,
    )
Beispiel #8
0
def index(document_id: int):
    document: Document = get_or_404(
        db.session.query(Document).filter_by(id=document_id)
    )
    doc_schema = DocumentSchema(session=db.session, instance=document)
    var_schemas = VariableSchema(session=db.session, many=True)
    template_schema = TemplateSchema(session=db.session)
    stylesheet_schemas = StylesheetSchema(session=db.session, many=True)
    return render_template(
        "sites/edit.html",
        document_id=document_id,
        document_json=doc_schema.dumps(document),
        variable_json=var_schemas.dumps(document.variables, many=True),
        template_json=template_schema.dumps(document.template),
        stylesheets_json=stylesheet_schemas.dumps(document.template.styles),
    )
Beispiel #9
0
 def get(cls, instance_id: int):
     instance = get_or_404(
         db.session.query(cls.Meta.model).filter_by(id=instance_id))
     schema = cls(many=False)
     return jsonify(schema.dump(instance))
Beispiel #10
0
def view(resource_id: int):
    resource: Resource = get_or_404(
        db.session.query(Resource).filter_by(id=resource_id)
    )
    return send_from_directory(current_app.config["UPLOAD_FOLDER"], resource.filename)