@@ -89,42 +89,62 @@ def post(self, request, *args, **kwargs):
8989 if form .is_valid () and entities_set .is_valid ():
9090 data_set = [entity_form .cleaned_data for entity_form in entities_set ]
9191 # Otherwise choices are empty
92- attributes .fields ["attributes" ].choices = utils .load_attributes (
93- self .project , data_set
94- )
95- if attributes .is_valid ():
96- instance = form .save (commit = False )
92+ try :
93+ attributes .fields ["attributes" ].choices = utils .load_attributes (
94+ self .project , data_set
95+ )
96+ if attributes .is_valid ():
97+ instance = form .save (commit = False )
9798
98- entities = []
99+ entities = []
99100
100- for entity_form in entities_set :
101- if entity_form .cleaned_data :
102- entity_selector = entity_form .cleaned_data ["entity_selector" ]
103- type_selector = entity_form .cleaned_data ["type_selector" ]
104- pattern = EntityPattern (
105- id = (
106- entity_form .cleaned_data ["entity_id" ]
107- if entity_selector == "id"
108- else None
109- ),
110- idPattern = (
111- re .compile (entity_form .cleaned_data ["entity_id" ])
112- if entity_selector == "id_pattern"
113- else None
114- ),
115- type = (
116- entity_form .cleaned_data ["entity_type" ]
117- if entity_form .cleaned_data ["entity_type" ]
118- and type_selector == "type"
119- else None
120- ),
121- typePattern = (
122- re .compile (entity_form .cleaned_data ["entity_type" ])
123- if type_selector == "type_pattern"
124- else None
125- ),
126- )
127- entities .append (pattern )
101+ for entity_form in entities_set :
102+ if entity_form .cleaned_data :
103+ entity_selector = entity_form .cleaned_data [
104+ "entity_selector"
105+ ]
106+ type_selector = entity_form .cleaned_data ["type_selector" ]
107+ pattern = EntityPattern (
108+ id = (
109+ entity_form .cleaned_data ["entity_id" ]
110+ if entity_selector == "id"
111+ else None
112+ ),
113+ idPattern = (
114+ utils .safe_compile (
115+ entity_form .cleaned_data ["entity_id" ]
116+ )
117+ if entity_selector == "id_pattern"
118+ else None
119+ ),
120+ type = (
121+ entity_form .cleaned_data ["entity_type" ]
122+ if entity_form .cleaned_data ["entity_type" ]
123+ and type_selector == "type"
124+ else None
125+ ),
126+ typePattern = (
127+ utils .safe_compile (
128+ entity_form .cleaned_data ["entity_type" ]
129+ )
130+ if type_selector == "type_pattern"
131+ else None
132+ ),
133+ )
134+ entities .append (pattern )
135+ except re .error as e :
136+ logger .error (
137+ str (
138+ self .request .user .first_name
139+ if self .request .user .first_name
140+ else self .request .user .username
141+ )
142+ + " tried creating the subscription "
143+ + " but failed "
144+ f" in project { self .project .name } "
145+ )
146+ messages .error (request , e )
147+ return self .form_invalid (form )
128148 with ContextBrokerClient (
129149 url = settings .CB_URL ,
130150 fiware_header = FiwareHeader (
0 commit comments