Exemple #1
0
    def filter_queryset(self, request, queryset, view):
        # TODO: this needs to be filled in with the same logic that implements
        # search/filtering in `seed.views.main.search_buildings`.
        params = request.query_params.dict()
        # Since this is being passed in as a query string, the object ends up
        # coming through as a string.
        params['filter_params'] = json.loads(params.get('filter_params', '{}'))

        params = search.process_search_params(
            params=params,
            user=request.user,
            is_api_request=True,
        )
        buildings_queryset = search.orchestrate_search_filter_sort(
            params=params,
            user=request.user,
        )

        if request.query_params.get('select_all_checkbox', 'false') == 'true':
            pass
        elif 'selected_buildings' in request.query_params:
            return buildings_queryset.filter(
                id__in=request.query_params.getlist('selected_buildings'),
            )
        return buildings_queryset
Exemple #2
0
    def filter_queryset(self, request, queryset, view):
        # TODO: this needs to be filled in with the same logic that implements
        # search/filtering in `seed.views.main.search_buildings`.
        params = request.query_params.dict()
        # Since this is being passed in as a query string, the object ends up
        # coming through as a string.
        params['filter_params'] = json.loads(params.get('filter_params', '{}'))

        params = search.process_search_params(
            params=params,
            user=request.user,
            is_api_request=True,
        )
        buildings_queryset = search.orchestrate_search_filter_sort(
            params=params,
            user=request.user,
            skip_sort=True,
        )

        if request.query_params.get('select_all_checkbox', 'false') == 'true':
            pass
        elif 'selected_buildings' in request.query_params:
            return buildings_queryset.filter(
                id__in=request.query_params.getlist('selected_buildings'), )
        return buildings_queryset
Exemple #3
0
 def filter_queryset(self, request):
     params = request.query_params.dict()
     # Since this is being passed in as a query string, the object ends up
     # coming through as a string.
     params['filter_params'] = json.loads(params.get('filter_params', '{}'))
     inventory_type = params.pop('inventory_type', None)
     params = search.process_search_params(
         params=params,
         user=request.user,
         is_api_request=True,
     )
     queryset = search.inventory_search_filter_sort(
         inventory_type,
         params=params,
         user=request.user,
     )
     if 'selected' in request.data:
         # Return labels limited to the 'selected' list.  Otherwise, if selected is empty, return all
         if request.data['selected']:
             return queryset.filter(id__in=request.data['selected'], )
     return queryset
Exemple #4
0
 def filter_queryset(self, request):
     params = request.query_params.dict()
     # Since this is being passed in as a query string, the object ends up
     # coming through as a string.
     params['filter_params'] = json.loads(params.get('filter_params', '{}'))
     inventory_type = params.pop('inventory_type', None)
     params = search.process_search_params(
         params=params,
         user=request.user,
         is_api_request=True,
     )
     queryset = search.inventory_search_filter_sort(
         inventory_type,
         params=params,
         user=request.user,
     )
     if 'selected' in request.data:
         # Return labels limited to the 'selected' list.  Otherwise, if selected is empty, return all
         if request.data['selected']:
             return queryset.filter(
                 id__in=request.data['selected'],
             )
     return queryset
Exemple #5
0
def filter_labels_for_inv_type(request, inventory_type=None):
    """
    Method used to filter labels by inventory type and return is_applied inventory id's
    Method was initially built as a class defined function above to handle more parameters.
    """
    params = request.query_params.dict()
    # Since this is being passed in as a query string, the object ends up
    # coming through as a string.
    params['filter_params'] = json.loads(params.get('filter_params', '{}'))
    params = search.process_search_params(
        params=params,
        user=request.user,
        is_api_request=True,
    )
    queryset = search.inventory_search_filter_sort(
        inventory_type,
        params=params,
        user=request.user,
    )
    if 'selected' in request.data:
        # Return labels limited to the 'selected' list.  Otherwise, if selected is empty, return all
        if request.data['selected']:
            return queryset.filter(id__in=request.data['selected'], )
    return queryset
