@@ -107,6 +107,8 @@ def get_queryset(self):
107107
108108
109109class BaseConfigSerializer (ValidatedDeviceIdSerializer ):
110+ exclude_validation = ['device' ]
111+
110112 class Meta :
111113 model = Config
112114 fields = ['status' , 'error_reason' , 'backend' , 'templates' , 'context' , 'config' ]
@@ -131,14 +133,16 @@ def validate(self, data):
131133 """
132134 # Existing device
133135 device = self .context .get ('device' )
136+ # data.pop('device', None)
137+ # import ipdb; ipdb.set_trace()
134138 if not self .instance and device :
135139 # Existing device with existing config
136140 # Or it's an exsiting device with a new config
137141 if device ._has_config ():
138142 self .instance = device .config
139- return super ().validate (data )
143+ # return super().validate(data)
140144 # New device
141- self .exclude_validation = ['device' ]
145+ # self.exclude_validation = ['device']
142146 return super ().validate (data )
143147
144148
@@ -153,11 +157,29 @@ def _prepare_config(self, device, config_data):
153157 config .full_clean ()
154158 return config
155159
160+ def _is_config_data_relevant (self , config_data ):
161+ """
162+ Returns True if ``config_data`` does not equal
163+ the default values and hence the config is useful.
164+ """
165+ return not (
166+ config_data .get ('backend' ) == app_settings .DEFAULT_BACKEND
167+ and not config_data .get ('templates' )
168+ and not config_data .get ('context' )
169+ and not config_data .get ('config' )
170+ )
171+
156172 @transaction .atomic
157173 def _create_config (self , device , config_data ):
174+ # templates = config_data.get('templates')
175+ # if len(templates) == 1 and templates[0].name == 't1-org2':
176+ # import ipdb; ipdb.set_trace()
158177 config_templates = self ._get_config_templates (config_data )
159178 try :
160179 if not device ._has_config ():
180+ # if the user hasn't set any useful config data, skip
181+ if not self ._is_config_data_relevant (config_data ):
182+ return
161183 config = Config (device = device , ** config_data )
162184 config .full_clean ()
163185 config .save ()
@@ -173,15 +195,10 @@ def _create_config(self, device, config_data):
173195 raise serializers .ValidationError ({'config' : error .messages })
174196
175197 def _update_config (self , device , config_data ):
176- if (
177- config_data .get ('backend' ) == app_settings .DEFAULT_BACKEND
178- and not config_data .get ('templates' )
179- and not config_data .get ('context' )
180- and not config_data .get ('config' )
181- ):
182- # Do not create Config object if config_data only
183- # contains the default value.
184- # See https://github.com/openwisp/openwisp-controller/issues/699
198+ # Do not create Config object if config_data only
199+ # contains the default values.
200+ # See https://github.com/openwisp/openwisp-controller/issues/699
201+ if not self ._is_config_data_relevant (config_data ):
185202 return
186203 if not device ._has_config ():
187204 return self ._create_config (device , config_data )
@@ -199,6 +216,10 @@ def _update_config(self, device, config_data):
199216 except ValidationError as error :
200217 raise serializers .ValidationError ({'config' : error .messages })
201218
219+ # def run_validation(self, *args, **kwargs):
220+ # import ipdb; ipdb.set_trace()
221+ # return super().run_validation(*args, **kwargs)
222+
202223
203224class DeviceListConfigSerializer (BaseConfigSerializer ):
204225 config = serializers .JSONField (
@@ -265,6 +286,10 @@ class DeviceDetailConfigSerializer(BaseConfigSerializer):
265286 )
266287 templates = FilterTemplatesByOrganization (many = True )
267288
289+ # def validate(self, *args, **kwargs):
290+ # import ipdb; ipdb.set_trace()
291+ # return super().validate(*args, **kwargs)
292+
268293
269294class DeviceDetailSerializer (DeviceConfigSerializer ):
270295 config = DeviceDetailConfigSerializer (allow_null = True )
@@ -291,6 +316,10 @@ class Meta(BaseMeta):
291316 'modified' ,
292317 ]
293318
319+ # def to_internal_value(self, *args, **kwargs):
320+ # import ipdb; ipdb.set_trace()
321+ # return super().to_internal_value(*args, **kwargs)
322+
294323 def update (self , instance , validated_data ):
295324 config_data = validated_data .pop ('config' , {})
296325 raw_data_for_signal_handlers = {
@@ -299,7 +328,7 @@ def update(self, instance, validated_data):
299328 if config_data :
300329 self ._update_config (instance , config_data )
301330
302- elif hasattr ( instance , 'config' ) and validated_data .get ('organization' ):
331+ elif instance . _has_config ( ) and validated_data .get ('organization' ):
303332 if instance .organization != validated_data .get ('organization' ):
304333 # config.device.organization is used for validating
305334 # the organization of templates. It is also used for adding
0 commit comments