Skip to content

Update Admin Panel to work via iframes #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 61 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
d238b5b
wrap admin panel in iframe
WillNilges Jan 4, 2025
3a867d2
slightly better
WillNilges Jan 4, 2025
8577796
Extract object info and query it with the meshdb API
WillNilges Jan 4, 2025
dc55709
Move script to its own file
WillNilges Jan 4, 2025
7db0687
Now if only I could get my admin map to behave
WillNilges Jan 4, 2025
3fb5d51
Aha, I have to listen for messages
WillNilges Jan 4, 2025
4eb5e15
Victory
WillNilges Jan 4, 2025
d545a8b
Allow map to change location of admin panel
WillNilges Jan 4, 2025
1099bf4
Refactor style sheets
WillNilges Jan 4, 2025
c0c8983
Restore map.js
WillNilges Jan 4, 2025
f20e2a6
make it not crash
WillNilges Jan 5, 2025
98baeba
Update environment variables
WillNilges Jan 5, 2025
d016323
Checkpoint: Subway refactor. This works!?
WillNilges Jan 10, 2025
35a1c3e
another checkpoint
WillNilges Jan 10, 2025
e64d4c7
another checkpoint: going to refactor iframe_panel to admin_panel_iframe
WillNilges Jan 10, 2025
b007f38
Capture back button requests
WillNilges Jan 10, 2025
3be7d48
Delete dead code
WillNilges Jan 10, 2025
357fa37
getting the map to be resizable through css
WillNilges Jan 11, 2025
35d1a44
computers are fucking dumb;
WillNilges Jan 11, 2025
b6cd4b4
some cleanup
WillNilges Jan 11, 2025
9fd0a2c
debugging weird flow control stuff
WillNilges Jan 11, 2025
93bf978
OK I think we have feature pairity
WillNilges Jan 11, 2025
eedd693
Pick up where we left off (protect against refreshes)
WillNilges Jan 11, 2025
8b21202
Pass URL through to the Admin Panel iFrame
WillNilges Jan 12, 2025
3e21134
Ope my code sucks
WillNilges Jan 12, 2025
3c6cc25
I have no idea what is going on
WillNilges Jan 12, 2025
e79ad04
Fix admin panel using relative URLs
WillNilges Jan 12, 2025
2fde80d
refactoring checkpoint
WillNilges Jan 12, 2025
d2f516e
Refactoring checkpoint 2
WillNilges Jan 12, 2025
52db0e0
Strip 'panel' out of get_admin_url
WillNilges Jan 12, 2025
27b0919
Skip iframe in search tests
WillNilges Jan 12, 2025
75c25a3
same for list
WillNilges Jan 12, 2025
5409788
same for change
WillNilges Jan 12, 2025
4358d7c
Lint
WillNilges Jan 12, 2025
149637e
Delete a bunch of stuff
WillNilges Jan 12, 2025
3564bf2
checkpoint: delete more things
WillNilges Jan 12, 2025
6d37aa9
fix layout issues
WillNilges Jan 12, 2025
69ace4c
Swap /admin and /admin/panel
WillNilges Jan 13, 2025
63dded2
Revert "same for change"
WillNilges Jan 13, 2025
f52d1cb
Revert "same for list"
WillNilges Jan 13, 2025
8d37570
Revert "Skip iframe in search tests"
WillNilges Jan 13, 2025
dee6453
Add simple test
WillNilges Jan 14, 2025
eb9e896
Add a slightly more complex test
WillNilges Jan 14, 2025
2a65892
Acutally, just add one test. See comment as to why
WillNilges Jan 14, 2025
da6abd4
whoops
WillNilges Jan 14, 2025
33c38bb
Add a way to get to /admin/panel and update map button logic
WillNilges Jan 14, 2025
c46f4dd
Swap /admin out from under users
WillNilges Jan 15, 2025
307222d
Fix URL if user goes to /admin/panel, I should probs make the URL a v…
WillNilges Jan 15, 2025
820893a
Refactor
WillNilges Jan 16, 2025
7ec13d0
andrew comments 1: Restore password reset, remove state override, fix…
WillNilges Jan 17, 2025
f42e5d7
andrew comments 2: Path
WillNilges Jan 17, 2025
2058a85
lint
WillNilges Jan 18, 2025
09cba15
Redirect top to login on logout
WillNilges Jan 18, 2025
57e5908
Move iframe escape logic to onAdminPanelLoad
WillNilges Jan 21, 2025
f3b67a6
Make clicking an los work
WillNilges Jan 21, 2025
764eec1
Fixes, disable map if on mobile
WillNilges Jan 21, 2025
78a2221
Remove override that was breaking mobile
WillNilges Jan 24, 2025
c4825dc
Update src/meshweb/static/admin/iframe_check.js
WillNilges Jan 27, 2025
c0dcf94
Update src/meshweb/static/admin/map.js
WillNilges Jan 27, 2025
6051f88
Fix variable and delete log lines
WillNilges Jan 27, 2025
d5eb255
Merge branch 'main' into wdn/map-iframe-2
WillNilges Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/meshapi/admin/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from django.contrib import admin
from django.urls import path
from django.urls import path, re_path

from meshapi.admin.password_reset import (
AdminPasswordResetCompleteView,
AdminPasswordResetConfirmView,
AdminPasswordResetDoneView,
AdminPasswordResetView,
)
from meshdb.views import admin_iframe_view

urlpatterns = [
path("password_reset/", AdminPasswordResetView.as_view(), name="admin_password_reset"),
path("password_reset/done/", AdminPasswordResetDoneView.as_view(), name="password_reset_done"),
path("password_reset/<uidb64>/<token>/", AdminPasswordResetConfirmView.as_view(), name="password_reset_confirm"),
path("password_reset/done/", AdminPasswordResetCompleteView.as_view(), name="password_reset_complete"),
re_path(r"^panel/.*$", admin_iframe_view), # Match any other /admin/* URL
path("", admin.site.urls),
]
107 changes: 107 additions & 0 deletions src/meshapi/tests/test_admin_panel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import datetime

from bs4 import BeautifulSoup
from django.contrib.auth.models import Group, User
from django.test import Client, TestCase
from rest_framework.authtoken.models import TokenProxy

from meshapi.models import LOS, AccessPoint, Building, Device, Install, Link, Member, Node, Sector
from meshapi.tests.sample_data import sample_building, sample_device, sample_install, sample_member, sample_node
from meshapi_hooks.hooks import CelerySerializerHook


class TestAdminPanel(TestCase):
c = Client()

def setUp(self) -> None:
sample_install_copy = sample_install.copy()
self.building_1 = Building(**sample_building)
self.building_1.save()
sample_install_copy["building"] = self.building_1

self.building_2 = Building(**sample_building)
self.building_2.save()

self.los = LOS(
from_building=self.building_1,
to_building=self.building_2,
analysis_date=datetime.date(2024, 1, 1),
source=LOS.LOSSource.HUMAN_ANNOTATED,
)
self.los.save()

self.member = Member(**sample_member)
self.member.save()
sample_install_copy["member"] = self.member

self.install = Install(**sample_install_copy)
self.install.save()

self.node1 = Node(**sample_node)
self.node1.save()
self.node2 = Node(**sample_node)
self.node2.save()

self.device1 = Device(**sample_device)
self.device1.node = self.node1
self.device1.save()

self.device2 = Device(**sample_device)
self.device2.node = self.node2
self.device2.save()

self.sector = Sector(
radius=1,
azimuth=45,
width=180,
**sample_device,
)
self.sector.node = self.node2
self.sector.save()

self.access_point = AccessPoint(
**sample_device,
latitude=0,
longitude=0,
)
self.access_point.node = self.node2
self.access_point.save()

self.link = Link(
from_device=self.device1,
to_device=self.device2,
status=Link.LinkStatus.ACTIVE,
)
self.link.save()

