From 380bf3605404cf062bbd7110ca45bc235be1e476 Mon Sep 17 00:00:00 2001 From: Daniel W Bond Date: Sat, 9 May 2015 18:37:24 -0400 Subject: [PATCH] set up page to list majors --- roomlist/accounts/models.py | 3 +++ roomlist/accounts/urls.py | 5 ++++- roomlist/accounts/views.py | 12 ++++++++++-- roomlist/templates/layouts/navbar.html | 4 ++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/roomlist/accounts/models.py b/roomlist/accounts/models.py index ea18b84..306e66c 100644 --- a/roomlist/accounts/models.py +++ b/roomlist/accounts/models.py @@ -16,6 +16,9 @@ class Major(TimeStampedModel): name = models.CharField(max_length=50) # I believe the longest is "Government and International Politics" + def first_letter(self): + return self.name and self.name[0] or '' + def __str__(self): return self.name diff --git a/roomlist/accounts/urls.py b/roomlist/accounts/urls.py index cb6f27f..8376801 100644 --- a/roomlist/accounts/urls.py +++ b/roomlist/accounts/urls.py @@ -2,13 +2,16 @@ from django.conf.urls import patterns, include, url # imports from your apps from .views import DetailStudent, UpdateStudent, DetailStudentSettings,\ - DetailCurrentStudent, DetailCurrentStudentSettings, UpdateStudentMajor + DetailCurrentStudent, DetailCurrentStudentSettings, UpdateStudentMajor,\ + ListMajors urlpatterns = patterns('', url(r'', include('allauth.urls')), + url(r'^majors/$', ListMajors.as_view(), name='list_majors'), + url(r'^student/(?P[\w-]+)/$', DetailStudent.as_view(), name='detail_student'), diff --git a/roomlist/accounts/views.py b/roomlist/accounts/views.py index da4d1c4..f674a42 100644 --- a/roomlist/accounts/views.py +++ b/roomlist/accounts/views.py @@ -1,11 +1,11 @@ # core django imports from django.shortcuts import get_object_or_404 from django.http import HttpResponseForbidden -from django.views.generic import DetailView, UpdateView +from django.views.generic import ListView, DetailView, UpdateView # third party imports from braces.views import LoginRequiredMixin # imports from your apps -from .models import Student +from .models import Student, Major # update a student (students are *created* on first login via CAS) @@ -117,3 +117,11 @@ class DetailCurrentStudentSettings(LoginRequiredMixin, DetailView): def get_object(self): return get_object_or_404(Student, pk=self.request.session['_auth_user_id']) + +class ListMajors(LoginRequiredMixin, ListView): + model = Major + queryset = Major.objects.all().order_by('name') + context_object_name = 'majors' + template_name = 'list_majors.html' + + login_url = 'majors' diff --git a/roomlist/templates/layouts/navbar.html b/roomlist/templates/layouts/navbar.html index b4aaefb..6aedf3b 100644 --- a/roomlist/templates/layouts/navbar.html +++ b/roomlist/templates/layouts/navbar.html @@ -12,8 +12,8 @@