Skip to content

Commit

Permalink
Don't show events menu entry if there are no events to display
Browse files Browse the repository at this point in the history
  • Loading branch information
autoantwort committed Sep 2, 2024
1 parent cf8bdaf commit 533124c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
29 changes: 20 additions & 9 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import defaultdict
from datetime import timedelta

from django.db.models import QuerySet
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.urls import reverse
Expand All @@ -19,7 +20,20 @@
from studibars.settings import TIME_ZONE


# Create your views here.
def exclude_symposion_events(events: QuerySet):
now = datetime.datetime.now()
# Check if it's a weekday (Monday=0, ..., Sunday=6) + if the time is between 6:00 and 19:00
if 0 <= now.weekday() <= 4 and 6 <= now.hour < 19:
return events.exclude(bar__name__icontains="symposion")
return events


def should_display_events_menu_entry():
events = Event.objects.filter(start_date__gte=datetime.date.today())
events = exclude_symposion_events(events)
return events.exists()


def main_view(request):
json_ld = []
bars_by_weekday = defaultdict(list)
Expand All @@ -32,12 +46,10 @@ def main_view(request):
events = Event.objects.filter(start_date__gte=datetime.date.today())
for event in events:
json_ld.append(event.to_json_ld())
# Check if it's a weekday (Monday=0, ..., Sunday=6) + if the time is between 6:00 and 19:00
now = datetime.datetime.now()
if 0 <= now.weekday() <= 4 and 6 <= now.hour < 19:
events = events.exclude(bar__name__icontains="symposion")
events = exclude_symposion_events(events)
return render(request, 'main/main.html', {
'title': 'Home',
'display_events_menu_entry': should_display_events_menu_entry(),
'json_ld': mark_safe(json.dumps(json_ld)),
'weekdays': Weekday.choices[:-3],
'bars_by_day': bars,
Expand Down Expand Up @@ -69,12 +81,10 @@ def bar_view_base(request, bar: Bar):
events = bar.event_set.filter(start_date__gte=datetime.date.today())
for event in events:
json_ld.append(event.to_json_ld())
# Check if it's a weekday (Monday=0, ..., Sunday=6) + if the time is between 6:00 and 19:00
now = datetime.datetime.now()
if 0 <= now.weekday() <= 4 and 6 <= now.hour < 19:
events = events.exclude(bar__name__icontains="symposion")
events = exclude_symposion_events(events)
return render(request, 'main/bar.html', {
'title': bar.name,
'display_events_menu_entry': should_display_events_menu_entry(),
'json_ld': mark_safe(json.dumps(json_ld)),
'bars': Bar.objects.all().order_by('day', 'start_time'),
'bar': bar,
Expand All @@ -91,6 +101,7 @@ def event_view(request, event_id, name, bar=""):
raise Http404
return render(request, 'main/event.html', {
'title': f"{event.bar.name} - {event.name} - {event.start_date.date().strftime("%d.%m.%Y")}",
'display_events_menu_entry': should_display_events_menu_entry(),
'bars': Bar.objects.all().order_by('day', 'start_time'),
'json_ld': mark_safe(json.dumps(event.to_json_ld())),
'event': event,
Expand Down
10 changes: 6 additions & 4 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% bootstrap_css %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<meta name="google-site-verification" content="tCBRqO09x3c4t3EzdjSln0DcPjbufFOj6135ULKOQkU"/>
<meta name="google-site-verification" content="Lj4deWKwoZ3efh3ULwaCTKaemgKTuz3ZQyo9G6i4KVU" />
<meta name="google-site-verification" content="Lj4deWKwoZ3efh3ULwaCTKaemgKTuz3ZQyo9G6i4KVU"/>
{% if content_description %}
<meta content="{{ content_description }}"
name="description">
Expand Down Expand Up @@ -73,9 +73,11 @@
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/#events">Events</a>
</li>
{% if display_events_menu_entry %}
<li class="nav-item">
<a class="nav-link" href="/#events">Events</a>
</li>
{% endif %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-expanded="false">
Expand Down

0 comments on commit 533124c

Please sign in to comment.