>
> Problem1 - (Primary issue)
> I have a table as below - How do I handle unique = True and
> notnull= True fields i.e when I am doing negative testing and adding
> similar fields, it throws me an exception.
If you set unique=True and notnull=True and then try to insert non-unique
or null values, respectively, it is supposed to throw an exception. If you
want to enter non-unique or null values, then don't set those arguments to
true. On the other hand, if you want to catch those errors more gracefully
during form submission, then you should use field validators. For
unique=True, use the IS_NOT_IN_DB validator, and for notnull=True, use the
IS_NOT_EMPTY validator. Note, if you don't specify any "requires" attribute
for the field, these validators should be added automatically. However, the
validators will only be used for form submissions when using SQLFORM or
when using the validate_and_insert() method for making inserts. If you are
doing manual inserts via the insert() method, the validators won't have any
effect.
Another option is to put your insert in a try...except, and catch the
IntegrityError.
> Problem2:
> How can I cache images that I have added for users who have added
> comments.
> Example-
> I have a web page where I have users comment over my posts. These
> users have id's defined and photos specified, how can I prevent
> loading one image at a time?
>
If it is OK for the images to be publicly accessible, you could upload them
to the /static folder instead of /uploads -- in that case, once initially
loaded by the browser, the browser should cache them for subsequent
displays. Otherwise, if you are using the download() function to download
from the /uploads folder, you could add some logic to the download function
to have it set the response headers to tell the browser to cache the images
(i.e., same as static files) -- you could use a URL arg or var as a flag to
tell the download function to do that.
> Problem3:
> How to restrict upload of files (any files including images) to say
> 512 kb or any size there of?
>
Use the IS_LENGTH validator:
Field('image', 'upload', requires=IS_LENGTH(524288))
The first argument to IS_LENGTH is the maximum file size in bytes (512KB =
524288 bytes). You can also specify a minimum size as the second argument
if needed (default is 0).
Anthony