示例#1
0
    def get(self, request: HttpRequest):

        # Check to see if a recent cached results exists and return that instead if it exists

        hashed_query = hashlib.sha1(
            request.GET.urlencode().encode('utf8')).hexdigest()
        cached_filename = os.path.join(self.cache_dir,
                                       "{}.csv".format(hashed_query))
        if os.path.exists(cached_filename):
            if time.time() - os.path.getmtime(cached_filename) > 600:
                os.remove(cached_filename)
            else:
                if settings.EXPORT_FILE_CACHE_URL == "":
                    return FileResponse(open(cached_filename, 'rb'),
                                        as_attachment=True)
                else:
                    return HttpResponseRedirect(
                        settings.EXPORT_FILE_CACHE_URL +
                        "{}.csv".format(hashed_query))

        solr_search_terms = search_util.get_search_terms(request)

        # Retrieve search results and transform facets results to python dict

        solr_search_orgs: str = request.GET.get('bn-search-orgs', '')
        solr_search_year: str = request.GET.get('bn-search-year', '')
        solr_search_month: str = request.GET.get('bn-search-month', '')
        solr_search_ar: str = request.GET.get('bn-search-action', '')
        solr_search_addrs: str = request.GET.get('bn-search-addressee', '')

        solr_search_facets = self.solr_facet_fields_en
        solr_query_fields = self.solr_query_fields_en
        if request.LANGUAGE_CODE == 'fr':
            facets_dict = dict(owner_org_fr_s=solr_search_orgs,
                               year_i=solr_search_year,
                               month_i=solr_search_month,
                               action_required_fr_s=solr_search_ar,
                               addressee_fr_s=solr_search_addrs)
            solr_search_facets = self.solr_facet_fields_fr
            solr_query_fields = self.solr_query_fields_fr
        else:
            facets_dict = dict(owner_org_en_s=solr_search_orgs,
                               year_i=solr_search_year,
                               month_i=solr_search_month,
                               action_required_en_s=solr_search_ar,
                               addressee_en_s=solr_search_addrs)

        search_results = search_util.solr_query_for_export(
            solr_search_terms, settings.SOLR_BN, self.solr_fields,
            solr_query_fields, solr_search_facets, "id asc", facets_dict,
            self.phrase_xtras)

        if search_util.cache_search_results_file(
                cached_filename=cached_filename, sr=search_results):
            if settings.EXPORT_FILE_CACHE_URL == "":
                return FileResponse(open(cached_filename, 'rb'),
                                    as_attachment=True)
            else:
                return HttpResponseRedirect(settings.EXPORT_FILE_CACHE_URL +
                                            "{}.csv".format(hashed_query))
示例#2
0
    def get(self, request: HttpRequest):

        # Check to see if a recent cached results exists and return that instead if it exists
        hashed_query = hashlib.sha1(request.GET.urlencode().encode('utf8')).hexdigest()
        cached_filename = os.path.join(self.cache_dir, "{}.csv".format(hashed_query))
        if os.path.exists(cached_filename):
            if time.time() - os.path.getmtime(cached_filename) > 600:
                os.remove(cached_filename)
            else:
                if settings.EXPORT_FILE_CACHE_URL == "":
                    return FileResponse(open(cached_filename, 'rb'), as_attachment=True)
                else:
                    return HttpResponseRedirect(settings.EXPORT_FILE_CACHE_URL + "{}.csv".format(hashed_query))

        solr_search_terms = search_util.get_search_terms(request)

        # Retrieve search results and transform facets results to python dict
        solr_search_orgs: str = request.GET.get('ap-search-orgs', '')
        solr_search_periods: str = request.GET.get('ap-reporting-period', '')
        solr_search_commitments: str = request.GET.get('ap-commitment', '')
        solr_search_milestones: str = request.GET.get('ap-milestone', '')
        solr_search_status: str = request.GET.get('ap-status', '')
        solr_search_due_dates: str = request.GET.get('ap-due-date', '')

        solr_search_facets = self.solr_facet_fields_en
        if request.LANGUAGE_CODE == 'fr':
            facets_dict = dict(owner_org_fr_s=solr_search_orgs,
                               reporting_period_s=solr_search_periods,
                               commitments_fr_s=solr_search_commitments,
                               milestone_fr_s=solr_search_milestones,
                               status_fr_s=solr_search_status,
                               due_date_s=solr_search_due_dates,
                               )
        else:
            facets_dict = dict(owner_org_en_s=solr_search_orgs,
                               reporting_period_s=solr_search_periods,
                               commitments_en_s=solr_search_commitments,
                               milestone_en_s=solr_search_milestones,
                               status_en_s=solr_search_status,
                               due_date_s=solr_search_due_dates,
                               )

        search_results = search_util.solr_query_for_export(solr_search_terms,
                                                           settings.SOLR_NAP,
                                                           self.solr_fields,
                                                           self.solr_query_fields_en,
                                                           solr_search_facets,
                                                           "id asc",
                                                           facets_dict,
                                                           self.phrase_xtras)

        if search_util.cache_search_results_file(cached_filename=cached_filename, sr=search_results):
            if settings.EXPORT_FILE_CACHE_URL == "":
                return FileResponse(open(cached_filename, 'rb'), as_attachment=True)
            else:
                return HttpResponseRedirect(settings.EXPORT_FILE_CACHE_URL + "{}.csv".format(hashed_query))
示例#3
0
    def get(self, request: HttpRequest):
        # Check to see if a recent cached results exists and return that instead if it exists
        hashed_query = hashlib.sha1(
            request.GET.urlencode().encode('utf8')).hexdigest()
        cached_filename = os.path.join(
            self.cache_dir, "{}_{}.csv".format(hashed_query,
                                               request.LANGUAGE_CODE))
        if os.path.exists(cached_filename):
            if time.time() - os.path.getmtime(cached_filename) > 600:
                os.remove(cached_filename)
            else:
                if settings.EXPORT_FILE_CACHE_URL == "":
                    return FileResponse(open(cached_filename, 'rb'),
                                        as_attachment=True)
                else:
                    return HttpResponseRedirect(
                        settings.EXPORT_FILE_CACHE_URL +
                        "{}_{}.csv".format(hashed_query, request.LANGUAGE_CODE)
                    )

        # Retrieve any selected search facets
        params = get_user_facet_parameters(request)

        solr_search_terms = search_util.get_search_terms(request)

        if request.LANGUAGE_CODE == 'fr':
            facets_dict = dict(owner_org_fr_s=params['solr_search_orgs'],
                               status_fr_s=params['solr_search_status'],
                               reason_fr_s=params['solr_search_reason'])
            solr_fields = self.solr_fields_fr
            solr_search_facets = self.solr_facet_fields_fr
            solr_query_fields = self.solr_query_fields_fr
        else:
            facets_dict = dict(owner_org_en_s=params['solr_search_orgs'],
                               status_en_s=params['solr_search_status'],
                               reason_en_s=params['solr_search_reason'])
            solr_fields = self.solr_fields_en
            solr_search_facets = self.solr_facet_fields_en
            solr_query_fields = self.solr_query_fields_en

        search_results = search_util.solr_query_for_export(
            solr_search_terms, settings.SOLR_SD, solr_fields,
            solr_query_fields, solr_search_facets, "id asc", facets_dict,
            self.phrase_xtras)

        if search_util.cache_search_results_file(
                cached_filename=cached_filename, sr=search_results):
            if settings.EXPORT_FILE_CACHE_URL == "":
                return FileResponse(open(cached_filename, 'rb'),
                                    as_attachment=True)
            else:
                return HttpResponseRedirect(
                    settings.EXPORT_FILE_CACHE_URL +
                    "{}_{}.csv".format(hashed_query, request.LANGUAGE_CODE))
