def create(self, request, *args, **kwargs): """ Create a new report, or if an identical report already exists, update the existing one. """ try: reading_id = request.data["reading"] existing_report = Report.objects.get( reading__id=reading_id, created_by=request.user ) logger.info( "User {} is updating their report on reading {}".format( request.user.username, request.data["reading"] ) ) serializer = ReportSerializer( existing_report, data=request.data, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data) except Report.DoesNotExist: logger.info( "User {} is creating report on reading {}".format( request.user.username, request.data["reading"] ) ) serializer = ReportSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save(created_by=self.request.user) return Response(serializer.data)
def create(self, request, *args, **kwargs): """ Create a new report, or if an identical report already exists, update the existing one. """ try: reading_id = request.data["reading"] existing_report = Report.objects.get(reading__id=reading_id, created_by=request.user) logger.info( f"User {request.user.username} is updating their report on reading {request.data['reading']}" ) serializer = ReportSerializer(existing_report, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data) except Report.DoesNotExist: logger.info( f"User {request.user.username} is creating report on reading {request.data['reading']}" ) serializer = ReportSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save(created_by=self.request.user) return Response(serializer.data)
async def save_message_and_ack(self, consumer_record): """Save and ack the uploaded kafka message.""" self.prefix = 'SAVING MESSAGE' if consumer_record.topic == QPC_TOPIC: try: missing_fields = [] self.upload_message = self.unpack_consumer_record(consumer_record) # rh_account is being deprecated so we use it as a backup if # account is not there rh_account = self.upload_message.get('rh_account') request_id = self.upload_message.get('request_id') url = self.upload_message.get('url') self.account_number = self.upload_message.get('account', rh_account) if not self.account_number: missing_fields.append('account') if not request_id: missing_fields.append('request_id') if not url: missing_fields.append('url') if missing_fields: raise QPCKafkaMsgException( format_message( self.prefix, 'Message missing required field(s): %s.' % ', '.join(missing_fields))) self.check_if_url_expired(url, request_id) try: uploaded_report = { 'upload_srv_kafka_msg': json.dumps(self.upload_message), 'account': self.account_number, 'request_id': request_id, 'state': Report.NEW, 'state_info': json.dumps([Report.NEW]), 'last_update_time': datetime.now(pytz.utc), 'arrival_time': datetime.now(pytz.utc), 'retry_count': 0 } report_serializer = ReportSerializer(data=uploaded_report) report_serializer.is_valid(raise_exception=True) report_serializer.save() MSG_UPLOADS.labels(account_number=self.account_number).inc() LOG.info(format_message( self.prefix, 'Upload service message saved with request_id: %s. Ready for processing.' % request_id)) await self.consumer.commit() except Exception as error: # pylint: disable=broad-except DB_ERRORS.inc() LOG.error(format_message( self.prefix, 'The following error occurred while trying to save and ' 'commit the message: %s' % error)) print_error_loop_event() except QPCKafkaMsgException as message_error: LOG.error(format_message( self.prefix, 'Error processing records. Message: %s, Error: %s' % (consumer_record, message_error))) await self.consumer.commit() else: LOG.debug(format_message( self.prefix, 'Message not on %s topic: %s' % (QPC_TOPIC, consumer_record)))
def report_list(request): if request.method == 'GET': report = Report.objects.order_by('-missing_date') serializer = ReportSerializer(report, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ReportSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def report_detail(request, pk): try: report = Report.objects.get(pk=pk) except Report.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = ReportSerializer(report) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = ReportSerializer(report, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': report.delete() return HttpResponse(status=204)