Merge Request #2

Merged
softwarepublico/colab!2
Created by Carlos Coêlho

Middleware

Assignee: Sergio Oliveira
Milestone: None

Merged by Sergio Oliveira

Source branch has been removed
Commits (1)
3 participants
colab/accounts/middleware.py
... ... @@ -0,0 +1,33 @@
  1 +from json import dumps as json_dumps
  2 +
  3 +from colab.accounts.models import User
  4 +from colab.proxy.utils.views import ColabProxyView
  5 +
  6 +
  7 +class RemoteUserMiddleware(object):
  8 +
  9 + def process_view(self, request, view_func, view_args, view_kwargs):
  10 + if not request.user.is_authenticated():
  11 + return
  12 +
  13 + if not hasattr(view_func, 'im_class'):
  14 + return
  15 +
  16 + if not issubclass(view_func.im_class, ColabProxyView):
  17 + return
  18 +
  19 + user = User.objects.get(
  20 + username=request.user.get_username()
  21 + )
  22 +
  23 + remote_user_data = {}
  24 +
  25 + remote_user_data['EMAIL'] = user.email
  26 + remote_user_data['NAME'] = user.username
  27 +
  28 + request.META['REMOTE_USER_DATA'] = json_dumps(
  29 + remote_user_data,
  30 + sort_keys=True,
  31 + )
  32 +
  33 + return None
... ...
    9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
    Sergio Oliveira started a discussion on the outdated diff
    last updated by Sergio Oliveira
    colab/remote_user/middleware.py
      1 +from json import dumps as json_dumps
      2 +
      3 +from colab.accounts.models import User
      4 +from colab.proxy.utils.views import ColabProxyView
      5 +
      6 +
      7 +class RemoteUserMiddleware(object):
      8 +
      9 + def process_view(self, request, view_func, view_args, view_kwargs):
      10 + if request.user.is_authenticated():
    1
    • 9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
      Sergio Oliveira @seocam (Edited )

      Ao invés de fazer aninhar vários ifs é melhor fazermos assim:

      if not request.user.is_authenticated():
          return
      

      Um dos versos do Zen do Python é "Flat is better than nested."

      Choose File ...   File name...
      Cancel
    9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
    Sergio Oliveira started a discussion on the outdated diff
    last updated by Sergio Oliveira
    colab/remote_user/middleware.py
      1 +from json import dumps as json_dumps
      2 +
      3 +from colab.accounts.models import User
      4 +from colab.proxy.utils.views import ColabProxyView
      5 +
      6 +
      7 +class RemoteUserMiddleware(object):
      8 +
      9 + def process_view(self, request, view_func, view_args, view_kwargs):
      10 + if request.user.is_authenticated():
      11 + if next((True for method in dir(view_func) if method == 'im_class'), False):
    1
    • 9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
      Sergio Oliveira @seocam (Edited )

      Isso vai rodar pra todo request que o usuário está autenticado. Fazer um loop em todos os atributos pra todo request provavelmente não é uma boa ideia no quesito performance.

      Tudo isso é pra saber se o objeto view_func tem o atributo im_class? Se for bastava fazer um getattr(view_func, 'im_class', False) - vai retornar False se o atributo não existir.

      Eu acho que provavelmente seria melhor fazer algo na linha:

      
      if not hasattr(view_func, 'im_class'):
         return
      
      if issubclass(view_func.im_class, ColabProxyView):
         ...
      
      
      Choose File ...   File name...
      Cancel
    9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
    Sergio Oliveira started a discussion on the outdated diff
    last updated by Sergio Oliveira
    colab/remote_user/middleware.py
    ... ... @@ -0,0 +1,31 @@
    9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
    Sergio Oliveira started a discussion on the outdated diff
    last updated by Carlos Coêlho
    colab/remote_user/middleware.py
      1 +from json import dumps as json_dumps
      2 +
      3 +from colab.accounts.models import User
      4 +from colab.proxy.utils.views import ColabProxyView
      5 +
      6 +
      7 +class RemoteUserMiddleware(object):
      8 +
      9 + def process_view(self, request, view_func, view_args, view_kwargs):
      10 + if not request.user.is_authenticated():
      11 + return
      12 +
      13 + if not hasattr(view_func, 'im_class'):
      14 + return
      15 +
      16 + if issubclass(view_func.im_class, ColabProxyView):
    2
  • 5eb59358fc7b3b7402ae353f8fb36293?s=40&d=identicon
    Macartur Sousa @macartur

    mentioned in issue #42

    Choose File ...   File name...
    Cancel