views.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "serverTimeZone": settings.TIME_ZONE,
  43. }
  44. )
  45. class APIRootView(APIView):
  46. """ /api/0/ gives information about the server and current user """
  47. def get(self, request, *args, **kwargs):
  48. user_data = None
  49. auth_data = None
  50. if request.user.is_authenticated:
  51. user_data = UserSerializer(instance=request.user).data
  52. if request.auth:
  53. auth_data = APITokenAuthScopesSerializer(instance=request.auth).data
  54. return Response({"version": "0", "user": user_data, "auth": auth_data,})
  55. def health(request):
  56. return HttpResponse("ok", content_type="text/plain")