def test_format_value_boolean_or_none(self): """ Tests format_value with booleans and None """ self.assertEqual(format_value(True), '<code>true</code>') self.assertEqual(format_value(False), '<code>false</code>') self.assertEqual(format_value(None), '<code>null</code>')
def test_format_value_list(self): """ Tests format_value with a list of strings """ list_items = ['item1', 'item2', 'item3'] self.assertEqual(format_value(list_items), '\n item1, item2, item3\n') self.assertEqual(format_value([]), '\n\n')
def test_format_value_boolean_or_none(self): """ Tests format_value with booleans and None """ self.assertEqual(format_value(True), '<code>true</code>') self.assertEqual(format_value(False), '<code>false</code>') self.assertEqual(format_value(None), '<code>null</code>')
def test_format_value_list(self): """ Tests format_value with a list of strings """ list_items = ['item1', 'item2', 'item3'] self.assertEqual(format_value(list_items), '\n item1, item2, item3\n') self.assertEqual(format_value([]), '\n\n')
def test_format_value_table(self): """ Tests format_value with a list of lists/dicts """ list_of_lists = [['list1'], ['list2'], ['list3']] expected_list_format = """ <tableclass="tabletable-striped"> <tbody> <tr> <th>0</th> <td>list1</td> </tr> <tr> <th>1</th> <td>list2</td> </tr> <tr> <th>2</th> <td>list3</td> </tr> </tbody> </table>""" self.assertEqual(format_html(format_value(list_of_lists)), format_html(expected_list_format)) expected_dict_format = """ <tableclass="tabletable-striped"> <tbody> <tr> <th>0</th> <td></td> </tr> <tr> <th>1</th> <td></td> </tr> <tr> <th>2</th> <td></td> </tr> </tbody> </table>""" list_of_dicts = [{ 'item1': 'value1' }, { 'item2': 'value2' }, { 'item3': 'value3' }] self.assertEqual(format_html(format_value(list_of_dicts)), format_html(expected_dict_format))
def render_to_table(self, value, row_data): """ Renders field value for table view :param value: field value :param row_data: data for entire row (for more complex renderers) :return: rendered value for table view """ choices = getattr(self, 'choices', {}) if value in choices: # choice field: let's render display names, not values return drftt.format_value(choices[value]) return drftt.format_value(value)
def test_format_value_table(self): """ Tests format_value with a list of lists/dicts """ list_of_lists = [['list1'], ['list2'], ['list3']] expected_list_format = """ <tableclass="tabletable-striped"> <tbody> <tr> <th>0</th> <td>list1</td> </tr> <tr> <th>1</th> <td>list2</td> </tr> <tr> <th>2</th> <td>list3</td> </tr> </tbody> </table>""" self.assertEqual( format_html(format_value(list_of_lists)), format_html(expected_list_format) ) expected_dict_format = """ <tableclass="tabletable-striped"> <tbody> <tr> <th>0</th> <td></td> </tr> <tr> <th>1</th> <td></td> </tr> <tr> <th>2</th> <td></td> </tr> </tbody> </table>""" list_of_dicts = [{'item1': 'value1'}, {'item2': 'value2'}, {'item3': 'value3'}] self.assertEqual( format_html(format_value(list_of_dicts)), format_html(expected_dict_format) )
def test_format_value_string_newlines(self): """ Tests format_value with a string with newline characters :return: """ text = 'Dear user, \n this is a message \n from,\nsomeone' self.assertEqual(format_value(text), '<pre>Dear user, \n this is a message \n from,\nsomeone</pre>')
def render_to_table(self, value, row_data): """ Renders field value for table view :param value: field value :param row_data: data for entire row (for more complex renderers) :return: rendered value for table view """ get_queryset = getattr(self, 'get_queryset', None) if isinstance(self, RelatedField) or get_queryset: return self.display_value(value) elif isinstance(self, ManyRelatedField): # Hm, not sure if this is the final thing to do: an example of this field is in # ALC plane editor (modes of takeoff). However, value is a queryset here. There seem to still be DB queries # However, in the example I have, the problem is solved by doing prefetch_related on the m2m relation cr = self.child_relation return ', '.join((cr.display_value(item) for item in value)) # return ', '.join((cr.display_value(item) for item in cr.get_queryset().filter(pk__in=value))) else: choices = getattr(self, 'choices', {}) # Now that we got our choices for related & choice fields, let's first get the value as it would be by DRF check_for_none = value.pk if isinstance(value, PKOnlyObject) else value if check_for_none is None: value = None else: value = super().to_representation(value) if isinstance(value, Hashable) and value in choices: # choice field: let's render display names, not values value = choices[value] if value is None: return DYNAMICFORMS.null_text_table return drftt.format_value(value)
def test_format_value_string_newlines(self): """ Tests format_value with a string with newline characters :return: """ text = 'Dear user, \n this is a message \n from,\nsomeone' self.assertEqual(format_value(text), '<pre>Dear user, \n this is a message \n from,\nsomeone</pre>')
def test_format_value_hyperlink(self): """ Tests format_value with a URL """ url = 'http://url.com' name = 'name_of_url' hyperlink = Hyperlink(url, name) self.assertEqual(format_value(hyperlink), '<a href=%s>%s</a>' % (url, name))
def test_format_value_string_hyperlink(self): """ Tests format_value with a url """ url = 'http://www.example.com' self.assertEqual( format_value(url), '<a href="http://www.example.com">http://www.example.com</a>')
def test_lazy_hyperlink_names(self): global str_called context = {'request': None} serializer = ExampleSerializer(self.example, context=context) JSONRenderer().render(serializer.data) assert not str_called hyperlink_string = format_value(serializer.data['url']) assert hyperlink_string == '<a href=/example/1/>An example</a>' assert str_called
def test_format_value_string_email(self): """ Tests format_value with an email address """ email = '*****@*****.**' self.assertEqual( format_value(email), '<a href="mailto:[email protected]">[email protected]</a>' )
def test_format_value_dict(self): """ Tests format_value with a dict """ test_dict = {'a': 'b'} expected_dict_format = """ <table class="table table-striped"> <tbody> <tr> <th>a</th> <td>b</td> </tr> </tbody> </table>""" self.assertEqual(format_html(format_value(test_dict)), format_html(expected_dict_format))
def test_format_value_dict(self): """ Tests format_value with a dict """ test_dict = {'a': 'b'} expected_dict_format = """ <table class="table table-striped"> <tbody> <tr> <th>a</th> <td>b</td> </tr> </tbody> </table>""" self.assertEqual( format_html(format_value(test_dict)), format_html(expected_dict_format) )
def format_value(value): # TODO: this one pending removal on tasks #62 in #63 completion return drftt.format_value(value)
def admin_format_value(value): return format_value(value)
def test_format_value_object(self): """ Tests that format_value with a object returns the object's __str__ method """ obj = object() self.assertEqual(format_value(obj), obj.__str__())
def test_format_value_string_email(self): """ Tests format_value with an email address """ email = '*****@*****.**' self.assertEqual(format_value(email), '<a href="mailto:[email protected]">[email protected]</a>')
def test_format_value_string_hyperlink(self): """ Tests format_value with a url """ url = 'http://www.example.com' self.assertEqual(format_value(url), '<a href="http://www.example.com">http://www.example.com</a>')
def test_format_value_simple_string(self): """ Tests format_value with a simple string """ simple_string = 'this is an example of a string' self.assertEqual(format_value(simple_string), simple_string)
def test_format_value_simple_string(self): """ Tests format_value with a simple string """ simple_string = 'this is an example of a string' self.assertEqual(format_value(simple_string), simple_string)
def test_format_value_object(self): """ Tests that format_value with a object returns the object's __str__ method """ obj = object() self.assertEqual(format_value(obj), obj.__str__())