self.admin_user = User.objects.create_superuser(
username="admin", password="admin_password", email="[email protected]"
)
self.c.login(username="admin", password="admin_password")

self.test_group = Group.objects.create(name="Test group")

self.test_auth_token = TokenProxy.objects.create(user=self.admin_user)

self.test_webhook = CelerySerializerHook.objects.create(
user=self.admin_user, target="http://example.com", event="building.created", headers=""
)

def test_iframe_loads(self):
route = "/admin/panel/"
code = 200
response = self.c.get(route)
self.assertEqual(code, response.status_code, f"Could not view {route} in the admin panel.")

decoded_panel = response.content.decode()
soup = BeautifulSoup(decoded_panel, "html.parser")
iframe = soup.find(id="admin_panel_iframe")
iframe_src = iframe.attrs["src"]
self.assertEqual("/admin/", iframe_src)
iframe_response = self.c.get(iframe_src)
self.assertEqual(code, iframe_response.status_code, f"Could not view {route} in the admin panel.")

# TODO (wdn): Add more tests checking if navigating to xyz page works
# Unfortunately, because that is a lot of javascript, it's tricky to test.
# It may be possible to run selenium integration tests or something to validate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd support selenium-based tests for integration testing deployed copies of the application (dev, gamma, etc) but probably we don't want it in our unit testing pipeline, those things are slow enough already

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not in our unit testing pipeline, no. Are you OK with me handling this in a separate PR or would you like me to take a crack at a solution for this here?

# that functionality
3 changes: 3 additions & 0 deletions src/meshdb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

USE_X_FORWARDED_HOST = True

X_FRAME_OPTIONS = "SAMEORIGIN"

SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_PRELOAD = False
SECURE_HSTS_INCLUDE_SUBDOMAINS = False
Expand Down Expand Up @@ -167,6 +169,7 @@

CORS_ALLOWED_ORIGINS += [
"http://127.0.0.1:3000",
"http://127.0.0.1:3001",
"http://localhost:3000",
"http://127.0.0.1:80",
"http://localhost:80",
Expand Down
61 changes: 29 additions & 32 deletions src/meshdb/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" dir="{{ LANGUAGE_BIDI|yesno:'rtl,ltr,auto' }}">
<head>
<script>const PANEL_URL = "/admin/panel/";</script>
<script src="{% static 'admin/iframe_check.js' %}"></script>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
{% block dark-mode-vars %}
Expand Down Expand Up @@ -82,40 +84,35 @@
{% endblock %}
{% endif %}

<div id="map-wrapper">
<div class="main flex" id="main">
{% if not is_popup and is_nav_sidebar_enabled %}
{% block nav-sidebar %}
{% include "admin/nav_sidebar.html" %}
{% endblock %}
<div class="main flex" id="main">
{% if not is_popup and is_nav_sidebar_enabled %}
{% block nav-sidebar %}
{% include "admin/nav_sidebar.html" %}
{% endblock %}
{% endif %}
<div id="content-start" class="content" tabindex="-1">
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
<div id="content-start" class="content" tabindex="-1">
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content_subtitle %}{% if subtitle %}<h2>{{ subtitle }}</h2>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear">
</div>
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
</div>
{% endblock messages %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content_subtitle %}{% if subtitle %}<h2>{{ subtitle }}</h2>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear">
</div>
{% block map_sidebar %}
{% include "admin/map_sidebar.html" %}
{% endblock %}
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
</div>
</div>

</div>
Expand Down
3 changes: 0 additions & 3 deletions src/meshdb/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ <h1 id="site-name"><a href="{% url 'admin:index' %}"><img src="{% static 'meshwe

{% block userlinks %}
{{ block.super }}
<a href="#" class="hidden" style="margin-left: 5px; vertical-align: middle; border: none;" id="show_map_button">
<img src="{% static '/admin/map/img/map.png' %}" height="16px" title="Show Map">
</a>
{% endblock %}

