예제 #1
0
 def test_get_attribute_3_level(self):
     self.assertEqual(
         get_value_for_db_field(self.prod1, 'del_line__delivery__address'),
         'My Home')
     self.assertEqual(
         get_value_for_db_field(self.prod1,
                                'del_line__delivery__prop_address'),
         'My Home')
예제 #2
0
    def filter_pks(self, initial_pk_list, queryset, value, *, or_pk_list=None):
        """
        Filter the Given Queryset against the given value and return a list of matching Primary Keys.

        if initial_pk_list is not None only those Primary Keys will be considered
        """
        # If no Value given we don't need to filter at all
        if value in EMPTY_VALUES:
            return initial_pk_list

        # Not None but empty List, Nothing to do, No chance for a find
        if initial_pk_list is not None and not initial_pk_list:
            return []

        # Filter all values from queryset, get the pk list
        wanted_pks = set()
        for obj in queryset:
            # If we have an initial pk list (AND condition) we must check only those items
            if initial_pk_list is not None and obj.pk not in initial_pk_list:
                continue

            # If we have an or_pk_list then those entries we don't need to check anymore and can just add them
            if or_pk_list is not None and obj.pk in or_pk_list:
                wanted_pks.add(obj.pk)
                continue

            property_value = get_value_for_db_field(obj,
                                                    self.property_fld_name)
            if self._compare_lookup_with_qs_entry(self.lookup_expr, value,
                                                  property_value):
                wanted_pks.add(obj.pk)

        return wanted_pks
예제 #3
0
    def field(self):
        """Filed Property to setup default choices."""
        queryset = self.model._default_manager.distinct()  # pylint: disable=no-member,protected-access

        value_list = set()
        for obj in queryset:
            property_value = get_value_for_db_field(obj,
                                                    self.property_fld_name)
            value_list.add(property_value)

        value_list = sorted(value_list, key=lambda x: (x is None, x))

        self.extra['choices'] = [(prop, str(prop)) for prop in value_list]

        # Need to Call parent's Parent since our Parent uses DB fields directly
        return super(AllValuesMultipleFilter, self).field
예제 #4
0
    def sorted_pk_list_from_property(self, sort_property, queryset):  # pylint: disable=no-self-use
        """Sorting the primary key list of the given queryset based on the given property."""
        # Identify the sort order
        descending = False
        if sort_property.startswith('-'):
            descending = True
            sort_property = sort_property[1:]

        # Build a list of pk and value, this might become very large depending on data type
        # Need to use a list because set will loose order
        value_list = []
        for obj in queryset:
            property_value = get_value_for_db_field(obj, sort_property)
            value_list.append((obj.pk, property_value))

        # Sort the list of tuples
        value_list = sorted(value_list, key=lambda x: x[1], reverse=descending)

        # Get a list of sorted primary keys
        value_list = [entry[0] for entry in value_list]

        return value_list
예제 #5
0
 def test_get_attribute_2_level(self):
     self.assertEqual(
         get_value_for_db_field(self.prod1, 'del_line__line_no'), 1)
     self.assertEqual(
         get_value_for_db_field(self.prod1, 'del_line__prop_line_no'), 1)
예제 #6
0
 def test_get_attribute_1_level(self):
     self.assertEqual(get_value_for_db_field(self.prod1, 'name'),
                      'Sun Rice')
     self.assertEqual(get_value_for_db_field(self.prod1, 'prop_name'),
                      'Sun Rice')