Repository: django/channels

Issue:
Replace cookie_date in sessions.py
In `channels/sessions.py`, line 13:
`from django.utils.http import cookie_date`

The [deprecation notice is in Django 2.1 release notes](https://docs.djangoproject.com/en/2.1/releases/2.1/#id2) (the recommended replacement is [django.utils.http.http_date](https://docs.djangoproject.com/en/2.1/ref/utils/#django.utils.http.http_date)). Since it was deprecated in 2.1, now that stable/2.2.x has been forked, the Django master branch has had `cookie_date` removed, and users with the development version are no longer able to use Channels.

See also: [Django commit 958a7b4c: Refs #28965 -- Removed utils.http.cookie_date() per deprecation timeline.](https://github.com/django/django/commit/958a7b4ca69434d0145fd569cf007e21841bb36c)

The following information is provided as it is requested in the template, but I don't see it being useful:

Invocation: `python manage.py`

```
Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\apps\registry.py", line 122, in populate
    app_config.ready()
  File "D:\Professional\firstinspires\fll-scoring\fllfms\fllfms\apps.py", line 9, in ready
    from . import signals  # Bind signals.
  File "D:\Professional\firstinspires\fll-scoring\fllfms\fllfms\signals.py", line 6, in <module>
    from .consumers import TimerConsumer
  File "D:\Professional\firstinspires\fll-scoring\fllfms\fllfms\consumers.py", line 7, in <module>
    from channels.auth import get_user
  File "D:\Professional\firstinspires\fll-scoring\fllfms\venv\lib\site-packages\channels\auth.py", line 19, in <module>
    from channels.sessions import CookieMiddleware, SessionMiddleware
  File "D:\Professional\firstinspires\fll-scoring\fllfms\venv\lib\site-packages\channels\sessions.py", line 13, in <module>
    from django.utils.http import cookie_date
ImportError: cannot import name 'cookie_date'
```

System:

Python 3.6.6 x64, Windows 10 (x64) Education, version 1809 (build 17763.194).

```
$ pip freeze
asgiref==2.3.2
async-timeout==3.0.1
attrs==18.2.0
autobahn==18.12.1
Automat==0.7.0
channels==2.1.6
constantly==15.1.0
cx-Freeze==5.1.1
daphne==2.2.4
-e git+https://github.com/django/django@bff748df3e1e1c0077e02df2b77bda2b827ad129#egg=Django
django-reversion==3.0.2
hyperlink==18.0.0
idna==2.8
incremental==17.5.0
Nuitka==0.6.1
PyHamcrest==1.9.0
pytz==2018.5
pywin32==224
six==1.12.0
sqlparse==0.2.4
Twisted==18.9.0
txaio==18.8.1
tzlocal==1.5.1
zope.interface==4.6.0
```


Replace cookie_date in sessions.py
In `channels/sessions.py`, line 13:
`from django.utils.http import cookie_date`

The [deprecation notice is in Django 2.1 release notes](https://docs.djangoproject.com/en/2.1/releases/2.1/#id2) (the recommended replacement is [django.utils.http.http_date](https://docs.djangoproject.com/en/2.1/ref/utils/#django.utils.http.http_date)). Since it was deprecated in 2.1, now that stable/2.2.x has been forked, the Django master branch has had `cookie_date` removed, and users with the development version are no longer able to use Channels.

See also: [Django commit 958a7b4c: Refs #28965 -- Removed utils.http.cookie_date() per deprecation timeline.](https://github.com/django/django/commit/958a7b4ca69434d0145fd569cf007e21841bb36c)

The following information is provided as it is requested in the template, but I don't see it being useful:

Invocation: `python manage.py`

```
Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "d:\professional\firstinspires\fll-scoring\fllfms\venv\src\django\django\apps\registry.py", line 122, in populate
    app_config.ready()
  File "D:\Professional\firstinspires\fll-scoring\fllfms\fllfms\apps.py", line 9, in ready
    from . import signals  # Bind signals.
  File "D:\Professional\firstinspires\fll-scoring\fllfms\fllfms\signals.py", line 6, in <module>
    from .consumers import TimerConsumer
  File "D:\Professional\firstinspires\fll-scoring\fllfms\fllfms\consumers.py", line 7, in <module>
    from channels.auth import get_user
  File "D:\Professional\firstinspires\fll-scoring\fllfms\venv\lib\site-packages\channels\auth.py", line 19, in <module>
    from channels.sessions import CookieMiddleware, SessionMiddleware
  File "D:\Professional\firstinspires\fll-scoring\fllfms\venv\lib\site-packages\channels\sessions.py", line 13, in <module>
    from django.utils.http import cookie_date
ImportError: cannot import name 'cookie_date'
```

System:

Python 3.6.6 x64, Windows 10 (x64) Education, version 1809 (build 17763.194).

```
$ pip freeze
asgiref==2.3.2
async-timeout==3.0.1
attrs==18.2.0
autobahn==18.12.1
Automat==0.7.0
channels==2.1.6
constantly==15.1.0
cx-Freeze==5.1.1
daphne==2.2.4
-e git+https://github.com/django/django@bff748df3e1e1c0077e02df2b77bda2b827ad129#egg=Django
django-reversion==3.0.2
hyperlink==18.0.0
idna==2.8
incremental==17.5.0
Nuitka==0.6.1
PyHamcrest==1.9.0
pytz==2018.5
pywin32==224
six==1.12.0
sqlparse==0.2.4
Twisted==18.9.0
txaio==18.8.1
tzlocal==1.5.1
zope.interface==4.6.0
```




Discussion:
Hi @ShadowLNC. This looks right. A PR switching to `http_date` would be great! 
Hi @ShadowLNC. This looks right. A PR switching to `http_date` would be great! 

Resolving patch:
diff --git a/channels/sessions.py b/channels/sessions.py
--- a/channels/sessions.py
+++ b/channels/sessions.py
@@ -10,10 +10,14 @@
 from django.utils import timezone
 from django.utils.encoding import force_str
 from django.utils.functional import LazyObject
-from django.utils.http import cookie_date
 
 from channels.db import database_sync_to_async
 
+try:
+    from django.utils.http import http_date
+except ImportError:
+    from django.utils.http import cookie_date as http_date
+
 
 class CookieMiddleware:
     """
@@ -87,7 +91,7 @@ def set_cookie(
             cookies[key]["max-age"] = max_age
             # IE requires expires, so set it if hasn't been already.
             if not expires:
-                cookies[key]["expires"] = cookie_date(time.time() + max_age)
+                cookies[key]["expires"] = http_date(time.time() + max_age)
         if path is not None:
             cookies[key]["path"] = path
         if domain is not None:
@@ -215,7 +219,7 @@ async def send(self, message):
                         else:
                             max_age = self.scope["session"].get_expiry_age()
                             expires_time = time.time() + max_age
-                            expires = cookie_date(expires_time)
+                            expires = http_date(expires_time)
                         # Set the cookie
                         CookieMiddleware.set_cookie(
                             message,


Test patch: