Commit 2a9edd990f102b292ef4fb59c0688f6ed5ab56f5
1 parent
64ad0b96d5
Exists in
master
Flashcard detail view
Showing 5 changed files with 47 additions and 9 deletions Side-by-side Diff
flashcards/serializers.py
View file @
2a9edd9
1 | -from flashcards.models import Section, LecturePeriod, User | |
1 | +from flashcards.models import Section, LecturePeriod, User, Flashcard | |
2 | 2 | from rest_framework import serializers |
3 | 3 | from rest_framework.fields import EmailField, BooleanField, CharField, IntegerField |
4 | 4 | from rest_framework.relations import HyperlinkedRelatedField |
... | ... | @@ -78,4 +78,10 @@ |
78 | 78 | class Meta: |
79 | 79 | model = User |
80 | 80 | fields = ("sections", "email", "is_confirmed", "last_login", "date_joined") |
81 | + | |
82 | +class FlashcardSerializer(HyperlinkedModelSerializer): | |
83 | + | |
84 | + class Meta: | |
85 | + model = Flashcard | |
86 | + exclude = 'author', 'mask', |
flashcards/tests.py
View file @
2a9edd9
flashcards/tests/test_api.py
View file @
2a9edd9
1 | 1 | from django.core import mail |
2 | -from flashcards.models import User | |
2 | +from flashcards.models import User, Section, Flashcard | |
3 | 3 | from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_401_UNAUTHORIZED |
4 | 4 | from rest_framework.test import APITestCase |
5 | 5 | from re import search |
6 | +from django.utils.timezone import now | |
6 | 7 | |
7 | 8 | |
8 | 9 | class LoginTests(APITestCase): |
... | ... | @@ -186,4 +187,25 @@ |
186 | 187 | self.client.delete(url) |
187 | 188 | |
188 | 189 | self.assertFalse(User.objects.filter(email='none@none.com').exists()) |
190 | + | |
191 | +class FlashcardDetailTest(APITestCase): | |
192 | + def setUp(self): | |
193 | + section = Section( department="cse", course_num="5", \ | |
194 | + course_title="cool course", \ | |
195 | + instructor="gary", quarter="fa15") | |
196 | + section.save() | |
197 | + | |
198 | + user = User.objects.create_user( email="jacob@aaa.com", \ | |
199 | + password="top_secret" ) | |
200 | + user.save() | |
201 | + | |
202 | + self.flashcard = Flashcard( text="jason", section=section, \ | |
203 | + material_date=now(), author=user ) | |
204 | + self.flashcard.save() | |
205 | + | |
206 | + def test_get_flashcard(self): | |
207 | + | |
208 | + response = self.client.get("/api/flashcards/%d"%self.flashcard.id, format="json") | |
209 | + self.assertEqual(response.status_code, HTTP_200_OK) | |
210 | + self.assertEqual(response.data["text"], "jason") |
flashcards/views.py
View file @
2a9edd9
1 | 1 | from flashcards.api import StandardResultsSetPagination |
2 | -from flashcards.models import Section, User | |
2 | +from flashcards.models import Section, User, Flashcard | |
3 | 3 | from flashcards.serializers import SectionSerializer, UserUpdateSerializer, RegistrationSerializer, UserSerializer, \ |
4 | - PasswordResetSerializer, PasswordResetRequestSerializer, EmailPasswordSerializer | |
4 | + PasswordResetSerializer, PasswordResetRequestSerializer, EmailPasswordSerializer, FlashcardSerializer | |
5 | 5 | from rest_framework.permissions import IsAuthenticated |
6 | 6 | from rest_framework.viewsets import ReadOnlyModelViewSet, ModelViewSet |
7 | 7 | from django.core.mail import send_mail |
... | ... | @@ -10,6 +10,7 @@ |
10 | 10 | from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_401_UNAUTHORIZED, HTTP_201_CREATED |
11 | 11 | from rest_framework.views import APIView |
12 | 12 | from rest_framework.response import Response |
13 | +from rest_framework.generics import RetrieveAPIView | |
13 | 14 | from rest_framework.exceptions import AuthenticationFailed, NotAuthenticated, ValidationError |
14 | 15 | from simple_email_confirmation import EmailAddress |
15 | 16 | from rest_framework import filters |
... | ... | @@ -182,4 +183,12 @@ |
182 | 183 | else: |
183 | 184 | raise ValidationError('Could not verify reset token') |
184 | 185 | return Response(status=HTTP_204_NO_CONTENT) |
186 | + | |
187 | +class FlashcardDetail(RetrieveAPIView): | |
188 | + queryset = Flashcard.objects.all() | |
189 | + serializer_class = FlashcardSerializer | |
190 | +## def get(self, request): | |
191 | + | |
192 | + | |
193 | + |
flashy/urls.py
View file @
2a9edd9
1 | 1 | from django.conf.urls import include, url |
2 | 2 | from django.contrib import admin |
3 | -from flashcards.views import SectionViewSet, UserDetail, UserLogin, UserLogout, PasswordReset, UserSectionViewSet | |
3 | +from flashcards.views import SectionViewSet, UserDetail, UserLogin, UserLogout, PasswordReset, UserSectionViewSet, FlashcardDetail | |
4 | 4 | from rest_framework.routers import DefaultRouter |
5 | 5 | from flashcards.api import * |
6 | 6 | |
... | ... | @@ -19,7 +19,9 @@ |
19 | 19 | url(r'^admin/doc/', include('django.contrib.admindocs.urls')), |
20 | 20 | url(r'^admin/', include(admin.site.urls)), |
21 | 21 | url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework' |
22 | -)) | |
22 | +)), | |
23 | + url(r'^api/flashcards/(?P<pk>[0-9]+)$', FlashcardDetail.as_view(), name \ | |
24 | + ="flashcard-detail"), | |
23 | 25 | ] |
24 | 26 | |
25 | 27 | urlpatterns += (url(r'^admin/django-ses/', include('django_ses.urls')),) |