Ejemplo n.º 1
0
    def post(self, request):
        user = request.user
        data = request.data
        # Now, we post the data directly to OSM.
        try:
            responses = []
            facilities_data = data['healthsites']
            for facility_data in facilities_data:
                # Verify data uploader and owner/collector
                is_valid, message = \
                    verify_user(user, facility_data['osm_user'])
                if not is_valid:
                    return HttpResponseForbidden(message)

                # Validate data
                validate_osm_data(facility_data)

            for facility_data in facilities_data:
                # Map Healthsites tags to OSM tags
                mapping_file_path = ABS_PATH('api', 'fixtures', 'mapping.yml')
                facility_data['tag'] = convert_to_osm_tag(
                    mapping_file_path, facility_data['tag'], 'node')

                # Push data to OSM
                response = create_osm_node(user, facility_data)
                responses.append(response)

            return Response(responses)

        except Exception as e:
            return HttpResponseBadRequest('%s' % e)
Ejemplo n.º 2
0
def create_osm_node_by_data(data, user, duplication_check=True):
    """ Create data based on data and user"""
    # Now, we post the data directly to OSM.
    try:
        # Split osm and extension attribute
        osm_attr, locality_attr = split_osm_and_extension_attr(data['tag'])
        data['tag'] = osm_attr

        validate_osm_data(data, duplication_check=duplication_check)

        # Map Healthsites tags to OSM tags
        mapping_file_path = ABS_PATH('api', 'fixtures', 'mapping.yml')
        data['tag'] = convert_to_osm_tag(mapping_file_path, data['tag'],
                                         'node')

        # Push data to OSM
        response = create_osm_node(user, data)

        # create pending index
        create_pending_update('node', response['id'], data['tag']['name'],
                              user, response['version'])

        save_extensions('node', response['id'], locality_attr)
        return response

    except Exception as e:
        create_pending_review(user, data, '%s' % e)
        output = {
            'error': '%s' % e,
            'payload': data,
        }
        return output
Ejemplo n.º 3
0
    def validate_data(self):
        """Validate parsed data based on healthsites rules.

        :return: Flag indicating whether the data is valid or not.
        :rtype: bool
        """
        self._validation_status['total'] = len(self._parsed_data)
        for row_number, data in enumerate(self._parsed_data):
            is_valid = True
            validation_status = {'is_valid': is_valid, 'message': 'Valid'}

            # check 2nd row based on the hxl
            if row_number == 0 and 'lat' in data and '#' in data['lat']:
                mapping_file_path = ABS_PATH('api', 'fixtures',
                                             'hxl_tags.json')
                _file = open(mapping_file_path, 'r')
                hxl_tag = json.loads(_file.read())
                _file.close()

                # get the newest hxl tag
                all_tag = copy.deepcopy(data['tag'])
                all_tag['lon'] = data['lon']
                all_tag['lat'] = data['lat']

                for tag, hxl in all_tag.items():
                    try:
                        hxl_tag[tag]
                    except KeyError:
                        hxl_tag[tag] = hxl
                _file = open(mapping_file_path, 'w')
                _file.write(json.dumps(hxl_tag, indent=4))
                _file.close()
                continue

            # Set default user to data loader author
            user = self.data_loader.author

            # Split osm and extension attribute
            osm_attr, locality_attr = split_osm_and_extension_attr(data['tag'])
            data['tag'] = osm_attr
            data['tag'].update(locality_attr)

            # Verify data uploader and owner/collector if the API is being used
            # for uploading data from other osm user.
            if not data.get('osm_user'):
                data['osm_user'] = user.username

            if user.username != data.get('osm_user'):
                is_valid, message = verify_user(user,
                                                data['osm_user'],
                                                ignore_uploader_staff=True)
                validation_status.update({
                    'is_valid': is_valid,
                    'message': message
                })
                if is_valid:
                    try:
                        _ = get_object_or_404(User, username=data['osm_user'])
                    except Http404:
                        message = 'User %s is not exist.' % data['osm_user']
                        validation_status.update({
                            'is_valid': False,
                            'message': message
                        })

            if is_valid:
                try:
                    # Validate data
                    is_valid = validate_osm_data(data)
                    validation_status.update({
                        'is_valid': is_valid,
                        'message': 'OSM data is valid.'
                    })
                except Exception as e:
                    validation_status.update({
                        'is_valid': False,
                        'message': '%s' % e
                    })

            self._validation_status['status'][row_number +
                                              1] = validation_status
            if self.is_valid() and row_number + 1 == len(self._parsed_data):
                # prepare for the next step, upload data
                self._validation_status['count'] = row_number
            else:
                self._validation_status['count'] = row_number + 1

            self._validation_status['summary'] = (
                'Validation failed. Please see the status detail for more '
                'information.' if not self.is_valid() else '')

            # write status to progress file
            f = open(self.progress_file, 'w+')
            f.write(json.dumps(self._validation_status))
            f.close()

        del self._parsed_data[0]
        return False not in ([
            status['is_valid']
            for status in self._validation_status['status'].values()
        ])
