bureaucrat 0.0.3 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,36 +1,30 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: bureaucrat
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Bruno Deferrari
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2009-11-03 00:00:00 -02:00
13
- default_executable:
12
+ date: 2012-04-14 00:00:00.000000000 Z
14
13
  dependencies: []
15
-
16
- description:
14
+ description: Bureaucrat is a form handling library for Ruby.
17
15
  email: utizoc@gmail.com
18
16
  executables: []
19
-
20
17
  extensions: []
21
-
22
18
  extra_rdoc_files: []
23
-
24
- files:
19
+ files:
25
20
  - lib/bureaucrat/fields.rb
26
21
  - lib/bureaucrat/forms.rb
27
22
  - lib/bureaucrat/formsets.rb
28
23
  - lib/bureaucrat/quickfields.rb
24
+ - lib/bureaucrat/temporary_uploaded_file.rb
29
25
  - lib/bureaucrat/utils.rb
30
- - lib/bureaucrat/validation.rb
31
- - lib/bureaucrat/validation_old.rb
26
+ - lib/bureaucrat/validators.rb
32
27
  - lib/bureaucrat/widgets.rb
33
- - lib/bureaucrat/wizard.rb
34
28
  - lib/bureaucrat.rb
35
29
  - README.md
36
30
  - LICENSE
@@ -39,33 +33,30 @@ files:
39
33
  - test/formsets_test.rb
40
34
  - test/test_helper.rb
41
35
  - test/widgets_test.rb
42
- has_rdoc: true
36
+ - Rakefile
37
+ - bureaucrat.gemspec
43
38
  homepage: http://github.com/tizoc/bureaucrat
44
39
  licenses: []
45
-
46
40
  post_install_message:
47
41
  rdoc_options: []
48
-
49
- require_paths:
42
+ require_paths:
50
43
  - lib
51
- required_ruby_version: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
56
- version:
57
- required_rubygems_version: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: "0"
62
- version:
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
63
56
  requirements: []
64
-
65
57
  rubyforge_project:
66
- rubygems_version: 1.3.5
58
+ rubygems_version: 1.8.11
67
59
  signing_key:
68
- specification_version: 2
60
+ specification_version: 3
69
61
  summary: Form handling for Ruby inspired by Django forms.
70
62
  test_files: []
