|
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/59721478/serializing-sqlalchemy-with-marshmallow |
|
|
# https://stackoverflow.com/questions/59721478/serializing-sqlalchemy-with-marshmallow |
|
|
|
|
|
|
|
|
from . import ma |
|
|
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 |
|
|
# schema for JSON transformation of User table via Marshmallow |
|
|
class UserSchema(ma.Schema): |
|
|
class UserSchema(ma.Schema): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PracticeSchema(ma.Schema): |
|
|
class PracticeSchema(ma.Schema): |
|
|
class Meta: |
|
|
class Meta: |
|
|
fields = ('id', 'name', 'description', 'experimental', 'lessonsLearned', 'references') |
|
|
|
|
|
|
|
|
fields = ('id', 'name', 'content') |
|
|
ordered = True |
|
|
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 BookSchema(ma.Schema): |
|
|
class Meta: |
|
|
class Meta: |
|
|
fields = ('id', 'name', 'description', 'author', 'year', 'bookUrl', 'videoUrl', 'isbn') |
|
|
fields = ('id', 'name', 'description', 'author', 'year', 'bookUrl', 'videoUrl', 'isbn') |