Skip to content

Commit 10808b5

Browse files
refactored core app views to class based format
1 parent ca2edd7 commit 10808b5

File tree

2 files changed

+95
-88
lines changed

2 files changed

+95
-88
lines changed

src/core/urls.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
from . import views
44

55
urlpatterns = [
6-
# contest suite homepage
7-
path('', views.index, name='index'),
8-
path('contact/', views.contact, name='contact'),
9-
path('faq/', views.faq, name='faq'),
10-
path('teams/', views.teams, name='teams'),
6+
path('', views.IndexTemplateView.as_view(), name='index'),
7+
path('contact/', views.ContactTemplateView.as_view(), name='contact'),
8+
path('faq/', views.FaqTemplateView.as_view(), name='faq'),
9+
path('teams/', views.TeamsTemplateView.as_view(), name='teams'),
1110
]

src/core/views.py

Lines changed: 91 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from django.shortcuts import render
21
from django.core.cache import cache
2+
from django.views.generic.base import TemplateView
33

44
import requests as req
55

@@ -12,114 +12,122 @@
1212

1313
# Create your views here.
1414

15-
def index(request):
15+
class IndexTemplateView(TemplateView):
1616
"""
1717
View to display site index(home) page. Displays announcements, DOMjudge server status, and information on
1818
extra credit courses, participation, teams, and looking for group participants.
1919
"""
2020

21-
context = {}
21+
template_name = 'core/index.html'
2222

23-
# Get cached DOMjudge server status or ping server
24-
if cache.get('domjudge_status'):
25-
context['domjudge_status'] = cache.get('domjudge_status')
26-
else:
27-
try:
28-
r = req.head(DOMJUDGE_URL)
29-
except req.ConnectionError:
30-
context['domjudge_status'] = None
23+
def get_context_data(self, **kwargs):
24+
# Call the base implementation first to get a context
25+
context = super().get_context_data(**kwargs)
26+
27+
# Get cached DOMjudge server status or ping server
28+
if cache.get('domjudge_status'):
29+
context['domjudge_status'] = cache.get('domjudge_status')
3130
else:
32-
context['domjudge_status'] = r.status_code
33-
cache.set('domjudge_status', r.status_code, CACHE_TIMEOUT)
34-
35-
# Get contest object or set to None
36-
context['contest'] = cache.get_or_set(
37-
'contest_model', Contest.objects.first(), CACHE_TIMEOUT)
38-
39-
# Get published announcements
40-
context['announcements'] = (Announcement.objects.filter(status=1))
41-
# Get all courses
42-
context['courses'] = Course.objects.all()
43-
44-
if context['contest'] and context['contest'].lfg_active:
45-
# Get Looking For Group profile totals
46-
context['lfg_profiles_upper'] = LFGProfile.objects.filter(active=True).filter(division=1).count()
47-
context['lfg_profiles_lower'] = LFGProfile.objects.filter(active=True).filter(division=2).count()
48-
49-
### Teams ###
50-
51-
teams_set = Team.objects.all()
52-
participants_set = Profile.objects.all()
53-
54-
# Aggregate upper division team and participant info
55-
upper_teams_set = teams_set.filter(division=1).filter(
56-
faculty=False).exclude(num_members=0)
57-
context['num_upper_teams'] = upper_teams_set.count()
58-
context['num_upper_participants'] = participants_set.filter(
59-
team__division=1).count()
60-
61-
# Aggregate lower division team and participant info
62-
lower_teams_set = teams_set.filter(division=2).filter(
63-
faculty=False).exclude(num_members=0)
64-
context['num_lower_teams'] = lower_teams_set.count()
65-
context['num_lower_participants'] = participants_set.filter(
66-
team__division=2).count()
67-
68-
# Aggregate faculty team and participant info
69-
faculty_teams_set = teams_set.filter(faculty=True).exclude(num_members=0)
70-
context['num_faculty_teams'] = faculty_teams_set.count()
71-
context['num_faculty_participants'] = participants_set.filter(
72-
team__faculty=True).count()
73-
74-
return render(request, 'core/index.html', context)
75-
76-
77-
def contact(request):
31+
try:
32+
r = req.head(DOMJUDGE_URL)
33+
except req.ConnectionError:
34+
context['domjudge_status'] = None
35+
else:
36+
context['domjudge_status'] = r.status_code
37+
cache.set('domjudge_status', r.status_code, CACHE_TIMEOUT)
38+
39+
# Get contest object or set to None
40+
context['contest'] = cache.get_or_set(
41+
'contest_model', Contest.objects.first(), CACHE_TIMEOUT)
42+
43+
# Get published announcements
44+
context['announcements'] = (Announcement.objects.filter(status=1))
45+
# Get all courses
46+
context['courses'] = Course.objects.all()
47+
48+
if context['contest'] and context['contest'].lfg_active:
49+
# Get Looking For Group profile totals
50+
context['lfg_profiles_upper'] = LFGProfile.objects.filter(active=True).filter(division=1).count()
51+
context['lfg_profiles_lower'] = LFGProfile.objects.filter(active=True).filter(division=2).count()
52+
53+
### Teams ###
54+
55+
teams_set = Team.objects.all()
56+
participants_set = Profile.objects.all()
57+
58+
# Aggregate upper division team and participant info
59+
upper_teams_set = teams_set.filter(division=1).filter(
60+
faculty=False).exclude(num_members=0)
61+
context['num_upper_teams'] = upper_teams_set.count()
62+
context['num_upper_participants'] = participants_set.filter(
63+
team__division=1).count()
64+
65+
# Aggregate lower division team and participant info
66+
lower_teams_set = teams_set.filter(division=2).filter(
67+
faculty=False).exclude(num_members=0)
68+
context['num_lower_teams'] = lower_teams_set.count()
69+
context['num_lower_participants'] = participants_set.filter(
70+
team__division=2).count()
71+
72+
# Aggregate faculty team and participant info
73+
faculty_teams_set = teams_set.filter(faculty=True).exclude(num_members=0)
74+
context['num_faculty_teams'] = faculty_teams_set.count()
75+
context['num_faculty_participants'] = participants_set.filter(
76+
team__faculty=True).count()
77+
78+
return context
79+
80+
81+
class ContactTemplateView(TemplateView):
7882
"""
7983
View to display contact us page.
8084
"""
8185

