@login_required()
def comment_form_view(
request: WSGIRequest, comment_pk: int, obj_pk: int, app_label: str, model_name: str
) -> HttpResponseRedirect:
has_app_permission_or_404(request.user, app_label, model_name, 'change')
if comment_pk == 0:
comment = get_object_or_null_obj(models.Comment, pk=comment_pk)
else:
comment = get_object_or_404(models.Comment, pk=comment_pk, user__id=request.user.pk)
obj = model_object_from_app_label(app_label, model_name, obj_pk)
if not hasattr(obj, 'add_comment'):
message = f'Object {obj} does not have the comment model mixin.'
raise Exception(message)
if request.method == 'POST':
form = CommentForm(request.POST, instance=comment)
if form.is_valid():
if comment_pk == 0:
obj.add_comment(user=request.user, information=form.cleaned_data['information'])
else:
comment.information = form.cleaned_data['information']
comment.is_edited = True
comment.save()
else:
show_form_errors(request, form)
return HttpResponseRedirect(safe_redirect_url(request))