Mercurial > public > sg101
comparison comments/views.py @ 1221:d6b3f7e77f6c modernize tip
Add unit tests for comments views.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 01 Mar 2025 15:52:11 -0600 |
parents | 02181fa5ac9d |
children |
comparison
equal
deleted
inserted
replaced
1220:d63f9ece1129 | 1221:d6b3f7e77f6c |
---|---|
51 return HttpResponseBadRequest('Missing content_type or object_pk field.') | 51 return HttpResponseBadRequest('Missing content_type or object_pk field.') |
52 | 52 |
53 try: | 53 try: |
54 model = apps.get_model(*ctype.split('.', 1)) | 54 model = apps.get_model(*ctype.split('.', 1)) |
55 target = model.objects.get(pk=object_pk) | 55 target = model.objects.get(pk=object_pk) |
56 except TypeError: | 56 except LookupError: |
57 return HttpResponseBadRequest( | 57 return HttpResponseBadRequest( |
58 "Invalid content_type value: %r" % escape(ctype)) | 58 "Invalid content_type value: %r" % escape(ctype)) |
59 except AttributeError: | 59 except ValueError: |
60 return HttpResponseBadRequest( | 60 return HttpResponseBadRequest( |
61 "The given content-type %r does not resolve to a valid model." % \ | 61 "The given content-type %r does not resolve to a valid model." % \ |
62 escape(ctype)) | 62 escape(ctype)) |
63 except ObjectDoesNotExist: | 63 except ObjectDoesNotExist: |
64 return HttpResponseBadRequest( | 64 return HttpResponseBadRequest( |
65 "No object matching content-type %r and object PK %r exists." % \ | 65 "No object matching content-type %r and object PK %r exists." % \ |
66 (escape(ctype), escape(object_pk))) | 66 (escape(ctype), escape(object_pk))) |
67 except: | |
68 return HttpResponseBadRequest('Unexpected error') | |
67 | 69 |
68 # Can we comment on the target object? | 70 # Can we comment on the target object? |
69 if hasattr(target, 'can_comment_on'): | 71 if hasattr(target, 'can_comment_on'): |
70 if callable(target.can_comment_on): | 72 if callable(target.can_comment_on): |
71 can_comment_on = target.can_comment_on() | 73 can_comment_on = target.can_comment_on() |
108 """ | 110 """ |
109 This function handles the flagging of comments by users. This function should | 111 This function handles the flagging of comments by users. This function should |
110 be the target of an AJAX post. | 112 be the target of an AJAX post. |
111 """ | 113 """ |
112 if not request.user.is_authenticated(): | 114 if not request.user.is_authenticated(): |
113 return HttpResponse('Please login or register to flag a comment.') | 115 return HttpResponseForbidden('Please login or register to flag a comment.') |
114 | 116 |
115 id = request.POST.get('id', None) | 117 id = request.POST.get('id', None) |
116 if id is None: | 118 if id is None: |
117 return HttpResponseBadRequest('No id') | 119 return HttpResponseBadRequest('No id') |
118 | 120 |