def addStakeholders(self): self.connect(self.stakeholderProtocol, SIGNAL("created( bool, int, QString"), self.addStakeholdersFinished) # Dummy stakeholder s = Stakeholder() tag = Tag(key="Name", value="Adrian Weber Investment") tagGroup = TagGroup() tagGroup.setMainTag(tag) tagGroup.addTag(tag) tagGroup.addTag(Tag(key="Country", value="Swaziland")) s.addTagGroup(tagGroup) msg, rawBody = self.stakeholderProtocol.add(s) self.log(msg) self.log(rawBody)
def parseStakeholdersResponse(self, jsonBody): stakeholders = [] data = json.loads(str(jsonBody)) for stakeholder in data['data']: s = Stakeholder(id=stakeholder['id'], version=stakeholder['version']) for taggroup in stakeholder['taggroups']: tg = TagGroup(id=taggroup['id']) mainTag = taggroup['main_tag'] tg.setMainTag(Tag(id=mainTag['id'], key=mainTag['key'], value=mainTag['value'])) for tag in taggroup['tags']: t = Tag(id=tag['id'], key=tag['key'], value=tag['value']) tg.addTag(t) s.addTagGroup(tg) stakeholders.append(s) return stakeholders
def addStakeholdersFromLayer(self): """ Import all stakeholders from the active layer to the Land Observatory platform. It is not (yet) tested if a stakeholder already exists or not. """ # Connect to the protocol to get noticed as soon as the stakeholder has # been created self.connect(self.stakeholderProtocol, SIGNAL("created( bool, int, QString"), self.addStakeholdersFinished) # Get the dict maps the attribute names from the landmatrix input Shapefile to the # fields defined in the global definition yaml identifierColumn, transformMap, groups = self.getTagGroupsConfiguration("landmatrix.stakeholder.ini") # Get the active layer and its data provider layer = self.iface.activeLayer() provider = layer.dataProvider() # The current feature feature = QgsFeature() # List of attribute indexes to select attributeIndexes = [] # Dict that maps the field index to the fields defined in the global YAML fieldIndexMap = {} for (i, field) in provider.fields().iteritems(): if str(field.name()) in transformMap: attributeIndexes.append(i) fieldIndexMap[i] = transformMap[str(field.name())] # Start data retreival: fetch geometry and necessary attributes for each feature provider.select(attributeIndexes) stakeholders = [] # retreive every feature with its geometry and attributes while provider.nextFeature(feature): tagGroups = list(TagGroup() for i in range(len(groups))) # fetch map of attributes attrs = feature.attributeMap() # attrs is a dictionary: key = field index, value = QgsFeatureAttribute # show all attributes and their values for (k, attr) in attrs.iteritems(): self.log("%s: %s" % (fieldIndexMap[k], attr.toString())) # First search the correct taggroup to append attributeName = provider.fields()[k].name() currentTagGroup = 0 for g in groups: if attributeName in g: break else: currentTagGroup += 1 if attr is not None and attr.toString() != '': tag = Tag(key=fieldIndexMap[k], value=attr.toString()) tagGroups[currentTagGroup].addTag(tag) if tagGroups[currentTagGroup].mainTag() is None: tagGroups[currentTagGroup].setMainTag(tag) s = Stakeholder() for tg in tagGroups: if len(tg.tags) > 0: s.addTagGroup(tg) stakeholders.append(s) msg, rawBody = self.stakeholderProtocol.add(stakeholders) self.log(msg) self.log(rawBody) # Disconnect the signal self.disconnect(self.stakeholderProtocol, SIGNAL("created( bool, int, QString"), self.addStakeholdersFinished)