@ -53,8 +53,10 @@ def page_post_save(sender, instance, created, *args, **kwargs):
 | 
				
			|||||||
def program_post_save(sender, instance, created, *args, **kwargs):
 | 
					def program_post_save(sender, instance, created, *args, **kwargs):
 | 
				
			||||||
    """Clean-up later diffusions when a program becomes inactive."""
 | 
					    """Clean-up later diffusions when a program becomes inactive."""
 | 
				
			||||||
    if not instance.active:
 | 
					    if not instance.active:
 | 
				
			||||||
        Diffusion.object.program(instance).after(tz.now()).delete()
 | 
					        Diffusion.objects.program(instance).after(tz.now()).delete()
 | 
				
			||||||
        Episode.object.parent(instance).filter(diffusion__isnull=True).delete()
 | 
					        Episode.objects.parent(instance).filter(
 | 
				
			||||||
 | 
					            diffusion__isnull=True
 | 
				
			||||||
 | 
					        ).delete()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cover = getattr(instance, "__initial_cover", None)
 | 
					    cover = getattr(instance, "__initial_cover", None)
 | 
				
			||||||
    if cover is None and instance.cover is not None:
 | 
					    if cover is None and instance.cover is not None:
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										54
									
								
								aircox/tests/test_converters.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								aircox/tests/test_converters.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					from datetime import date
 | 
				
			||||||
 | 
					from django.utils.safestring import SafeString
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from aircox import converters
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def page_path_conv():
 | 
				
			||||||
 | 
					    return converters.PagePathConverter()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def week_conv():
 | 
				
			||||||
 | 
					    return converters.WeekConverter()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def date_conv():
 | 
				
			||||||
 | 
					    return converters.DateConverter()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestPagePathConverter:
 | 
				
			||||||
 | 
					    def test_to_python(self, page_path_conv):
 | 
				
			||||||
 | 
					        val = "path_value"
 | 
				
			||||||
 | 
					        result = page_path_conv.to_python(val)
 | 
				
			||||||
 | 
					        assert result == "/" + val + "/"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_to_url(self, page_path_conv):
 | 
				
			||||||
 | 
					        val = "/val"
 | 
				
			||||||
 | 
					        result = page_path_conv.to_url(val)
 | 
				
			||||||
 | 
					        assert isinstance(result, SafeString)
 | 
				
			||||||
 | 
					        assert result == val[1:] + "/"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestWeekConverter:
 | 
				
			||||||
 | 
					    def test_to_python(self, week_conv):
 | 
				
			||||||
 | 
					        val = "2023/02"
 | 
				
			||||||
 | 
					        assert week_conv.to_python(val) == date(2023, 1, 9)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_to_url(self, week_conv):
 | 
				
			||||||
 | 
					        val = date(2023, 1, 10)
 | 
				
			||||||
 | 
					        assert week_conv.to_url(val) == "2023/02"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestDateConverter:
 | 
				
			||||||
 | 
					    def test_to_python(self, date_conv):
 | 
				
			||||||
 | 
					        val = "2023/02/05"
 | 
				
			||||||
 | 
					        assert date_conv.to_python(val) == date(2023, 2, 5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_to_url(self, date_conv):
 | 
				
			||||||
 | 
					        val = date(2023, 5, 10)
 | 
				
			||||||
 | 
					        assert date_conv.to_url(val) == "2023/05/10"
 | 
				
			||||||
@ -4,7 +4,6 @@ import django.utils.timezone as tz
 | 
				
			|||||||
__all__ = (
 | 
					__all__ = (
 | 
				
			||||||
    "Redirect",
 | 
					    "Redirect",
 | 
				
			||||||
    "redirect",
 | 
					    "redirect",
 | 
				
			||||||
    "date_range",
 | 
					 | 
				
			||||||
    "cast_date",
 | 
					    "cast_date",
 | 
				
			||||||
    "date_or_default",
 | 
					    "date_or_default",
 | 
				
			||||||
    "to_timedelta",
 | 
					    "to_timedelta",
 | 
				
			||||||
@ -29,31 +28,17 @@ def redirect(url):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def str_to_date(value, sep="/"):
 | 
					def str_to_date(value, sep="/"):
 | 
				
			||||||
    """Return a date from the provided `value` string, formated as "yyyy/mm/dd"
 | 
					    """Return a date from the provided `value` string, formated as
 | 
				
			||||||
    (or "dd/mm/yyyy" if `reverse` is True).
 | 
					    "yyyy/mm/dd".
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Raises ValueError for incorrect value format.
 | 
					    :raises: ValueError for incorrect value format.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    value = value.split(sep)[:3]
 | 
					    value = value.split(sep)[:3]
 | 
				
			||||||
    if len(value) < 3:
 | 
					    if len(value) < 3:
 | 
				
			||||||
        return ValueError("incorrect date format")
 | 
					        raise ValueError("incorrect date format")
 | 
				
			||||||
    return datetime.date(int(value[0]), int(value[1]), int(value[2]))
 | 
					    return datetime.date(int(value[0]), int(value[1]), int(value[2]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def date_range(date, delta=None, **delta_kwargs):
 | 
					 | 
				
			||||||
    """Return a range of provided date such as `[date-delta, date+delta]`.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    :param date: the reference date
 | 
					 | 
				
			||||||
    :param delta: timedelta
 | 
					 | 
				
			||||||
    :param **delta_kwargs: timedelta init arguments
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Return a datetime range for a given day, as:
 | 
					 | 
				
			||||||
    ```(date, 0:0:0:0; date, 23:59:59:999)```.
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    delta = tz.timedelta(**delta_kwargs) if delta is None else delta
 | 
					 | 
				
			||||||
    return [date - delta, date + delta]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def cast_date(date, into=datetime.date):
 | 
					def cast_date(date, into=datetime.date):
 | 
				
			||||||
    """Cast a given date into the provided class' instance.
 | 
					    """Cast a given date into the provided class' instance.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user