/ app.py
app.py
  1  import os
  2  import json
  3  
  4  from elements.core import Bottle, chevron_template, static_file
  5  from elements.core import request, redirect, abort, TEMPLATE_PATH
  6  from elements.core import Config
  7  
  8  from elements.accounts import Members
  9  
 10  from elements.notes import Note, Notes
 11  
 12  from elements.sessions.utilities import session
 13  
 14  from elements.api import endpoint
 15  from elements.api.accounts import register
 16  
 17  ##################################################
 18  ### Application Setup ############################
 19  ##################################################
 20  
 21  app = Bottle()
 22  c = Config.load()
 23  members = Members.all()
 24  
 25  # The tag is what defines this application in
 26  # the backend of the system - routes, redirects,
 27  # and scripted filesystem management.
 28  tag = 'info'
 29  path = f'/{tag}/'
 30  
 31  app.data = {
 32          'name': 'Solar',
 33          'host': c.host,
 34          'static': c.static_path + f'{tag}/',
 35          'path': path,
 36          'root': path,
 37          'public': False,
 38          'session': None
 39  }
 40  
 41  # This value refers to the folder that this file is stored in.
 42  # We use it to find the local 'views/' folder and append it
 43  # to the template path. It is important to keep the views in
 44  # a unique subfolder so they don't conflict with any other
 45  # files on the template path (there are many different
 46  # index.html files on a solar system!)
 47  here = os.path.dirname(os.path.realpath(__file__))
 48  TEMPLATE_PATH.append(f'{here}/views')
 49  
 50  # This builds the data structure which is passed to the
 51  # templating engine.
 52  def defaults(new_data={}):
 53      # Get any flashed messages.
 54      flashes = request.app.get_flashed_messages()
 55  
 56      # We need to update the data, but overwrite
 57      # the app data without changing it.
 58      data = { 
 59          **app.data, 
 60          **new_data,
 61          'flashes': flashes
 62      }
 63  
 64      return {
 65              'partials_path': os.path.join('planets', tag, 'views', tag, 'components', ''),
 66              'partials_ext': 'mo',
 67              'data': data
 68      }
 69  
 70  ##################################################
 71  ### API Section ##################################
 72  ##################################################
 73  
 74  # When deployed, this path can be replaced with NGINX
 75  # or another static file server.
 76  @app.route('/static/<filepath:path>')
 77  def static(filepath):
 78      return static_file(filepath, root=f'{os.getcwd()}/static')
 79  
 80  ##################################################
 81  ### Frontend Routes ##############################
 82  ##################################################
 83  
 84  @app.route('/')
 85  def index():
 86      data = {}
 87      return chevron_template(tag + '/index.html', **defaults(data));
 88  
 89  @app.route('/generic/')
 90  def index():
 91      data = {}
 92      return chevron_template(tag + '/generic.html', **defaults(data));
 93  
 94  @app.get('/login/')
 95  def login_view():
 96      if session() is not None:
 97          return redirect(path)
 98  
 99      data = { 'redirect_url': path }
100      return chevron_template(tag + '/login.html', **defaults(data));
101  
102  @app.get('/register/')
103  def register_view():
104      if session() is not None:
105          return redirect(path)
106  
107      data = {
108          'accounts': json.dumps(members.dict())
109      }
110      return chevron_template(tag + '/register.html', **defaults(data));
111  
112  ##################################################
113  ### Error Handling ###############################
114  ##################################################
115  
116  ## These are the routes that expect to render content for the client
117  #@app.error(404)
118  #def error404(error):
119  #    data = { "error": error }
120  #    return chevron_template(tag + '/404.html', **defaults(data));
121  
122  
123  ##################################################
124  ### Running the code #############################
125  ##################################################
126  # Run the server!
127  if __name__ == "__main__":
128      app.run(host='0.0.0.0', port=c.PORT, debug=True)