-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from rcthomas/separate-handlers
Separate handlers
- Loading branch information
Showing
3 changed files
with
88 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import json | ||
|
||
from html_sanitizer import Sanitizer | ||
from jinja2 import Environment | ||
from jupyterhub.services.auth import HubOAuthenticated | ||
from jupyterhub.utils import url_path_join | ||
from tornado import escape, web | ||
|
||
from jupyterhub_announcement.encoder import _JSONEncoder | ||
|
||
|
||
class AnnouncementHandler(HubOAuthenticated, web.RequestHandler): | ||
def initialize(self, queue): | ||
self.queue = queue | ||
|
||
|
||
class AnnouncementViewHandler(AnnouncementHandler): | ||
"""View announcements page""" | ||
|
||
def initialize(self, queue, fixed_message, loader): | ||
super().initialize(queue) | ||
self.fixed_message = fixed_message | ||
self.loader = loader | ||
self.env = Environment(loader=self.loader) | ||
self.template = self.env.get_template("index.html") | ||
|
||
@web.authenticated | ||
def get(self): | ||
user = self.get_current_user() | ||
prefix = self.hub_auth.hub_prefix | ||
logout_url = url_path_join(prefix, "logout") | ||
self.write( | ||
self.template.render( | ||
user=user, | ||
fixed_message=self.fixed_message, | ||
announcements=self.queue.announcements, | ||
static_url=self.static_url, | ||
login_url=self.hub_auth.login_url, | ||
logout_url=logout_url, | ||
base_url=prefix, | ||
no_spawner_check=True, | ||
) | ||
) | ||
|
||
|
||
class AnnouncementLatestHandler(AnnouncementHandler): | ||
"""Return the latest announcement as JSON""" | ||
|
||
def initialize(self, queue, allow_origin): | ||
super().initialize(queue) | ||
self.allow_origin = allow_origin | ||
|
||
def get(self): | ||
latest = {"announcement": ""} | ||
if self.queue.announcements: | ||
latest = self.queue.announcements[-1] | ||
self.set_header("Content-Type", "application/json; charset=UTF-8") | ||
if self.allow_origin: | ||
self.add_header("Access-Control-Allow-Headers", "Content-Type") | ||
self.add_header("Access-Control-Allow-Origin", "*") | ||
self.add_header("Access-Control-Allow-Methods", "OPTIONS,GET") | ||
self.write(escape.utf8(json.dumps(latest, cls=_JSONEncoder))) | ||
|
||
|
||
class AnnouncementUpdateHandler(AnnouncementHandler): | ||
"""Update announcements page""" | ||
|
||
hub_users = [] | ||
allow_admin = True | ||
|
||
@web.authenticated | ||
async def post(self): | ||
"""Update announcement""" | ||
user = self.get_current_user() | ||
sanitizer = Sanitizer() | ||
announcement = sanitizer.sanitize(self.get_body_argument("announcement")) | ||
await self.queue.update(user["name"], announcement) | ||
self.redirect(self.application.reverse_url("view")) |