Exemple #6
0
    def add(self, request, pk):
        """
        Add inventory to project
        :PUT: Expects organization_id in query string.
        ---
        parameters:
            - name: organization_id
              description: ID of organization to associate new project with
              type: integer
              required: true
            - name: inventory_type
              description: type of inventory to add: 'property' or 'taxlot'
              type: string
              required: true
              paramType: query
            - name: project slug or pk
              description: The project slug identifier or primary key for this project
              required: true
              paramType: path
            - name:  selected
              description: ids of property or taxlot views to add
              type: array[int]
              required: true
        Returns:
            {
                'status': 'success',
                'added': [list of property/taxlot view ids added]
            }
        """
        error = None
        inventory = None
        status_code = status.HTTP_200_OK
        inventory_type = request.query_params.get(
            'inventory_type', request.data.get('inventory_type', None)
        )
        if not inventory_type:
            error, status_code = self.get_error(
                'missing param', 'inventory_type'
            )
        else:
            key = self.get_key(pk)
            project = self.get_project(key, pk)
            if not project:
                error, status_code = self.get_error(
                    'not found', key=key, val=pk
                )
        if not error:
            project = project[0]
            view_type = "{}_view".format(inventory_type)
            request.data['inventory_type'] = view_type
            params = search.process_search_params(
                request.data, request.user, is_api_request=False
            )
            organization_id = self.get_organization()
            params['organization_id'] = organization_id
            qs = search.inventory_search_filter_sort(
                view_type, params=params, user=request.user
            )
            if request.data.get('selected', None) \
                    and isinstance(request.data.get('selected'), list):
                inventory = qs.filter(pk__in=request.data.get('selected'))
            # TODO is this still relevant
            elif request.data.get('select_all_checkbox', None):
                inventory = qs

            if not inventory:
                error, status_code = self.get_error(
                    'missing inventory', key=inventory_type
                )
        if error:
            result = {'status': 'error', 'message': error}
        else:
            Model = self.ProjectViewModels[inventory_type]
            new_project_views = [
                self.project_view_factory(inventory_type, project.id, view.id)
                for view in inventory
            ]
            Model.objects.bulk_create(new_project_views)
            added = [view.id for view in inventory]
            project.last_modified_by = request.user
            project.save()
            result = {'status': 'success', 'added': added}
        return Response(result, status=status_code)
Exemple #7
0
def add_buildings(project_slug, project_dict, user_pk):
    """adds buildings to a project. if a user has selected all buildings,
       then the the search parameters within project_dict are used to determine
       the total set
       of buildings.
       also creates a Compliance inst. if satisfying params are present

       :param str project_slug: a project's slug used to get the project
       :param dict project_dict: contains search params, and browser state
       information
       :user_pk int or str: the user's pk or id

    """
    project = Project.objects.get(slug=project_slug)

    # Initialize the progress cache
    prog_key = project.adding_buildings_status_percentage_cache_key
    data = {
        'status': 'processing',
        'progress': 0,
        'progress_key': prog_key,
        'numerator': 0,
        'denominator': 0,
    }
    set_cache(project.adding_buildings_status_percentage_cache_key,
              data['status'], data)

    user = User.objects.get(pk=user_pk)
    project.last_modified_by = user
    project.save()

    # Perform the appropriate filtering to get the raw list of buildings.
    params = search.process_search_params(project_dict,
                                          user,
                                          is_api_request=False)
    buildings_queryset = search.orchestrate_search_filter_sort(
        params=params,
        user=user,
    )

    # Get selected buildings based on either individual selection or select-all
    # selection.
    if project_dict.get('select_all_checkbox'):
        selected_buildings = buildings_queryset
    else:
        selected_buildings = buildings_queryset.filter(id__in=project_dict.get(
            'selected_buildings', []), )

    denominator = len(selected_buildings)

    # Loop over the buildings adding them to the project and updating the
    # progress cache.
    for idx, bs in enumerate(selected_buildings):
        data = {
            'status': 'processing',
            'progress': (float(idx) / denominator * 100),
            'progress_key': prog_key,
            'numerator': idx,
            'denominator': denominator
        }
        set_cache(prog_key, data['status'], data)
        ProjectBuilding.objects.get_or_create(project=project,
                                              building_snapshot=bs)

    # Mark the progress cache as complete.
    result = {
        'status': 'completed',
        'progress': 100,
        'progress_key': prog_key,
        'numerator': denominator,
        'denominator': denominator
    }
    set_cache(prog_key, result['status'], result)

    deadline_date = time_utils.parse_datetime(
        project_dict.get('deadline_date'))

    end_date = time_utils.parse_datetime(project_dict.get('end_date'))

    if end_date:
        last_day_of_month = calendar.monthrange(end_date.year,
                                                end_date.month)[1]
        end_date = datetime.datetime(end_date.year, end_date.month,
                                     last_day_of_month)

    if project_dict.get('compliance_type'):
        compliance = Compliance.objects.create(
            compliance_type=project_dict.get('compliance_type'),
            end_date=end_date,
            deadline_date=deadline_date,
            project=project)
        compliance.save()
Exemple #8
0
    def add(self, request, pk):
        """
        Add inventory to project
        :PUT: Expects organization_id in query string.
        ---
        parameters:
            - name: organization_id
              description: ID of organization to associate new project with
              type: integer
              required: true
            - name: inventory_type
              description: type of inventory to add: 'property' or 'taxlot'
              type: string
              required: true
              paramType: query
            - name: project slug or pk
              description: The project slug identifier or primary key for this project
              required: true
              paramType: path
            - name:  selected
              description: ids of property or taxlot views to add
              type: array[int]
              required: true
        Returns:
            {
                'status': 'success',
                'added': [list of property/taxlot view ids added]
            }
        """
        error = None
        inventory = None
        status_code = status.HTTP_200_OK
        inventory_type = request.query_params.get(
            'inventory_type', request.data.get('inventory_type', None)
        )
        if not inventory_type:
            error, status_code = self.get_error(
                'missing param', 'inventory_type'
            )
        else:
            key = self.get_key(pk)
            project = self.get_project(key, pk)
            if not project:
                error, status_code = self.get_error(
                    'not found', key=key, val=pk
                )
        if not error:
            project = project[0]
            view_type = "{}_view".format(inventory_type)
            request.data['inventory_type'] = view_type
            params = search.process_search_params(
                request.data, request.user, is_api_request=False
            )
            organization_id = self.get_organization()
            params['organization_id'] = organization_id
            qs = search.inventory_search_filter_sort(
                view_type, params=params, user=request.user
            )
            if request.data.get('selected', None)\
                    and isinstance(request.data.get('selected'), list):
                inventory = qs.filter(pk__in=request.data.get('selected'))
            # TODO is this still relevant
            elif request.data.get('select_all_checkbox', None):
                inventory = qs

            if not inventory:
                error, status_code = self.get_error(
                    'missing inventory', key=inventory_type
                )
        if error:
            result = {'status': 'error', 'message': error}
        else:
            Model = self.ProjectViewModels[inventory_type]
            new_project_views = [
                self.project_view_factory(inventory_type, project.id, view.id)
                for view in inventory
            ]
            Model.objects.bulk_create(new_project_views)
            added = [view.id for view in inventory]
            project.last_modified_by = request.user
            project.save()
            result = {'status': 'success', 'added': added}
        return Response(result, status=status_code)
Exemple #9
0
def add_buildings(project_slug, project_dict, user_pk):
    """adds buildings to a project. if a user has selected all buildings,
       then the the search parameters within project_dict are used to determine
       the total set
       of buildings.
       also creates a Compliance inst. if satisfying params are present

       :param str project_slug: a project's slug used to get the project
       :param dict project_dict: contains search params, and browser state
       information
       :user_pk int or str: the user's pk or id

    """
    project = Project.objects.get(slug=project_slug)

    # Initialize the progress cache
    prog_key = project.adding_buildings_status_percentage_cache_key
    data = {
        'status': 'processing',
        'progress': 0,
        'progress_key': prog_key,
        'numerator': 0,
        'denominator': 0,
    }
    set_cache(project.adding_buildings_status_percentage_cache_key,
              data['status'], data)

    user = User.objects.get(pk=user_pk)
    project.last_modified_by = user
    project.save()

    # Perform the appropriate filtering to get the raw list of buildings.
    params = search.process_search_params(project_dict, user,
                                          is_api_request=False)
    buildings_queryset = search.orchestrate_search_filter_sort(
        params=params,
        user=user,
    )

    # Get selected buildings based on either individual selection or select-all
    # selection.
    if project_dict.get('select_all_checkbox'):
        selected_buildings = buildings_queryset
    else:
        selected_buildings = buildings_queryset.filter(
            id__in=project_dict.get('selected_buildings', []),
        )

    denominator = len(selected_buildings)

    # Loop over the buildings adding them to the project and updating the
    # progress cache.
    for idx, bs in enumerate(selected_buildings):
        data = {
            'status': 'processing',
            'progress': (float(idx) / denominator * 100),
            'progress_key': prog_key,
            'numerator': idx,
            'denominator': denominator
        }
        set_cache(prog_key, data['status'], data)
        ProjectBuilding.objects.get_or_create(
            project=project, building_snapshot=bs
        )

    # Mark the progress cache as complete.
    result = {
        'status': 'completed',
        'progress': 100,
        'progress_key': prog_key,
        'numerator': denominator,
        'denominator': denominator
    }
    set_cache(prog_key, result['status'], result)

    deadline_date = time_utils.parse_datetime(
        project_dict.get('deadline_date'))

    end_date = time_utils.parse_datetime(project_dict.get('end_date'))

    if end_date:
        last_day_of_month = calendar.monthrange(
            end_date.year, end_date.month
        )[1]
        end_date = datetime.datetime(
            end_date.year, end_date.month, last_day_of_month
        )

    if project_dict.get('compliance_type'):
        compliance = Compliance.objects.create(
            compliance_type=project_dict.get('compliance_type'),
            end_date=end_date,
            deadline_date=deadline_date,
            project=project
        )
        compliance.save()