Commit a6ad65cb by Arthur Barrett

trying to fix pylint errors

parent a407c84b
...@@ -68,7 +68,7 @@ def api_request(request, course_id, **kwargs): ...@@ -68,7 +68,7 @@ def api_request(request, course_id, **kwargs):
func = resource.get(resource_method) func = resource.get(resource_method)
module = globals() module = globals()
if func not in module: if func not in module:
log.debug('Function "{0}" does not exist for request {1} {2}'.format(action, resource_method, resource_name)) log.debug('Function "{0}" does not exist for request {1} {2}'.format(func, resource_method, resource_name))
raise Http404 raise Http404
log.debug('API request: {0} {1}'.format(resource_method, resource_name)) log.debug('API request: {0} {1}'.format(resource_method, resource_name))
......
...@@ -23,6 +23,9 @@ class Note(models.Model): ...@@ -23,6 +23,9 @@ class Note(models.Model):
updated = models.DateTimeField(auto_now=True, db_index=True) updated = models.DateTimeField(auto_now=True, db_index=True)
def clean(self, json_body): def clean(self, json_body):
'''
Cleans the note object or raises a ValidationError.
'''
if json_body is None: if json_body is None:
raise ValidationError('Note must have a body.') raise ValidationError('Note must have a body.')
...@@ -49,13 +52,19 @@ class Note(models.Model): ...@@ -49,13 +52,19 @@ class Note(models.Model):
self.tags = ",".join(tags) self.tags = ",".join(tags)
def get_absolute_url(self): def get_absolute_url(self):
kwargs = {'course_id': self.course_id, 'note_id': str(self.id)} '''
Returns the aboslute url for the note object.
'''
kwargs = {'course_id': self.course_id, 'note_id': str(self.pk)}
return reverse('notes_api_note', kwargs=kwargs) return reverse('notes_api_note', kwargs=kwargs)
def as_dict(self): def as_dict(self):
'''
Returns the note object as a dictionary.
'''
return { return {
'id': self.id, 'id': self.pk,
'user_id': self.user.id, 'user_id': self.user.pk,
'uri': self.uri, 'uri': self.uri,
'text': self.text, 'text': self.text,
'quote': self.quote, 'quote': self.quote,
......
...@@ -14,7 +14,6 @@ import json ...@@ -14,7 +14,6 @@ import json
import logging import logging
from . import utils, api, models from . import utils, api, models
from .models import Note
class UtilsTest(TestCase): class UtilsTest(TestCase):
...@@ -204,12 +203,12 @@ class ApiTest(TestCase): ...@@ -204,12 +203,12 @@ class ApiTest(TestCase):
self.assertEqual(len(notes), 3) self.assertEqual(len(notes), 3)
for note in notes: for note in notes:
resp = self.client.get(self.url('notes_api_note', {'note_id': note.id})) resp = self.client.get(self.url('notes_api_note', {'note_id': note.pk}))
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
self.assertNotEqual(resp.content, '') self.assertNotEqual(resp.content, '')
content = json.loads(resp.content) content = json.loads(resp.content)
self.assertEqual(content['id'], note.id) self.assertEqual(content['id'], note.pk)
self.assertEqual(content['user_id'], note.user_id) self.assertEqual(content['user_id'], note.user_id)
def test_note_doesnt_exist_to_read(self): def test_note_doesnt_exist_to_read(self):
...@@ -227,7 +226,7 @@ class ApiTest(TestCase): ...@@ -227,7 +226,7 @@ class ApiTest(TestCase):
# set the student id to a different student (not the one that created the notes) # set the student id to a different student (not the one that created the notes)
self.login(as_student=self.student2) self.login(as_student=self.student2)
resp = self.client.get(self.url('notes_api_note', {'note_id': note.id})) resp = self.client.get(self.url('notes_api_note', {'note_id': note.pk}))
self.assertEqual(resp.status_code, 403) self.assertEqual(resp.status_code, 403)
self.assertEqual(resp.content, '') self.assertEqual(resp.content, '')
...@@ -239,13 +238,13 @@ class ApiTest(TestCase): ...@@ -239,13 +238,13 @@ class ApiTest(TestCase):
note = notes[0] note = notes[0]
resp = self.client.delete(self.url('notes_api_note', { resp = self.client.delete(self.url('notes_api_note', {
'note_id': note.id 'note_id': note.pk
})) }))
self.assertEqual(resp.status_code, 204) self.assertEqual(resp.status_code, 204)
self.assertEqual(resp.content, '') self.assertEqual(resp.content, '')
with self.assertRaises(models.Note.DoesNotExist): with self.assertRaises(models.Note.DoesNotExist):
models.Note.objects.get(pk=note.id) models.Note.objects.get(pk=note.pk)
def test_note_does_not_exist_to_delete(self): def test_note_does_not_exist_to_delete(self):
self.login() self.login()
...@@ -263,13 +262,13 @@ class ApiTest(TestCase): ...@@ -263,13 +262,13 @@ class ApiTest(TestCase):
self.login(as_student=self.student2) self.login(as_student=self.student2)
resp = self.client.delete(self.url('notes_api_note', { resp = self.client.delete(self.url('notes_api_note', {
'note_id': note.id 'note_id': note.pk
})) }))
self.assertEqual(resp.status_code, 403) self.assertEqual(resp.status_code, 403)
self.assertEqual(resp.content, '') self.assertEqual(resp.content, '')
try: try:
models.Note.objects.get(pk=note.id) models.Note.objects.get(pk=note.pk)
except models.Note.DoesNotExist: except models.Note.DoesNotExist:
self.fail('note should exist and not be deleted because the student does not have permission to do so') self.fail('note should exist and not be deleted because the student does not have permission to do so')
...@@ -284,14 +283,14 @@ class ApiTest(TestCase): ...@@ -284,14 +283,14 @@ class ApiTest(TestCase):
}) })
self.login() self.login()
resp = self.client.put(self.url('notes_api_note', {'note_id': note.id}), resp = self.client.put(self.url('notes_api_note', {'note_id': note.pk}),
json.dumps(updated_dict), json.dumps(updated_dict),
content_type='application/json', content_type='application/json',
HTTP_X_REQUESTED_WITH='XMLHttpRequest') HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(resp.status_code, 303) self.assertEqual(resp.status_code, 303)
self.assertEqual(resp.content, '') self.assertEqual(resp.content, '')
actual = models.Note.objects.get(pk=note.id) actual = models.Note.objects.get(pk=note.pk)
actual_dict = actual.as_dict() actual_dict = actual.as_dict()
for field in ['text', 'tags']: for field in ['text', 'tags']:
self.assertEqual(actual_dict[field], updated_dict[field]) self.assertEqual(actual_dict[field], updated_dict[field])
...@@ -361,10 +360,10 @@ class NoteTest(TestCase): ...@@ -361,10 +360,10 @@ class NoteTest(TestCase):
} }
def test_clean_valid_note(self): def test_clean_valid_note(self):
reference_note = Note(**self.note) reference_note = models.Note(**self.note)
body = reference_note.as_dict() body = reference_note.as_dict()
note = Note(course_id=self.course_id, user=self.student) note = models.Note(course_id=self.course_id, user=self.student)
try: try:
note.clean(json.dumps(body)) note.clean(json.dumps(body))
self.assertEqual(note.uri, body['uri']) self.assertEqual(note.uri, body['uri'])
...@@ -379,7 +378,7 @@ class NoteTest(TestCase): ...@@ -379,7 +378,7 @@ class NoteTest(TestCase):
self.fail('a valid note should not raise an exception') self.fail('a valid note should not raise an exception')
def test_clean_invalid_note(self): def test_clean_invalid_note(self):
note = Note(course_id=self.course_id, user=self.student) note = models.Note(course_id=self.course_id, user=self.student)
for empty_type in (None, '', 0, []): for empty_type in (None, '', 0, []):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
note.clean(None) note.clean(None)
...@@ -392,7 +391,7 @@ class NoteTest(TestCase): ...@@ -392,7 +391,7 @@ class NoteTest(TestCase):
})) }))
def test_as_dict(self): def test_as_dict(self):
note = Note(course_id=self.course_id, user=self.student) note = models.Note(course_id=self.course_id, user=self.student)
d = note.as_dict() d = note.as_dict()
self.assertNotIsInstance(d, basestring) self.assertNotIsInstance(d, basestring)
self.assertEqual(d['user_id'], self.student.id) self.assertEqual(d['user_id'], self.student.id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment