A flat-file CMS written in Python and Flask
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.

post.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import json
  2. from os.path import join
  3. from wtforms import DateField, Form, StringField, TextAreaField
  4. from yird.settings import Settings
  5. class NewPostForm(Form):
  6. title = StringField('Title')
  7. slug = StringField('Slug')
  8. date = DateField('Date')
  9. content = TextAreaField('Content')
  10. class Post:
  11. def __init__(self, id, title, slug, date, content='', changed=True):
  12. self.id = id
  13. self.title = title
  14. self.slug = slug
  15. self.date = date
  16. self.content = content
  17. self.changed = changed
  18. self.settings = Settings()
  19. def metadata(self):
  20. return {
  21. "id": self.id,
  22. "title": self.title,
  23. "slug": self.slug,
  24. "date": str(self.date),
  25. "changed": self.changed
  26. }
  27. def load_metadata(self):
  28. with open(join(self.settings.POSTS_PATH, self.id + '.json')) as f:
  29. return json.loads(f.read())
  30. def write(self):
  31. with open(join(self.settings.POSTS_PATH, self.id + '.md'), 'w') as f:
  32. f.write(self.content)
  33. with open(join(self.settings.POSTS_PATH, self.id + '.json'), 'w') as f:
  34. f.write(json.dumps(self.metadata()))