def upload_parties(self, api, party_name, party_type, attributes=None):
        """Upload party data.

        :param api: Api url to upload party
        :type api: str

        :param party_name: Party name
        :type party_name: str

        :param party_type: Party type
        :type party_type: str

        :param attributes: Project-specific attributes that are defined
                           through the project's questionnaire
        :type attributes: dict
        """
        post_data = {
            'name': party_name,
            'type': party_type
        }

        if attributes:
            post_data['attributes'] = attributes

        connector = ApiConnect(get_url_instance() + api)
        status, result = connector.patch_json(Utilities.json_dumps(post_data))

        if status:
            self.set_status(
                self.tr('Party updated.')
            )
        else:
            self.set_status(
                Utilities.extract_error_detail(result)
            )
    def upload_relationship(self, api, relationship_type, attributes=None):
        """Upload relationship data.

        :param api: Api url to upload party
        :type api: str

        :param relationship_type: Relationship type
        :type relationship_type: str

        :param attributes: Project-specific attributes that are defined
                           through the project's questionnaire
        :type attributes: dict
        """
        post_data = {
            'tenure_type': relationship_type,
        }

        if attributes:
            post_data['attributes'] = attributes

        connector = ApiConnect(get_url_instance() + api)
        status, result = connector.patch_json(Utilities.json_dumps(post_data))

        if status:
            self.set_status(
                self.tr('Relationship updated.')
            )
        else:
            self.set_status(
                Utilities.extract_error_detail(result)
            )
Ejemplo n.º 3
0
    def upload_parties(self):
        """Upload party from this project."""
        self.set_status(tr('Uploading parties'))

        party = 0

        # reset progress bar
        current_progress = 0
        self.set_progress_bar(current_progress)
        total_layer = len(self.data['locations']['features'])
        progress_block = 100 / total_layer

        post_url = self._url_post_parties()

        if not post_url:
            # Project is not uploaded
            return

        for layer in self.data['locations']['features']:
            if 'party_name' in layer['fields'] and \
                            'party_type' in layer['fields']:
                post_data = {
                    'name': layer['fields']['party_name'],
                    'type': layer['fields']['party_type']
                }

                post_data = Utilities.json_dumps(post_data)

                connector = ApiConnect(get_url_instance() + post_url)
                status, result = self._call_json_post(connector, post_data)

                if status:
                    party += 1
                    try:
                        result_dict = result
                        if 'id' in result_dict:
                            layer['party_id'] = result_dict['id']
                    except ValueError as e:
                        LOGGER.exception('message')
                else:
                    self.set_progress_bar(0)
                    self.set_status(Utilities.extract_error_detail(result))
            else:
                self.set_status(tr('No party attributes found'))
            current_progress += progress_block
            self.set_progress_bar(current_progress)

        if party == 0:
            self.set_status(tr('Not uploading any party'))
        else:
            self.set_status(
                tr('Finished uploading {party} party'.format(party=party)))

        self.set_progress_bar(100)
Ejemplo n.º 4
0
    def upload_project(self):
        """Upload project to cadasta."""
        # check requirement attributes
        if self.check_requirement():
            return

        self.set_status(tr('Uploading project'))
        self.current_progress = 0
        self.set_progress_bar(self.current_progress)

        post_data = {
            'name': self.data['project_name'],
            'description': self.data['description'],
            'extent': self.data['extent'],
            'access': 'private' if self.data['private'] else 'public',
            'contacts': self.data['contacts']
        }

        if self.data['project_url']:
            post_data['urls'] = [self.data['project_url']]

        post_url = '/api/v1/organizations/%s/projects/' % (
            self.data['organisation']['slug'])

        post_data = Utilities.json_dumps(post_data)

        connector = ApiConnect(get_url_instance() + post_url)
        status, result = self._call_json_post(connector, post_data)

        self.set_progress_bar(self.current_progress + self.upload_increment)
        self.set_status(tr('Finished uploading project'))

        if status:
            self.project_upload_result = result
            upload_questionnaire_attribute = False
            # create questionnaire first
            # after creating location, questionnaire is blocked
            if self.data['questionnaire']:
                upload_questionnaire_attribute = True
                self.upload_questionnaire_project()
            total_locations = len(self.data['locations']['features'])
            if total_locations > 0:
                self.upload_locations(upload_questionnaire_attribute)
                self.upload_parties()
                self.upload_relationships()
            self.rerender_saved_layer()
            self.set_progress_bar(100)
        else:
            self.set_progress_bar(0)
            self.set_status(Utilities.extract_error_detail(result))

        self.set_status(tr('Finished'))
        self.parent.close()