71
-
@@ -1,130 +0,0 @@
1
- require 'bureaucrat/utils'
2
-
3
- module Bureaucrat
4
- module Validation
5
-
6
- class ValidationError < Exception
7
- attr_reader :error_code, :parameters
8
-
9
- def initialize(error_code, parameters=nil)
10
- @error_code = error_code
11
- @parameters = parameters || {}
12
- end
13
- end
14
-
15
- module Validates
16
- module_function
17
- def fail_with(error_code, parameters=nil)
18
- raise ValidationError.new(error_code, parameters)
19
- end
20
- end
21
-
22
- module Converters
23
- include Validates
24
-
25
- module_function
26
-
27
- def to_integer(string)
28
- Integer(string)
29
- rescue ArgumentError
30
- fail_with(:invalid)
31
- end
32
-
33
- def to_float(string)
34
- Utils.make_float(string)
35
- rescue ArgumentError
36
- fail_with(:invalid)
37
- end
38
-
39
- def to_big_decimal(string)
40
- Utils.make_float(string)
41
- BigDecimal.new(string)
42
- rescue ArgumentError
43
- fail_with(:invalid)
44
- end
45
-
46
- def to_bool(string)
47
- ['false', '0'].include?(string) ? false : Utils.make_bool(string)
48
- end
49
- end
50
-
51
- module Validators
52
- include Validates
53
-
54
- module_function
55
-
56
- def empty_value?(value)
57
- value.nil? || value == ''
58
- end
59
-
60
- def is_present(value)
61
- fail_with(:required) if empty_value?(value)
62
- end
63
-
64
- def not_empty(value)
65
- fail_with(:required) if value.empty?
66
- end
67
-
68
- def is_true(value)
69
- fail_with(:required) unless value
70
- end
71
-
72
- def is_array(value)
73
- fail_with(:invalid_list) unless value.kind_of(Array)
74
- end
75
-
76
- def has_min_length(value, min_length)
77
- value_length = value.length
78
- fail_with(:min_length, :min => min_length,
79
- :length => value_length) if value_length < min_length
80
- end
81
-
82
- def has_max_length(value, max_length)
83
- value_length = value.length
84
- fail_with(:max_length, :max => max_length,
85
- :length => value_length) if value_length > max_length
86
- end
87
-
88
- def is_not_lesser_than(value, min_value)
89
- fail_with(:min_value, :min => min_value) if value < min_value
90
- end
91
-
92
- def is_not_greater_than(value, max_value)
93
- fail_with(:max_value, :max => max_value) if value > max_value
94
- end
95
-
96
- def has_max_digits(value, max_digits)
97
- sign, alldigits, _, whole_digits = value.split
98
- fail_with(:max_digits, :max => max_digits) if alldigits > max_digits
99
- end
100
-
101
- def has_max_decimal_places(values, decimal_places)
102
- sign, alldigits, _, whole_digits = value.split
103
- decimals = alldigits.length - whole_digits
104
- fail_with(:max_decimal_places, :max => decimal_places) if
105
- decimals > decimal_places
106
- end
107
-
108
- def has_max_whole_digits(value, max_digits)
109
- sign, alldigits, _, whole_digits = value.split
110
- fail_with(:max_digits, :max => max_digits) if alldigits > max_digits
111
- end
112
-
113
- def included_in(value, collection)
114
- fail_with(:not_included, :collection => collection) unless
115
- collection.include?(value)
116
- end
117
-
118
- def matches_regex(value, regex, error_code=:invalid)
119
- fail_with(error_code, :regex => regex) if regex !~ value
120
- end
121
-
122
- EMAIL_RE = /(^[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$/i
123
-
124
- def is_email(value)
125
- matches_regex(value, EMAIL_RE)
126
- end
127
- end
128
-
129
- end
130
- end
@@ -1,148 +0,0 @@
1
- module ValueValidator
2
- def initialize_vv(format_validator, adaptor, value_validator)
3
- @vv_format_validator = format_validator
4
- @vv_adaptor = adaptor
5
- @vv_value_validator = value_validator
6
- end
7
-
8
- # add_format_validator
9
- # add_value_validator
10
-
11
- def clean(value, all_values={}, object=nil)
12
- return if @vv_format_validator.validate(value, all_values, object)
13
- @vv_value_validator.validate(@vv_adaptor.call(value), all_values, object)
14
- end
15
- end
16
-
17
- module Adaptors
18
- class Adaptor
19
- include Validates
20
-
21
- def initialize(allow_blank=false)
22
- @allow_blank = allow_blank
23
- end
24
-
25
- def to_object(value)
26
- if blank?(value)
27
- fail_with(:blank) unless @allow_blank
28
- nil
29
- else
30
- adapt(value)
31
- end
32
- end
33
-
34
- def adapt(value)
35
- value
36
- end
37
- end
38
-
39
- class IntegerAdaptor < Adaptor
40
- def adapt(value)
41
- begin
42
- Integer(value)
43
- rescue ArgumentError
44
- fail_with(:not_integer)
45
- end
46
- end
47
- end
48
-
49
- class FloatAdaptor < Adaptor
50
- include Bureaucrat::Utils
51
-
52
- def adapt(value)
53
- begin
54
- make_float(value)
55
- rescue ArgumentError
56
- fail_with(:not_float)
57
- end
58
- end
59
- end
60
-
61
- end
62
-
63
- class Validator
64
- include Validates
65
-
66
- def initialize(allow_blank=false)
67
- @allow_blank = allow_blank
68
- end
69
-
70
- def valid?(value, all_values={}, object=nil)
71
- validate(value, all_values, object)
72
- true
73
- end
74
-
75
- def validate(value, all_values={}, object=nil)
76
- blank?(value) && @allow_blank
77
- end
78
-
79
- def blank?(value)
80
- value.nil? || value.empty?
81
- end
82
- end
83
-
84
- class HasMinimumLengthOf < Validator
85
- def initialize(min_length)
86
- @min_length = min_length
87
- end
88
-
89
- def validate(value, all_values={}, object=nil)
90
- value_length = value.length
91
- fail_with(:min_length,
92
- :min_length => @min_length,
93
- :value_length => value_length) if value_length < @min_length
94
- end
95
- end
96
-
97
- class HasMaximumLengthOf < Validator
98
- def initialize(max_length)
99
- @max_length = max_length
100
- end
101
-
102
- def validate(value, all_values={}, object=nil)
103
- value_length = value.length
104
- fail_with(:max_length,
105
- :max_length => @max_length,
106
- :value_length => value_length) if value_length < @max_length
107
- end
108
- end
109
-
110
- class IsEqualOrGreaterThan < Validator
111
- def initialize(min_value)
112
- @min_value = min_value
113
- end
114
-
115
- def validate(value, all_values={}, object=nil)
116
- fail_with(:less_than, :min_value => @min_value) if value < @min_value
117
- end
118
- end
119
-
120
- class IsEqualOrLessThan < Validator
121
- def initialize(max_value)
122
- @max_value = max_value
123
- end
124
-
125
- def validate(value, all_values={}, object=nil)
126
- fail_with(:more_than, :max_value => @max_value) if value > @max_value
127
- end
128
- end
129
-
130
- class MatchesRegex < Validator
131
- def initialize(regex, custom_error_code=nil)
132
- @regex = regex
133
- @custom_error_code = custom_error_code
134
- end
135
-
136
- def validate(value, all_values={}, object=nil)
137
- fail_with(@custom_error_code || :not_matching_regex,
138
- :regex => @regex) if @regex !~ value
139
- end
140
- end
141
-
142
- class IsEmail < MatchesRegex
143
- EMAIL_RE = /(^[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$/i
144
-
145
- def initialize
146
- super(EMAIL_RE, :is_not_email)
147
- end
148
- end
@@ -1,220 +0,0 @@
1
- require 'digest'
2
-
3
- require 'bureaucrat/forms'
4
- # import cPickle as pickle
5
-
6
- # from django.conf import settings
7
- # from django.http import Http404
8
- # from django.shortcuts import render_to_response
9
- # from django.template.context import RequestContext
10
- # from django.utils.hashcompat import md5_constructor
11
- # from django.utils.translation import ugettext_lazy as _
12
-
13
- module Bureaucrat; Wizard
14
- module Utils
15
- def security_hash(request, form, *args)
16
- data = []
17
- form.each do |bf|
18
- value = if form.empty_permitted? && ! form.changed? then bf.data
19
- else bf.field.clean(bf.data)
20
- end
21
- value ||= ''
22
- value = value.strip if value.respond_to? :strip
23
- data.append([bf.name, value])
24
- end
25
-
26
- data += args
27
- data << settings.SECRET_KEY # FIXME
28
-
29
- Digest::MD5.hexdigest(Marshal.dump(data))
30
- end
31
-
32
- module_function :security_hash
33
- end
34
-
35
- class FormWizard
36
- # FIXME
37
- # The HTML (and POST data) field name for the "step" variable.
38
-
39
- class << self
40
- attr_accessor :step_field_name
41
- end
42
-
43
- self.step_field_name = "wizard_step"
44
-
45
- def initialize(form_list, initial=Hash.new)
46
- @form_list = form_list.dup
47
- @initial = initial
48
- @step = 0
49
- end
50
-
51
- def get_form(step, data=nil)
52
- @form_list[step].new(data,
53
- :prefix => prefix_for_step(step),
54
- :initial => @initial.fetch(step, nil))
55
- end
56
-
57
- def num_steps
58
- @form_list.length
59
- end
60
-
61
- def new(request, *args)
62
- current_step = determine_step(request, *args)
63
- parse_params(request, *args)
64
-
65
- raise ArgumentError("Step #{current_step} does not exist") if
66
- current_step >= num_steps
67
-
68
- (0...current_step).each do |i|
69
- form = get_form(i, request.POST)
70
-
71
- return render_hash_failure(request, i) if
72
- request.POST.fetch("hash_#{i}", '') != security_hash(request, form)
73
-
74
- process_step(request, form, i)
75
- end
76
-
77
- form = get_form(current_step, request.post? ? request.POST : nil)
78
-
79
- if form.valid?
80
- process_step(request, form, current_step)
81
- next_step = current_step + 1
82
-
83
- if next_step == num_steps
84
- final_form_list = (0...num_steps).map {|i| get_form(i, request.POST)}
85
-
86
- final_form_list.each_with_index do |f, i|
87
- return render_revalidation_failure(request, i, f) unless f.valid?
88
- end
89
-
90
- return done(request, final_form_list)
91
- else
92
- form = get_form(next_step)
93
- @step = current_step = next_step
94
- end
95
- end
96
-
97
- render(form, request, current_step)
98
- end
99
-
100
- def render(form, request, step, context=nil)
101
- "Renders the given Form object, returning an HttpResponse." # FIXME
102
- old_data = request.POST
103
- prev_fields = []
104
-
105
- if old_data
106
- hidden = Widgets::HiddenInput.new
107
- # Collect all data from previous steps and render it as HTML hidden fields.
108
- (0...step).each do |i|
109
- old_form = get_form(i, old_data)
110
- hash_name = "hash_#{i}"
111
- hash = old_data[hash_name] || security_hash(request, old_form)
112
- prev_fields += old_form.map {|bf| bf.as_hidden}
113
- prev_fields << hidden.render(hash_name, hash)
114
- end
115
- end
116
-
117
- render_template(request, form, prev_fields.join(''), step, context)
118
- end
119
-
120
- # METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE
121
-
122
- def prefix_for_step(step)
123
- step.to_s
124
- end
125
-
126
- def render_hash_failure(request, step)
127
- # Hook for rendering a template if a hash check failed.
128
-
129
- # step is the step that failed. Any previous step is guaranteed to be
130
- # valid.
131
-
132
- # This default implementation simply renders the form for the given step,
133
- # but subclasses may want to display an error message, etc.
134
- render(get_form(step), request, step, :context => {:wizard_error => _('We apologize, but your form has expired. Please continue filling out the form from this page.')})
135
- end
136
-
137
- def render_revalidation_failure(request, step, form)
138
- # Hook for rendering a template if final revalidation failed.
139
-
140
- # It is highly unlikely that this point would ever be reached, but See
141
- # the comment in __call__() for an explanation.
142
- render(form, request, step)
143
- end
144
-
145
- def security_hash(request, form)
146
- Utils::security_hash(request, form)
147
- end
148
-
149
- def determine_step(request, *args)
150
- if request.post?
151
- begin
152
- Integer(request.POST.fetch(step_field_name, 0))
153
- rescue ArgumentError
154
- 0
155
- end
156
- else
157
- 0
158
- end
159
- end
160
-
161
- def parse_params(request, *args)
162
- end
163
-
164
- def get_template(step)
165
- 'forms/wizard.erb'
166
- end
167
-
168
- def render_template(request, form, previous_fields, step, context=nil)
169
-
170
- # Renders the template for the given step, returning an HttpResponse object.
171
-
172
- # Override this method if you want to add a custom context, return a
173
- # different MIME type, etc. If you only need to override the template
174
- # name, use get_template() instead.
175
-
176
- # The template will be rendered with the following context:
177
- # step_field -- The name of the hidden field containing the step.
178
- # step0 -- The current step (zero-based).
179
- # step -- The current step (one-based).
180
- # step_count -- The total number of steps.
181
- # form -- The Form instance for the current step (either empty
182
- # or with errors).
183
- # previous_fields -- A string representing every previous data field,
184
- # plus hashes for completed forms, all in the form of
185
- # hidden fields. Note that you'll need to run this
186
- # through the 'safe' template filter, to prevent
187
- # auto-escaping, because it's raw HTML.
188
-
189
- context ||= {}
190
- # FIXME
191
- render_to_response(get_template(step),
192
- context.merge(:step_field => step_field_name,
193
- :step0 => step,
194
- :step => step + 1,
195
- :step_count => num_steps,
196
- :form => form,
197
- :previous_fields => previous_fields),
198
- :context_instance => RequestContext(request))
199
- end
200
-
201
- def process_step(request, form, step)
202
- # Hook for modifying the FormWizard's internal state, given a fully
203
- # validated Form object. The Form is guaranteed to have clean, valid
204
- # data.
205
-
206
- # This method should *not* modify any of that data. Rather, it might want
207
- # to set self.extra_context or dynamically alter self.form_list, based on
208
- # previously submitted forms.
209
-
210
- # Note that this method is called every time a page is rendered for *all*
211
- # submitted steps.
212
- end
213
-
214
- # METHODS SUBCLASSES MUST OVERRIDE ########################################
215
-
216
- def done(request, form_list)
217
- raise NotImplementedError.new("#{self.class.name} doesn't define the required 'done' method.")
218
- end
219
- end
220
- end