hotpocket/services/backend/hotpocket_backend/apps/ui/views/integrations/android.py

48 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import annotations
import http
import logging
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from hotpocket_backend.apps.accounts.decorators import account_required
from hotpocket_backend.apps.ui.services.workflows import CreateSaveWorkflow
LOGGER = logging.getLogger(__name__)
@csrf_exempt
@account_required
def share_sheet(request: HttpRequest) -> HttpResponse:
LOGGER.debug('POST=`%s`', request.POST)
try:
assert request.user.is_anonymous is False, 'Login required'
assert 'text' in request.POST, 'Bad request: Missing `text`'
url = request.POST['text'].split('\n')[0].strip()
assert url != '', 'Bad request: Empty `text`'
return CreateSaveWorkflow().run(
request=request,
account=request.user,
url=url,
force_post_save=True,
)
except Exception as exception:
LOGGER.error(
'Unhandled exception: %s',
exception,
exc_info=exception,
)
return render(
request,
'ui/errors/internal_server_error.html',
# Returning 200 here to avoid browsers showing generic error pages.
status=http.HTTPStatus.OK,
)