def __init__(self, project): self.project = project tenure_types = get_types( 'tenure_type', TENURE_RELATIONSHIP_TYPES, questionnaire_id=project.current_questionnaire, include_labels=True) self.tenure_type_choices = dict(tenure_types) schema_attrs = self.get_attributes(self.project) get_content_type = ContentType.objects.get self.metadata = { 'location': { 'title': 'locations', 'model_name': 'SpatialUnit', 'content_type': get_content_type(app_label='spatial', model='spatialunit'), 'schema_attrs': schema_attrs['spatial.spatialunit'], 'model_attrs': ['id', 'geometry.ewkt', 'type'], }, 'party': { 'title': 'parties', 'model_name': 'Party', 'content_type': get_content_type(app_label='party', model='party'), 'schema_attrs': schema_attrs['party.party'], 'model_attrs': ['id', 'name', 'type'], }, 'tenure_rel': { 'title': 'relationships', 'model_name': 'TenureRelationship', 'content_type': get_content_type(app_label='party', model='tenurerelationship'), 'schema_attrs': schema_attrs['party.tenurerelationship'], 'model_attrs': ['id', 'party_id', 'spatial_unit_id', 'tenure_type_id'], }, } # Create ordered dict of all attributes, conditional or not for metadatum in self.metadata.values(): attr_columns = OrderedDict( (a, '') for a in metadatum['model_attrs']) schema_columns = OrderedDict( (a.name, '') for attrs in metadatum['schema_attrs'].values() for a in attrs.values()) attr_columns.update(schema_columns) metadatum['attr_columns'] = attr_columns
def validate_tenure_type(self, value): prj = self.context['project'] allowed_types = get_types('tenure_type', TENURE_RELATIONSHIP_TYPES, questionnaire_id=prj.current_questionnaire) if value not in allowed_types: msg = "'{}' is not a valid choice for field 'tenure_type'." raise serializers.ValidationError(msg.format(value)) return value
def done(self, form_list, **kwargs): form_data = [form.cleaned_data for form in form_list] name = form_data[0]['name'] description = form_data[0]['description'] mime_type = form_data[0]['mime_type'] is_resource = form_data[0]['is_resource'] original_file = form_data[0]['original_file'] file = form_data[0]['file'] type = self.storage.get_step_data('select_file').get( 'select_file-type') entity_types = self.storage.get_step_data('select_file').getlist( 'select_file-entity_types') path = self.file_storage.path(file.name) map_attrs_data = self.storage.get_step_data('map_attributes') project = self.get_project() org = project.organization allowed_tenure_types = get_types( 'tenure_type', TENURE_RELATIONSHIP_TYPES, questionnaire_id=project.current_questionnaire) allowed_location_types = get_types( 'location_type', TYPE_CHOICES, questionnaire_id=project.current_questionnaire) config_dict = { 'project': project, 'file': path, 'type': type, 'entity_types': entity_types.copy(), 'party_name_field': form_data[2]['party_name_field'], 'party_type_field': form_data[2]['party_type_field'], 'location_type_field': form_data[2]['location_type_field'], 'geometry_field': form_data[2]['geometry_field'], 'attributes': map_attrs_data.getlist('attributes', None), 'allowed_tenure_types': allowed_tenure_types, 'allowed_location_types': allowed_location_types } importer = self._get_importer(type, path) importer.import_data(config_dict) if is_resource: default_storage = DefaultStorage() file.seek(0) ext = file.name[file.name.rfind('.'):] resource = Resource(name=name, description=description, original_file=original_file, mime_type=mime_type, contributor=self.request.user, project=self.get_project()) upload_to = getattr(resource.file.field, 'upload_to') url = '' while not url: temp_url = upload_to + '/' + random_id() + ext if not Resource.objects.filter( file__contains=temp_url).exists(): url = default_storage.save(temp_url, file.read()) resource.file.url = url resource.save() ContentObject.objects.create(resource=resource, content_object=resource.project) return redirect('organization:project-dashboard', organization=org.slug, project=project.slug)
def post(self, request, *args, **kwargs): query = request.data.get('q') start_idx = int(request.data.get('start', 0)) page_size = int(request.data.get('length', 10)) dataTablesDraw = int(request.data['draw']) project = self.get_project() tenure_types = get_types( 'tenure_type', TENURE_RELATIONSHIP_TYPES, questionnaire_id=project.current_questionnaire, include_labels=True) self.tenure_types = dict(tenure_types) spatial_types = get_types( 'location_type', SPATIAL_TYPE_CHOICES, questionnaire_id=project.current_questionnaire, include_labels=True) self.spatial_types = dict(spatial_types) results_as_html = [] num_hits = 0 timestamp = '' if query: raw_results = self.query_es(self.get_project().id, query, start_idx, page_size) if raw_results is None: return Response({ 'draw': dataTablesDraw, 'recordsTotal': 0, 'recordsFiltered': 0, 'data': [], 'error': 'unavailable', }) num_hits = min(raw_results['hits']['total'], settings.ES_MAX_RESULTS) results = raw_results['hits']['hits'] if len(results) == 0: timestamp = self.query_es_timestamp(project.id) else: timestamp = results[0]['_source'].get('@timestamp') for result in results: if result['_type'] == 'project': continue augmented_result = self.augment_result(result) if augmented_result is None: continue html = self.htmlize_result(augmented_result) results_as_html.append([html]) return Response({ 'draw': dataTablesDraw, 'recordsTotal': num_hits, 'recordsFiltered': num_hits, 'data': results_as_html, 'timestamp': timestamp, })