def post(self, request): serializer = ProviderSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, provider_id=None): if provider_id is not None: provider = Provider.objects.get(id=provider_id) serializer = ProviderSerializer(provider, many=False) return Response(serializer.data) else: providers = Provider.objects.all() serializer = ProviderSerializer(providers, many=True) return Response(serializer.data)
def patch(self, request, provider_uuid): user = request.user data = request.DATA try: provider = CoreProvider.objects.get(cloudadministrator__user=user, uuid=provider_uuid) except CoreProvider.DoesNotExist: return failure_response(status.HTTP_404_NOT_FOUND, "The provider does not exist.") serializer = ProviderSerializer(provider, data=data, partial=True) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data)
def setUp(self): self.provider_attributes = { "drg_defintion": "test", "provider_id": 3453424, "name": "Test Health Provider", "street_address": "2337 Vargas Place", "city": "Santa Clara", "state": "CA", "zipcode": "95050", "hospital_referral": "loremispum", "total_discharges": "34", "avg_cov_charges": "$2300", "avg_total_payments": "$3200", "avg_medicare_payments": "$1500" } provider = Provider.objects.create(drg_defintion="test", provider_id=3453424, name="Test Health Provider", street_address="2337 Vargas Place", city="Santa Clara", state="CA", zipcode=95050, hospital_referral="loremispum", total_discharges="34", avg_cov_charges="2300", avg_total_payments="3200", avg_medicare_payments="1500") self.serializer = ProviderSerializer(instance=provider)
def setUp(self): # Have group name match the users name self.group = GroupWithDataFactory.create(name="test") user = self.group.leaders.first() self.providers = self.group.providers.all() self.provider_data = ProviderSerializer(self.providers).data self.client = APIClient() self.client.force_authenticate(user=user)
def create(self, request): provider_data = request.data provider = Provider.objects.create(name=provider_data['name'], description = provider_data['description']) provider.save() serializer = ProviderSerializer(provider) logger.debug(f'Provider {serializer.instance} was created') logger.info(f'Provider {serializer.instance} was created') return Response(serializer.data)
def patch(self, request, provider_uuid): user = request.user data = request.DATA try: provider = CoreProvider.objects.get( cloudadministrator__user=user, uuid=provider_uuid) except CoreProvider.DoesNotExist: return failure_response( status.HTTP_404_NOT_FOUND, "The provider does not exist.") serializer = ProviderSerializer(provider, data=data, partial=True) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data)
def update(self, request, pk): provider = Provider.objects.get(id=pk) provider.name = request.data['name'] provider.description = request.data['description'] provider.save() serializer = ProviderSerializer(provider) logger.debug(f'Provider {serializer.instance} was updated') logger.info(f'Provider {serializer.instance} was updated') return Response(serializer.data)
def setUp(self): # Have group name match the users name self.group = GroupWithDataFactory.create(name="test") #TODO: Create IDENTITIES to use 'self.providers' user = self.group.leaders.first() provider_ids = self.group.identities.filter( only_current_provider(), provider__active=True).values_list('provider', flat=True) self.providers = Provider.objects.filter(id__in=provider_ids) self.provider_data = ProviderSerializer(self.providers, many=True).data self.client = APIClient() self.client.force_authenticate(user=user)
def get(self, request): """ List all providers accessible by request user """ username = request.user.username group = Group.objects.get(name=username) try: providers = group.providers.filter(active=True, end_date=None).order_by('id') except CoreProvider.DoesNotExist: return failure_response(status.HTTP_404_NOT_FOUND, "The provider does not exist.") serialized_data = ProviderSerializer(providers, many=True).data return Response(serialized_data)
def get(self, request): """ Authentication Required, list of Providers on your account. """ username = request.user.username group = Group.objects.get(name=username) try: providers = group.providers.filter(active=True, end_date=None).order_by('id') except CoreProvider.DoesNotExist: return failure_response(status.HTTP_404_NOT_FOUND, "The provider does not exist.") serialized_data = ProviderSerializer(providers, many=True).data return Response(serialized_data)
def get(self, request, provider_uuid): """ Authentication Required, return specific provider. """ username = request.user.username group = Group.objects.get(name=username) try: provider = group.providers.get(uuid=provider_uuid, active=True, end_date=None) except CoreProvider.DoesNotExist: return failure_response( status.HTTP_404_NOT_FOUND, "The provider does not exist.") serialized_data = ProviderSerializer(provider).data return Response(serialized_data)
def get(self, request, provider_id): """ return provider if accessible by request user """ username = request.user.username group = Group.objects.get(name=username) try: provider = group.providers.get(id=provider_id, active=True, end_date=None) except CoreProvider.DoesNotExist: return failure_response(status.HTTP_404_NOT_FOUND, "The provider does not exist.") serialized_data = ProviderSerializer(provider).data return Response(serialized_data)
def provider_list(request): if request.method == 'GET': provider = Provider.objects.all() serializer = ProviderSerializer(provider, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = ProviderSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): """ Authentication Required, list of Providers on your account. """ username = request.user.username group = Group.objects.get(name=username) try: provider_ids = group.identities.filter( only_current_provider(), provider__active=True).values_list('provider', flat=True) providers = CoreProvider.objects.filter( id__in=provider_ids).order_by('id') except CoreProvider.DoesNotExist: return failure_response(status.HTTP_404_NOT_FOUND, "The provider does not exist.") serialized_data = ProviderSerializer(providers, many=True).data return Response(serialized_data)
def get(self, request, provider_uuid): """ Authentication Required, return specific provider. """ username = request.user.username group = Group.objects.get(name=username) try: provider_ids = group.identities.filter( only_current_provider(), provider__active=True).values_list('provider', flat=True) provider = CoreProvider.objects.get(uuid=provider_uuid, id__in=provider_ids) except CoreProvider.DoesNotExist: return failure_response(status.HTTP_404_NOT_FOUND, "The provider does not exist.") serialized_data = ProviderSerializer(provider).data return Response(serialized_data)
def create_provider(self, request, *args, **kwargs): """ Customized "create provider" API call. This is distinct from the built-in 'POST to the list URL' call because we need it to work for users who are not authenticated (otherwise, they can't register). Expected data is basically the same as for creating a provider, except that in place of the 'user' field, there should be an 'email' and 'password' field. They'll be used to create a new user, send them an activation email, and create a provider using that user. """ with atomic( ): # If we throw an exception anywhere in here, rollback all changes serializer = CreateProviderSerializer(data=request.data) serializer.is_valid(raise_exception=True) # Create User user = get_user_model().objects.create_user( email=request.data['email'], password=request.data['password'], is_active=False) provider_group, _ = Group.objects.get_or_create(name='Providers') user.groups.add(provider_group) # Create Provider data = dict(request.data, user=user.get_api_url()) serializer = ProviderSerializer(data=data, context={'request': request}) serializer.is_valid(raise_exception=True) serializer.save() # returns provider if we need it headers = self.get_success_headers(serializer.data) # If we got here without blowing up, send the user's activation email user.send_activation_email(request.site, request, data['base_activation_link']) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def provider_detail(request, pk): try: provider = Provider.objects.get(pk=pk) except Provider.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': provider_detail = Provider.objects.get(pk=pk) serializer = ProviderSerializer(provider_detail) return Response(serializer.data) elif request.method == 'PUT': serializer = ProviderSerializer(provider, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': provider.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def retrieve(self, request, pk=None): queryset = Provider.objects.all() user = get_object_or_404(queryset, pk=pk) serializer = ProviderSerializer(user) return Response(serializer.data)
def list(self, request): queryset = Provider.objects.all() serializer = ProviderSerializer(queryset, many=True) return Response(serializer.data)