You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 line
2.1KB

  1. # @name: schemas.py
  2. # @creation_date: 2023-01-16
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: Data schemas for API export
  6. # @acknowledgements:
  7. # https://marshmallow.readthedocs.io/en/stable/quickstart.html#next-steps
  8. # https://stackoverflow.com/questions/59721478/serializing-sqlalchemy-with-marshmallow
  9. from . import ma
  10. import os
  11. from marshmallow import Schema, fields, post_dump
  12. from flask_marshmallow import Marshmallow
  13. # schema for JSON transformation of User table via Marshmallow
  14. class UserSchema(ma.Schema):
  15. class Meta:
  16. fields = ('id', 'email', 'name')
  17. ordered = True
  18. # schema for JSON transformation of Resource table via Marshmallow
  19. class ToolSchema(ma.Schema):
  20. class Meta:
  21. fields = ('id', 'name', 'description', 'developer', 'developerUrl', 'projectUrl', 'repositoryUrl', 'license', 'scriptingLanguage', 'expertiseToUse', 'expertiseToHost', 'dependencies', 'ingestFormats', 'outputFormats', 'videoUrl', 'status')
  22. ordered = True
  23. class PracticeSchema(ma.Schema):
  24. class Meta:
  25. fields = ('id', 'name', 'content')
  26. ordered = True
  27. id = fields.Int(required=True)
  28. name = fields.Str(required=True)
  29. content = fields.Str(dump_only=True) # Will store the Markdown content
  30. @post_dump
  31. def load_markdown(self, data, **kwargs):
  32. practice_name = data['name'].replace(" ", "_")
  33. # Reads the markdown content based on the name field after serialization.
  34. file_path = f"content/practices/{practice_name}.md" # Adjust the path as needed
  35. if os.path.exists(file_path):
  36. with open(file_path, "r", encoding="utf-8") as file:
  37. data["content"] = file.read()
  38. else:
  39. data["content"] = "Markdown file not found."
  40. return data
  41. class BookSchema(ma.Schema):
  42. class Meta:
  43. fields = ('id', 'name', 'description', 'author', 'year', 'bookUrl', 'videoUrl', 'isbn')
  44. ordered = True
  45. # subschemas for nested fields
  46. class DeveloperSchema(ma.Schema):
  47. class Meta:
  48. fields = ('developer', 'developerUrl')
  49. ordered = True