82-
return render(request, 'core/contact.html')
86+
template_name = 'core/contact.html'
8387

8488

85-
def faq(request):
89+
class FaqTemplateView(TemplateView):
8690
"""
8791
View to display faq page.
8892
"""
8993

90-
return render(request, 'core/faq.html')
94+
template_name = 'core/faq.html'
9195

9296

93-
def teams(request):
97+
class TeamsTemplateView(TemplateView):
9498
"""
9599
View to display teams page.
96100
"""
97101

98-
context = {}
102+
template_name = 'core/teams.html'
103+
104+
def get_context_data(self, **kwargs):
105+
# Call the base implementation first to get a context
106+
context = super().get_context_data(**kwargs)
99107

100-
# Get contest object or set to None
101-
context['contest'] = cache.get_or_set(
102-
'contest_model', Contest.objects.first(), CACHE_TIMEOUT)
108+
# Get contest object or set to None
109+
context['contest'] = cache.get_or_set(
110+
'contest_model', Contest.objects.first(), CACHE_TIMEOUT)
103111

104-
teams_set = Team.objects.all()
105-
participants_set = Profile.objects.all()
112+
teams_set = Team.objects.all()
113+
participants_set = Profile.objects.all()
106114

107-
# Aggregate upper division team and participant info
108-
upper_teams_set = teams_set.filter(division=1).filter(faculty=False).exclude(num_members=0)
109-
context['upper_teams'] = upper_teams_set.order_by('-questions_answered', 'score', 'name')
110-
context['num_upper_teams'] = upper_teams_set.count()
111-
context['num_upper_participants'] = participants_set.filter(team__division=1).count()
115+
# Aggregate upper division team and participant info
116+
upper_teams_set = teams_set.filter(division=1).filter(faculty=False).exclude(num_members=0)
117+
context['upper_teams'] = upper_teams_set.order_by('-questions_answered', 'score', 'name')
118+
context['num_upper_teams'] = upper_teams_set.count()
119+
context['num_upper_participants'] = participants_set.filter(team__division=1).count()
112120

113-
# Aggregate lower division team and participant info
114-
lower_teams_set = teams_set.filter(division=2).filter(faculty=False).exclude(num_members=0)
115-
context['lower_teams'] = lower_teams_set.order_by('-questions_answered', 'score', 'name')
116-
context['num_lower_teams'] = lower_teams_set.count()
117-
context['num_lower_participants'] = participants_set.filter(team__division=2).count()
121+
# Aggregate lower division team and participant info
122+
lower_teams_set = teams_set.filter(division=2).filter(faculty=False).exclude(num_members=0)
123+
context['lower_teams'] = lower_teams_set.order_by('-questions_answered', 'score', 'name')
124+
context['num_lower_teams'] = lower_teams_set.count()
125+
context['num_lower_participants'] = participants_set.filter(team__division=2).count()
118126

119-
# Aggregate faculty team and participant info
120-
faculty_teams_set = teams_set.filter(faculty=True).exclude(num_members=0)
121-
context['faculty_teams'] = faculty_teams_set.order_by('-questions_answered', 'score', 'name')
122-
context['num_faculty_teams'] = faculty_teams_set.count()
123-
context['num_faculty_participants'] = participants_set.filter(team__faculty=True).count()
127+
# Aggregate faculty team and participant info
128+
faculty_teams_set = teams_set.filter(faculty=True).exclude(num_members=0)
129+
context['faculty_teams'] = faculty_teams_set.order_by('-questions_answered', 'score', 'name')
130+
context['num_faculty_teams'] = faculty_teams_set.count()
131+
context['num_faculty_participants'] = participants_set.filter(team__faculty=True).count()
124132

125-
return render(request, 'core/teams.html', context)
133+
return context

0 commit comments

Comments
 (0)