def save_attachments(self, src, val): updated = False attachments_to_delete = list( Attachment.objects.attachments_for_object(self.obj)) for url, legend, author in self.filter_attachments(src, val): url = self.base_url + url legend = legend or "" author = author or "" basename, ext = os.path.splitext(os.path.basename(url)) name = '%s%s' % (basename[:128], ext) found = False for attachment in attachments_to_delete: upload_name, ext = os.path.splitext( attachment_upload(attachment, name)) existing_name = attachment.attachment_file.name if re.search( r"^{name}(_[a-zA-Z0-9]{{7}})?{ext}$".format( name=upload_name, ext=ext), existing_name) and not self.has_size_changed( url, attachment): found = True attachments_to_delete.remove(attachment) if author != attachment.author or legend != attachment.legend: attachment.author = author attachment.legend = textwrap.shorten(legend, width=127) attachment.save() updated = True break if found: continue parsed_url = urlparse(url) attachment = Attachment() attachment.content_object = self.obj attachment.filetype = self.filetype attachment.creator = self.creator attachment.author = author attachment.legend = textwrap.shorten(legend, width=127) if (parsed_url.scheme in ('http', 'https') and self.download_attachments) or parsed_url.scheme == 'ftp': content = self.download_attachment(url) if content is None: continue f = ContentFile(content) attachment.attachment_file.save(name, f, save=False) else: attachment.attachment_link = url attachment.save() updated = True if self.delete_attachments: for att in attachments_to_delete: att.delete() return updated
def save_attachments(self, src, val): updated = False for url, legend, author in self.filter_attachments(src, val): url = self.base_url + url legend = legend or u"" author = author or u"" name = os.path.basename(url) found = False for attachment in self.attachments_to_delete.get( self.obj.pk, set()): upload_name, ext = os.path.splitext( attachment_upload(attachment, name)) existing_name = attachment.attachment_file.name if re.search( ur"^{name}(_\d+)?{ext}$".format(name=upload_name, ext=ext), existing_name) and not self.has_size_changed( url, attachment): found = True self.attachments_to_delete[self.obj.pk].remove(attachment) if author != attachment.author or legend != attachment.legend: attachment.author = author attachment.legend = legend attachment.save() updated = True break if found: continue if url[:6] == 'ftp://': try: response = urllib2.urlopen(url) except: self.add_warning( _(u"Failed to download '{url}'").format(url=url)) continue content = response.read() else: response = requests.get(url) if response.status_code != requests.codes.ok: self.add_warning( _(u"Failed to download '{url}'").format(url=url)) continue content = response.content f = ContentFile(content) attachment = Attachment() attachment.content_object = self.obj attachment.attachment_file.save(name, f, save=False) attachment.filetype = self.filetype attachment.creator = self.creator attachment.author = author attachment.legend = legend attachment.save() updated = True
def save_attachments(self, src, val): updated = False for url, legend, author in self.filter_attachments(src, val): url = self.base_url + url legend = legend or u"" author = author or u"" name = os.path.basename(url) found = False for attachment in self.attachments_to_delete.get(self.obj.pk, set()): upload_name, ext = os.path.splitext(attachment_upload(attachment, name)) existing_name = attachment.attachment_file.name if re.search( ur"^{name}(_\d+)?{ext}$".format(name=upload_name, ext=ext), existing_name ) and not self.has_size_changed(url, attachment): found = True self.attachments_to_delete[self.obj.pk].remove(attachment) if author != attachment.author or legend != attachment.legend: attachment.author = author attachment.legend = legend attachment.save() updated = True break if found: continue if url[:6] == "ftp://": try: response = urllib2.urlopen(url) except: self.add_warning(_(u"Failed to download '{url}'").format(url=url)) continue content = response.read() else: response = requests.get(url) if response.status_code != requests.codes.ok: self.add_warning(_(u"Failed to download '{url}'").format(url=url)) continue content = response.content f = ContentFile(content) attachment = Attachment() attachment.content_object = self.obj attachment.attachment_file.save(name, f, save=False) attachment.filetype = self.filetype attachment.creator = self.creator attachment.author = author attachment.legend = legend attachment.save() updated = True
def save_attachments(self, src, val): updated = False attachments_to_delete = list(Attachment.objects.attachments_for_object(self.obj)) for url, legend, author in self.filter_attachments(src, val): url = self.base_url + url legend = legend or u"" author = author or u"" name = os.path.basename(url) found = False for attachment in attachments_to_delete: upload_name, ext = os.path.splitext(attachment_upload(attachment, name)) existing_name = attachment.attachment_file.name if re.search(ur"^{name}(_[a-zA-Z0-9]{{7}})?{ext}$".format( name=upload_name, ext=ext), existing_name ) and not self.has_size_changed(url, attachment): found = True attachments_to_delete.remove(attachment) if author != attachment.author or legend != attachment.legend: attachment.author = author attachment.legend = legend attachment.save() updated = True break if found: continue parsed_url = urlparse(url) attachment = Attachment() attachment.content_object = self.obj attachment.filetype = self.filetype attachment.creator = self.creator attachment.author = author attachment.legend = legend if (parsed_url.scheme in ('http', 'https') and self.download_attachments) or parsed_url.scheme == 'ftp': content = self.download_attachment(url) if content is None: continue f = ContentFile(content) attachment.attachment_file.save(name, f, save=False) else: attachment.attachment_link = url attachment.save() updated = True
def save_attachments(self, src, val): updated = False attachments_to_delete = list( Attachment.objects.attachments_for_object(self.obj)) for url, legend, author in self.filter_attachments(src, val): url = self.base_url + url legend = legend or u"" author = author or u"" name = os.path.basename(url) found = False for attachment in attachments_to_delete: upload_name, ext = os.path.splitext( attachment_upload(attachment, name)) existing_name = attachment.attachment_file.name if re.search( ur"^{name}(_\d+)?{ext}$".format(name=upload_name, ext=ext), existing_name) and not self.has_size_changed( url, attachment): found = True attachments_to_delete.remove(attachment) if author != attachment.author or legend != attachment.legend: attachment.author = author attachment.legend = legend attachment.save() updated = True break if found: continue content = self.download_attachment(url) if content is None: continue f = ContentFile(content) attachment = Attachment() attachment.content_object = self.obj attachment.attachment_file.save(name, f, save=False) attachment.filetype = self.filetype attachment.creator = self.creator attachment.author = author attachment.legend = legend attachment.save() updated = True