コード例 #1
0
ファイル: views.py プロジェクト: tmforum/tmffiware
    def create(self, request):
        try:
            # Extract SDR document from the HTTP request
            data = json.loads(request.body)
        except:
            # The usage document is not valid, so the state cannot be changed
            return build_response(request, 400, 'The request does not contain a valid JSON object')

        # Validate usage information
        response = None
        try:
            sdr_manager = SDRManager()
            sdr_manager.validate_sdr(data)
        except PermissionDenied as e:
            response = build_response(request, 403, unicode(e))
        except ValueError as e:
            response = build_response(request, 422, unicode(e))
        except:
            response = build_response(request, 500, 'The SDR document could not be processed due to an unexpected error')

        usage_client = UsageClient()
        if response is not None:
            # The usage document is not valid, change its state to Rejected
            usage_client.update_usage_state('Rejected', data)
        else:
            # The usage document is valid, change its state to Guided
            usage_client.update_usage_state('Guided', data)
            response = build_response(request, 200, 'OK')

        # Update usage document state
        return response
コード例 #2
0
    def create(self, request):
        try:
            # Extract SDR document from the HTTP request
            data = json.loads(request.body)
        except:
            # The usage document is not valid, so the state cannot be changed
            return build_response(
                request, 400,
                'The request does not contain a valid JSON object')

        # Validate usage information
        response = None
        sdr_manager = SDRManager()
        try:
            sdr_manager.validate_sdr(data)
        except PermissionDenied as e:
            response = build_response(request, 403, str(e))
        except ValueError as e:
            response = build_response(request, 422, str(e))
        except:
            response = build_response(
                request, 500,
                'The SDR document could not be processed due to an unexpected error'
            )

        usage_client = UsageClient()
        if response is not None:
            # The usage document is not valid, change its state to Rejected
            usage_client.update_usage_state(data['id'], 'Rejected')
        else:
            # The usage document is valid, change its state to Guided
            usage_client.update_usage_state(data['id'], 'Guided')
            sdr_manager.update_usage()
            response = build_response(request, 200, 'OK')

        # Update usage document state
        return response
コード例 #3
0
    def on_usage_refresh(self, asset, contract, order):
        if not self._model.pull_accounting:
            return

        pending_accounting, last_usage = self.get_pending_accounting(
            asset, contract, order)
        usage_template = {
            'type':
            'event',
            'status':
            'Received',
            'usageCharacteristic': [{
                'name': 'orderId',
                'value': order.order_id
            }, {
                'name': 'productId',
                'value': contract.product_id
            }],
            'relatedParty': [{
                'role': 'customer',
                'id': order.owner_organization.name,
                'href': order.owner_organization.get_party_url()
            }]
        }

        usage_client = UsageClient()
        for usage_record in pending_accounting:
            if 'date' not in usage_record or 'unit' not in usage_record or 'value' not in usage_record:
                raise PluginError(
                    'Invalid usage record, it must include date, unit and value'
                )

            # Generate a TMForum usage document for each usage record
            usage = deepcopy(usage_template)

            usage['date'] = usage_record['date']

            usage['usageSpecification'] = {
                'href': self._model.options['usage'][usage_record['unit']],
                'name': usage_record['unit']
            }

            usage['usageCharacteristic'].append({
                'name': 'unit',
                'value': usage_record['unit']
            })

            usage['usageCharacteristic'].append({
                'name':
                'correlationNumber',
                'value':
                contract.correlation_number
            })

            usage['usageCharacteristic'].append({
                'name': 'value',
                'value': usage_record['value']
            })

            usage_doc = usage_client.create_usage(usage)
            # All the  information is known so the document is directly created in Guided state
            usage_client.update_usage_state(usage_doc['id'], 'Guided')

            contract.correlation_number += 1
            order.save()

        if last_usage is not None:
            contract.last_usage = last_usage
            order.save()