Get details of current signed in user in parameterization class

Hello,

I’m trying to load the parameterization. I’m using API().get_current_user().full_name and it works in the Controller class. But it doesn’t work in the parameterization class because I get the error 'User' object has no attribute '_generate_entity_type'.

Is it possible to get the user details in parameterization? Or should I load it into one of the Views and do my processing there?

It’s okay I just figured it out. I just have to declare an empty property in Parameterization and then update it after it’s been initialised in the Controller class.

class Parametrization(ViktorParametrization):
    self.user = None
    ...

class Controller(ViktorController):
    user = API().get_current_user().full_name
    parametrization = Parametrization
    parametrization.user = Text("Welcome " + user)
1 Like

Hi @PanjiBrotoisworo ,

Welcome to the community! And great to see how you are experimenting.

I believe that, although you got it working, that this won’t give the expected results in production. It seems that you want to have a means to present a dynamic text in the Parametrization.

There is an approach for this, by making use of the OutputField. This, however, renders the result as a greyed-out field. I believe what you ideally want is an OutputField type of feature that renders your text as normal text. There is already an existing feature request for this. If this is what you want, I would recommend upvoting this feature request, and maybe add some context to that thread:

I believe that, although you got it working, that this won’t give the expected results in production.

To add a bit more on how this works (and that it might not behave like you expected)

  • the code directly underneath the Controller and Parametrization classes is only executed when the app is started.
  • in development this happens every time you explicitly call viktor-cli start / viktor-cli clean-start or when the code autoreloads
  • in production the app is not often restarted and it can run for a long time without reloading. this would mean that it does not change every time some-one enters the app

@PanjiBrotoisworo: im also curious how you got it to work in the first place. could you share a fully working code snippet?

Hi @matthijs, I’m still learning about the platform, so there is a bunch of random code.

Here is the relevant code regarding my current question.

class Parametrization(ViktorParametrization):

    step1_fields = Step("Input Fields Testing")
    step1_fields.title = Text("# Input Fields Testing")
    
    step1_fields.user = HiddenField("")
    step1_fields.email = HiddenField("")
    step1_fields.welcome = Text("")

class Controller(ViktorController):
    label = 'Test Entity'
    user = API(environment="Placeholder", token="Placeholder")
    user = user.get_current_user()
    parametrization = Parametrization
    parametrization.step1_fields.welcome = Text(f"""### Signed in user: {user.full_name} ({user.email})""")
    parametrization.step1_fields.user = HiddenField(user.full_name)
    parametrization.step1_fields.email = HiddenField(user.email)

This results in:

Thanks for sharing @PanjiBrotoisworo. This code still suffers from the limitation i outlined in the previous comment (only updated on app start)

I believe your usecase might be better handled by an OutputField:


def get_current_user(params, **kwargs):
    user = API()
    user = user.get_current_user()
    return f'{user.full_name} ({user.email})'

class Parametrization(ViktorParametrization):

    step1_fields = Step("Input Fields Testing")
    step1_fields.title = Text("# Input Fields Testing")
    step1_fields.welcome = Text("Signed in user:")
    step1_fields.lb1 = LineBreak()
    step1_fields.user = OutputField('', value=get_current_user)

class Controller(ViktorController):
    label = 'Test Entity'
    parametrization = Parametrization
  • this correctly updates on every editor entry
  • the API is now automatically instantiated on the right environment and the right token (current user). this page explains more on these differences

Thanks! I tested it with another test account and this works perfectly.

1 Like

Hi @matthijs, I’m currently using the SDK API externally in order to obtain more detailed reports than allowed by the audit.

It would be very helpful to access the full name of a user who created an entity.

I believe some of the content in this thread could serve as a workaround, but I wanted to check if you knew of a ‘proper’ way to retrieve the full name of a user who created an entity (assuming you already have the entity object) ?

1 Like

Hi @kassi, in order to get these full names i think you should resort to the REST API (the SDK API functions don’t have a python equivalent to get this information i think)

A nice resource to learn about the difference between the two:/