|
1 |
| -from django.shortcuts import render |
2 | 1 | from django.core.cache import cache
|
| 2 | +from django.views.generic.base import TemplateView |
3 | 3 |
|
4 | 4 | import requests as req
|
5 | 5 |
|
|
12 | 12 |
|
13 | 13 | # Create your views here.
|
14 | 14 |
|
15 |
| -def index(request): |
| 15 | +class IndexTemplateView(TemplateView): |
16 | 16 | """
|
17 | 17 | View to display site index(home) page. Displays announcements, DOMjudge server status, and information on
|
18 | 18 | extra credit courses, participation, teams, and looking for group participants.
|
19 | 19 | """
|
20 | 20 |
|
21 |
| - context = {} |
| 21 | + template_name = 'core/index.html' |
22 | 22 |
|
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') |
31 | 30 | 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): |
78 | 82 | """
|
79 | 83 | View to display contact us page.
|
80 | 84 | """
|
81 | 85 |
|
82 |
| - return render(request, 'core/contact.html') |
| 86 | + template_name = 'core/contact.html' |
83 | 87 |
|
84 | 88 |
|
85 |
| -def faq(request): |
| 89 | +class FaqTemplateView(TemplateView): |
86 | 90 | """
|
87 | 91 | View to display faq page.
|
88 | 92 | """
|
89 | 93 |
|
90 |
| - return render(request, 'core/faq.html') |
| 94 | + template_name = 'core/faq.html' |
91 | 95 |
|
92 | 96 |
|
93 |
| -def teams(request): |
| 97 | +class TeamsTemplateView(TemplateView): |
94 | 98 | """
|
95 | 99 | View to display teams page.
|
96 | 100 | """
|
97 | 101 |
|
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) |
99 | 107 |
|
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) |
103 | 111 |
|
104 |
| - teams_set = Team.objects.all() |
105 |
| - participants_set = Profile.objects.all() |
| 112 | + teams_set = Team.objects.all() |
| 113 | + participants_set = Profile.objects.all() |
106 | 114 |
|
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() |
112 | 120 |
|
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() |
118 | 126 |
|
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() |
124 | 132 |
|
125 |
| - return render(request, 'core/teams.html', context) |
| 133 | + return context |
0 commit comments