{% block footer %}
Expand Down
81 changes: 81 additions & 0 deletions src/meshdb/templates/admin/iframed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{% load static %}

{% load env_extras %}

<!DOCTYPE html>
<html>
<head>
<script>
const MAP_BASE_URL = "{% get_env_var 'ADMIN_MAP_BASE_URL' %}";
const PANEL_URL = "/admin/panel/";
</script>
<!--We don't want people navigating to this URL, so we're gonna redirect them
if they find themselves here-->
<script src="{% static 'admin/panel_url_check.js' %}"></script>
<!--Script that powers this iframed view and communicates between the admin panel
and the map-->
<script src="{% static '/admin/map.js' %}" defer></script>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
{% block dark-mode-vars %}
<link rel="stylesheet" href="{% static "admin/css/dark_mode.css" %}">
<script src="{% static "admin/js/theme.js" %}" defer></script>
{% endblock %}
{% if not is_popup and is_nav_sidebar_enabled %}
<link rel="stylesheet" href="{% static "admin/css/nav_sidebar.css" %}">
<script src="{% static 'admin/js/nav_sidebar.js' %}" defer></script>
{% endif %}
{% block extrastyle %}{% endblock %}
<link rel="stylesheet" href="{% static "admin/admin_ext.css" %}">
{% if LANGUAGE_BIDI %}<link rel="stylesheet" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}">{% endif %}
{% block extrahead %}{% endblock %}
{% block responsive %}
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="{% static "admin/css/responsive.css" %}">
{% if LANGUAGE_BIDI %}<link rel="stylesheet" href="{% static "admin/css/responsive_rtl.css" %}">{% endif %}
{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE">{% endblock %}
<link rel="stylesheet" href="{% static '/admin/iframed.css'%}"/>
</head>
<body>
<div id="page_container">
<div id="admin_panel_div" class="frameGrow">
<iframe src="/admin/" id="admin_panel_iframe" class="frameGrow"></iframe>
</div>

<div class="floating-button-above">
<a href="#" class="button" style="display: inline-block" id="show_map_button">
<img src="{% static '/admin/map/img/map.png' %}" height="16px" title="Show Map">
</a>
</div>

<div id="map_controls">
<!-- This handle is always visible, unless you're resizing, in which case
goes big and invisible to block the iframes from stealing focus -->
<div class="handle" id="handle">
<span class="vert-align-helper"></span>
<img class="handlebar" id="handlebar" src="{% static '/admin/map/img/handlebar.svg' %}" height="60px"/>
</div>
<!-- Only shows up during resizes -->
<div class="handle hidden" id="substituteHandle">
<span class="vert-align-helper"></span>
<img class="handlebar" id="substituteHandlebar" src="{% static '/admin/map/img/handlebar.svg' %}" height="60px"/>
</div>
<div class="floating-button">
<a href="#" class="button" style="display: inline-block" id="map_hide_button">
<img src="{% static '/admin/map/img/cross.png' %}" height="24px" title="Hide Map">
</a>
</div>
<div class="floating-button-below">
<a href="#" class="button" style="display: inline-block" id="map_recenter_button">
<img src="{% static '/admin/map/img/recenter.png' %}" height="24px" title="Recenter map">
</a>
</div>
</div>

<div id="map_panel_div">
<iframe src="{% get_env_var 'ADMIN_MAP_BASE_URL' %}" id="map_panel"></iframe>
</div>
</div>
</body>
</html>
2 changes: 0 additions & 2 deletions src/meshdb/templates/admin/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ <h1 id="site-name"><a href="{% url 'admin:index' %}"><img src="{% static 'meshwe
{% endif %}
{% endblock %}


{% block map_sidebar %}{% endblock %}
27 changes: 0 additions & 27 deletions src/meshdb/templates/admin/map_sidebar.html

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 8 additions & 0 deletions src/meshdb/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render


@staff_member_required
def admin_iframe_view(request: HttpRequest) -> HttpResponse:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to add a test for this authentication/redirect

return render(request, "admin/iframed.html")
Loading
Loading