44
44
import werkzeug .exceptions as httpexceptions
45
45
from wsgiref .simple_server import WSGIRequestHandler
46
46
47
+ from mig .lib .coresvc .payloads import ValidationReport , validate_payload , \
48
+ PAYLOAD_POST_USER as _REQUEST_ARGS_POST_USER
47
49
from mig .shared .base import canonical_user , keyword_auto , force_native_str_rec
48
50
from mig .shared .useradm import fill_user , \
49
51
create_user as useradm_create_user , search_users as useradm_search_users
@@ -58,20 +60,6 @@ def http_error_from_status_code(http_status_code, http_url, description=None):
58
60
return httpexceptions_by_code [http_status_code ](description )
59
61
60
62
61
- class ValidationReport (RuntimeError ):
62
- def __init__ (self , errors_by_field ):
63
- self .errors_by_field = errors_by_field
64
-
65
- def serialize (self , output_format = 'text' ):
66
- if output_format == 'json' :
67
- return dict (errors = self .errors_by_field )
68
- else :
69
- lines = ["- %s: required %s" %
70
- (k , v ) for k , v in self .errors_by_field .items ()]
71
- lines .insert (0 , '' )
72
- return 'payload failed to validate:%s' % ('\n ' .join (lines ),)
73
-
74
-
75
63
def _create_user (user_dict , conf_path , ** kwargs ):
76
64
try :
77
65
useradm_create_user (user_dict , conf_path , keyword_auto , ** kwargs )
@@ -80,71 +68,11 @@ def _create_user(user_dict, conf_path, **kwargs):
80
68
return 0
81
69
82
70
83
- def _define_payload (payload_name , payload_fields ):
84
- class Payload (namedtuple ('PostUserArgs' , payload_fields )):
85
- def keys (self ):
86
- return Payload ._fields
87
-
88
- def items (self ):
89
- return self ._asdict ().items ()
90
-
91
- Payload .__name__ = payload_name
92
-
93
- return Payload
94
-
95
-
96
- def _is_not_none (value ):
97
- """value is not None"""
98
- return value is not None
99
-
100
-
101
- def _is_string_and_non_empty (value ):
102
- """value is a non-empty string"""
103
- return isinstance (value , str ) and len (value ) > 0
104
-
105
-
106
- _REQUEST_ARGS_POST_USER = _define_payload ('PostUserArgs' , [
107
- 'full_name' ,
108
- 'organization' ,
109
- 'state' ,
110
- 'country' ,
111
- 'email' ,
112
- 'comment' ,
113
- 'password' ,
114
- ])
115
-
116
-
117
- _REQUEST_ARGS_POST_USER ._validators = defaultdict (lambda : _is_not_none , dict (
118
- full_name = _is_string_and_non_empty ,
119
- organization = _is_string_and_non_empty ,
120
- state = _is_string_and_non_empty ,
121
- country = _is_string_and_non_empty ,
122
- email = _is_string_and_non_empty ,
123
- comment = _is_string_and_non_empty ,
124
- password = _is_string_and_non_empty ,
125
- ))
126
-
127
-
128
71
def search_users (configuration , search_filter ):
129
72
_ , hits = useradm_search_users (search_filter , configuration , keyword_auto )
130
73
return list ((obj for _ , obj in hits ))
131
74
132
75
133
- def validate_payload (definition , payload ):
134
- args = definition (* [payload .get (field , None )
135
- for field in definition ._fields ])
136
-
137
- errors_by_field = {}
138
- for field_name , field_value in args ._asdict ().items ():
139
- validator_fn = definition ._validators [field_name ]
140
- if not validator_fn (field_value ):
141
- errors_by_field [field_name ] = validator_fn .__doc__
142
- if errors_by_field :
143
- raise ValidationReport (errors_by_field )
144
- else :
145
- return args
146
-
147
-
148
76
def _create_and_expose_server (server , configuration ):
149
77
app = Flask ('coreapi' )
150
78
0 commit comments