I'm trying to update a django/mezzanine application from python 2.7 to python 3.7. Can you help me in fixing the error below (CheckNewsDateStatus() takes no arguments
)?
Seems that this class is not used at all; if I grep through all code only the attached settings.py and middleware.py match. Is it something partly implemented in django/mezzanine or it it so that the whole class can be removed as unnecessary ?
I don't know how the code was planned to work and I don't know is the feature has been used at all...
(python-3.7) miettinj@ramen:~/pika> python manage.py runserver
BASE_DIR /srv/work/miettinj/pika
PROJECT_ROOT /srv/work/miettinj/pika/pika
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/timezone.py:13: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
zone_name = tzlocal.get_localzone().zone
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/conf.py:67: UserWarning: TIME_ZONE setting is not set, using closest match: Europe/Helsinki
warn("TIME_ZONE setting is not set, using closest match: %s" % tz)
BASE_DIR /srv/work/miettinj/pika
PROJECT_ROOT /srv/work/miettinj/pika/pika
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/timezone.py:13: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
zone_name = tzlocal.get_localzone().zone
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/utils/conf.py:67: UserWarning: TIME_ZONE setting is not set, using closest match: Europe/Helsinki
warn("TIME_ZONE setting is not set, using closest match: %s" % tz)
Watching for file changes with StatReloader
.....
_d^^^^^^^^^b_
.d'' ``b.
.p' `q.
.d' `b.
.d' `b. * Mezzanine 5.0.0
:: :: * Django 2.2
:: M E Z Z A N I N E :: * Python 3.7.10
:: :: * PostgreSQL 9.3.0
`p. .q' * Linux 5.3.18-lp152.102-default
`p. .q'
`b. .d'
`q.. ..p'
^q........p^
''''
Performing system checks...
System check identified no issues (0 silenced).
December 30, 2021 - 13:44:54
Django version 2.2, using settings 'pika.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Exception in thread Thread-1:
Traceback (most recent call last):
File "/opt/python/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/opt/python/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/core/management/commands/runserver.py", line 170, in inner_run
super().inner_run(*args, **kwargs)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/mezzanine/core/management/commands/runserver.py", line 173, in get_handler
handler = super().get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 42, in get_internal_wsgi_application
return get_wsgi_application()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
self.load_middleware()
File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: CheckNewsDateStatus() takes no arguments
middlewary.py:
class CheckNewsDateStatus(object):
def process_request(self, request):
if '/uutinen/' in request.path:
try:
path_to_go_raw = request.path
true_slug = path_to_go_raw.split('/uutinen/')[1:]
news_obj = Uutinen.objects.get(slug=true_slug[0])
now_utc = pytz.utc.localize(datetime.now())
hel = pytz.timezone("Europe/Helsinki")
foo = news_obj.publish_date.astimezone(hel)
if foo.date() < now_utc.date() and news_obj.status == 2:
pass
elif foo.date() == now_utc.date() and foo.time() < now_utc.time() and news_obj.status == 2:
pass
else:
if request.user.is_authenticated():
pass
elif news_obj.status == 1:
return HttpResponseNotFound('404')
else:
return HttpResponseNotFound('404')
except:
pass
settings.py:
# List of middleware classes to use. Order is important; in the request phase,
# these middleware classes will be applied in the order given, and in the
# response phase the middleware will be applied in reverse order.
MIDDLEWARE = (
'page_types.middleware.SetDynamicSite',
# 'debug_toolbar.middleware.DebugToolbarMiddleware',
"mezzanine.core.middleware.UpdateCacheMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
#"django.middleware.locale.LocaleMiddleware",
"statfi_search.middleware.locale.LocaleMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"mezzanine.core.request.CurrentRequestMiddleware",
"mezzanine.core.middleware.RedirectFallbackMiddleware",
"mezzanine.core.middleware.TemplateForDeviceMiddleware",
"mezzanine.core.middleware.TemplateForHostMiddleware",
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
"mezzanine.core.middleware.SitePermissionMiddleware",
# Uncomment the following if using any of the SSL settings:
# "mezzanine.core.middleware.SSLRedirectMiddleware",
"mezzanine.pages.middleware.PageMiddleware",
"mezzanine.core.middleware.FetchFromCacheMiddleware",
'page_types.middleware.RedirectMiddleware',
'page_types.middleware.CheckNewsDateStatus',
)
CodePudding user response:
Since you are using Django 2.2, your Middleware class must accept a get_response argument:
you should define your class like this.
class CheckNewsDateStatus(object):
def __init__(self, get_response):
self.get_response = get_response
def process_request(self, request):
....
Also, there is no need to write (object) while defining your class in Python3 .
For your reference, documentation.