def add_column_names(self, request): """ Allow columns to be added based on an existing record. This may be necessary to make column selections available when records are upload through API endpoint rather than the frontend. """ model_obj = None org = self.get_organization(request, return_obj=True) inventory_pk = request.query_params.get('inventory_pk') inventory_type = request.query_params.get('inventory_type', 'property') if inventory_type in ['property', 'propertystate']: if not inventory_pk: model_obj = PropertyState.objects.filter( organization=org ).order_by('-id').first() try: model_obj = PropertyState.objects.get(id=inventory_pk) except PropertyState.DoesNotExist: pass elif inventory_type in ['taxlot', 'taxlotstate']: if not inventory_pk: model_obj = TaxLotState.objects.filter( organization=org ).order_by('-id').first() else: try: model_obj = TaxLotState.objects.get(id=inventory_pk) inventory_type = 'taxlotstate' except TaxLotState.DoesNotExist: pass else: msg = "{} is not a valid inventory type".format(inventory_type) raise ParseError(msg) if not model_obj: msg = "No {} was found matching {}".format( inventory_type, inventory_pk ) raise NotFound(msg) Column.save_column_names(model_obj) columns = Column.objects.filter( organization=model_obj.organization, table_name=model_obj.__class__.__name__, is_extra_data=True, ) columns = ColumnSerializer(columns, many=True) return Response(columns.data, status=status.HTTP_200_OK)
def list(self, request): """ Retrieves all columns for the user's organization including the raw database columns. Will return all the columns across both the Property and Tax Lot tables. The related field will be true if the column came from the other table that is not the "inventory_type" (which defaults to Property) Note that this is the same results as calling /api/v2/<inventory_type>/columns/?organization_id={} Example: /api/v2/columns/?inventory_type=(property|taxlot)&organization_id={} --- type: status: required: true type: string description: Either success or error columns: required: true type: array[column] description: Returns an array where each item is a full column structure. parameters: - name: organization_id description: The organization_id for this user's organization required: true paramType: query - name: inventory_type description: Which inventory type is being matched (for related fields and naming). property or taxlot required: true paramType: query - name: used_only description: Determine whether or not to show only the used fields. Ones that have been mapped type: boolean required: false paramType: query """ organization_id = request.query_params.get('organization_id', None) inventory_type = request.query_params.get('inventory_type', 'property') only_used = request.query_params.get('only_used', False) columns = Column.retrieve_all(organization_id, inventory_type, only_used) # for c in Column.objects.filter(organization=org).order_by('table_name', 'column_name'): # columns.append(c.to_dict()) return JsonResponse({ 'status': 'success', 'columns': columns, })
def delete_all(self, request): """ Delete all columns for an organization. This method is typically not recommended if there are data in the inventory as it will invalidate all extra_data fields. This also removes all the column mappings that existed. --- parameters: - name: organization_id description: The organization_id required: true paramType: query type: status: description: success or error type: string required: true column_mappings_deleted_count: description: Number of column_mappings that were deleted type: integer required: true columns_deleted_count: description: Number of columns that were deleted type: integer required: true """ organization_id = request.query_params.get('organization_id', None) try: org = Organization.objects.get(pk=organization_id) c_count, cm_count = Column.delete_all(org) return JsonResponse({ 'status': 'success', 'column_mappings_deleted_count': cm_count, 'columns_deleted_count': c_count, }) except Organization.DoesNotExist: return JsonResponse( { 'status': 'error', 'message': 'organization with with id {} does not exist'.format( organization_id) }, status=status.HTTP_404_NOT_FOUND)
def retrieve_all(self, request): """ Retrieves all columns for the user's organization including the raw database columns Example: /api/v2/columns/retrieve_all/?inventory_type=(property|taxlot) --- type: status: required: true type: string description: Either success or error columns: required: true type: array[column] description: Returns an array where each item is a full column structure. parameters: - name: organization_id description: The organization_id for this user's organization required: true paramType: query - name: inventory_type description: Which inventory type is being matched (for related fields and naming). property or taxlot required: true paramType: query """ organization_id = request.query_params.get('organization_id', None) inventory_type = request.query_params.get('inventory_type', 'property') columns = Column.retrieve_all(organization_id, inventory_type) # for c in Column.objects.filter(organization=org).order_by('table_name', 'column_name'): # columns.append(c.to_dict()) return JsonResponse({ 'status': 'success', 'columns': columns, })