Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adventure editing #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions web/adventures/test_adventures.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_Author_str(self):

class URLListFieldTestCase(TestCase):
def setUp(self):
Adventure.objects.create(name="LMoP", links=["www.google.com", "another.website.io"])
Adventure.objects.create(
name="LMoP",
links=["www.google.com", "another.website.io"])
Adventure.objects.create(name="HotDQ")

def test_URLListField_db_type(self):
Expand All @@ -45,7 +47,8 @@ def test_URLListField_from_db_value_empty_list(self):
def test_URLListField_from_db_value_with_links(self):
"""Test URLListField's from_db_value with links"""
adventure = Adventure.objects.get(name="LMoP")
self.assertEqual(adventure.links, ["www.google.com", "another.website.io"])
self.assertEqual(adventure.links,
["www.google.com", "another.website.io"])

def test_URLListField_to_python_none(self):
"""Test URLListField's to_python with none"""
Expand All @@ -56,26 +59,29 @@ def test_URLListField_to_python_with_links(self):
"""Test URLListField's to_python with links"""
url_list = URLListField()
self.assertEqual(
url_list.to_python("www.google.com,another.website.io"), ["www.google.com", "another.website.io"]
)
url_list.to_python("www.google.com,another.website.io"),
["www.google.com", "another.website.io"])

def test_URLListField_get_prep_value(self):
"""Test URLListField's to_python with none"""
url_list = URLListField()
self.assertEqual(
url_list.get_prep_value(["www.google.com", "another.website.io"]), "www.google.com,another.website.io"
)
url_list.get_prep_value(["www.google.com", "another.website.io"]),
"www.google.com,another.website.io")


class AdventureByIdTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.client = Client()
cls.test_data = {'name': 'LMoP', 'links': ["www.google.com", "another.website.io"]}
cls.test_data = {'name': 'LMoP',
'links': ["www.google.com", "another.website.io"]}
cls.test_adv = Adventure.objects.create(**cls.test_data)
cls.put_test_data = {'name': 'Rage of Demons', 'description': 'test'}

def test_adventure_by_id_get_success(self):
lmop = self.client.get(reverse('adventures:adventure-by-id', args=(self.test_adv.id, )))
lmop = self.client.get(reverse('adventures:adventure-by-id',
args=(self.test_adv.id, )))
actual_data = json.loads(lmop.content.decode('utf-8'))
self.assertEqual(actual_data['name'], self.test_adv.name)
self.assertEqual(actual_data['id'], self.test_adv.id)
Expand All @@ -84,9 +90,30 @@ def test_adventure_by_id_get_success(self):
self.assertEqual(actual_data['description'], self.test_adv.description)

def test_adventure_by_id_get_404(self):
notfound = self.client.get(reverse('adventures:adventure-by-id', args=(100, )))
notfound = self.client.get(reverse('adventures:adventure-by-id',
args=(100, )))
self.assertEqual(notfound.status_code, 404)

def test_adventure_by_id_get_put(self):
put = self.client.put(reverse('adventures:adventure-by-id', args=(self.test_adv.id, )))
self.assertEqual(put.status_code, 405)
def test_adventure_by_id_put_success(self):
self.client.put(
reverse('adventures:adventure-by-id',
args=(self.test_adv.id, )),
data=json.dumps(self.put_test_data),
content_type='application/json')
get = self.client.get(reverse('adventures:adventure-by-id',
args=(self.test_adv.id, )))
get_data = json.loads(get.content.decode('utf-8'))
self.assertEqual(get_data['name'], self.put_test_data['name'])
self.assertEqual(get_data['id'], self.test_adv.id)
self.assertEqual(get_data['links'], self.test_adv.links)
self.assertEqual(get_data['authors'], [])
self.assertEqual(get_data['description'],
self.put_test_data['description'])

def test_adventure_by_id_put_404(self):
put = self.client.put(
reverse('adventures:adventure-by-id',
args=(self.test_adv.id + 1, )),
data=json.dumps(self.put_test_data),
content_type='application/json')
self.assertEqual(put.status_code, 404)
19 changes: 16 additions & 3 deletions web/adventures/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django.forms.models import model_to_dict
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
Expand All @@ -8,10 +10,21 @@

class AdventureById(View):
def dispatch(self, request, *args, **kwargs):
adventure_id = kwargs.get('adventure_id')
self.adventure = get_object_or_404(models.Adventure, pk=adventure_id)
self.adventure_id = kwargs.get('adventure_id')
return super().dispatch(request)

def get(self, request):
data = model_to_dict(self.adventure)
adventure = get_object_or_404(models.Adventure, pk=self.adventure_id)
data = model_to_dict(adventure)
return JsonResponse(data)

def put(self, request):
new_data = json.loads(request.body.decode('utf-8'))
adventure_qset = models.Adventure.objects.filter(pk=self.adventure_id)
if not adventure_qset:
return JsonResponse({},
status=404,
reason='No Adventure matches the given query.')
adventure_qset.update(**new_data)
adventure_data = model_to_dict(adventure_qset[0])
return JsonResponse(adventure_data)