app.py 3.4 KB

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