示例#4
0
    def get(self, request: HttpRequest):

        # Check to see if a recent cached results exists and return that instead if it exists

        hashed_query = hashlib.sha1(
            request.GET.urlencode().encode('utf8')).hexdigest()
        cached_filename = os.path.join(self.cache_dir,
                                       "{}.csv".format(hashed_query))
        if os.path.exists(cached_filename):
            if time.time() - os.path.getmtime(cached_filename) > 600:
                os.remove(cached_filename)
            else:
                if settings.EXPORT_FILE_CACHE_URL == "":
                    return FileResponse(open(cached_filename, 'rb'),
                                        as_attachment=True)
                else:
                    return HttpResponseRedirect(
                        settings.EXPORT_FILE_CACHE_URL +
                        "{}.csv".format(hashed_query))

        # Get any search terms

        solr_search_terms = search_util.get_search_terms(request)

        # Retrieve search results and transform facets results to python dict

        solr_search_orgs: str = request.GET.get('si-search-orgs', '')
        solr_search_years: str = request.GET.get('si-search-year', '')
        solr_search_xis: str = request.GET.get('si-search-ext-int', '')
        solr_search_stype: str = request.GET.get('si-search-service-type', '')
        solr_search_designations: str = request.GET.get(
            'si-search-designations', '')
        solr_search_targets: str = request.GET.get('si-search-target-groups',
                                                   '')
        solr_search_fees: str = request.GET.get('si-search-service-fee', '')
        solr_search_cra_no: str = request.GET.get('si-search-cra-number', '')
        solr_search_e_reg: str = request.GET.get('si-search-e-reg', '')
        solr_search_e_authenticate: str = request.GET.get(
            'si-search-e-authenticate', '')
        solr_search_e_decision: str = request.GET.get('si-search-e-decision',
                                                      '')
        solr_search_e_issuance: str = request.GET.get('si-search-e-issuance',
                                                      '')
        solr_search_e_feedback: str = request.GET.get('si-search-e-feedback',
                                                      '')

        solr_search_facets = self.solr_facet_fields_en
        if request.LANGUAGE_CODE == 'fr':
            facets_dict = dict(
                owner_org_fr_s=solr_search_orgs,
                fiscal_year_s=solr_search_years,
                external_internal_fr_s=solr_search_xis,
                service_type_fr_s=solr_search_stype,
                special_designations_fr_s=solr_search_designations,
                client_target_groups_fr_s=solr_search_targets,
                service_fee_fr_s=solr_search_fees,
                cra_business_number_fr_s=solr_search_cra_no,
                e_registration_fr_s=solr_search_e_reg,
                e_authentication_fr_s=solr_search_e_authenticate,
                e_decision_fr_s=solr_search_e_decision,
                e_issuance_fr_s=solr_search_e_issuance,
                e_feedback_fr_s=solr_search_e_feedback,
            )
        else:
            facets_dict = dict(
                owner_org_en_s=solr_search_orgs,
                fiscal_year_s=solr_search_years,
                external_internal_en_s=solr_search_xis,
                service_type_en_s=solr_search_stype,
                special_designations_en_s=solr_search_designations,
                client_target_groups_en_s=solr_search_targets,
                service_fee_en_s=solr_search_fees,
                cra_business_number_en_s=solr_search_cra_no,
                e_registration_en_s=solr_search_e_reg,
                e_authentication_en_s=solr_search_e_authenticate,
                e_decision_en_s=solr_search_e_decision,
                e_issuance_en_s=solr_search_e_issuance,
                e_feedback_en_s=solr_search_e_feedback,
            )

        search_results = search_util.solr_query_for_export(
            solr_search_terms, settings.SOLR_SI, self.solr_fields,
            self.solr_query_fields_en, solr_search_facets, "id asc",
            facets_dict, self.phrase_xtras)

        if search_util.cache_search_results_file(
                cached_filename=cached_filename, sr=search_results):
            if settings.EXPORT_FILE_CACHE_URL == "":
                return FileResponse(open(cached_filename, 'rb'),
                                    as_attachment=True)
            else:
                return HttpResponseRedirect(settings.EXPORT_FILE_CACHE_URL +
                                            "{}.csv".format(hashed_query))