Ejemplo n.º 5
0
    def upload_relationships(self):
        """Upload relationships attribute to cadasta."""
        self.set_status(tr('Uploading relationship'))

        # reset progress bar
        current_progress = 0
        self.set_progress_bar(current_progress)
        total_layer = len(self.data['locations']['features'])
        progress_block = 100 / total_layer

        relationship = 0

        url = self._url_post_relationships()

        for layer in self.data['locations']['features']:

            if 'relationship_type' in layer['fields'] and \
                            'spatial_id' in layer and \
                            'party_id' in layer:

                post_data = {
                    'tenure_type': layer['fields']['relationship_type'],
                    'spatial_unit': layer['spatial_id'],
                    'party': layer['party_id']
                }

                post_data = Utilities.json_dumps(post_data)

                connector = ApiConnect(get_url_instance() + url)
                status, result = self._call_json_post(connector, post_data)

                if status:
                    relationship += 1
                else:
                    self.set_progress_bar(0)
                    self.set_status(Utilities.extract_error_detail(result))
            else:
                self.set_status(tr('No relationship attributes found'))

            current_progress += progress_block
            self.set_progress_bar(current_progress)

        if relationship == 0:
            self.set_status(tr('Not uploading any relationship'))
        else:
            self.set_status(
                tr('Finished uploading {num} relationship'.format(
                    num=relationship)))

        self.set_progress_bar(100)
    def update_project(self):
        """Update a basic project information in an organization."""
        step2 = self.parent.step_project_update02

        contact_item_list = step2.project_contact_list.selectedItems()
        contacts = []
        for contact_item in contact_item_list:
            contact = contact_item.data(Qt.UserRole)
            contact_data = {}
            if contact.name:
                contact_data['name'] = contact.name
            if contact.phone:
                contact_data['tel'] = contact.phone
            if contact.email:
                contact_data['email'] = contact.email
            contacts.append(contact_data)

        access = 'private' if step2.access_checkbox.isChecked() else 'public'
        post_data = {
            'name': step2.project_name_text.displayText(),
            'description': step2.project_desc_text.toPlainText(),
            'access': access,
            'contacts': contacts
        }

        if step2.project_url_text.displayText():
            post_data['urls'] = [
                step2.project_url_text.displayText()
            ]

        status, response = step2.send_update_request(post_data)
        if status:
            Utilities.update_project_basic_information(
                information=response,
                vlayers=self.vlayers
            )
            self.set_status(
                self.tr('Update success')
            )
        else:
            self.set_status(
                Utilities.extract_error_detail(response)
            )
Ejemplo n.º 7
0
    def check_requirement(self):
        """Checking attributes that required.

        :return: Is missed
        :rtype: bool
        """
        requirement_miss = False
        for location in self.data['locations']['features']:
            try:
                location['fields']['location_type']
            except KeyError:
                self.set_progress_bar(0)
                self.set_status(
                    Utilities.extract_error_detail(
                        tr('Location_type is not found in attribute. '
                           'Please update before uploading again.')))
                requirement_miss = True
                break
        return requirement_miss
    def add_new_locations(self, geometry, location_type, attributes=None):
        """Add new location

        :param geometry: Location geojson geometry
        :type geometry: str

        :param location_type: Location type
        :type location_type: str

        :param attributes: Project-specific attributes that are defined
           through the project's questionnaire
        :type attributes: dict
        """
        api = u'/api/v1/organizations/{organization_slug}/projects/' \
              u'{project_slug}/spatial/'.format(
                organization_slug=self.project['organization']['slug'],
                project_slug=self.project['slug'])

        post_data = {
            'geometry': geometry
        }

        if location_type:
            post_data['type'] = location_type

        if attributes:
            post_data['attributes'] = attributes

        connector = ApiConnect(get_url_instance() + api)
        status, result = connector.post_json(Utilities.json_dumps(post_data))

        if status:
            self.set_status(
                self.tr('Location added.')
            )
            return result['properties']['id']
        else:
            self.set_status(
                Utilities.extract_error_detail(result)
            )
            return None
    def upload_update_locations(
            self,
            api,
            geometry,
            location_type,
            attributes=None):
        """Upload update location data.

        :param api: Api url to upload location
        :type api: str

        :param geometry: Location geojson geometry
        :type geometry: str

        :param location_type: Location type
        :type location_type: str

        :param attributes: Project-specific attributes that are defined
                   through the project's questionnaire
        :type attributes: dict
        """
        post_data = {
            'geometry': geometry,
            'type': location_type
        }

        if attributes:
            post_data['attributes'] = attributes

        connector = ApiConnect(get_url_instance() + api)
        status, result = connector.patch_json(Utilities.json_dumps(post_data))

        if status:
            self.set_status(
                self.tr('Location updated.')
            )
        else:
            self.set_status(
                Utilities.extract_error_detail(result)
            )
Ejemplo n.º 10
0
    def upload_questionnaire_project(self):
        """Upload questionnaire."""
        self.set_status(tr('Update questionnaire'))
        post_url = '/api/v1/organizations/' \
                   '%s/projects/%s/questionnaire/' % (
                       self.data['organisation']['slug'],
                       self.project_upload_result['slug']
                   )

        post_data = QByteArray()
        post_data.append(self.data['questionnaire'])

        connector = ApiConnect(get_url_instance() + post_url)
        status, result = self._call_json_put(connector, post_data)

        self.set_status(tr('Finished update questionnaire'))
        if status:
            self.set_progress_bar(self.current_progress +
                                  self.upload_increment)
        else:
            self.set_progress_bar(0)
            self.set_status(Utilities.extract_error_detail(result))
Ejemplo n.º 11
0
    def upload_locations(self, update_questionnaire_attribute):
        """Upload project locations to cadasta.

        :param update_questionnaire_attribute: Boolean to check if it need to
         upload the attributes
        :type update_questionnaire_attribute: bool
        """
        self.set_status(tr('Uploading locations'))
        total_locations = len(self.data['locations']['features'])
        progress_left = \
            (100 - self.current_progress - self.upload_increment) \
            / total_locations

        post_url = '/api/v1/organizations/%s/projects/%s/spatial/' % (
            self.data['organisation']['slug'],
            self.project_upload_result['slug'])

        failed = 0

        try:
            questionnaire = json.loads(self.questionnaire)
        except ValueError:
            questionnaire = {}

        for location in self.data['locations']['features']:
            post_data = {
                'geometry': location['geometry'],
                'type': location['fields']['location_type']
            }

            if update_questionnaire_attribute:
                if location['properties']:
                    post_data['attributes'] = location['properties']
                for key, value in post_data['attributes'].iteritems():
                    # Set None to empty string
                    if not value:
                        post_data['attributes'][key] = u''
                if 'id' in post_data['attributes']:
                    del post_data['attributes']['id']

                for question in \
                        questionnaire['question_groups'][0]['questions']:
                    if question['name'] not in post_data['attributes']:
                        post_data['attributes'][question['name']] = u''

            connector = ApiConnect(get_url_instance() + post_url)
            post_data = Utilities.json_dumps(post_data)
            status, result = self._call_json_post(connector, post_data)

            if status:
                self.set_progress_bar(self.current_progress + progress_left)
                try:
                    result_obj = result
                    if 'properties' in result_obj:
                        location['spatial_id'] = result_obj['properties']['id']
                except ValueError as e:
                    LOGGER.exception('message')
            else:
                self.set_progress_bar(0)
                self.set_status(Utilities.extract_error_detail(result))
                failed += 1

        self.set_progress_bar(100)

        if failed == 0:
            self.set_status(tr('Finished uploading all locations'))
        else:
            self.set_status(tr('Finish with %d failed' % failed))