def validation_save_and_message(object, is_valid, notes): # remove any path information from outputs user_home = object.owner.path media_path = settings.MEDIA_ROOT notes = notes\ .replace(user_home, '/***')\ .replace(media_path, '/***')\ # remove extra whitespace from all lines notes = '\n'.join([l.strip() for l in notes.splitlines()]) # intentionally omit post_save signal object.__class__.objects\ .filter(id=object.id)\ .update( validated=is_valid, validation_notes=notes) msg = '{} {}: '.format(object._meta.verbose_name.title(), object) if is_valid: msg += 'validation complete!' messages.success(object.owner, msg) else: msg += "<a href='{}'>validation failed (view errors)</a>"\ .format(object.get_absolute_url()) messages.warning(object.owner, msg)
def download(self): related_ds = list(self.related_datasets()) fn = self.data.path self.reset() try: r = requests.get(self.url, stream=True) with open(fn, 'wb') as f: for chunk in r.iter_content(chunk_size=self.CHUNK): if chunk: # filter out keep-alive new chunks f.write(chunk) self.end_time = now() self.status_code = self.FINISHED_SUCCESS self.md5 = self.get_md5() self.filesize = os.path.getsize(fn) for ds in related_ds: msg = 'Download complete (will validate next): {}'.format(self.url) messages.success(ds.owner, msg) except Exception as e: self.start_time = None self.status_code = self.FINISHED_ERROR self.status = str(e) for ds in related_ds: msg = 'Download failed: {}'.format(self.url) messages.warning(ds.owner, msg) self.save() for ds in related_ds: ds.validate_and_save()
def test_warning(self): messages.warning(self.user, "Hello") self.assertMessageOk(constants.WARNING)