File: ex_63_hooks_field_validation.py

package info (click to toggle)
sqlkit 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,184 kB
  • sloc: python: 17,477; sql: 166; makefile: 95; xml: 23; sh: 11
file content (31 lines) | stat: -rw-r--r-- 743 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""hooks/on_field_validation on_save_as

you can set hooks on single field validation. It will be called from within
the field. The hook 'on_save_as' is called when a record is duplicated.


"""
from sqlkit.exc import ValidationError

class Hooks(object):

    def on_field_validation__year(self, mask, field_name, field_value, field):
        if field_value > 2020:
            raise ValidationError("Hei: how can you know the future!")

    def on_save_as(self, mask, old, new):
        print "Old title: %s, \nnew title: %s" % (old.title, new.title)


lay = """
   title
   year
   director_id
   m2m=genres -
   """

t = SqlMask(model.Movie, layout=lay, label_map={'genres.name':'genres'},
         dbproxy=db, hooks=Hooks())

t.reload()