18
18
19
19
20
20
class LinkCheckHandler (ClientHandler ):
21
-
22
21
# Customize the ClientHandler to allow us removing some middlewares
23
22
24
23
def load_middleware (self ):
25
- self .ignore_keywords = [' reversion.middleware' , ' MaintenanceModeMiddleware' , ' raven_compat' ]
24
+ self .ignore_keywords = [" reversion.middleware" , " MaintenanceModeMiddleware" , " raven_compat" ]
26
25
super ().load_middleware ()
27
26
new_request_middleware = []
28
27
@@ -122,22 +121,20 @@ def check_links(external_recheck_interval=10080, limit=-1, check_internal=True,
122
121
123
122
124
123
def update_urls (urls , content_type , object_id ):
125
-
126
124
# Structure of urls param is [(field, link text, url), ... ]
127
125
128
126
urls_created = links_created = 0
129
127
new_url_ids = set ()
130
128
new_link_ids = set ()
131
129
132
130
for field , link_text , url in urls :
133
-
134
- if url is not None and url .startswith ('#' ):
131
+ if url is not None and url .startswith ("#" ):
135
132
instance = content_type .get_object_for_this_type (id = object_id )
136
133
url = instance .get_absolute_url () + url
137
134
138
135
if len (url ) > MAX_URL_LENGTH :
139
136
# We cannot handle url longer than MAX_URL_LENGTH at the moment
140
- logger .warning (' URL exceeding max length will be skipped: %s' , url )
137
+ logger .warning (" URL exceeding max length will be skipped: %s" , url )
141
138
continue
142
139
143
140
url , url_created = Url .objects .get_or_create (url = url )
@@ -171,9 +168,8 @@ def update_urls(urls, content_type, object_id):
171
168
172
169
173
170
def find_all_links (linklists = None ):
174
-
175
171
if linklists is None :
176
- linklists = apps .get_app_config (' linkcheck' ).all_linklists
172
+ linklists = apps .get_app_config (" linkcheck" ).all_linklists
177
173
178
174
urls_created = links_created = 0
179
175
new_url_ids = set ()
@@ -183,13 +179,12 @@ def find_all_links(linklists=None):
183
179
links_before = Link .objects .count ()
184
180
185
181
for linklist_name , linklist_cls in linklists .items ():
186
-
187
182
content_type = linklist_cls .content_type ()
188
183
linklists = linklist_cls ().get_linklist ()
189
184
190
185
for linklist in linklists :
191
- object_id = linklist [' object' ].id
192
- urls = linklist [' urls' ] + linklist [' images' ]
186
+ object_id = linklist [" object" ].id
187
+ urls = linklist [" urls" ] + linklist [" images" ]
193
188
if urls :
194
189
new = update_urls (urls , content_type , object_id )
195
190
@@ -227,6 +222,7 @@ def unignore():
227
222
228
223
# Utilities for testing models coverage
229
224
225
+
230
226
def is_interesting_field (field ):
231
227
return is_url_field (field ) or is_image_field (field ) or is_html_field (field )
232
228
@@ -244,68 +240,63 @@ def is_html_field(field):
244
240
245
241
246
242
def has_active_field (klass ):
247
- return any (
248
- field .name == 'active' and isinstance (field , models .BooleanField )
249
- for field in klass ._meta .fields
250
- )
243
+ return any (field .name == "active" and isinstance (field , models .BooleanField ) for field in klass ._meta .fields )
251
244
252
245
253
246
def get_ignore_empty_fields (klass ):
254
- return [
255
- field
256
- for field in klass ._meta .fields
257
- if is_interesting_field (field ) and (field .blank or field .null )
258
- ]
247
+ return [field for field in klass ._meta .fields if is_interesting_field (field ) and (field .blank or field .null )]
259
248
260
249
261
250
def get_type_fields (klass , the_type ):
262
251
check_funcs = {
263
- ' html' : is_html_field ,
264
- ' url' : is_url_field ,
265
- ' image' : is_image_field ,
252
+ " html" : is_html_field ,
253
+ " url" : is_url_field ,
254
+ " image" : is_image_field ,
266
255
}
267
256
check_func = check_funcs [the_type ]
268
257
return [field for field in klass ._meta .fields if check_func (field )]
269
258
270
259
271
260
def is_model_covered (klass ):
272
- app = apps .get_app_config (' linkcheck' )
261
+ app = apps .get_app_config (" linkcheck" )
273
262
return any (linklist [1 ].model == klass for linklist in app .all_linklists .items ())
274
263
275
264
276
265
def format_config (meta , active_field , html_fields , image_fields , url_fields , ignore_empty_fields ):
277
- config = f' from { meta .app_label } .models import { meta .object_name } \n \n '
278
- config += f' class { meta .object_name } Linklist(Linklist):\n '
279
- config += f' model = { meta .object_name } \n '
266
+ config = f" from { meta .app_label } .models import { meta .object_name } \n \n "
267
+ config += f" class { meta .object_name } Linklist(Linklist):\n "
268
+ config += f" model = { meta .object_name } \n "
280
269
if html_fields :
281
- config += f' html_fields = [{ ", " .join (map (str , html_fields ))} ]\n '
270
+ config += f" html_fields = [{ ', ' .join (map (str , html_fields ))} ]\n "
282
271
if image_fields :
283
- config += f' image_fields = [{ ", " .join (map (str , image_fields ))} ]\n '
272
+ config += f" image_fields = [{ ', ' .join (map (str , image_fields ))} ]\n "
284
273
if url_fields :
285
- config += f' url_fields = [{ ", " .join (map (str , url_fields ))} ]\n '
274
+ config += f" url_fields = [{ ', ' .join (map (str , url_fields ))} ]\n "
286
275
if ignore_empty_fields :
287
- config += f' ignore_empty = [{ ", " .join (map (str , ignore_empty_fields ))} ]\n '
276
+ config += f" ignore_empty = [{ ', ' .join (map (str , ignore_empty_fields ))} ]\n "
288
277
if active_field :
289
278
config += ' object_filter = {"active": True}\n '
290
- config += f'\n linklists = {{\n "{ meta .object_name } ": { meta .object_name } Linklist,\n }}\n '
279
+ config += f'\n linklists = {{\n "{ meta .object_name } ": { meta .object_name } Linklist,\n }}\n '
291
280
return config
292
281
293
282
294
283
def get_suggested_linklist_config (klass ):
295
284
meta = klass ._meta
296
- html_fields = get_type_fields (klass , ' html' )
297
- url_fields = get_type_fields (klass , ' url' )
298
- image_fields = get_type_fields (klass , ' image' )
285
+ html_fields = get_type_fields (klass , " html" )
286
+ url_fields = get_type_fields (klass , " url" )
287
+ image_fields = get_type_fields (klass , " image" )
299
288
active_field = has_active_field (klass )
300
289
ignore_empty_fields = get_ignore_empty_fields (klass )
301
- return format_config (** {
302
- 'meta' : meta ,
303
- 'html_fields' : html_fields ,
304
- 'url_fields' : url_fields ,
305
- 'image_fields' : image_fields ,
306
- 'active_field' : active_field ,
307
- 'ignore_empty_fields' : ignore_empty_fields ,
308
- })
290
+ return format_config (
291
+ ** {
292
+ "meta" : meta ,
293
+ "html_fields" : html_fields ,
294
+ "url_fields" : url_fields ,
295
+ "image_fields" : image_fields ,
296
+ "active_field" : active_field ,
297
+ "ignore_empty_fields" : ignore_empty_fields ,
298
+ }
299
+ )
309
300
310
301
311
302
def get_coverage_data ():
@@ -318,7 +309,7 @@ def get_coverage_data():
318
309
for app in apps .get_app_configs ():
319
310
for model in app .get_models ():
320
311
should_append = False
321
- if getattr (model , ' get_absolute_url' , None ):
312
+ if getattr (model , " get_absolute_url" , None ):
322
313
should_append = True
323
314
else :
324
315
for field in model ._meta .fields :
@@ -327,11 +318,13 @@ def get_coverage_data():
327
318
break
328
319
if should_append :
329
320
if is_model_covered (model ):
330
- covered .append (f' { model ._meta .app_label } .{ model ._meta .object_name } ' )
321
+ covered .append (f" { model ._meta .app_label } .{ model ._meta .object_name } " )
331
322
else :
332
- uncovered .append ((
333
- f'{ model ._meta .app_label } .{ model ._meta .object_name } ' ,
334
- get_suggested_linklist_config (model ),
335
- ))
323
+ uncovered .append (
324
+ (
325
+ f"{ model ._meta .app_label } .{ model ._meta .object_name } " ,
326
+ get_suggested_linklist_config (model ),
327
+ )
328
+ )
336
329
337
330
return covered , uncovered
0 commit comments