Commit 6d5fa0bba8e96e11b11c661e8eef8b268421a541
Exists in
master
Merge branch 'master' of git.ucsd.edu:110swag/flashy-backend
Showing 3 changed files Side-by-side Diff
flashcards/models.py
View file @
6d5fa0b
... | ... | @@ -57,6 +57,17 @@ |
57 | 57 | user_card.pulled = datetime.now() |
58 | 58 | user_card.save() |
59 | 59 | |
60 | + def unpull(self, flashcard): | |
61 | + if not self.is_in_section(flashcard.section): | |
62 | + raise ValueError("User not in the section this flashcard belongs to") | |
63 | + | |
64 | + try: | |
65 | + user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard) | |
66 | + except UserFlashcard.DoesNotExist: | |
67 | + raise ValueError('Cannot unpull card that is not pulled.') | |
68 | + | |
69 | + user_card.delete() | |
70 | + | |
60 | 71 | def get_deck(self, section): |
61 | 72 | if not self.is_in_section(section): |
62 | 73 | raise ObjectDoesNotExist("User not enrolled in section") |
flashcards/tests/test_api.py
View file @
6d5fa0b
... | ... | @@ -282,7 +282,7 @@ |
282 | 282 | |
283 | 283 | def test_section_search(self): |
284 | 284 | response = self.client.get('/api/sections/search/?q=Kramer') |
285 | - self.assertEqual(response.status_code, HTTP_404_NOT_FOUND) | |
285 | + self.assertEqual(response.status_code, HTTP_200_OK) | |
286 | 286 | |
287 | 287 | def test_section_deck(self): |
288 | 288 | self.user.sections.add(self.section) |
... | ... | @@ -300,4 +300,30 @@ |
300 | 300 | self.user.save() |
301 | 301 | response = self.client.get('/api/sections/1/ordered_deck/') |
302 | 302 | self.assertEqual(response.status_code, HTTP_200_OK) |
303 | + | |
304 | + | |
305 | +class FlashcardViewSetTest(APITestCase): | |
306 | + fixtures = ['testusers', 'testsections'] | |
307 | + | |
308 | + def setUp(self): | |
309 | + self.client.login(email='none@none.com', password='1234') | |
310 | + self.user = User.objects.get(email='none@none.com') | |
311 | + self.flashcard = Flashcard(text="jason", section=Section.objects.get(pk=1), material_date=now(), | |
312 | + author=self.user) | |
313 | + self.flashcard.save() | |
314 | + self.section = Section.objects.get(pk=1) | |
315 | + | |
316 | + def test_hide_flashcard(self): | |
317 | + url = '/api/flashcards/1/hide/' | |
318 | + data = {1, ''} | |
319 | + response = self.client.post(url, data, format='json') | |
320 | + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | |
321 | + | |
322 | + def test_unhide_flashcard(self): | |
323 | + url = '/api/flashcards/1/unhide/' | |
324 | + flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard) | |
325 | + flashcard_hide.save() | |
326 | + | |
327 | + response = self.client.post(url, format='json') | |
328 | + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) |
flashcards/views.py
View file @
6d5fa0b
... | ... | @@ -303,6 +303,24 @@ |
303 | 303 | return Response(response_data.data, status=HTTP_201_CREATED, headers=headers) |
304 | 304 | |
305 | 305 | @detail_route(methods=['post']) |
306 | + def hide(self, request, pk): | |
307 | + """ | |
308 | + Hide a flashcard | |
309 | + --- | |
310 | + omit_serializer: true | |
311 | + parameters: | |
312 | + - fake: None | |
313 | + parameters_strategy: | |
314 | + form: replace | |
315 | + """ | |
316 | + obj, created = FlashcardHide.objects.get_or_create(user=request.user, flashcard=self.get_object()) | |
317 | + if not created: | |
318 | + raise ValidationError("The card has already been hidden.") | |
319 | + | |
320 | + obj.save() | |
321 | + return Response(status=HTTP_204_NO_CONTENT) | |
322 | + | |
323 | + @detail_route(methods=['post']) | |
306 | 324 | def unhide(self, request, pk): |
307 | 325 | """ |
308 | 326 | Report the given card |
... | ... | @@ -315,6 +333,7 @@ |
315 | 333 | """ |
316 | 334 | hide = get_object_or_404(FlashcardHide ,user=request.user, flashcard=self.get_object()) |
317 | 335 | hide.delete() |
336 | + return Response(status=HTTP_204_NO_CONTENT) | |
318 | 337 | |
319 | 338 | @detail_route(methods=['post']) |
320 | 339 | def report(self, request, pk): |
... | ... | @@ -344,6 +363,19 @@ |
344 | 363 | user = request.user |
345 | 364 | flashcard = self.get_object() |
346 | 365 | user.pull(flashcard) |
366 | + return Response(status=HTTP_204_NO_CONTENT) | |
367 | + | |
368 | + @detail_route(methods=['POST']) | |
369 | + def unpull(self, request, pk): | |
370 | + """ | |
371 | + Unpull a card from the user's deck | |
372 | + :param request: The request object | |
373 | + :param pk: The primary key | |
374 | + :return: A 204 response upon success. | |
375 | + """ | |
376 | + user = request.user | |
377 | + flashcard = self.get_object() | |
378 | + user.unpull(flashcard) | |
347 | 379 | return Response(status=HTTP_204_NO_CONTENT) |
348 | 380 | |
349 | 381 | def update(self, request, *args, **kwargs): |