Sfoglia il codice sorgente

updating API for practices

main
Simon Bowie 1 mese fa
parent
commit
247f462a5d
2 ha cambiato i file con 24 aggiunte e 5 eliminazioni
  1. +4
    -4
      web/app/models.py
  2. +20
    -1
      web/app/schemas.py

+ 4
- 4
web/app/models.py Vedi File

@@ -45,10 +45,10 @@ class Resource(db.Model):
outputFormats = db.Column(db.Text)
status = db.Column(db.Text)
# practices
longDescription = db.Column(db.Text)
experimental = db.Column(db.Text)
considerations = db.Column(db.Text)
references = db.Column(db.Text)
#longDescription = db.Column(db.Text)
#experimental = db.Column(db.Text)
#considerations = db.Column(db.Text)
#references = db.Column(db.Text)
# books
author = db.Column(db.Text)
year = db.Column(db.Text)

+ 20
- 1
web/app/schemas.py Vedi File

@@ -8,6 +8,9 @@
# https://stackoverflow.com/questions/59721478/serializing-sqlalchemy-with-marshmallow

from . import ma
import os
from marshmallow import Schema, fields, post_dump
from flask_marshmallow import Marshmallow

# schema for JSON transformation of User table via Marshmallow
class UserSchema(ma.Schema):
@@ -23,9 +26,25 @@ class ToolSchema(ma.Schema):

class PracticeSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'description', 'experimental', 'lessonsLearned', 'references')
fields = ('id', 'name', 'content')
ordered = True

id = fields.Int(required=True)
name = fields.Str(required=True)
content = fields.Str(dump_only=True) # Will store the Markdown content

@post_dump
def load_markdown(self, data, **kwargs):
practice_name = data['name'].replace(" ", "_")
# Reads the markdown content based on the name field after serialization.
file_path = f"content/practices/{practice_name}.md" # Adjust the path as needed
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as file:
data["content"] = file.read()
else:
data["content"] = "Markdown file not found."
return data

class BookSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'description', 'author', 'year', 'bookUrl', 'videoUrl', 'isbn')

Loading…
Annulla
Salva