import json from os.path import join from wtforms import DateField, Form, StringField, TextAreaField from yird.settings import Settings class NewPostForm(Form): title = StringField('Title') slug = StringField('Slug') date = DateField('Date') content = TextAreaField('Content') class Post: def __init__(self, id, title, slug, date, content='', changed=True): self.id = id self.title = title self.slug = slug self.date = date self.content = content self.changed = changed self.settings = Settings() def metadata(self): return { "id": self.id, "title": self.title, "slug": self.slug, "date": str(self.date), "changed": self.changed } def load_metadata(self): with open(join(self.settings.POSTS_PATH, self.id + '.json')) as f: return json.loads(f.read()) def write(self): with open(join(self.settings.POSTS_PATH, self.id + '.md'), 'w') as f: f.write(self.content) with open(join(self.settings.POSTS_PATH, self.id + '.json'), 'w') as f: f.write(json.dumps(self.metadata()))