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
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
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()
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()