From e9f9e784512fa5d7fa951bc48feaf10979ae1104 Mon Sep 17 00:00:00 2001 From: jakobsn Date: Mon, 11 Nov 2019 13:26:43 +0100 Subject: [PATCH] Make app.py to handle urls and session, then refactor --- src/app/main.py | 2 +- src/app/templates/index.html | 38 ++++++++++++++-------------- src/app/views/app.py | 48 ++++++++++++++++++++++++++++++++++++ src/app/views/index.py | 44 ++------------------------------- src/app/views/utils.py | 2 +- 5 files changed, 72 insertions(+), 62 deletions(-) create mode 100644 src/app/views/app.py diff --git a/src/app/main.py b/src/app/main.py index 815ac6c..6f3fc4c 100644 --- a/src/app/main.py +++ b/src/app/main.py @@ -1,4 +1,4 @@ -from views.index import app +from views.app import app if __name__ == "__main__": app.run() diff --git a/src/app/templates/index.html b/src/app/templates/index.html index 9d576fd..a230682 100644 --- a/src/app/templates/index.html +++ b/src/app/templates/index.html @@ -15,29 +15,31 @@ $def with (nav, project_bulk_one, project_bulk_two, projects) Honeybee -

What would you like to do today?

+ $if session.username: -
-
My Projects
-
Customer Projects
-
Finished Projects
-
+

What would you like to do today?

+ +
+
My Projects
+
Customer Projects
+
Finished Projects
+
- $if projects == 'my': -

Your open projects

+ $if projects == 'my': +

Your open projects

-
- $for project in project_bulk_one: -

$project[3]

$project[4]

-
+
+ $for project in project_bulk_one: +

$project[3]

$project[4]

+
- $if projects == 'my': -

Projects in progress

+ $if projects == 'my': +

Projects in progress

-
- $for project in project_bulk_two: -

$project[3]

$project[4]

-
+
+ $for project in project_bulk_two: +

$project[3]

$project[4]

+
\ No newline at end of file diff --git a/src/app/views/app.py b/src/app/views/app.py new file mode 100644 index 0000000..a64ca28 --- /dev/null +++ b/src/app/views/app.py @@ -0,0 +1,48 @@ +import web +from views.utils import get_nav_bar +from views.guestbook import Guestbook +from views.login import Login +from views.logout import Logout +from views.register import Register +from views.admin import Admin +from views.new_project import New_project +from views.open_projects import Open_projects +from views.project import Project +from views.index import Index + +# Define application routes +urls = ( + '/', 'Index', + '/login', 'Login', + '/logout', 'Logout', + '/register', 'Register', + '/guestbook', 'Guestbook', + '/new_project', 'New_project', + '/open_projects', 'Open_projects', + '/project', 'Project', + '/admin', 'Admin', +) + +# Initialize application using the web py framework +app = web.application(urls, globals()) + +# Get html templates +render = web.template.render('templates/') + +# Workaround to use sessions with reloader (debugger) http://webpy.org/cookbook/session_with_reloader +if web.config.get('_session') is None: + session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={"username": None}) + web.config._session = session +else: + session = web.config._session + +# Add session to global variables +render._add_global(session, 'session') + +# Make the session available cross modules through webctx +def session_hook(): + web.ctx.session = session + web.template.Template.globals['session'] = session + +app.add_processor(web.loadhook(session_hook)) + diff --git a/src/app/views/index.py b/src/app/views/index.py index 092c384..8745533 100644 --- a/src/app/views/index.py +++ b/src/app/views/index.py @@ -1,55 +1,15 @@ import web -from views.utils import get_nav_bar -from views.guestbook import Guestbook -from views.login import Login -from views.logout import Logout -from views.register import Register -from views.admin import Admin -from views.new_project import New_project -from views.open_projects import Open_projects -from views.project import Project import models.project - -# Define application routes -urls = ( - '/', 'Index', - '/login', 'Login', - '/logout', 'Logout', - '/register', 'Register', - '/guestbook', 'Guestbook', - '/new_project', 'New_project', - '/open_projects', 'Open_projects', - '/project', 'Project', - '/admin', 'Admin', -) - -# Initialize application using the web py framework -app = web.application(urls, globals()) +from views.utils import get_nav_bar # Get html templates render = web.template.render('templates/') -# Workaround to use sessions with reloader (debugger) http://webpy.org/cookbook/session_with_reloader -if web.config.get('_session') is None: - session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={"username": None}) - web.config._session = session -else: - session = web.config._session - -# Add session to global variables -render._add_global(session, 'session') - -# Make the session available cross modules through webctx -def session_hook(): - web.ctx.session = session - web.template.Template.globals['session'] = session - -app.add_processor(web.loadhook(session_hook)) - class Index: # Get main page def GET(self): + session = web.ctx.session nav = get_nav_bar(session) data = web.input(projects=None) project_bulk_one = [] diff --git a/src/app/views/utils.py b/src/app/views/utils.py index bae6492..ccb1d53 100644 --- a/src/app/views/utils.py +++ b/src/app/views/utils.py @@ -6,10 +6,10 @@ def get_nav_bar(session): result += '
  • Home
  • ' if session.username: result += '
  • Logout
  • ' + result += '
  • New
  • ' else: result += '
  • Register
  • ' result += '
  • Login
  • ' - result += '
  • New
  • ' result += '
  • Projects
  • ' result += '
  • Guestbook
  • ' result += ' '