From b174377d15aa84c63853401369cb759cac7b2ddb Mon Sep 17 00:00:00 2001 From: Joe Wang Date: Sun, 24 May 2026 11:59:33 -0700 Subject: [PATCH 1/3] feat: first try --- ocfweb/main/home.py | 19 +++++++++++++++++++ ocfweb/main/templates/main/home.html | 1 + ocfweb/urls.py | 2 ++ tests/main/random_hosted_site_test.py | 23 +++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 tests/main/random_hosted_site_test.py diff --git a/ocfweb/main/home.py b/ocfweb/main/home.py index 070382aee..8de6c9e51 100644 --- a/ocfweb/main/home.py +++ b/ocfweb/main/home.py @@ -1,11 +1,16 @@ +import random from datetime import date from datetime import timedelta from operator import attrgetter +from typing import Mapping from django.http import HttpRequest from django.http import HttpResponse +from django.http import HttpResponseRedirect +from django.shortcuts import redirect from django.shortcuts import render from ocflib.lab.staff_hours import get_staff_hours_soonest_first +from ocflib.vhost.web import get_vhosts from ocfweb.api.hours import get_hours_listing from ocfweb.caching import periodic @@ -19,6 +24,20 @@ def get_staff_hours() -> str: return get_staff_hours_soonest_first()[:2] +def hosted_site_urls(vhosts: Mapping[str, object] | None = None) -> list[str]: + if vhosts is None: + vhosts = get_vhosts() + + return sorted( + f'https://{hostname}/' + for hostname in vhosts.keys() + ) + + +def random_hosted_site(request: HttpRequest) -> HttpResponseRedirect: + return redirect(random.choice(hosted_site_urls())) + + def home(request: HttpRequest) -> HttpResponse: hours_listing = get_hours_listing() hours = [ diff --git a/ocfweb/main/templates/main/home.html b/ocfweb/main/templates/main/home.html index 1a914cb3f..4e20e6f51 100644 --- a/ocfweb/main/templates/main/home.html +++ b/ocfweb/main/templates/main/home.html @@ -17,6 +17,7 @@

Welcome to the Open Computing Facility!

Learn what we do Create an account + Random hosted site

diff --git a/ocfweb/urls.py b/ocfweb/urls.py index 5869b1a29..f2711a9c7 100644 --- a/ocfweb/urls.py +++ b/ocfweb/urls.py @@ -16,6 +16,7 @@ from ocfweb.login.urls import urlpatterns as login from ocfweb.main.favicon import favicon from ocfweb.main.home import home +from ocfweb.main.home import random_hosted_site from ocfweb.main.hosting_logos import hosting_logo from ocfweb.main.lab import lab from ocfweb.main.robots import robots_dot_txt @@ -36,6 +37,7 @@ re_path('', include('django_prometheus.urls')), re_path(r'^$', home, name='home'), + re_path(r'^random-hosted-site$', random_hosted_site), re_path(r'^robots\.txt$', robots_dot_txt, name='robots.txt'), re_path(r'^favicon.ico$', favicon, name='favicon'), re_path(r'^.well-known/security\.txt$', security_dot_txt, name='security.txt'), diff --git a/tests/main/random_hosted_site_test.py b/tests/main/random_hosted_site_test.py new file mode 100644 index 000000000..70bb4c677 --- /dev/null +++ b/tests/main/random_hosted_site_test.py @@ -0,0 +1,23 @@ +from ocfweb.main.home import hosted_site_urls + + +def test_hosted_site_urls_uses_primary_vhost_names(): + vhosts = { + 'lift.studentorg.berkeley.edu': { + 'username': 'lift', + 'aliases': [], + 'docroot': '/', + 'flags': [], + }, + 'bearbites.asuc.org': { + 'username': 'bearbites', + 'aliases': ['www.bearbites.asuc.org'], + 'docroot': '/', + 'flags': [], + }, + } + + assert hosted_site_urls(vhosts) == [ + 'https://bearbites.asuc.org/', + 'https://lift.studentorg.berkeley.edu/', + ] From 74762cdc652a0027cba5f3a0d163d0025096ffd1 Mon Sep 17 00:00:00 2001 From: Joe Wang Date: Sun, 24 May 2026 12:41:43 -0700 Subject: [PATCH 2/3] fix: move button to ocf description --- ocfweb/main/templates/main/home.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ocfweb/main/templates/main/home.html b/ocfweb/main/templates/main/home.html index 4e20e6f51..889f83825 100644 --- a/ocfweb/main/templates/main/home.html +++ b/ocfweb/main/templates/main/home.html @@ -17,7 +17,6 @@

Welcome to the Open Computing Facility!

Learn what we do Create an account - Random hosted site

@@ -34,7 +33,7 @@

Welcome to the Open Computing Facility!

OCF volunteers maintain services for the Berkeley community, including: