-1

Going through the documentation of pylon project wiki2 tutorial found this codes and What I know is a class has a function definition defined with indentation after class declaration. Checked the python documentation too and is the same but the project documentation has something like a class definition with indentation and without indentation. Can anybody make me clear on this.

from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.security import (
    Authenticated,
    Everyone,
)

from .models import User


class MyAuthenticationPolicy(AuthTktAuthenticationPolicy):
    def authenticated_userid(self, request):
        user = request.user
        if user is not None:
            return user.id

    def effective_principals(self, request):
        principals = [Everyone]
        user = request.user
        if user is not None:
            principals.append(Authenticated)
            principals.append(str(user.id))
            principals.append('role:' + user.role)
        return principals

def get_user(request):
    user_id = request.unauthenticated_userid
    if user_id is not None:
        user = request.dbsession.query(User).get(user_id)
        return user

I have pasted the few lines of codes from the documentation as we can see the first two function def are indented but the third function get_user is not. what is the purpose exactly.

Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76
  • Not sure about the tutorial, but that `get_user` function is probably used with `add_request_method`, so that request.user is the current user (or None) – Antoine Leclair Apr 19 '16 at 20:41
  • yes thats exactly what you mean but am wondering about the indentation why is the indentation not done for the functions like other functions. As we know the class member functions are indented but why not here – Tara Prasad Gurung Apr 20 '16 at 09:55
  • Because it's an independent function, not a class method. It could be above the class definition, it would still work. – Antoine Leclair Apr 20 '16 at 18:20

2 Answers2

1

get_user is a simple function that is registered later at the bottom of the module using config.add_request_method(get_user, 'user', reify=True). This enables the usage of request.user inside the policy methods.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
0

It is because that function is not associated to the class MyAuthenticationPolicy. Only the functions with indentation belongs to that class. The extra function you are talking about is being called to the bottom of the module as mention above. that function without indentation is associated with the another function that you haven't provided here as in documentation its called in the function definition below

def includeme(config):
    settings = config.get_settings()
    authn_policy = MyAuthenticationPolicy(
        settings['auth.secret'],
        hashalg='sha512',
    )
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(ACLAuthorizationPolicy())
    config.add_request_method(get_user, 'user', reify=True)
TaraGurung
  • 135
  • 1
  • 21