@@ -50,6 +50,65 @@ def find_schema_for_view(view: View):
5050 return prop_schema
5151
5252
53+ def build_forms_for_view (rules : list , view : View , op : list ):
54+ """Build a W3C form description for a particular View
55+
56+ Args:
57+ rules (list): List of Flask rules
58+ view (View): View class
59+ op (list): List of Form operations
60+
61+ Returns:
62+ [dict]: Form description
63+ """
64+ forms = []
65+ prop_urls = [rule_to_path (rule ) for rule in rules ]
66+
67+ content_type = get_topmost_spec_attr (view , "_content_type" ) or "application/json"
68+
69+ for url in prop_urls :
70+ forms .append ({"op" : op , "href" : url , "contentType" : content_type })
71+
72+ return forms
73+
74+
75+ def view_to_thing_property_forms (rules : list , view : View ):
76+ """Build a W3C form description for a PropertyView
77+
78+ Args:
79+ rules (list): List of Flask rules
80+ view (View): View class
81+ op (list): List of Form operations
82+
83+ Returns:
84+ [dict]: Form description
85+ """
86+ readable = hasattr (view , "post" ) or hasattr (view , "put" ) or hasattr (view , "delete" )
87+ writeable = hasattr (view , "get" )
88+
89+ op = []
90+ if readable :
91+ op .append ("readproperty" )
92+ if writeable :
93+ op .append ("writeproperty" )
94+
95+ return build_forms_for_view (rules , view , op = op )
96+
97+
98+ def view_to_thing_action_forms (rules : list , view : View ):
99+ """Build a W3C form description for an ActionView
100+
101+ Args:
102+ rules (list): List of Flask rules
103+ view (View): View class
104+ op (list): List of Form operations
105+
106+ Returns:
107+ [dict]: Form description
108+ """
109+ return build_forms_for_view (rules , view , op = ["invokeaction" ])
110+
111+
53112class ThingDescription :
54113 def __init__ (self , apispec : APISpec ):
55114 self ._apispec = weakref .ref (apispec )
@@ -91,21 +150,26 @@ def add_link(self, view, rel, kwargs=None, params=None):
91150
92151 def to_dict (self ):
93152 return {
94- "@context" : ["https://iot.mozilla.org/schemas/" ],
153+ "@context" : [
154+ "https://www.w3.org/2019/wot/td/v1" ,
155+ "https://iot.mozilla.org/schemas/" ,
156+ ],
95157 "@type" : current_labthing ().types ,
96158 "id" : url_for ("root" , _external = True ),
97159 "base" : request .host_url ,
98160 "title" : current_labthing ().title ,
99161 "description" : current_labthing ().description ,
100162 "properties" : self .properties ,
101163 "actions" : self .actions ,
102- "events" : self .events , # TODO: Enable once properly populated
164+ # "events": self.events, # TODO: Enable once properly populated
103165 "links" : self .links ,
166+ "securityDefinitions" : {"nosec_sc" : {"scheme" : "nosec" }},
167+ "security" : "nosec_sc" ,
104168 }
105169
106170 def event_to_thing_event (self , event : Event ):
107171 # TODO: Include event schema
108- return {}
172+ return {"forms" : [] }
109173
110174 def view_to_thing_property (self , rules : list , view : View ):
111175 prop_urls = [rule_to_path (rule ) for rule in rules ]
@@ -122,8 +186,8 @@ def view_to_thing_property(self, rules: list, view: View):
122186 hasattr (view , "post" ) or hasattr (view , "put" ) or hasattr (view , "delete" )
123187 ),
124188 "writeOnly" : not hasattr (view , "get" ),
125- # TODO: Make URLs absolute
126189 "links" : [{"href" : f"{ url } " } for url in prop_urls ],
190+ "forms" : view_to_thing_property_forms (rules , view ),
127191 "uriVariables" : {},
128192 ** get_semantic_type (view ),
129193 }
@@ -181,6 +245,7 @@ def view_to_thing_action(self, rules: list, view: View):
181245 or (get_docstring (view .post ) if hasattr (view , "post" ) else "" ),
182246 # TODO: Make URLs absolute
183247 "links" : [{"href" : f"{ url } " } for url in action_urls ],
248+ "forms" : view_to_thing_action_forms (rules , view ),
184249 "safe" : is_safe ,
185250 "idempotent" : is_idempotent ,
186251 }
0 commit comments