Changeset View
Changeset View
Standalone View
Standalone View
films/views/api/comment.py
| Show All 11 Lines | |||||
| from films.models import AssetComment | from films.models import AssetComment | ||||
| @require_POST | @require_POST | ||||
| @login_required | @login_required | ||||
| def comment(request: HttpRequest, *, asset_pk: int) -> JsonResponse: | def comment(request: HttpRequest, *, asset_pk: int) -> JsonResponse: | ||||
| parsed_body = json.loads(request.body) | parsed_body = json.loads(request.body) | ||||
| reply_to_pk = None if parsed_body['reply_to'] is None else int(parsed_body['reply_to']) | reply_to_pk = int(parsed_body['reply_to']) if parsed_body.get('reply_to') else None | ||||
| message = assert_cast(str, parsed_body['message']) | message = assert_cast(str, parsed_body['message']) | ||||
| @transaction.atomic | @transaction.atomic | ||||
| def create_comment( | def create_comment( | ||||
| *, user_pk: int, asset_pk: int, message: str, reply_to_pk: Optional[int] | *, user_pk: int, asset_pk: int, message: str, reply_to_pk: Optional[int] | ||||
| ) -> Comment: | ) -> Comment: | ||||
| comment = Comment.objects.create(user_id=user_pk, message=message, reply_to_id=reply_to_pk) | comment = Comment.objects.create(user_id=user_pk, message=message, reply_to_id=reply_to_pk) | ||||
| AssetComment.objects.create(comment_id=comment.id, asset_id=asset_pk) | AssetComment.objects.create(comment_id=comment.id, asset_id=asset_pk) | ||||
| return comment | return comment | ||||
| comment = create_comment( | comment = create_comment( | ||||
| user_pk=request.user.pk, asset_pk=asset_pk, message=message, reply_to_pk=reply_to_pk | user_pk=request.user.pk, asset_pk=asset_pk, message=message, reply_to_pk=reply_to_pk | ||||
| ) | ) | ||||
| return JsonResponse(comment.to_dict()) | return JsonResponse(comment.to_dict()) | ||||