def get_full_path(cls, community_class, query_path, configuration=None, created_at=None): warnings.warn("CommunityView.get_full_path is deprecated in favor of get_uri", DeprecationWarning) configuration = configuration or {} service_view_name = cls.get_service_view(community_class) service_view_url = reverse(service_view_name, kwargs={"path": query_path}) if created_at: configuration["t"] = format_datetime(created_at) config_query = "?" + get_standardized_configuration(configuration, as_hash=False) if configuration else "" return service_view_url + config_query
def get_full_path(cls, community_class, query_path, configuration=None, created_at=None): warnings.warn("CommunityView.get_full_path is deprecated in favor of get_uri", DeprecationWarning) configuration = configuration or {} service_view_name = "v1:{}_service".format(community_class.get_name()) service_view_url = reverse(service_view_name, kwargs={"path": query_path}) if created_at: configuration["t"] = format_datetime(created_at) config_query = "?" + get_standardized_configuration(configuration, as_hash=False) if configuration else "" return service_view_url + config_query
def finish_download( self, out, err): # TODO: move images in downloads folder? another way? translation_growth = self.growth_set.filter(type="translations").last() query = translation_growth.input.individual_set.last( ).properties["query"] grids = { "{}_{}".format(language, country): (grid, factor) for language, country, grid, factor in self.LOCALES } grouped_translations = translation_growth.output.group_by("locale") directory = "visual_translations/{}/{}".format( query, format_datetime(self.created_at)) os.makedirs(os.path.join(settings.MEDIA_ROOT, directory), 0o0755, True) for locale, translations in six.iteritems(grouped_translations): expansion_processor = ExpansionProcessor(self.config.to_dict()) translations = expansion_processor.collective_content( [translation.properties for translation in translations]) image_sources = [] for translation in translations: image_sources.append(iter(translation["images"])) images = iroundrobin(*image_sources) downloads = [] grid, xlarge_factor = grids[locale] while True: try: image = next(images) except StopIteration: break try: download = ImageDownload.objects.get( uri=ImageDownload.uri_from_url(image["url"])) except ImageDownload.DoesNotExist: continue content_type, image = download.content if image is not None: downloads.append(image) if len(downloads) >= (grid["rows"] * grid["columns"] + 10): break for size, factor in six.iteritems(self.zoom_levels): grid, xlarge_factor = grids[locale] factor = factor if size != "XL" else xlarge_factor grid_specs = copy(grid) grid_specs["cell_width"] = int(grid_specs["cell_width"] * factor) grid_specs["cell_height"] = int(grid_specs["cell_height"] * factor) image_grid = ImageGrid(**grid_specs) image_grid.fill(downloads) image_grid.export("{}/{}_{}.jpg".format( directory, size, locale))
def handle(self, *args, **options): dirs, files = default_storage.listdir('visual_translations') for term in dirs: euc = VisualTranslationsEUCommunity.objects.get(signature=term) time = format_datetime(euc.created_at) source_path = default_storage.path( 'visual_translations/{}'.format(term)) temp_path = default_storage.path( 'visual_translations/{}'.format(time)) destination_path = os.path.join(source_path, time) shutil.move(source_path, temp_path) os.mkdir(source_path, 0o0755) shutil.move(temp_path, destination_path)
def test_get_latest_or_create_by_signature(self): # Static config with an illegal key constant_config = {"setting1": "const", "illegal": "please"} signature = CommunityMock.get_signature_from_input( "test", **constant_config) community, created = CommunityMock.objects.get_latest_or_create_by_signature( signature, **constant_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertFalse(created) self.assertEqual(community.config.setting1, "const") self.assertFalse(hasattr(community.config, "illegal")) # Variable config variable_config = {"$setting2": "variable"} signature = CommunityMock.get_signature_from_input( "test", **variable_config) community, created = CommunityMock.objects.get_latest_or_create_by_signature( signature, **variable_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertFalse(created) self.assertTrue(hasattr(community.config, "$setting2")) # Non existant config non_existant = { "setting1": "created", } signature = CommunityMock.get_signature_from_input( "test", **non_existant) community, created = CommunityMock.objects.get_latest_or_create_by_signature( signature, **non_existant) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertTrue(created) self.assertEqual(community.config.setting1, "created") # Multiple instances with signature variable_config = {"$setting2": "variable"} signature = CommunityMock.get_signature_from_input( "test-multiple", **variable_config) community, created = CommunityMock.objects.get_latest_or_create_by_signature( signature, **variable_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertFalse(created) self.assertTrue(hasattr(community.config, "$setting2")) self.assertEqual(format_datetime(community.created_at), "20150605161754000000")
def get_uri(cls, community_class, query_path, configuration=None, created_at=None): simple_configuration = { key: value for key, value in configuration.items() if isinstance(value, (int, float, str, bool)) } service_view_name = cls.get_service_view(community_class) service_view_url = reverse(service_view_name, kwargs={"path": query_path}) if created_at: configuration["t"] = format_datetime(created_at) query = "?" + get_standardized_configuration(simple_configuration, as_hash=False) if configuration else "" anchor = "#" + get_standardized_configuration(configuration) if configuration else "" # We need to make sure that URI's fit within the Resource URI limit of 255 chars # However the anchor part is more important than the query part so we assure that it stays here max_length = 255 - len(anchor) base_uri = service_view_url + query if len(base_uri) > max_length: base_uri = base_uri[:max_length] return base_uri + anchor
def get_uri(cls, community_class, query_path, configuration=None, created_at=None): simple_configuration = { key: value for key, value in configuration.items() if isinstance(value, (int, float, str, bool)) } service_view_name = "v1:{}_service".format(community_class.get_name()) service_view_url = reverse(service_view_name, kwargs={"path": query_path}) if created_at: configuration["t"] = format_datetime(created_at) query = "?" + get_standardized_configuration(simple_configuration, as_hash=False) if configuration else "" anchor = "#" + get_standardized_configuration(configuration) if configuration else "" # We need to make sure that URI's fit within the Resource URI limit of 255 chars # However the anchor part is more important than the query part so we assure that it stays here max_length = 255 - len(anchor) base_uri = service_view_url + query if len(base_uri) > max_length: base_uri = base_uri[:max_length] return base_uri + anchor
def handle_community(self, community, **options): if not options["debug"]: LocaforaOrders.objects.all().delete() community.signature = format_datetime(datetime.utcnow()) super(Command, self).handle_community(community, **options) result = DataFrame() for customer in community.kernel.individual_set.all(): customer_frame = DataFrame(customer["order_lines"]) result = result.append(customer_frame, ignore_index=True) if result.empty: log.warning("No orders to process") return result.sort_values(["product", "customer"], inplace=True) csv_file_name = "alfabetische-lijst.csv" dst = get_media_path("nautilus", "locafora") if not os.path.exists(dst): os.makedirs(dst) result[["customer", "quantity", "product", "unit"]].to_csv(os.path.join(dst, csv_file_name))
def finish_download(self, out, err): # TODO: move images in downloads folder? another way? translation_growth = self.growth_set.filter(type="translations").last() query = translation_growth.input.individual_set.last().properties["query"] grids = {"{}_{}".format(language, country): (grid, factor) for language, country, grid, factor in self.LOCALES} grouped_translations = translation_growth.output.group_by("locale") directory = "visual_translations/grids/{}/{}".format(query, format_datetime(self.created_at)) os.makedirs(os.path.join(settings.MEDIA_ROOT, directory), 0o0755, True) for locale, translations in six.iteritems(grouped_translations): expansion_processor = ExpansionProcessor(self.config.to_dict()) translations = expansion_processor.collective_content( [translation.properties for translation in translations] ) image_sources = [] for translation in translations: image_sources.append(iter(translation["images"])) images = iroundrobin(*image_sources) downloads = [] grid, xlarge_factor = grids[locale] while True: try: image = next(images) except StopIteration: break try: download = WebImageDownload.objects.get(uri=WebImageDownload.uri_from_url(image["url"])) except WebImageDownload.DoesNotExist: continue content_type, image = download.content if image is not None: downloads.append(image) if len(downloads) >= (grid["rows"] * grid["columns"] + 10): break for size, factor in six.iteritems(self.zoom_levels): grid, xlarge_factor = grids[locale] factor = factor if size != "XL" else xlarge_factor grid_specs = copy(grid) grid_specs["cell_width"] = int(grid_specs["cell_width"] * factor) grid_specs["cell_height"] = int(grid_specs["cell_height"] * factor) image_grid = ImageGrid(**grid_specs) image_grid.fill(downloads) image_grid.export("{}/{}_{}.jpg".format(directory, size, locale))
def test_get_latest_or_create_by_signature(self): # Static config with an illegal key constant_config = { "setting0": "possible?!", "setting1": "const", "illegal": "please", "$setting2": "variable" } signature = CommunityMock.get_signature_from_input("test", **constant_config) community, created = CommunityMock.objects.get_latest_or_create_by_signature(signature, **constant_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertFalse(created) self.assertEqual(community.config.setting1, "const") self.assertFalse(hasattr(community.config, "illegal")) self.assertFalse(hasattr(community.config, "setting0")) self.assertFalse(hasattr(community.config, "$setting2")) # Non existent config non_existent = { "setting1": "created", } signature = CommunityMock.get_signature_from_input("test", **non_existent) community, created = CommunityMock.objects.get_latest_or_create_by_signature(signature, **non_existent) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertTrue(created) self.assertEqual(community.config.setting1, "created") # Multiple instances with signature constant_config = { "setting1": "const", } signature = CommunityMock.get_signature_from_input("test-multiple", **constant_config) community, created = CommunityMock.objects.get_latest_or_create_by_signature(signature, **constant_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertFalse(created) self.assertTrue(hasattr(community.config, "setting1")) self.assertEqual(format_datetime(community.created_at), "20150605161754000000")
def test_get_latest_by_signature(self): # Static config with an illegal key constant_config = { "setting0": "possible?!", "setting1": "const", "illegal": "please", "$setting2": "variable" } signature = CommunityMock.get_signature_from_input("test", **constant_config) community = CommunityMock.objects.get_latest_by_signature(signature, **constant_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertEqual(community.config.setting1, "const") self.assertFalse(hasattr(community.config, "illegal")) self.assertFalse(hasattr(community.config, "$setting2")) # Non existent config non_existent = { "setting1": "variable", } signature = CommunityMock.get_signature_from_input("test", **non_existent) try: CommunityMock.objects.get_latest_by_signature(signature, **non_existent) self.fail("CommunityManager.get_latest_by_signature did not raise with non-existant community") except CommunityMock.DoesNotExist: pass # Multiple instances with signature constant_config = { "setting1": "const" } signature = CommunityMock.get_signature_from_input("test-multiple", **constant_config) community = CommunityMock.objects.get_latest_by_signature(signature, **constant_config) self.assertIsInstance(community, CommunityMock) self.assertIsNotNone(community) self.assertIsNotNone(community.id) self.assertTrue(hasattr(community.config, "setting1")) self.assertEqual(format_datetime(community.created_at), "20150605161754000000")