views.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from django.conf import settings
  2. from django.http import HttpResponse
  3. from rest_framework.response import Response
  4. from rest_framework.views import APIView
  5. from rest_framework.permissions import AllowAny
  6. from allauth.socialaccount.models import SocialApp
  7. from users.utils import is_user_registration_open
  8. from users.serializers import SocialAppSerializer, UserSerializer
  9. from api_tokens.serializers import APITokenAuthScopesSerializer
  10. try:
  11. from djstripe.settings import STRIPE_PUBLIC_KEY
  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, format=None):
  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 = STRIPE_PUBLIC_KEY
  23. social_apps = SocialAppSerializer(SocialApp.objects.all().order_by('name'), many=True).data
  24. return Response(
  25. {
  26. "socialApps": social_apps,
  27. "billingEnabled": billing_enabled,
  28. "iPaidForGlitchTip": settings.I_PAID_FOR_GLITCHTIP,
  29. "enableUserRegistration": enable_user_registration,
  30. "stripePublicKey": stripe_public_key,
  31. "matomoURL": settings.MATOMO_URL,
  32. "matomoSiteId": settings.MATOMO_SITE_ID,
  33. "chatwootWebsiteToken": settings.CHATWOOT_WEBSITE_TOKEN,
  34. "sentryDSN": settings.SENTRY_FRONTEND_DSN,
  35. "sentryTracesSampleRate": settings.SENTRY_TRACES_SAMPLE_RATE,
  36. "environment": settings.ENVIRONMENT,
  37. "version": settings.GLITCHTIP_VERSION,
  38. }
  39. )
  40. class APIRootView(APIView):
  41. """ /api/0/ gives information about the server and current user """
  42. def get(self, request, format=None):
  43. user_data = None
  44. auth_data = None
  45. if request.user.is_authenticated:
  46. user_data = UserSerializer(instance=request.user).data
  47. if request.auth:
  48. auth_data = APITokenAuthScopesSerializer(instance=request.auth).data
  49. return Response({"version": "0", "user": user_data, "auth": auth_data,})
  50. def health(request):
  51. return HttpResponse("ok", content_type="text/plain")