comparison core/management/commands/ssl_images.py @ 871:6900040df0f8

More WIP on ssl_images command & unit test.
author Brian Neal <bgneal@gmail.com>
date Tue, 16 Dec 2014 20:59:49 -0600
parents ee56c8c8cf98
children 13f2d4393ec4
comparison
equal deleted inserted replaced
870:ee56c8c8cf98 871:6900040df0f8
22 22
23 23
24 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log') 24 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
25 logger = logging.getLogger(__name__) 25 logger = logging.getLogger(__name__)
26 26
27 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE, re.UNICODE) 27 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE,
28 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE, re.UNICODE) 28 re.DOTALL | re.UNICODE)
29 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE,
30 re.DOTALL | re.UNICODE)
29 31
30 SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com']) 32 SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com'])
31 MODEL_CHOICES = ['comments', 'posts'] 33 MODEL_CHOICES = ['comments', 'posts']
32 34
33 quit_flag = False 35 quit_flag = False
65 title = '' 67 title = ''
66 if len(src_parts) > 1: 68 if len(src_parts) > 1:
67 title = " ".join(src_parts[1:]) 69 title = " ".join(src_parts[1:])
68 alt = match.group(1) 70 alt = match.group(1)
69 71
70 new_src = '' 72 new_src = None
71 if src: 73 if src:
72 r = urlparse.urlparse(src) 74 r = urlparse.urlparse(src)
73 if r.scheme == 'http': 75 if r.hostname in SG101_HOSTS:
74 if r.hostname in SG101_HOSTS: 76 new_src = r.path # convert to relative path
75 new_src = r.path # convert to relative path 77 elif r.scheme == 'http':
76 else: 78 new_src = save_image_to_cloud(src)
77 new_src = save_image_to_cloud(src)
78 elif r.scheme == 'https': 79 elif r.scheme == 'https':
79 new_src = src # already https, accept it as-is 80 new_src = src # already https, accept it as-is
80 81
81 if new_src: 82 if new_src:
82 if title: 83 if title:
83 s = u'![{alt}]({src} "{title}")'.format(alt=alt, src=new_src, title=title) 84 s = u'![{alt}]({src} {title})'.format(alt=alt, src=new_src, title=title)
84 else: 85 else:
85 s = u'![{alt}]({src})'.format(alt=alt, src=new_src) 86 s = u'![{alt}]({src})'.format(alt=alt, src=new_src)
86 else: 87 else:
87 # something's messed up, convert to a link using original src 88 # something's messed up, convert to a link using original src
88 s = u'[{alt}]({src})'.format(alt=alt, src=src) 89 s = u'[{alt}]({src})'.format(alt=alt, src=src)