Beispiel #1
0
 def _update_license(self, request, license_id, partial=False):
     data = request.DATA
     license = get_object_or_404(CoreLicense, id=license_id)
     serializer = LicenseSerializer(
         license, context={"request": request},
         data=data, partial=partial)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def _update_license(self, request, license_id, partial=False):
     data = request.data
     license = get_object_or_404(CoreLicense, id=license_id)
     serializer = LicenseSerializer(license,
                                    context={"request": request},
                                    data=data,
                                    partial=partial)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
 def get(self, request, license_id):
     license = get_object_or_404(CoreLicense, id=license_id)
     serialized_data = LicenseSerializer(license,
                                         context={
                                             "request": request
                                         }).data
     return Response(serialized_data)
Beispiel #4
0
 def get(self, request):
     user = request.user
     licenses = CoreLicense.objects.filter(created_by=user)
     serialized_data = LicenseSerializer(licenses,
                                         many=True,
                                         context={
                                             "request": request
                                         }).data
     return Response(serialized_data)
Beispiel #5
0
 def get(self, request, provider_uuid, identity_uuid, machine_id):
     """
     Lookup the machine information
     (Lookup using the given provider/identity)
     Update on server (If applicable)
     """
     core_machine = ProviderMachine.objects.filter(
         provider__uuid=provider_uuid, identifier=machine_id)
     if not core_machine:
         return failure_response(
             status.HTTP_400_BAD_REQUEST,
             "Machine id %s does not exist" % machine_id)
     core_machine = core_machine.get()
     licenses = core_machine.licenses.all()
     serialized_data = LicenseSerializer(licenses, many=True).data
     return Response(serialized_data, status=status.HTTP_200_OK)
Beispiel #6
0
    def post(self, request, provider_uuid, identity_uuid, machine_id):
        """
        TODO: Determine who is allowed to edit machines besides
        core_machine.owner
        """
        data = request.data

        logger.info('data = %s' % request.data)
        core_machine = ProviderMachine.objects.filter(
            provider__uuid=provider_uuid, identifier=machine_id
        )
        if not core_machine:
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                "Machine id %s does not exist" % machine_id
            )

        core_machine = core_machine.get()
        if core_machine.instance_source.created_by == request.user:
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                "You are NOT the owner of Machine id=%s " % machine_id
            )

        if 'licenses' not in data \
                or not isinstance(data['licenses'], list):
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                "Licenses missing from data. Expected a list of License IDs"
                " ex:[1,2,3,]"
            )

        licenses = []
        # Out with the old
        core_machine.licenses.all().delete()
        for license_id in data['licenses']:
            license = License.objects.get(id=license_id)
            # In with the new
            core_machine.licenses.add(license)
        # Return the new set.
        licenses = core_machine.licenses.all()
        logger.info('licenses = %s' % licenses)
        serialized_data = LicenseSerializer(licenses, many=True).data
        return Response(serialized_data, status=status.HTTP_202_ACCEPTED)