示例#5
0
    def get(self, request: HttpRequest):

        # Check to see if a recent cached results exists and return that instead if it exists

        hashed_query = hashlib.sha1(
            request.GET.urlencode().encode('utf8')).hexdigest()
        cached_filename = os.path.join(self.cache_dir,
                                       "{}.csv".format(hashed_query))
        if os.path.exists(cached_filename):
            if time.time() - os.path.getmtime(cached_filename) > 600:
                os.remove(cached_filename)
            else:
                if settings.EXPORT_FILE_CACHE_URL == "":
                    return FileResponse(open(cached_filename, 'rb'),
                                        as_attachment=True)
                else:
                    return HttpResponseRedirect(
                        settings.EXPORT_FILE_CACHE_URL +
                        "{}.csv".format(hashed_query))

        # If a list of ids is provided, then the facets and search text are ignored.

        solr_search_terms = ''
        solr_search_portal = ''
        solr_search_col = ''
        solr_search_jur = ''
        solr_search_orgs = ''
        solr_search_keyw = ''
        solr_search_subj = ''
        solr_search_fmts = ''
        solr_search_rsct = ''
        solr_search_updc = ''

        solr_search_ids = request.GET.get('ids', '')
        mlt_search_id = request.GET.get("mlt_id", '')

        if solr_search_ids == '' and mlt_search_id == '':

            # Handle search text

            search_text = str(request.GET.get('search_text', ''))

            # Respect quoted strings
            search_terms = search_util.split_with_quotes(search_text)
            if len(search_terms) == 0:
                solr_search_terms = "*"
            elif len(search_terms) == 1:
                solr_search_terms = '"{0}"'.format(search_terms)
            else:
                solr_search_terms = ' '.join(search_terms)

            # Retrieve any search facets and add to context

            solr_search_portal = request.GET.get('od-search-portal', '')
            solr_search_col = request.GET.get('od-search-col', '')
            solr_search_jur = request.GET.get('od-search-jur', '')
            solr_search_orgs = request.GET.get('od-search-orgs', '')
            solr_search_keyw = request.GET.get('od-search-keywords', '')
            solr_search_subj = request.GET.get('od-search-subjects', '')
            solr_search_fmts = request.GET.get('od-search-format', '')
            solr_search_rsct = request.GET.get('od-search-rsct', '')
            solr_search_updc = request.GET.get('od-search-update', '')

        if request.LANGUAGE_CODE == 'fr':
            facets_dict = dict(portal_type_fr_s=solr_search_portal,
                               collection_type_fr_s=solr_search_col,
                               jurisdiction_fr_s=solr_search_jur,
                               owner_org_title_fr_s=solr_search_orgs,
                               keywords_fr_s=solr_search_keyw,
                               subject_fr_s=solr_search_subj,
                               resource_format_s=solr_search_fmts,
                               resource_type_fr_s=solr_search_rsct,
                               update_cycle_fr_s=solr_search_updc)
        else:
            facets_dict = dict(portal_type_en_s=solr_search_portal,
                               collection_type_en_s=solr_search_col,
                               jurisdiction_en_s=solr_search_jur,
                               owner_org_title_en_s=solr_search_orgs,
                               keywords_en_s=solr_search_keyw,
                               subject_en_s=solr_search_subj,
                               resource_format_s=solr_search_fmts,
                               resource_type_en_s=solr_search_rsct,
                               update_cycle_en_s=solr_search_updc)

        if mlt_search_id == "":
            search_results = search_util.solr_query_for_export(
                solr_search_terms, settings.SOLR_URL, self.solr_fields,
                self.solr_query_fields_en, self.solr_facet_fields_en, "id asc",
                facets_dict, self.phrase_xtras_en, solr_search_ids)
        else:
            if request.LANGUAGE_CODE == 'fr':
                search_results = search_util.solr_query_for_export_mlt(
                    mlt_search_id, settings.SOLR_URL, self.solr_fields,
                    self.mlt_fields_fr, self.solr_query_fields_fr,
                    'title_fr_s asc', 10)
            else:
                search_results = search_util.solr_query_for_export_mlt(
                    mlt_search_id, settings.SOLR_URL, self.solr_fields,
                    self.mlt_fields_en, self.solr_query_fields_en,
                    'title_en_s asc', 10)

        if search_util.cache_search_results_file(
                cached_filename=cached_filename, sr=search_results):
            if settings.EXPORT_FILE_CACHE_URL == "":
                return FileResponse(open(cached_filename, 'rb'),
                                    as_attachment=True)
            else:
                return HttpResponseRedirect(settings.EXPORT_FILE_CACHE_URL +
                                            "{}.csv".format(hashed_query))