def resource_is_validated(obj_id): from mcod.resources.models import Resource from mcod.resources.link_validation import session res = Resource.objects.get(pk=obj_id) if res.link and res.main_file: adapter = requests_mock.Adapter() adapter.register_uri('GET', res.link, content=res.main_file.read(), headers={'Content-Type': res.main_file_mimetype}) session.mount(res.link, adapter) res.revalidate()
def create_res(ds, editor, **kwargs): from mcod.resources.models import Resource, ResourceFile _fname = kwargs.pop('filename') _kwargs = { 'title': 'Analysis of fake news sites and viral posts', 'description': 'Over the past four years, BuzzFeed News has maintained lists of sites that ' 'publish completely fabricated stories. As we encounter new ones and debunk ' 'their content, we add them to the list.', 'file': _fname, 'link': f'https://falconframework.org/media/resources/{_fname}', 'format': 'csv', 'openness_score': 3, 'views_count': 10, 'downloads_count': 20, 'dataset': ds, 'created_by': editor, 'modified_by': editor, 'data_date': datetime.today(), } if is_enabled('S41_resource_has_high_value_data'): _kwargs['has_high_value_data'] = False _kwargs.update(**kwargs) if is_enabled('S40_new_file_model.be'): with open(os.path.join(settings.RESOURCES_MEDIA_ROOT, _fname), 'rb') as f: from mcod.resources.link_validation import session adapter = requests_mock.Adapter() adapter.register_uri('GET', _kwargs['link'], content=f.read(), headers={'Content-Type': 'application/csv'}) session.mount('https://falconframework.org', adapter) _kwargs.pop('file') res = Resource.objects.create(**_kwargs) ResourceFile.objects.create( is_main=True, resource=res, file=os.path.join(settings.RESOURCES_MEDIA_ROOT, _fname), format='csv' ) else: res = Resource(**_kwargs) res.tracker.saved_data['link'] = res.link res.save() res = Resource.objects.get(pk=res.pk) return res
def _create(cls, model, *args, **kwargs): from mcod.core.tests.fixtures import adapter from mcod.resources.link_validation import session file = kwargs.get('file') format = kwargs.get('format') content_type = kwargs.pop('content_type', None) if content_type or kwargs['resource'].link: if content_type is None: content_type = 'application/csv' adapter.register_uri('GET', kwargs['resource'].link, content=file.read(), headers={'Content-Type': content_type}) session.mount(kwargs['resource'].link, adapter) if hasattr(file, 'name'): filename = file.name else: filename = file ext = os.path.splitext(filename)[1][1:] if ext != format: kwargs['format'] = ext return super()._create(model, *args, **kwargs)
def _create(cls, model, *args, file=None, link=None, **kwargs): if is_enabled('S40_new_file_model.be'): return super()._create(model, *args, link=link, **kwargs) else: content_type = kwargs.pop('content_type', 'application/csv') from mcod.core.tests.fixtures import adapter from mcod.resources.link_validation import session if file and link: adapter.register_uri('GET', link, content=file.read(), headers={'Content-Type': content_type}) session.mount(link, adapter) kwargs.update({ 'format': 'csv', 'file_mimetype': 'application/csv', }) return super()._create(model, *args, file=file, link=link, **kwargs)
def pytest_sessionstart(session): from mcod.resources.link_validation import session session.mount('http://test.mcod', adapter)