-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cc124c
commit 9f02116
Showing
11 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/edrnsite.controls/src/edrnsite/controls/migrations/0007_informatics_ip_address.py
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,22 @@ | ||
# Generated by Django 4.2.10 on 2024-07-18 15:45 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("edrnsitecontrols", "0006_rename_socialmedia_socialmedialink"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="informatics", | ||
name="ip_address", | ||
field=models.CharField( | ||
default="unknown", | ||
help_text="Last known source IP address of the portal", | ||
max_length=40, | ||
), | ||
), | ||
] |
21 changes: 21 additions & 0 deletions
21
...edrnsite.controls/src/edrnsite/controls/migrations/0008_informatics_ip_address_service.py
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,21 @@ | ||
# Generated by Django 4.2.10 on 2024-07-18 16:12 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("edrnsitecontrols", "0007_informatics_ip_address"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="informatics", | ||
name="ip_address_service", | ||
field=models.URLField( | ||
default="https://api.ipify.org", | ||
help_text="API endpoint of IP address service", | ||
), | ||
), | ||
] |
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,30 @@ | ||
# encoding: utf-8 | ||
|
||
'''🎛 EDRN Site Controls: asynchronous tasks.''' | ||
|
||
from .models import Informatics | ||
from celery import shared_task | ||
from django.core.cache import cache | ||
from urllib.request import urlopen | ||
from wagtail.models import Site | ||
import logging | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task | ||
def do_update_my_ip(): | ||
_logger.info('🔓 Getting lock for `update_my_up`') | ||
with cache.lock('update_my_ip', timeout=300): | ||
settings = Informatics.for_site(Site.objects.filter(is_default_site=True).first()) | ||
_logger.info('🤓 Looking up my IP with %s', settings.ip_address_service) | ||
try: | ||
with urlopen(settings.ip_address_service) as io: | ||
my_ip = io.read().decode('utf-8') | ||
_logger.info('🎉 Got %s', my_ip) | ||
except Exception as ex: | ||
my_ip = str(ex) | ||
_logger.exception('😔 Could not get my_ip') | ||
settings.ip_address = my_ip | ||
settings.save() |
18 changes: 18 additions & 0 deletions
18
...drnsite.controls/src/edrnsite/controls/templates/edrnsite.controls/edrnsite-controls.html
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,18 @@ | ||
{% load wagtailadmin_tags %} | ||
<section class='panel summary nice-padding'> | ||
<div style='display: grid; grid-template-columns: repeat(2, 1fr); grid-gap: 1rem; | ||
grid-auto-rows: minmax(100px, auto);'> | ||
<div> | ||
<h2 class='title-wrapper'>EDRN Site Controls</h2> | ||
<dl> | ||
<dt>My IP</dt> | ||
<dd>{{my_ip}}</dd> | ||
</dl> | ||
|
||
</div> | ||
<div> | ||
<a href='{% url "update_my_ip" %}' class='button button-longrunning' role='button'>Update my IP</a> | ||
</div> | ||
</div> | ||
</section> | ||
{# -*- Django HTML -*- #} |
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,12 @@ | ||
# encoding: utf-8 | ||
|
||
'''🎛 EDRN Site Controls: URL patterns.''' | ||
|
||
|
||
from .views import update_my_ip | ||
from django.urls import path | ||
|
||
|
||
urlpatterns = [ | ||
path('update_my_ip', update_my_ip, name='update_my_ip'), | ||
] |
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,24 @@ | ||
# encoding: utf-8 | ||
|
||
'''🎛 EDRN Site Controls: views.''' | ||
|
||
from .tasks import do_update_my_ip | ||
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, HttpResponseForbidden | ||
from edrn.auth.views import logged_in_or_basicauth | ||
|
||
|
||
def _get_referrer(request: HttpRequest) -> str: | ||
try: | ||
return request.META['HTTP_REFERER'] | ||
except KeyError: | ||
return '/' | ||
|
||
|
||
@logged_in_or_basicauth('edrn') | ||
def update_my_ip(request: HttpRequest) -> HttpResponse: | ||
'''Update my IP address.''' | ||
if request.user.is_staff or request.user.is_superuser: | ||
do_update_my_ip.delay() | ||
return HttpResponseRedirect(_get_referrer(request)) | ||
else: | ||
return HttpResponseForbidden() |
29 changes: 29 additions & 0 deletions
29
src/edrnsite.controls/src/edrnsite/controls/wagtail_hooks.py
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,29 @@ | ||
# encoding: utf-8 | ||
|
||
'''🎛 EDRN Site Controls: Wagtail hooks and interceptors.''' | ||
|
||
from .models import Informatics | ||
from django.http import HttpRequest | ||
from django.template.loader import render_to_string | ||
from wagtail import hooks | ||
from wagtail.admin.ui.components import Component | ||
|
||
|
||
class EDRNSiteControlsControlPanel(Component): | ||
'''Custom control panel for EDRN site controls.''' | ||
|
||
name = 'edrnsite_controls' | ||
order = 210 | ||
|
||
def __init__(self, request: HttpRequest): | ||
self.request = request | ||
|
||
def render_html(self, parent_context: list) -> str: | ||
context = {'my_ip': Informatics.for_request(self.request).ip_address} | ||
return render_to_string('edrnsite.controls/edrnsite-controls.html', context, request=self.request) | ||
|
||
|
||
@hooks.register('construct_homepage_panels') | ||
def add_ingest_controls(request: HttpRequest, panels: list): | ||
'''Add the custom EDRN site controls control panel.''' | ||
panels.append(EDRNSiteControlsControlPanel(request)) |
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