Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
validators.py
Go to the documentation of this file.
1 ##
2 #
3 # Custom WTForms validators.
4 #
5
6 from __future__ import unicode_literals
7 import os
8 from cgi import FieldStorage
9 from wowf.lib.auth import Auth
10 from wowf.models import DBSession
11 from wtforms.validators import Required, ValidationError
12
13
14 ##
15 #
16 # Makes sure that a field does not already exist in the database.
17 #
18 # When editing rows, pass the id of the row as a hidden field so that
19 # validation does not fail if the field does not change.
20 #
21 class Unique(object):
22
23 ##
24 #
25 # @param model The model to query
26 # @param column The column that must be unique
27 #
28 def __init__(self, model, column, message=None):
29 self.model = model
30 self.column = column
31 self.message = message
32
33 def __call__(self, form, field):
34 id = None
35 if 'id' in form.state:
36 id = form.state.id
37 found = DBSession.query(self.model).filter(self.column==field.data).first()
38 if found and found.id != id:
39 if self.message is None:
40 self.message = 'Field must be unique.'
41 raise ValidationError(self.message)
42
43
44 ##
45 #
46 # Makes sure that a field exists in the database.
47 #
48 class Exists(Unique):
49
50 def __call__(self, form, field):
51 found = DBSession.query(self.model).filter(self.column==field.data).first()
52 if not found:
53 if self.message is None:
54 self.message = 'Field must exist.'
55 raise ValidationError(self.message)
56
57
58 ##
59 #
60 # Makes sure that the email and password combo exists in the database, and
61 # the user has the necessary priveleges to login.
62 #
63 class ValidLogin(object):
64
65 def __init__(self, message=None):
66 self.message = message
67
68 def __call__(self, form, field):
69 if not Auth.check_login(form.email.data, form.password.data):
70 if self.message is None:
71 self.message = 'Invalid login.'
72 raise ValidationError(self.message)
73
74
75 ##
76 #
77 # Makes sure a file was selected and uploaded.
78 #
79 class FileRequired(Required):
80
81 def __init__(self, message=None):
82 self.message = message
83
84 def __call__(self, form, field):
85 if not isinstance(field.data, FieldStorage):
86 if self.message is None:
87 self.message = 'A file is required.'
88 raise ValidationError(self.message)
89
90
91 ##
92 #
93 # Makes sure a file is of the proper type.
94 #
95 class FileType(object):
96
97 def __init__(self, allowed, message=None):
98 self.allowed = [x.upper() for x in allowed]
99 self.message = message
100
101 def __call__(self, form, field):
102 if isinstance(field.data, FieldStorage):
103 ext = os.path.splitext(field.data.filename)[1]
104 ext = ext.lstrip('.').upper()
105 if ext not in self.allowed:
106 if self.message is None:
107 self.message = 'File type must be %(allowed)s.'
108 if len(self.allowed) <= 2:
109 allowed = ' or '.join(self.allowed)
110 else:
111 allowed = ', '.join(self.allowed[:-1] + ['or ' + self.allowed[-1]])
112 self.message = self.message % dict(allowed=allowed)
113 raise ValidationError(self.message)
114