17
17
from rest_framework .routers import DefaultRouter
18
18
from rest_framework .viewsets import ModelViewSet
19
19
from rest_framework .decorators import api_view
20
+ from rest_framework .settings import api_settings
21
+ from rest_framework_swagger .compat import strip_tags
20
22
import rest_framework
21
23
22
24
from .decorators import wrapper_to_func , func_to_wrapper
@@ -1919,6 +1921,15 @@ def my_view_mocker2(view):
1919
1921
1920
1922
@override_settings (** reST_SETTINGS )
1921
1923
class RESTDocstringTests (TestCase ):
1924
+ def setUp (self ):
1925
+ from rest_framework_swagger .views import get_restructuredtext
1926
+ self .view_func = api_settings .VIEW_DESCRIPTION_FUNCTION
1927
+ api_settings .VIEW_DESCRIPTION_FUNCTION = get_restructuredtext
1928
+ self .assertEqual (get_restructuredtext , api_settings .VIEW_DESCRIPTION_FUNCTION )
1929
+
1930
+ def tearDown (self ):
1931
+ api_settings .VIEW_DESCRIPTION_FUNCTION = self .view_func
1932
+
1922
1933
def test_get_summary_empty (self ):
1923
1934
class MyViewSet (ModelViewSet ):
1924
1935
model = User
@@ -1935,6 +1946,7 @@ def test_get_summary_view(self):
1935
1946
class MyViewSet (ModelViewSet ):
1936
1947
"""
1937
1948
Oh yes this is reST
1949
+ Oh good there are waffles
1938
1950
============
1939
1951
"""
1940
1952
model = User
@@ -1945,7 +1957,7 @@ class MyViewSet(ModelViewSet):
1945
1957
class_introspector = make_viewset_introspector (MyViewSet )
1946
1958
introspector = get_introspectors (class_introspector )['create' ]
1947
1959
summary = introspector .get_summary ()
1948
- self .assertEqual ("Oh yes this is reST" , summary )
1960
+ self .assertEqual ("Oh yes this is reST" , summary . strip () )
1949
1961
1950
1962
def test_get_summary_raw (self ):
1951
1963
class MyViewSet (ModelViewSet ):
@@ -1964,7 +1976,7 @@ class MyViewSet(ModelViewSet):
1964
1976
tx = IntrospectorHelper .get_summary (
1965
1977
introspector .callback ,
1966
1978
'My comment\n ---\n omit_serializer: true' )
1967
- self .assertEqual ("My comment" , tx )
1979
+ self .assertEqual ("My comment" , tx . strip () )
1968
1980
1969
1981
def test_get_yaml_viewset (self ):
1970
1982
class MyViewSet (ModelViewSet ):
@@ -2043,3 +2055,61 @@ class MyViewSet(ModelViewSet):
2043
2055
docs = introspector .get_notes ()
2044
2056
self .assertIn ('Oh yes this is reST' , docs )
2045
2057
self .assertIn ('Oh yes so is this' , docs )
2058
+
2059
+
2060
+ def get_custom_description (view_cls , html = False ):
2061
+ description = view_cls .__doc__ or ''
2062
+ description = description .strip ()
2063
+ if html :
2064
+ description = description .replace ('\n ' , '<tacos />' )
2065
+ return description
2066
+
2067
+
2068
+ custom_SETTINGS = {
2069
+ 'REST_FRAMEWORK' : {
2070
+ 'VIEW_DESCRIPTION_FUNCTION' : get_custom_description
2071
+ }
2072
+ }
2073
+
2074
+
2075
+ @override_settings (** custom_SETTINGS )
2076
+ class CustomDocstringTests (TestCase ):
2077
+ def setUp (self ):
2078
+ self .view_func = api_settings .VIEW_DESCRIPTION_FUNCTION
2079
+ api_settings .VIEW_DESCRIPTION_FUNCTION = get_custom_description
2080
+ self .assertEqual (get_custom_description , api_settings .VIEW_DESCRIPTION_FUNCTION )
2081
+
2082
+ def tearDown (self ):
2083
+ api_settings .VIEW_DESCRIPTION_FUNCTION = self .view_func
2084
+
2085
+ def test_custom_function_works (self ):
2086
+ from rest_framework_swagger .introspectors import get_view_description
2087
+ s = get_view_description (
2088
+ CommentSerializer , html = True ,
2089
+ docstring = "hiya bad boy\n tacos" )
2090
+ self .assertEqual ("hiya bad boy<tacos />tacos" , s )
2091
+
2092
+ def test_get_summary_view (self ):
2093
+ class MyViewSet (ModelViewSet ):
2094
+ """
2095
+ Oh yes this is reST
2096
+ Oh good there are waffles
2097
+ ============
2098
+ """
2099
+ model = User
2100
+ serializer_class = CommentSerializer
2101
+ paginate_by = 20
2102
+ paginate_by_param = 'page_this_by'
2103
+
2104
+ class_introspector = make_viewset_introspector (MyViewSet )
2105
+ introspector = get_introspectors (class_introspector )['create' ]
2106
+ summary = introspector .get_summary ()
2107
+ self .assertEqual ("Oh yes this is reST" , summary )
2108
+
2109
+
2110
+ class TestStripTags (TestCase ):
2111
+ def test1 (self ):
2112
+ self .assertEqual ('tacos' , strip_tags ('tacos' ))
2113
+ self .assertEqual ('tacos' , strip_tags ('<p>tacos</p>' ))
2114
+ self .assertEqual ('tacobeans' , strip_tags ('<p>taco</p>beans' ))
2115
+ self .assertEqual ('tacobeans' , strip_tags ('taco<p>beans</p>' ))
0 commit comments