app.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. Backend
  3. """
  4. import os
  5. import shutil
  6. from flask_login import login_required
  7. from flask_menu import register_menu
  8. from flask import render_template, request, redirect, url_for, flash, Blueprint
  9. from app.models import Page, File, User
  10. BLUEPRINT = Blueprint(
  11. 'backend',
  12. __name__,
  13. template_folder='templates'
  14. )
  15. BASE_PATH = 'app/modules/static/pages/'
  16. @BLUEPRINT.route('/')
  17. @register_menu(BLUEPRINT, 'index', 'Home')
  18. @login_required
  19. def index():
  20. """Show homepage"""
  21. pages = Page.query.filter(Page.parent_id == None).all()
  22. files = File.query.all()
  23. users = User.query.all()
  24. return render_template(
  25. 'site/index.j2',
  26. pages=pages,
  27. files=files,
  28. users=users
  29. )
  30. @BLUEPRINT.route('/render')
  31. @register_menu(BLUEPRINT, 'render', 'Render')
  32. @login_required
  33. def render():
  34. """Render pages to file"""
  35. pages = Page.query.filter(Page.parent_id == None).all()
  36. menu = []
  37. for page in pages:
  38. if page.title != 'index':
  39. menu.append(generate_menu(page))
  40. path_base = 'app/modules/static/pages/'
  41. path_public = path_base + "public"
  42. path_private = path_base + "private"
  43. if os.path.exists(path_public):
  44. shutil.rmtree(path_public)
  45. os.makedirs(path_public)
  46. if os.path.exists(path_private):
  47. shutil.rmtree(path_private)
  48. os.makedirs(path_private)
  49. for page in pages:
  50. generate_directory('', page)
  51. for page in pages:
  52. render_page('', page, menu, False)
  53. flash('Successfully rendered pages.', 'success')
  54. return redirect(request.referrer, code=302)
  55. def generate_menu(page):
  56. """Generate menu based on pages"""
  57. menu_item = {}
  58. menu_item['title'] = page.title
  59. menu_item['url'] = page.path()
  60. menu_item['private'] = page.private
  61. if page.children.count():
  62. menu_item['children'] = []
  63. for child_page in page.children:
  64. menu_item['children'].append(generate_menu(child_page))
  65. return menu_item
  66. def generate_directory(path, page):
  67. """Generate directories for pages"""
  68. if page.children.count():
  69. parent_path = path + page.url() + '/'
  70. public_path = BASE_PATH + 'public/' + path + page.url()
  71. private_path = BASE_PATH + 'private/' + path + page.url()
  72. if not os.path.exists(public_path):
  73. os.makedirs(public_path)
  74. if not os.path.exists(private_path):
  75. os.makedirs(private_path)
  76. for child_page in page.children:
  77. generate_directory(parent_path, child_page)
  78. def render_page(path, page, menu, private):
  79. """Function for page generation, recursive"""
  80. if page.private:
  81. private = True
  82. if page.children.count():
  83. parent_path = path + page.url() + '/'
  84. for child_page in page.children:
  85. render_page(parent_path, child_page, menu, private)
  86. path += page.url()
  87. private_path = '%s%s/%s.html' % (BASE_PATH, 'private', path)
  88. with open(private_path, 'w') as file:
  89. rendered_page = render_template(
  90. 'public/private.j2',
  91. page=page,
  92. menu=menu
  93. )
  94. file.write(rendered_page)
  95. if not private:
  96. public_path = '%s%s/%s.html' % (BASE_PATH, 'public', path)
  97. with open(public_path, 'w') as file:
  98. rendered_page = render_template(
  99. 'public/public.j2',
  100. page=page,
  101. menu=menu
  102. )
  103. file.write(rendered_page)