Ejemplo n.º 4
0
    def post(self, request):
        user = request.user
        data = copy.deepcopy(request.data)

        if user.username in settings.TEST_USERS:
            raise Exception('Create osm : {}'.format(
                json.dumps({
                    'payload': data,
                    'user': user.username
                })))

        # Now, we post the data directly to OSM.
        try:
            # Split osm and extension attribute
            osm_attr, locality_attr = split_osm_and_extension_attr(data['tag'])
            data['tag'] = osm_attr

            # Verify data uploader and owner/collector if the API is being used
            # for uploading data from other osm user.
            if request.user.is_staff and request.GET.get('review', None):
                data['osm_user'] = get_pending_review(
                    request.GET.get('review')).uploader.username

            if data.get('osm_user'):
                is_valid, message = verify_user(user, data['osm_user'])
                if not is_valid:
                    return HttpResponseForbidden(message)
                else:
                    try:
                        user = get_object_or_404(User,
                                                 username=data['osm_user'])
                    except Http404:
                        message = 'User %s is not exist.' % data['osm_user']
                        return HttpResponseForbidden(message)

            duplication_check = request.GET.get('duplication-check', True)
            if duplication_check == 'false':
                duplication_check = False
            validate_osm_data(data, duplication_check=duplication_check)

            # Map Healthsites tags to OSM tags
            mapping_file_path = ABS_PATH('api', 'fixtures', 'mapping.yml')
            data['tag'] = convert_to_osm_tag(mapping_file_path, data['tag'],
                                             'node')

            # Push data to OSM
            response = create_osm_node(user, data)

            # create pending index
            create_pending_update('node', response['id'], data['tag']['name'],
                                  user, response['version'])

            save_extensions('node', response['id'], locality_attr)

            if request.GET.get('review', None):
                delete_pending_review(request.GET.get('review', None))
            return Response(response)

        except Exception as e:
            if not request.GET.get('review', None):
                if user != request.user:
                    create_pending_review(user, request.data, '%s' % e)
            else:
                try:
                    update_pending_review(request.GET.get('review', None),
                                          request.data, '%s' % e)
                except Exception as e:
                    return HttpResponseBadRequest('%s' % e)
            output = {
                'error': '%s' % e,
                'payload': request.data,
            }
            return HttpResponseBadRequest('%s' % json.dumps(output))
