views.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from allauth.socialaccount.models import SocialApp
  2. from django.conf import settings
  3. from django.http import HttpResponse
  4. from rest_framework.permissions import AllowAny
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from api_tokens.serializers import APITokenAuthScopesSerializer
  8. from users.serializers import SocialAppSerializer, UserSerializer
  9. from users.utils import is_user_registration_open
  10. try:
  11. from djstripe.settings import djstripe_settings
  12. except ImportError:
  13. pass
  14. class SettingsView(APIView):
  15. """ Global configuration to pass to client """
  16. permission_classes = [AllowAny]
  17. def get(self, request, *args, **kwargs):
  18. billing_enabled = settings.BILLING_ENABLED
  19. enable_user_registration = is_user_registration_open()
  20. stripe_public_key = None
  21. if billing_enabled:
  22. stripe_public_key = djstripe_settings.STRIPE_PUBLIC_KEY
  23. social_apps = SocialAppSerializer(
  24. SocialApp.objects.all().order_by("name"),
  25. many=True,
  26. context={"request": request},
  27. ).data
  28. return Response(
  29. {
  30. "socialApps": social_apps,
  31. "billingEnabled": billing_enabled,
  32. "iPaidForGlitchTip": settings.I_PAID_FOR_GLITCHTIP,
  33. "enableUserRegistration": enable_user_registration,
  34. "stripePublicKey": stripe_public_key,
  35. "plausibleURL": settings.PLAUSIBLE_URL,
  36. "plausibleDomain": settings.PLAUSIBLE_DOMAIN,
  37. "chatwootWebsiteToken": settings.CHATWOOT_WEBSITE_TOKEN,
  38. "sentryDSN": settings.SENTRY_FRONTEND_DSN,
  39. "sentryTracesSampleRate": settings.SENTRY_TRACES_SAMPLE_RATE,
  40. "environment": settings.ENVIRONMENT,
  41. "version": settings.GLITCHTIP_VERSION,
  42. }
  43. )
  44. class APIRootView(APIView):
  45. """ /api/0/ gives information about the server and current user """
  46. def get(self, request, *args, **kwargs):
  47. user_data = None
  48. auth_data = None
  49. if request.user.is_authenticated:
  50. user_data = UserSerializer(instance=request.user).data
  51. if request.auth:
  52. auth_data = APITokenAuthScopesSerializer(instance=request.auth).data
  53. return Response({"version": "0", "user": user_data, "auth": auth_data,})
  54. def health(request):
  55. return HttpResponse("ok", content_type="text/plain")