Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions ocfweb/main/home.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion ocfweb/main/templates/main/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>Welcome to the Open Computing Facility!</h1>
<p>OCF volunteers maintain services for the Berkeley community, including:</p>
<ul>
<li><a href="{% url 'lab' %}">A spiffy computer lab</a> in 171 MLK Student Union</li>
<li><a href="{{docs_url}}/user-docs/services/web/">Web &amp; email hosting</a> for thousands of student groups and individuals</li>
<li><a href="{{docs_url}}/user-docs/services/web/">Web &amp; email hosting</a> for thousands of student groups and individuals. <a href="/random-hosted-site">Visit a random website!</a></li>
<li><a href="{{docs_url}}/user-docs/services/lab/printing/">Free printing</a> for all UC Berkeley students</li>
<li><a href="{{docs_url}}/user-docs/services/shell/">Shell accounts</a> on our powerful <a href="{% url 'doc' 'staff/backend/servers' %}">on-campus servers</a></li>
<li><a href="{{docs_url}}/user-docs/services/hpc/">High-performance computing</a> on our GPU server</li>
Expand Down
2 changes: 2 additions & 0 deletions ocfweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'),
Expand Down
23 changes: 23 additions & 0 deletions tests/main/random_hosted_site_test.py
Original file line number Diff line number Diff line change
@@ -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/',
]