2020-10-22 21:45:05 +02:00
|
|
|
|
from django.contrib import admin
|
|
|
|
|
from .models import *
|
|
|
|
|
from .api import create_app
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
from requests.exceptions import Timeout
|
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
|
|
2023-02-01 07:26:25 -05:00
|
|
|
|
|
2020-10-22 21:45:05 +02:00
|
|
|
|
# Register your models here.
|
|
|
|
|
@admin.register(MastodonApplication)
|
|
|
|
|
class MastodonApplicationModelAdmin(admin.ModelAdmin):
|
2023-01-11 19:11:31 -05:00
|
|
|
|
def add_view(self, request, form_url="", extra_context=None):
|
2020-10-22 21:45:05 +02:00
|
|
|
|
"""
|
|
|
|
|
Dirty code here, use POST['domain_name'] to pass error message to user.
|
|
|
|
|
"""
|
2023-01-11 19:11:31 -05:00
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if not request.POST.get("client_id") and not request.POST.get(
|
|
|
|
|
"client_secret"
|
|
|
|
|
):
|
2020-10-22 21:45:05 +02:00
|
|
|
|
# make the post data mutable
|
|
|
|
|
request.POST = request.POST.copy()
|
|
|
|
|
# (is_proxy xor proxy_to) or (proxy_to!=null and is_proxy=false)
|
2023-01-11 19:11:31 -05:00
|
|
|
|
if (
|
|
|
|
|
(
|
|
|
|
|
bool(request.POST.get("is_proxy"))
|
|
|
|
|
or bool(request.POST.get("proxy_to"))
|
|
|
|
|
)
|
|
|
|
|
and not (
|
|
|
|
|
bool(request.POST.get("is_proxy"))
|
|
|
|
|
and bool(request.POST.get("proxy_to"))
|
|
|
|
|
)
|
|
|
|
|
or (
|
|
|
|
|
not bool(request.POST.get("is_proxy"))
|
|
|
|
|
and bool(request.POST.get("proxy_to"))
|
|
|
|
|
)
|
|
|
|
|
):
|
|
|
|
|
request.POST["domain_name"] = _("请同时填写is_proxy和proxy_to。")
|
2020-10-22 21:45:05 +02:00
|
|
|
|
else:
|
2020-10-22 22:50:56 +02:00
|
|
|
|
if request.POST.get("is_proxy"):
|
2020-10-22 21:45:05 +02:00
|
|
|
|
try:
|
2023-01-11 19:11:31 -05:00
|
|
|
|
origin = MastodonApplication.objects.get(
|
|
|
|
|
domain_name=request.POST["proxy_to"]
|
|
|
|
|
)
|
2020-10-22 21:45:05 +02:00
|
|
|
|
# set proxy credentials to those of its original site
|
2023-01-11 19:11:31 -05:00
|
|
|
|
request.POST["app_id"] = origin.app_id
|
|
|
|
|
request.POST["client_id"] = origin.client_id
|
|
|
|
|
request.POST["client_secret"] = origin.client_secret
|
|
|
|
|
request.POST["vapid_key"] = origin.vapid_key
|
2020-10-22 21:45:05 +02:00
|
|
|
|
except ObjectDoesNotExist:
|
2023-01-11 19:11:31 -05:00
|
|
|
|
request.POST["domain_name"] = _("proxy_to所指域名不存在,请先添加原站点。")
|
2020-10-22 21:45:05 +02:00
|
|
|
|
else:
|
|
|
|
|
# create mastodon app
|
|
|
|
|
try:
|
2023-01-11 19:11:31 -05:00
|
|
|
|
response = create_app(request.POST.get("domain_name"))
|
2020-10-22 21:45:05 +02:00
|
|
|
|
except (Timeout, ConnectionError):
|
2023-01-11 19:11:31 -05:00
|
|
|
|
request.POST["domain_name"] = _("联邦网络请求超时。")
|
2020-10-22 21:45:05 +02:00
|
|
|
|
except Exception as e:
|
2023-01-11 19:11:31 -05:00
|
|
|
|
request.POST["domain_name"] = str(e)
|
2020-10-22 21:45:05 +02:00
|
|
|
|
else:
|
|
|
|
|
# fill the form with returned data
|
|
|
|
|
data = response.json()
|
|
|
|
|
if response.status_code != 200:
|
2023-01-11 19:11:31 -05:00
|
|
|
|
request.POST["domain_name"] = str(data)
|
2020-10-22 21:45:05 +02:00
|
|
|
|
else:
|
2023-01-11 19:11:31 -05:00
|
|
|
|
request.POST["app_id"] = data["id"]
|
|
|
|
|
request.POST["client_id"] = data["client_id"]
|
|
|
|
|
request.POST["client_secret"] = data["client_secret"]
|
|
|
|
|
request.POST["vapid_key"] = data["vapid_key"]
|
2020-10-22 21:45:05 +02:00
|
|
|
|
|
|
|
|
|
return super().add_view(request, form_url=form_url, extra_context=extra_context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(CrossSiteUserInfo)
|