[web2py] Re: File download - disable link when no files are available

2015-02-27 Thread P T
Thank you Leonel for taking the time to explain the syntax!

Best Regards,
PT


On Friday, February 27, 2015 at 11:45:03 AM UTC-6, P T wrote:
>
> Hello All,
>
> Currently using Web2Py 2.8.2-stable+timestamp.2013.11.28.13.54.07
>
> I have a simple page for announcing seminars and for downloading 
> presentation files. 
>
> Here are definitions for table, controller and view :
>
>
> db.define_table("seminars",
> Field('sem_date', 'date', label='Date'),
> Field('Sem_Time', 'string', label='Time'),
> Field('room', 'string', label='Location'),
> Field('person', 'string', label='Presenter'),
> Field('title', 'string', label='Seminar Title'),
> Field('sem_file', 'upload', label='Files', requires=False)
> )
> db.seminars.sem_file.represent = lambda value,row:A('Download', _href=URL(
> 'download', args=value)) 
>
> --
>
> def index():
> db.seminars.id.readable=False
> grid = SQLFORM.grid(db.seminars, maxtextlength=150, 
> details=False, deletable=False, searchable=False, 
> csv=False,  editargs=dict(id=False),  formstyle='bootstrap')
> grid.element('.web2py_counter', replace=None)
> return dict(grid=grid)
>
> --
>
> {{extend 'seminar_layout.html'}}
>
> {{=grid}}
>
>
> It works fine as intended. But, I want to replace "Download" link with "No 
> Files Available" and disable the link when there are there are no files 
> available. Any suggestion or help is appreciated. 
>
> Thanks,
> PT
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: File download - disable link when no files are available

2015-02-27 Thread Leonel Câmara
The syntax is just regular python, what I've used is called a conditional 
expression  which is python's 
version of the ternary operator.

It has the form:

a if test else b

Which is equivalent to this in many languages:

test ? a : b;


So what's happening here is that the value is None if there's no file, None 
evaluates to False when used as a boolean. So what the syntax actually 
means is, return the link if there's a value otherwise return "No Files 
Available", which coincidentally is exactly what you wanted.

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: File download - disable link when no files are available

2015-02-27 Thread P T
Thanks Leonel, it works great. But, I am stumped by the syntax. 


On Friday, February 27, 2015 at 11:45:03 AM UTC-6, P T wrote:
>
> Hello All,
>
> Currently using Web2Py 2.8.2-stable+timestamp.2013.11.28.13.54.07
>
> I have a simple page for announcing seminars and for downloading 
> presentation files. 
>
> Here are definitions for table, controller and view :
>
>
> db.define_table("seminars",
> Field('sem_date', 'date', label='Date'),
> Field('Sem_Time', 'string', label='Time'),
> Field('room', 'string', label='Location'),
> Field('person', 'string', label='Presenter'),
> Field('title', 'string', label='Seminar Title'),
> Field('sem_file', 'upload', label='Files', requires=False)
> )
> db.seminars.sem_file.represent = lambda value,row:A('Download', _href=URL(
> 'download', args=value)) 
>
> --
>
> def index():
> db.seminars.id.readable=False
> grid = SQLFORM.grid(db.seminars, maxtextlength=150, 
> details=False, deletable=False, searchable=False, 
> csv=False,  editargs=dict(id=False),  formstyle='bootstrap')
> grid.element('.web2py_counter', replace=None)
> return dict(grid=grid)
>
> --
>
> {{extend 'seminar_layout.html'}}
>
> {{=grid}}
>
>
> It works fine as intended. But, I want to replace "Download" link with "No 
> Files Available" and disable the link when there are there are no files 
> available. Any suggestion or help is appreciated. 
>
> Thanks,
> PT
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: File download - disable link when no files are available

2015-02-27 Thread Leonel Câmara
Hey, 

How about this:

db.seminars.sem_file.represent = lambda value,row:A('Download', _href=URL(
'download', args=value)) if value else "No Files Available"


-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.