comparison core/management/commands/ssl_images.py @ 889:ae146e30d588

Ensure the cache is populated correctly.
author Brian Neal <bgneal@gmail.com>
date Tue, 10 Feb 2015 20:36:34 -0600
parents deef1536a54a
children 101728976f9c
comparison
equal deleted inserted replaced
888:deef1536a54a 889:ae146e30d588
169 # It has been observed that at least 2 different services 169 # It has been observed that at least 2 different services
170 # serve up the same image on https: with the URL otherwise the same. 170 # serve up the same image on https: with the URL otherwise the same.
171 # Check to see if the image is available via https first. 171 # Check to see if the image is available via https first.
172 new_url = check_https_availability(parsed_url) 172 new_url = check_https_availability(parsed_url)
173 if new_url: 173 if new_url:
174 url_cache[src] = new_url
174 return new_url 175 return new_url
175 176
176 # If none of the above worked, try to download and upload to our S3 bucket 177 # If none of the above worked, try to download and upload to our S3 bucket
177 return save_image_to_cloud(src) 178 new_url = save_image_to_cloud(src)
179 if new_url:
180 url_cache[src] = new_url
181 return new_url
178 182
179 183
180 def check_https_availability(parsed_url): 184 def check_https_availability(parsed_url):
181 """Given a urlparse.urlparse() result, perform a HEAD request over https 185 """Given a urlparse.urlparse() result, perform a HEAD request over https
182 using the same net location and path. If we get a response that indicates an 186 using the same net location and path. If we get a response that indicates an
213 Returns the new URL or None if unsuccessful. 217 Returns the new URL or None if unsuccessful.
214 """ 218 """
215 fn = download_image(src) 219 fn = download_image(src)
216 if fn: 220 if fn:
217 resize_image(fn) 221 resize_image(fn)
218 new_url = upload_image(fn) 222 return upload_image(fn)
219 if new_url:
220 url_cache[src] = new_url
221 return new_url
222 return None 223 return None
223 224
224 225
225 def replace_image_markup(match): 226 def replace_image_markup(match):
226 src_parts = match.group(8).split() 227 src_parts = match.group(8).split()
353 logger.info("Processing %s #%d (pk = %d)", model_name, n + i, model.pk) 354 logger.info("Processing %s #%d (pk = %d)", model_name, n + i, model.pk)
354 txt = getattr(model, text_attr) 355 txt = getattr(model, text_attr)
355 warn_if_image_refs(txt, model_name, model.pk) 356 warn_if_image_refs(txt, model_name, model.pk)
356 new_txt = process_post(txt) 357 new_txt = process_post(txt)
357 if txt != new_txt: 358 if txt != new_txt:
358 logger.info("Content changed on %s #%d (pk= %d)", 359 logger.info("Content changed on %s #%d (pk = %d)",
359 model_name, n + i, model.pk) 360 model_name, n + i, model.pk)
360 logger.debug("original: %s", txt) 361 logger.debug("original: %s", txt)
361 logger.debug("changed: %s", new_txt) 362 logger.debug("changed: %s", new_txt)
362 setattr(model, text_attr, new_txt) 363 setattr(model, text_attr, new_txt)
363 model.save() 364 model.save()