After Login Hide Sub Form from another Sub Form

I have a main form with sub forms that requests a user log in, if they are not already logged in.

On the UserLogin sub form I have a button click event

  def button_1_click(self, **event_args):
        user = anvil.users.login_with_form(allow_cancel=True)
        get_open_form().set_account_state(user)
        print('you are now logged in')   
        user = the_user()
        print(user)
        Quotation.self.card_travellers.visible = True
        Quotation.self.card_user.visible = False
        
  pass

But these lines are not doing what I want


        user = the_user()
        print(user)
        Quotation.self.card_travellers.visible = True
        Quotation.self.card_user.visible = False

I want to print the now logged in user
I want to make a sub form visible Quotation.self.card_travellers.visible = True
and I want to hide the login sub form as its no longer required Quotation.self.card_user.visible = False

Can anyone help

Thanks

Note that in

you’re already setting local variable user. So is

really necessary? You run the risk of losing user’s previous, known value here.

the_user isn’t a standard Anvil function, so we can’t guess what it will do or return. We don’t even know the type of value it is expected to return. But since

isn’t doing what you expect, I suggest you look at your definition of the_user to see what it is actually doing, as opposed to what you want it to do. That’s probably where your problem lies.

2 Likes

print(user)
You might want to try this instead: print(user['email'])

Quotation.self.card_travellers.visible = True
Quotation.self.card_user.visible = False

I’m guessing Quotation is the name of your main form? If so, it may work to replace Quotation.self with get_open_form().

1 Like

Hi

I’ve sorted out the user part thats now working fine, but still not gotten the correct code for showing and hiding the two cards.

I altered the code as follows:-

get_open_form().card_travellers.visible = True
get_open_form().card_user.visible = False

and now I get this error
AttributeError: 'HomeForm' object has no attribute 'card_travellers'

Homeform is essentially my front page and all other pages open within that form, depending on the menu link clicked, so yes that is the open form, just not the one I want it to look for those cards in.

How can I get it to look for Quotation and not Homeform?

You need to execute that code in a form that has a reference to the instance of the Quotation form. It sounds like right now it’s being executed in a subform. The clone link from your other post is not working again, so I can’t give any specifics, but in general:

If you have form A that contains subforms B and C, and you want subform B to affect the visibility of subform C, what I’d do is have form A register an event on subform B. Then subform B can raise that event, and the code that actually handles the event will be in form A, where you have the references to subforms to work with.

The following is vague since I can’t look at the actual code, so take it as an algorithm more than code:

# Form A, wherever subform B is created and added
self.b = B()
self.add_component(self.b)
self.b.set_event_handler('x-user-logged-in', self.user_logged_in)

# Also in Form A
def user_logged_in(self, **event_args):
  self.b.visible = False
  self.c.visible = True

# In Form B
def button_1_click(self, **event_args):
  user = anvil.users.login_with_form(allow_cancel=True)
  get_open_form().set_account_state(user)
  self.raise_event('x-user-logged-in')
1 Like

I’d need to know more about the structure of your app to say for sure. But let’s say you have a Quotation object in your HomeForm. It’s possible that you added it (with code) to your HomeForm without giving it a name. (If so, add a line like self.quotation_page = Quotation() to give it a name.) Once you can access it from the HomeForm as something like self.quotation_page, then from the UserLogin sub form you would be able to access it as get_open_form().quotation_page. So the card_user could be accessed as get_open_form().quotation_page.card_user, and so on.

For alternative approaches (and examples), see this post and the links therein:

1 Like

Hi

I set up the code as below, but am having problems hiding this card on the user_logged_in function.

self.card_user.visible = False

Is this because instead of showing and hiding the card, I used

self.add_component(self.userlogin)

Any ideas how I can hide that component after its no longer needed?


#START - Check Logged in Status, 
      if self.user is None:
        print("you need to login or register")
        self.userlogin = UserLogin()
        self.add_component(self.userlogin)
        self.userlogin.set_event_handler('x-user-logged-in', self.user_logged_in)
      else:
         print("you are logged in")
         self.raise_event('x-user-logged-in')
        
#AFTER Logged in via UserLogin  - set subform show/hide   
  def user_logged_in(self, **event_args):
    self.card_user.visible = False
    self.card_travellers.visible = True
    self.user = anvil.users.get_user()
    firstname = self.user["firstname"]
    surname = self.user["surname"] 
    print("Username: {}, {}".format(firstname, surname))

  def button_login_click(self, **event_args):
        user = login_flow.login_with_form(allow_cancel=True)
        get_open_form().set_account_state(user)
        print('you are now logged in')  
        self.raise_event('x-user-logged-in')
  pass

I solved it, I used self.userlogin.remove_from_parent()

2 Likes