No Description
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.

create_site.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import os
  2. from lodeladmin import manager
  3. from flask.templating import render_template_string
  4. from jinja2.exceptions import TemplateNotFound
  5. ## @brief Create a new Lodel site
  6. # @param name str
  7. # @todo dynamically define the lodelsites folder
  8. @manager.command
  9. def create_site(name, long_name=None):
  10. os.mkdir("sites/%s" % name)
  11. os.mkdir("sites/%s/templates" % name)
  12. os.mkdir("sites/%s/templates/%s" % (name, name))
  13. with open("sites/%s/__init__.py" % name, 'w') as init_file:
  14. init_file.write(render_template_string("""from flask import Blueprint
  15. from lodel.lodelsites.lodelsite import LodelSite
  16. import os
  17. settings_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "settings.ini")
  18. lodelsite = LodelSite(settings_file)
  19. lodelsite_{{shortname}} = Blueprint(
  20. lodelsite.settings['Description']['shortname'],
  21. __name__,
  22. template_folder=lodelsite.settings['Description']['templates_folder'],
  23. url_prefix="/{{ shortname }}")
  24. lodelsite_{{shortname}}.record_once(lodelsite.config)
  25. from .views import *""", **{'shortname':name}))
  26. with open("sites/%s/views.py" % name, "w") as views_file:
  27. views_file.write(render_template_string("""from flask import render_template, abort, current_app
  28. from jinja2 import TemplateNotFound
  29. from . import lodelsite_{{shortname}}
  30. @lodelsite_{{shortname}}.route('/', defaults={'page':'index'})
  31. @lodelsite_{{shortname}}.route('/{{ "<page>" | safe }}')
  32. def show(page):
  33. try:
  34. return render_template("{{ shortname }}/%s.html" % page, **{'settings': current_app.config['sites']['{{shortname}}']})
  35. except TemplateNotFound:
  36. abort(404)""", **{'shortname':name}))
  37. with open("sites/%s/templates/%s/index.html" % (name, name), "w") as index_tpl:
  38. index_tpl.write("<strong>{{ settings['Description']['name'] }}</strong>")
  39. with open("sites/%s/settings.ini" % name, "w") as settings_file:
  40. settings_file.write(render_template_string("""[Description]
  41. name={{ name }}
  42. shortname={{ shortname }}
  43. templates_folder=templates""", **{'name': name, 'shortname': name}))
  44. print("site %s created successfully" % name)