Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        data = request.data
        try:
            shake_id = kwargs.get('shake_id') or data.get('shake_id')
            data['shake_id'] = shake_id
            earthquake = Earthquake.objects.get(shake_id=shake_id)
            report = EarthquakeReport.objects.filter(earthquake=earthquake,
                                                     language=data['language'])
        except Earthquake.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if report:
            # cannot post report if it is already exists
            serializer = EarthquakeReportSerializer(report[0])
            return Response(serializer.data,
                            status=status.HTTP_400_BAD_REQUEST)

        serializer = EarthquakeReportSerializer(data=data)

        if serializer.is_valid():
            serializer.validated_data['earthquake'] = earthquake
            try:
                serializer.save()
                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)
            except (ValidationError, IntegrityError) as e:
                # This happens when simultaneuously two conn trying to save
                # the same unique_together fields (earthquake, language)
                # Should warn this to sentry
                LOGGER.warning(e.message)
                return Response(serializer.errors,
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
 def test_earthquake_report_serializer(self):
     report_dict = {
         'shake_id': u'20150619200628',
         'source_type': u'initial',
         'language': u'en',
         'report_pdf': File(
             open(
                 self.data_path(u'20150619200628-en.pdf'))),
         'report_image': File(
             open(
                 self.data_path(u'20150619200628-en.png'))),
         'report_thumbnail': File(
             open(
                 self.data_path(
                     u'20150619200628-thumb-en.png'))),
     }
     serializer = EarthquakeReportSerializer(data=report_dict)
     self.assertTrue(serializer.is_valid())
     earthquake = Earthquake.objects.get(
         shake_id=u'20150619200628', source_type=u'initial')
     serializer.validated_data['earthquake'] = earthquake
     serializer.save()
     report = EarthquakeReport.objects.get(
         earthquake__shake_id=u'20150619200628',
         earthquake__source_type=u'initial',
         language=u'en'
     )
     self.assertTrue(report)
     serializer = EarthquakeReportSerializer(report)
     self.assertEqual(serializer.data['shake_id'], u'20150619200628')
     self.assertEqual(serializer.data['source_type'], u'initial')
     self.assertEqual(serializer.data['language'], u'en')
Ejemplo n.º 3
0
 def put(self, request, shake_id=None, language=None):
     data = request.data
     try:
         if shake_id:
             data['shake_id'] = shake_id
             report = EarthquakeReport.objects.get(
                 earthquake__shake_id=shake_id, language=language)
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
     except EarthquakeReport.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
     # delete previous files
     new_report = deepcopy(report)
     new_report.pk = None
     report.delete()
     serializer = EarthquakeReportSerializer(new_report, data=data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        data = request.data
        try:
            shake_id = kwargs.get('shake_id') or data.get('shake_id')
            source_type = kwargs.get('source_type') or data.get('source_type')
            data['shake_id'] = shake_id
            data['source_type'] = source_type
            earthquake = Earthquake.objects.get(
                shake_id=shake_id,
                source_type=source_type)
            report = EarthquakeReport.objects.filter(
                earthquake=earthquake, language=data['language'])
        except Earthquake.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if report:
            # cannot post report if it is already exists
            serializer = EarthquakeReportSerializer(report[0])
            return Response(
                serializer.data, status=status.HTTP_400_BAD_REQUEST)

        serializer = EarthquakeReportSerializer(data=data)

        if serializer.is_valid():
            serializer.validated_data['earthquake'] = earthquake
            try:
                serializer.save()
                return Response(
                    serializer.data, status=status.HTTP_201_CREATED)
            except (ValidationError, IntegrityError) as e:
                # This happens when simultaneuously two conn trying to save
                # the same unique_together fields (earthquake, language)
                # Should warn this to sentry
                LOGGER.warning(e.message)
                return Response(
                    serializer.errors,
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
 def put(self, request, shake_id=None, source_type=None, language=None):
     data = request.data
     try:
         if shake_id and source_type and language:
             data['shake_id'] = shake_id
             data['source_type'] = source_type
             report = EarthquakeReport.objects.get(
                 earthquake__shake_id=shake_id,
                 earthquake__source_type=source_type,
                 language=language)
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
     except EarthquakeReport.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
     # delete previous files
     new_report = deepcopy(report)
     new_report.pk = None
     report.delete()
     serializer = EarthquakeReportSerializer(new_report, data=data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)