diff --git a/flashcards/models.py b/flashcards/models.py index 722662f..ed22a45 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -164,7 +164,7 @@ class Section(Model): class Meta: unique_together = (('department', 'course_num', 'quarter', 'instructor'),) - + ordering = ['-course_title'] class LecturePeriod(Model): """ diff --git a/flashcards/views.py b/flashcards/views.py index 8146c8e..255161d 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -3,7 +3,7 @@ from flashcards.models import Section, User from flashcards.serializers import SectionSerializer, UserUpdateSerializer, RegistrationSerializer, UserSerializer, \ PasswordResetSerializer, PasswordResetRequestSerializer, EmailPasswordSerializer from rest_framework.permissions import IsAuthenticated -from rest_framework.viewsets import ReadOnlyModelViewSet +from rest_framework.viewsets import ReadOnlyModelViewSet, ModelViewSet from django.core.mail import send_mail from django.contrib.auth import authenticate, login, logout from django.contrib.auth.tokens import default_token_generator @@ -12,7 +12,7 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated, ValidationError from simple_email_confirmation import EmailAddress - +from rest_framework import filters class SectionViewSet(ReadOnlyModelViewSet): queryset = Section.objects.all() @@ -20,11 +20,12 @@ class SectionViewSet(ReadOnlyModelViewSet): pagination_class = StandardResultsSetPagination class UserSectionViewSet(ModelViewSet): - def list(self, request, format=None): - queryset = User.sections.all() - serializer_class = SectionSerializer - return Response(serializer.data) - def get_queryset( + serializer_class = SectionSerializer + permission_classes = [IsAuthenticated] + def get_queryset(self): + return self.request.user.sections.all() + + def paginate_queryset(self, queryset): return None class UserDetail(APIView): def patch(self, request, format=None): diff --git a/flashy/settings.py b/flashy/settings.py index 61ef0da..4b3e681 100644 --- a/flashy/settings.py +++ b/flashy/settings.py @@ -10,7 +10,9 @@ DEBUG = not IN_PRODUCTION ALLOWED_HOSTS = ['127.0.0.1', 'flashy.cards'] AUTH_USER_MODEL = 'flashcards.User' - +REST_FRAMEWORK = { + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination' +} INSTALLED_APPS = ( 'simple_email_confirmation', 'flashcards', @@ -114,4 +116,4 @@ SECRET_KEY = os.environ.get('SECRET_KEY', 'LOL DEFAULT SECRET KEY') SWAGGER_SETTINGS = { 'doc_expansion': 'list' -} \ No newline at end of file +} diff --git a/flashy/urls.py b/flashy/urls.py index 0abfc10..6b42a4b 100644 --- a/flashy/urls.py +++ b/flashy/urls.py @@ -1,12 +1,14 @@ from django.conf.urls import include, url from django.contrib import admin -from flashcards.views import SectionViewSet, UserDetail, UserLogin, UserLogout, PasswordReset +from flashcards.views import SectionViewSet, UserDetail, UserLogin, UserLogout, PasswordReset, UserSectionViewSet from rest_framework.routers import DefaultRouter from flashcards.api import * router = DefaultRouter() router.register(r'sections', SectionViewSet) +router.register(r'users/me/sections', UserSectionViewSet, base_name = 'usersection') + urlpatterns = [ url(r'^api/docs/', include('rest_framework_swagger.urls')), url(r'^api/users/me$', UserDetail.as_view()), @@ -16,7 +18,8 @@ urlpatterns = [ url(r'^api/', include(router.urls)), url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/', include(admin.site.urls)), - url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) + url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework' +)) ] urlpatterns += (url(r'^admin/django-ses/', include('django_ses.urls')),)