def test_get_variance_application_status_codes(self, test_client,
                                                   db_session, auth_headers):
        """Should return the correct number of records with a 200 response code"""

        get_resp = test_client.get(f'/variances/status-codes',
                                   headers=auth_headers['full_auth_header'])
        get_data = json.loads(get_resp.data.decode())
        assert get_resp.status_code == 200
        assert len(get_data['records']) == len(
            VarianceApplicationStatusCode.get_active())
Beispiel #2
0
    def _apply_filters_and_pagination(self, args):
        sort_models = {
            'variance_id': 'Variance',
            'compliance_article_id': 'Variance',
            'lead_inspector': 'Variance',
            'received_date': 'Variance',
            'mine_name': 'Mine',
            'variance_application_status_code': 'Variance'
        }

        status_filter_values = list(
            map(lambda x: x.variance_application_status_code,
                VarianceApplicationStatusCode.get_active()))

        conditions = []
        if args["application_status"] is not None:
            status_filter_values = args["application_status"].split(',')
            conditions.append(
                self._build_filter('Variance',
                                   'variance_application_status_code', 'in',
                                   status_filter_values))

        if args["compliance_codes"] is not None:
            compliance_codes_values = args["compliance_codes"].split(',')
            conditions.append(
                self._build_filter('Variance', 'compliance_article_id', 'in',
                                   compliance_codes_values))

        if args["expiry_date_before"] is not None:
            conditions.append(
                self._build_filter('Variance', 'expiry_date', '<=',
                                   args["expiry_date_before"]))

        if args["expiry_date_after"] is not None:
            conditions.append(
                self._build_filter('Variance', 'expiry_date', '>=',
                                   args["expiry_date_after"]))

        if args["issue_date_before"] is not None:
            conditions.append(
                self._build_filter('Variance', 'issue_date', '<=',
                                   args["issue_date_before"]))

        if args["issue_date_after"] is not None:
            conditions.append(
                self._build_filter('Variance', 'issue_date', '>=',
                                   args["issue_date_after"]))

        if args["major"] is not None:
            conditions.append(
                self._build_filter('Mine', 'major_mine_ind', '==',
                                   args["major"]))

        if args["search_terms"] is not None:
            search_conditions = [
                self._build_filter('Mine', 'mine_name', 'ilike',
                                   '%{}%'.format(args["search_terms"])),
                self._build_filter('Mine', 'mine_no', 'ilike',
                                   '%{}%'.format(args["search_terms"]))
            ]
            conditions.append({'or': search_conditions})

        if args["region"] is not None:
            region_list = args["region"].split(',')
            conditions.append(
                self._build_filter('Mine', 'mine_region', 'in', region_list))

        query = Variance.query.join(Mine).join(ComplianceArticle)

        # Apply sorting
        if args['sort_field'] and args['sort_dir']:
            # The compliance sorting must be custom due to the code being stored in multiple columns.
            if args['sort_field'] == "compliance_article_id":
                if args['sort_dir'] == 'desc':
                    filtered_query = apply_filters(
                        query.order_by(
                            desc(cast(ComplianceArticle.section, NUMERIC)),
                            desc(cast(ComplianceArticle.sub_section, NUMERIC)),
                            desc(cast(ComplianceArticle.paragraph, NUMERIC)),
                            desc(ComplianceArticle.sub_paragraph)), conditions)
                elif args['sort_dir'] == 'asc':
                    filtered_query = apply_filters(
                        query.order_by(
                            cast(ComplianceArticle.section, NUMERIC),
                            cast(ComplianceArticle.sub_section, NUMERIC),
                            cast(ComplianceArticle.paragraph, NUMERIC),
                            ComplianceArticle.sub_paragraph), conditions)
            elif args['sort_field'] == "lead_inspector":
                query = query.outerjoin(
                    Party, Variance.inspector_party_guid == Party.party_guid)
                filtered_query = apply_filters(query, conditions)
                sort_criteria = [{
                    'model': 'Party',
                    'field': 'party_name',
                    'direction': args['sort_dir']
                }]
                filtered_query = apply_sort(filtered_query, sort_criteria)
            else:
                filtered_query = apply_filters(query, conditions)
                sort_criteria = [{
                    'model': sort_models[args['sort_field']],
                    'field': args['sort_field'],
                    'direction': args['sort_dir']
                }]
                filtered_query = apply_sort(filtered_query, sort_criteria)
        else:
            filtered_query = apply_filters(query, conditions)

            # default sorting is by submission date descending
            sort_criteria = [{
                'model': 'Variance',
                'field': 'received_date',
                'direction': 'desc'
            }]
            filtered_query = apply_sort(filtered_query, sort_criteria)

        return apply_pagination(filtered_query, args["page_number"],
                                args["page_size"])
 def get(self):
     return VarianceApplicationStatusCode.get_active()
Beispiel #4
0
def RandomVarianceApplicationStatusCode():
    return random.choice([
        x.variance_application_status_code for x in filter(
            lambda x: x.variance_application_status_code not in ['APP', 'DEN'],
            VarianceApplicationStatusCode.get_active())
    ])