Skip to content

Commit

Permalink
Merge pull request #17 from rcthomas/separate-handlers
Browse files Browse the repository at this point in the history
Separate handlers
  • Loading branch information
rcthomas authored May 19, 2022
2 parents 6db8d6e + f6a4ae5 commit 8757638
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ repos:
rev: 5.10.1
hooks:
- id: isort
args:
- "--profile=black"

# Autoformat: Python code
- repo: https://github.com/psf/black
Expand Down
84 changes: 8 additions & 76 deletions jupyterhub_announcement/announcement.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,25 @@
import binascii
import json
import os
import sys

from html_sanitizer import Sanitizer
from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PrefixLoader
from jinja2 import ChoiceLoader, FileSystemLoader, PrefixLoader
from jupyterhub._data import DATA_FILES_PATH
from jupyterhub.handlers.static import LogoHandler
from jupyterhub.services.auth import HubOAuthCallbackHandler, HubOAuthenticated
from jupyterhub.services.auth import HubOAuthCallbackHandler
from jupyterhub.utils import url_path_join
from tornado import escape, gen, ioloop, web
from tornado import gen, ioloop, web
from traitlets import Any, Bool, Dict, Integer, List, Unicode, default
from traitlets.config import Application

from jupyterhub_announcement.encoder import _JSONEncoder
from jupyterhub_announcement.handlers import (
AnnouncementLatestHandler,
AnnouncementUpdateHandler,
AnnouncementViewHandler,
)
from jupyterhub_announcement.queue import AnnouncementQueue
from jupyterhub_announcement.ssl import SSLContext


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 = self.get_body_argument("announcement")
announcement = sanitizer.sanitize(self.get_body_argument("announcement"))
await self.queue.update(user["name"], announcement)
self.redirect(self.application.reverse_url("view"))


class AnnouncementService(Application):

classes = [AnnouncementQueue, SSLContext]
Expand Down
78 changes: 78 additions & 0 deletions jupyterhub_announcement/handlers.py
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"))

0 comments on commit 8757638

Please sign in to comment.