Skip to content

Commit

Permalink
Add proper page titles to all server-returned pages (#1557)
Browse files Browse the repository at this point in the history
This makes pages appear much cleaner in the URL bar (we
already had client-side code to set the title for the notebook
view, but it would only be activated after the page had
fully loaded and its javascript code was interpreted/run)
  • Loading branch information
wlach authored Feb 28, 2019
1 parent 8052e09 commit 154c168
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- redesign / refactor of console entries
- fix bug where user would be prompted to save a notebook they already own after logging in
- add link to github from user pages
- add document-level titles to all pages returned by the iodide server

# 0.1.0 (2019-02-21)

Expand Down
4 changes: 3 additions & 1 deletion server/notebooks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def notebook_view(request, pk):
else:
notebook_info['forked_from'] = False
return render(request, 'notebook.html', {
'title': revision.title,
'user_info': _get_user_info_json(request.user),
'notebook_info': notebook_info,
'jsmd': revision.content,
Expand All @@ -64,7 +65,7 @@ def notebook_revisions(request, pk):
owner = get_object_or_404(User, pk=nb.owner_id)
owner_info = {
'username': owner.username,
'full_name': '{} {}'.format(owner.first_name, owner.last_name),
'full_name': owner.get_full_name(),
'avatar': owner.avatar,
'title': nb.title,
'notebookId': nb.id,
Expand All @@ -89,6 +90,7 @@ def notebook_revisions(request, pk):
'date': revision.created.isoformat()}
for revision in NotebookRevision.objects.filter(notebook_id=pk)])
return render(request, '../templates/index.html', {
'title': f'Revisions - {nb.title}',
'page_data': {
'userInfo': get_user_info_dict(request.user),
'ownerInfo': owner_info,
Expand Down
1 change: 1 addition & 0 deletions server/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<html lang="en">
<head>
{% block head %}{% endblock %}
<title>{{ title }}</title>
</head>
<body>
{% block content %}{% endblock %}
Expand Down
8 changes: 8 additions & 0 deletions server/tests/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ def get_script_block(page_content, id, mimetype='application/json'):
return json.loads(m.group(1))
raise Exception('Script block with id `%s` and mimetype %s not found', id,
mimetype)


# get the specified title of the page
def get_title_block(page_content):
m = re.search(r'<title>(.*)</title>', str(page_content))
if m:
return m.group(1)
raise Exception("Expected to find title element but didn't!")
36 changes: 35 additions & 1 deletion server/tests/test_notebook_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from django.urls import reverse

from helpers import (get_script_block,
get_title_block)
from server.notebooks.models import (Notebook,
NotebookRevision)

Expand All @@ -9,18 +11,20 @@ def test_notebook_view(client, test_notebook):
initial_revision = NotebookRevision.objects.filter(notebook=test_notebook).last()
resp = client.get(reverse('notebook-view', args=[str(test_notebook.id)]))
assert resp.status_code == 200
assert get_title_block(resp.content) == initial_revision.title
expected_content = '<script id="jsmd" type="text/jsmd">{}</script>'.format(
initial_revision.content)
assert expected_content in str(resp.content)

# add a new revision, verify that a fresh load gets it
new_revision_content = 'My new fun content'
NotebookRevision.objects.create(
new_revision = NotebookRevision.objects.create(
content=new_revision_content,
notebook=test_notebook,
title='Second revision')
resp = client.get(reverse('notebook-view', args=[str(test_notebook.id)]))
assert resp.status_code == 200
assert get_title_block(resp.content) == new_revision.title
new_expected_content = '<script id="jsmd" type="text/jsmd">{}</script>'.format(
new_revision_content)
assert new_expected_content in str(resp.content)
Expand Down Expand Up @@ -62,3 +66,33 @@ def test_tryit_view(client, fake_user, logged_in):
assert NotebookRevision.objects.count() == 0
assert Notebook.objects.count() == 0
assert len(response.redirect_chain) == 0


def test_notebook_revisions_page(fake_user, test_notebook, client):
# create another notebook revision
NotebookRevision.objects.create(
notebook=test_notebook,
title="second revision",
content="*fake notebook content 2*")
resp = client.get(reverse('notebook-revisions', args=[str(test_notebook.id)]))
assert get_title_block(resp.content) == f'Revisions - {test_notebook.title}'
assert get_script_block(resp.content, 'pageData') == {
'files': [],
'ownerInfo': {
'avatar': None,
'full_name': fake_user.get_full_name(),
'notebookId': test_notebook.id,
'title': test_notebook.title,
'username': fake_user.username
},
'revisions': [
{
'date': r.created.isoformat(),
'id': r.id,
'notebookId': test_notebook.id,
'title': r.title
} for r in NotebookRevision.objects.filter(
notebook_id=test_notebook.id)
],
'userInfo': {}
}
5 changes: 4 additions & 1 deletion server/tests/test_server_pages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from django.urls import reverse

from helpers import get_script_block
from helpers import (get_script_block,
get_title_block)
from server.base.models import User
from server.notebooks.models import (Notebook,
NotebookRevision)
Expand All @@ -22,6 +23,7 @@ def test_index_view(client, two_test_notebooks, fake_user, logged_in):
assert fake_user.avatar is None

# assert that the pageData element has the expected structure
assert get_title_block(resp.content) == 'Iodide'
assert get_script_block(resp.content, 'pageData') == {
'notebookList': [
{
Expand Down Expand Up @@ -62,6 +64,7 @@ def test_user_view_with_different_names(transactional_db, client, username):
content="*fake notebook content*")
resp = client.get(reverse('user', kwargs={'name': test_user.username}))
assert resp.status_code == 200
assert get_title_block(resp.content) == f'{test_user.username} ({test_user.get_full_name()})'
assert get_script_block(resp.content, 'pageData') == {
'notebookList': [
{
Expand Down
2 changes: 2 additions & 0 deletions server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def index(request):
for (nb_id, title, latest_revision) in get_formatted_notebooks(request.user)]
return render(
request, 'index.html', {
'title': 'Iodide',
'page_data': {
'userInfo': user_info,
# this is horrible and will not scale
Expand Down Expand Up @@ -76,6 +77,7 @@ def user(request, name=None):

notebooks = get_formatted_notebooks(user)
return render(request, 'index.html', {
'title': f"{this_user['name']} ({this_user['full_name']})",
'page_data': {
'userInfo': user_info,
'thisUser': this_user,
Expand Down

0 comments on commit 154c168

Please sign in to comment.