Button visibility function help

Hi there - I’ve got a job listing that was created by a specific user, and i want everyone to be able to apply for that job, EXCEPT the user that created the the job.

Therefore I want everyone to be able to see the Apply for Job button, except the user who is the ‘job owner’

The issue I’m running into with my show button function is, it’s showing if you’re not logged in (good), it’s not showing if you’re the user and job owner (good), but it’s also not showing if you’re logged in as a user but not the job owner (bad)…here’s the function:

Any help to get this working properly is appreciated, thanks! -Ian

Ok, here’s what I suggest.
Separate out the code for debugging purposes. Try this :

if current_user == None:
    # set visible
else
    is_it_me = app_tables.job_listings.search(...)
    print("is it me = ",is_it_me)
    if is_it_me == True:
        # set invisible
    else:
        # set visible

This is to check that your search is returning what you think it is.

Also, your job_owner field needs to be a link to the user’s.

1 Like

app_tables.job_listings.search(...) probably isn’t what you want!

What is the current job listing? Is it self.item? Could you just do:

if self.item['owner'] == anvil.users.get_user():
   ...blah blah...
1 Like

Got it! This works. Thanks both. David, I like your idea for testing for future issues.

image

1 Like

I thought this was resolved, but I’m getting a strange error when I change my use case a bit…

  1. If I log in, visit the home page, click a job for which I am the owner, the Apply button is not visible on the Job Details page (good!)

  2. If I log in, click a job for which I am the owner from the My Account page, the Job Details page gives me an error (bad!)

In both cases the ‘job’ i’m clicking on is a link in a repeating panel.

The home page repeating panel is a different ItemTemplate than the My Account page repeating panel, but I believe the code is identical in both ItemTemplates, and they are both passing item=self.item to Job Details, so I’m confused.

ItemTemplate1 (Home Page - working):

ItemTemplate5 (My Account - generating error):

Can anyone tell me what I’m missing? Thank you! -Ian

(Bit short on time today, but …)

Looks to me like in the second case the original items does not contain
job_owner, so the item variable you are passing around will not contain it either.

(edit for clearer steps) :

A good debugging process would be this -

  • inside the function that fetches the data, print out the result set to see if the column exists
  • if it does, print out the return value from the form that calls the function and check that
  • continue your way along the chain of data acquisition and variable passing until you find the point where the data is missing. Make sure you check every step.

If you can identify where in the chain the data gets lost then that will isolate the point in the code that needs fixing.

If you want, post or PM me a clone link for the app and I can take a look later today if it remains unsolved.

It’s interesting, the same print line in ItemTemplate1 (the good one), prints pretty much as expected:
image

image

The same print line in ItemTemplate5 doesn’t have any effect, the table.TableError triggers and nothing else prints
image

I will DM you a clone…

(edit) - changed my response.

You need to print out the result of the DataTable search to ensure you are always retrieving the data in both circumstances, and this has to be at the server function that retrieves the data.

If that is retrieving everything in both circumstances, then we can look more closely at the code that is handling it. When fault finding an issue like this you need to be very methodical and systematic in working your way through the chain.

EDIT -
It is right, actually, after all that :slight_smile:

I’ve PM’d you the fixed project.

You were returning a client writeable view from your server function and trying to iterate through that incorrectly. The best way to search for the data in this instance is, I think, server side. So …

Change your server function to this (I split the result from the return so I could debug the result) :

@anvil.server.callable
def get_jobs():
  current_user = anvil.users.get_user()
  
  if current_user:
    jobs = app_tables.job_listings.search(tables.order_by('date_created',ascending=False),job_owner = current_user)
    return jobs

And in your main form :

#--------------begin call jobs based on the logged in user----------------    
     self.my_jobs = anvil.server.call('get_jobs')
     self.repeating_panel_my_jobs.items=self.my_jobs

Now, the server function is doing the search for you with all the parameters, and the result is directly compatible with the repeating panel. The item is sent correctly to the itemtemplate form and (I think) it all displays as required.

There may be some knock on effects from my changes, but apart from that does it seem to work ok after that?

Glad it worked for you (carrying on from a PM exchange).

The load time is quite slow considering the small amount of data - you need to work out where the slowdowns are happening either by judicious use of print statements at various points (so you can time the slow sections) or by commenting out code blocks and seeing where the speed increases.

Also, move as much of the data tables work to server side functions and return the results. This is good practice from a security perspective.

Good luck!