views.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 organizations_ext.utils import is_organization_creation_open
  9. from users.serializers import SocialAppSerializer, UserSerializer
  10. from users.utils import is_user_registration_open
  11. try:
  12. from djstripe.settings import djstripe_settings
  13. except ImportError:
  14. pass
  15. class SettingsView(APIView):
  16. """Global configuration to pass to client"""
  17. permission_classes = [AllowAny]
  18. def get(self, request, *args, **kwargs):
  19. billing_enabled = settings.BILLING_ENABLED
  20. enable_user_registration = is_user_registration_open()
  21. enable_organization_creation = settings.ENABLE_ORGANIZATION_CREATION
  22. stripe_public_key = None
  23. if billing_enabled:
  24. stripe_public_key = djstripe_settings.STRIPE_PUBLIC_KEY
  25. social_apps = SocialAppSerializer(
  26. SocialApp.objects.all().order_by("name"),
  27. many=True,
  28. context={"request": request},
  29. ).data
  30. return Response(
  31. {
  32. "socialApps": social_apps,
  33. "billingEnabled": billing_enabled,
  34. "iPaidForGlitchTip": settings.I_PAID_FOR_GLITCHTIP,
  35. "enableUserRegistration": enable_user_registration,
  36. "enableOrganizationCreation": enable_organization_creation,
  37. "stripePublicKey": stripe_public_key,
  38. "plausibleURL": settings.PLAUSIBLE_URL,
  39. "plausibleDomain": settings.PLAUSIBLE_DOMAIN,
  40. "chatwootWebsiteToken": settings.CHATWOOT_WEBSITE_TOKEN,
  41. "sentryDSN": settings.SENTRY_FRONTEND_DSN,
  42. "sentryTracesSampleRate": settings.SENTRY_TRACES_SAMPLE_RATE,
  43. "environment": settings.ENVIRONMENT,
  44. "version": settings.GLITCHTIP_VERSION,
  45. "serverTimeZone": settings.TIME_ZONE,
  46. }
  47. )
  48. class APIRootView(APIView):
  49. """/api/0/ gives information about the server and current user"""
  50. def get(self, request, *args, **kwargs):
  51. user_data = None
  52. auth_data = None
  53. if request.user.is_authenticated:
  54. user_data = UserSerializer(instance=request.user).data
  55. if request.auth:
  56. auth_data = APITokenAuthScopesSerializer(instance=request.auth).data
  57. return Response(
  58. {
  59. "version": "0",
  60. "user": user_data,
  61. "auth": auth_data,
  62. }
  63. )
  64. def health(request):
  65. return HttpResponse("ok", content_type="text/plain")