Ejemplo n.º 5
0
    def validate_data(self):
        """Validate parsed data based on healthsites rules.

        :return: Flag indicating whether the data is valid or not.
        :rtype: bool
        """
        self._validation_status['total'] = len(self._parsed_data)
        for row_number, data in enumerate(self._parsed_data):
            is_valid = True
            validation_status = {'is_valid': is_valid, 'message': 'Valid'}

            # Set default user to data loader author
            user = self.data_loader.author

            # Split osm and extension attribute
            osm_attr, locality_attr = split_osm_and_extension_attr(data['tag'])
            data['tag'] = osm_attr

            # Verify data uploader and owner/collector if the API is being used
            # for uploading data from other osm user.
            if not data.get('osm_user'):
                data['osm_user'] = user.username

            if user.username != data.get('osm_user'):
                is_valid, message = verify_user(user,
                                                data['osm_user'],
                                                ignore_uploader_staff=True)
                validation_status.update({
                    'is_valid': is_valid,
                    'message': message
                })
                if is_valid:
                    try:
                        _ = get_object_or_404(User, username=data['osm_user'])
                    except Http404:
                        message = 'User %s is not exist.' % data['osm_user']
                        validation_status.update({
                            'is_valid': False,
                            'message': message
                        })

            if is_valid:
                try:
                    # Validate data
                    is_valid = validate_osm_data(data)
                    validation_status.update({
                        'is_valid': is_valid,
                        'message': 'OSM data is valid.'
                    })
                except Exception as e:
                    validation_status.update({
                        'is_valid': False,
                        'message': '%s' % e
                    })

            self._validation_status['status'][row_number +
                                              1] = validation_status
            if self.is_valid() and row_number + 1 == len(self._parsed_data):
                # prepare for the next step, upload data
                self._validation_status['count'] = row_number
            else:
                self._validation_status['count'] = row_number + 1

            self._validation_status['summary'] = (
                'Validation failed. Please see the status detail for more '
                'information.' if not self.is_valid() else '')

            # write status to progress file
            f = open(self.progress_file, 'w+')
            f.write(json.dumps(self._validation_status))
            f.close()

        return False not in ([
            status['is_valid']
            for status in self._validation_status['status'].values()
        ])
Ejemplo n.º 6
0
    def post(self, request, osm_type, osm_id):
        data = copy.deepcopy(request.data)
        user = request.user

        # delete uuid, because it is not editable
        try:
            del data['tag']['uuid']
        except KeyError:
            pass

        # Now, we post the data directly to OSM.
        try:
            if osm_type == 'node':
                osm_function = update_osm_node
            elif osm_type == 'way':
                osm_function = update_osm_way
            else:
                # For now, we only support Node
                return HttpResponseBadRequest(
                    '%s is not supported as osm type' % osm_type)
            locality = self.getLocalityOsm(osm_type, osm_id)
            data['id'] = osm_id
            data['type'] = osm_type
            data['version'] = locality.changeset_version

            # Verify data uploader and owner/collector if the API is being
            # used for uploading data from other osm user.
            if request.user.is_staff and request.GET.get('review', None):
                data['osm_user'] = get_pending_review(
                    request.GET.get('review')).uploader.username

            if data.get('osm_user'):
                is_valid, message = verify_user(user, data['osm_user'])
                if not is_valid:
                    return HttpResponseForbidden(message)
                else:
                    try:
                        user = get_object_or_404(User,
                                                 username=data['osm_user'])
                    except Http404:
                        message = 'User %s is not exist.' % data['osm_user']
                        return HttpResponseForbidden(message)

            # Validate data
            validate_osm_data(data, duplication_check=False)

            # Map Healthsites tags to OSM tags
            mapping_file_path = ABS_PATH('api', 'fixtures', 'mapping.yml')
            data['tag'] = convert_to_osm_tag(mapping_file_path, data['tag'],
                                             osm_type)

            # Push data to OSM
            response = osm_function(user, data)

            create_pending_update(osm_type, response['id'],
                                  data['tag']['name'], user,
                                  response['version'])
            if request.GET.get('review', None):
                delete_pending_review(request.GET.get('review', None))
            return Response(response)

        except KeyError as e:
            return HttpResponseBadRequest('%s is needed' % e)
        except Exception as e:
            if not request.GET.get('review', None):
                if user != request.user:
                    create_pending_review(user, request.data, '%s' % e)
            else:
                try:
                    update_pending_review(request.GET.get('review', None),
                                          request.data, '%s' % e)
                except Exception as e:
                    return HttpResponseBadRequest('%s' % e)
            output = {
                'error': '%s' % e,
                'payload': request.data,
            }
            return HttpResponseBadRequest('%s' % json.dumps(output))
        except (LocalityOSMNode.DoesNotExist, LocalityOSMNode.DoesNotExist):
            raise Http404()