def post(self): from page.models import Template from auth.models import User self._form = model_form(Template, Template.session, base_class=Form, exclude=['blocks', 'pages'])(request.form) if self._form.validate(): template = Template() self._form.populate_obj(template) template.body = request.form['body-body'] template.save() filename = template.filename if template.body is not None: body = template.body[:] else: body = '' from settings import BaseConfig import os templatedir = os.path.join(BaseConfig.ROOT_PATH, 'templates') os.system('touch {}'.format(os.path.join(templatedir, filename))) fp = open(os.path.join(templatedir, filename), 'w') fp.write(body + '\n') fp.close() flash('you have created a new template named: {}'.format( template.name)) return self.redirect('admin.templates') else: flash('You need to give the template a name') return self.render()
def post(self): from page.models import Template from auth.models import User self._form = model_form(Template,Template.session,base_class=Form,exclude=['blocks','pages'])(request.form) if self._form.validate(): template = Template() self._form.populate_obj(template) template.body = request.form['body-body'] template.save() filename = template.filename if template.body is not None: body = template.body[:] else: body = '' from settings import BaseConfig import os templatedir = os.path.join(BaseConfig.ROOT_PATH,'templates') os.system('touch {}'.format(os.path.join(templatedir,filename))) fp = open(os.path.join(templatedir,filename),'w') fp.write(body+'\n') fp.close() flash('you have created a new template named: {}'.format(template.name)) return self.redirect('admin.templates') else: flash('You need to give the template a name') return self.render()
def check_templates(): from ..page.models import Template from ..auth.models import User from flask import current_app templates = Template.query.all() template_dir = current_app.config['ROOT_PATH'] + '/' + 'templates' if not os.path.exists(template_dir): os.mkdir(template_dir) names = [t.name for t in templates] for t in os.listdir(template_dir): if not t in names: temp = Template() temp.name = t temp.filename = os.path.join(template_dir,t) temp.body = open(os.path.join(template_dir,t),'r').read() temp.save()