views.py 2.7 KB

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