简体   繁体   中英

How to make middleware object callable in Django 2.2

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 ( CTypeError: 'CheckNewsDateStatus' object is not callable )?

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... and I don't know how the callable objects should be presented in settings.py-file.

(python-3.7) miettinj@ramen:~/pika-py-3.7/pika> python manage.py runserver 0:8034
BASE_DIR /srv/work/miettinj/pika-py-3.7/pika
PROJECT_ROOT /srv/work/miettinj/pika-py-3.7/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. 
/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-py-3.7/pika
PROJECT_ROOT /srv/work/miettinj/pika-py-3.7/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. 
/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 31, 2021 - 14:08:56
Django version 2.2, using settings 'pika.settings'
Starting development server at http://0:8034/
Quit the server with CONTROL-C.
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/base.py:37: 
/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/base.py:37: FutureWarning: `TemplateForDeviceMiddleware` is deprecated. Please remove it from your middleware settings.
  mw_instance = middleware(handler)
Internal Server Error: /admin/
Traceback (most recent call last):
  File "/srv/work/miettinj/python-3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
TypeError: 'CheckNewsDateStatus' object is not callable

middlewary.py:

class CheckNewsDateStatus:
    def __init__(self, get_response):
        self.get_response = get_response 

    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:
                    print("authenticated-->", request.user.is_authenticated())
                    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.CheckNewsDateStatus', 
        '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',
    )

You have an old style mixin here, you need to inherit from MiddlewareMixin

Change your code so that CheckNewsDateStatus object inherits from MiddlewareMixin like this

# this will probably be in page_types/middleware.py file 
class CheckNewsDateStatus(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response 

    def process_request(self, request):
        if '/uutinen/' in request.path:
            # rest of code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM