This file is indexed.

/usr/share/pyshared/zope/formlib/interfaces.py is in python-zope.formlib 4.0.5-0ubuntu5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Form interfaces
"""
import re
from zope import schema
from zope.publisher.interfaces.browser import IBrowserPage
from zope.schema.interfaces import ValidationError
from zope.publisher.interfaces import IView
from zope.interface import Attribute, Interface, implements, Invalid
from zope.schema import Bool
from zope.exceptions.interfaces import UserError

class IWidgetInputError(Interface):
    """Placeholder for a snippet View"""

    def doc():
        """Returns a string that represents the error message."""

class WidgetInputError(UserError):
    """One or more user input errors occurred."""

    implements(IWidgetInputError)

    def __init__(self, field_name, widget_title, errors=None):
        """Initialize Error

        `errors` is a ``ValidationError`` or a list of ValidationError objects
        """
        UserError.__init__(self, field_name, widget_title, errors)
        self.field_name = field_name
        self.widget_title = widget_title
        self.errors = errors

    def doc(self):
        # TODO this duck typing is to get the code working.  See
        # collector issue 372
        if isinstance(self.errors, basestring):
            return self.errors
        elif getattr(self.errors, 'doc', None) is not None:
            return self.errors.doc()
        return ''


class MissingInputError(WidgetInputError):
    """Required data was not supplied."""


class ConversionError(Exception):
    """A conversion error occurred."""

    implements(IWidgetInputError)

    def __init__(self, error_name, original_exception=None):
        Exception.__init__(self, error_name, original_exception)
        self.error_name = error_name
        self.original_exception = original_exception

    def doc(self):
        return self.error_name

InputErrors = WidgetInputError, ValidationError, ConversionError


class ErrorContainer(Exception):
    """A base error class for collecting multiple errors."""

    def append(self, error):
        self.args += (error, )

    def __len__(self):
        return len(self.args)

    def __iter__(self):
        return iter(self.args)

    def __getitem__(self, i):
        return self.args[i]

    def __str__(self):
        return "\n".join(
            ["%s: %s" % (error.__class__.__name__, error)
             for error in self.args]
            )

    __repr__ = __str__

class WidgetsError(ErrorContainer):
    """A collection of errors from widget processing.

    widgetValues is a map containing the list of values that were obtained
    from the widgets, keyed by field name.
    """

    def __init__(self, errors, widgetsData={}):
        ErrorContainer.__init__(self, *errors)
        self.widgetsData = widgetsData

class IWidget(IView):
    """Generically describes the behavior of a widget.

    Note that this level must be still presentation independent.
    """

    name = Attribute(
        """The unique widget name

        This must be unique within a set of widgets.""")

    label = Attribute(
        """The widget label.

        Label may be translated for the request.

        The attribute may be implemented as either a read-write or read-only
        property, depending on the requirements for a specific implementation.

        """)

    hint = Attribute(
        """A hint regarding the use of the widget.

        Hints are traditionally rendered using tooltips in GUIs, but may be
        rendered differently depending on the UI implementation.

        Hint may be translated for the request.

        The attribute may be implemented as either a read-write or read-only
        property, depending on the requirements for a specific implementation.

        """)

    visible = Attribute(
        """A flag indicating whether or not the widget is visible.""")

    def setRenderedValue(value):
        """Set the value to be rendered by the widget.

        Calling this method will override any values provided by the user.

        For input widgets (`IInputWidget` implementations), calling
        this sets the value that will be rendered even if there is
        already user input.

        """

    def setPrefix(prefix):
        """Set the name prefix used for the widget

        The widget name is used to identify the widget's data within
        input data.  For example, for HTTP forms, the widget name is
        used for the form key.

        It is acceptable to *reset* the prefix: set it once to read
        values from the request, and again to redraw with a different
        prefix but maintained state.

        """

class IInputWidget(IWidget):
    """A widget for editing a field value."""

    required = Bool(
        title=u"Required",
        description=u"""If True, widget should be displayed as requiring input.

        By default, this value is the field's 'required' attribute. This
        field can be set to False for widgets that always provide input (e.g.
        a checkbox) to avoid unnecessary 'required' UI notations.
        """)

    def getInputValue():
        """Return value suitable for the widget's field.

        The widget must return a value that can be legally assigned to
        its bound field or otherwise raise ``WidgetInputError``.

        The return value is not affected by `setRenderedValue()`.
        """

    def applyChanges(content):
        """Validate the user input data and apply it to the content.

        Return a boolean indicating whether a change was actually applied.

        This raises an error if there is no user input.
        """

    def hasInput():
        """Returns ``True`` if the widget has input.

        Input is used by the widget to calculate an 'input value', which is
        a value that can be legally assigned to a field.

        Note that the widget may return ``True``, indicating it has input, but
        still be unable to return a value from `getInputValue`. Use
        `hasValidInput` to determine whether or not `getInputValue` will return
        a valid value.

        A widget that does not have input should generally not be used
        to update its bound field.  Values set using
        `setRenderedValue()` do not count as user input.

        A widget that has been rendered into a form which has been
        submitted must report that it has input.  If the form
        containing the widget has not been submitted, the widget
        shall report that it has no input.

        """

    def hasValidInput():
        """Returns ``True`` is the widget has valid input.

        This method is similar to `hasInput` but it also confirms that the
        input provided by the user can be converted to a valid field value
        based on the field constraints.
        """

class IDisplayWidget(IWidget):
    """A widget for displaying a field value."""

    required = Bool(
        title=u"Required",
        description=u"""If True, widget should be displayed as requiring input.

        Display widgets should never be required.
        """)

class IWidgetFactory(Interface):
    """A factory that creates the widget"""
    
    def __call__(context, request):
        """Return a widget"""

class FormError(Exception):
    """There was an error in managing the form
    """

class IBrowserWidget(IWidget):
    """A widget for use in a web browser UI."""

    def __call__():
        """Render the widget."""

    def hidden():
        """Render the widget as a hidden field."""

    def error():
        """Render the validation error for the widget, or return
        an empty string if no error"""


class ISimpleInputWidget(IBrowserWidget, IInputWidget):
    """A widget that uses a single HTML element to collect user input."""

    tag = schema.TextLine(
        title=u'Tag',
        description=u'The widget HTML element.')

    type = schema.TextLine(
        title=u'Type',
        description=u'The element type attribute',
        required=False)

    cssClass = schema.TextLine(
        title=u'CSS Class',
        description=u'The element class attribute.',
        required=False)

    extra = schema.TextLine(
        title=u'Extra',
        description=u'The element extra attribute.',
        required=False)


class ITextBrowserWidget(ISimpleInputWidget):

    convert_missing_value = schema.Bool(
        title=u'Translate Input Value',
        description=
            u'If True, an empty string is converted to field.missing_value.',
        default=True)

# XXX this seems to be buggy (since at least 2005)
# it returns None, and prefix_re is never defined.
def reConstraint(pat, explanation):
    pat = re.compile(pat)

    def constraint(value):
        if prefix_re.match(value):
            return True
        raise Invalid(value, explanation)

class IWidgetInputErrorView(Interface):
    """Display an input error as a snippet of text."""

    def snippet():
        """Convert a widget input error to an html snippet."""


class ISourceQueryView(Interface):
    """View support for querying non-iterable sources
    """

    def render(name):
        """Return a rendering of the search form elements

        The query view should use `name` as the prefix for its widgets.
        """

    def results(name):
        """Return the results of the query

        The query view should use `name` as the prefix for its widgets.

        The value returned is an iterable.

        None may be returned to indicate that there are no results.
        """

class ISubPage(Interface):
    """A component that computes part of a page
    """

    def update():
        """Update content ot view information based on user input
        """

    def render():
        """Render the sub page, returning a unicode string
        """

    prefix = schema.ASCII(
        constraint=reConstraint(
            '[a-zA-Z][a-zA-Z0-9_]*([.][a-zA-Z][a-zA-Z0-9_]*)*',
            "Must be a sequence of not-separated identifiers"),
        description=u"""Page-element prefix

        All named or identified page elements in a subpage should have
        names and identifiers that begin with a subpage prefix
        followed by a dot.
        """,
        readonly=True,
        )

    def setPrefix(prefix):
        """Update the subpage prefix
        """


class IFormAPI(Interface):
    """API to facilitate creating forms, provided by zope.formlib.form
    """

    def Field(schema_field, **options):
        """Define a form field from a schema field and usage options


        The following options are supported:

        name
          Provide a name to use for the field.

        prefix
          The form-field prefix.

        for_display
          A flag indicating whether the form-field is to be used
          for display. See IFormField.

        for_input
          A flag indicating whether the form-field is to be used
          for input. See IFormField.

        custom_widget
           Factory to use for widget construction. See IFormField.

        render_context
           A flag indicating whether the default value to render
           should come from the form context. See IFormField.

        get_rendered
           A callable or form method name to be used to get a default
           rendered value.  See IFormField.

        """


    def Fields(*arguments, **options):
        """Create form-fields collection (IFormFields)

        Creates a form-field collection from a collection of:

        - Schemas

        - Schema fields

        - form fields (IFormField)

        - form-field collections (IFormFields)

        An IFormFields is returned.

        The following options are supported:

        name
          Provide a name to use for the field.

        prefix
          The form-field prefix for new form-fields created.
          When form-field collections are passed, their contents keep
          their existing prefixes are retained.

        for_display
          A flag indicating whether the form-fiellds are to be used
          for display.  This value is used for for_display attributes
          of all created form fields.  This option does not effect
          input from form-field collections.

        for_input
          A flag indicating whether the form-fiellds are to be used
          for input.  This value is used for for_input attributes
          of all created form fields.  This option does not effect
          input from form-field collections.

        render_context
           A flag indicating whether the default values to render
           should come from the form context. See IFormField.

        """

    def setUpInputWidgets(form_fields, form_prefix, context, request,
                          ignore_request=False):
        """Set up widgets for input

        An IWidgets is returned based on the give form fields.

        All of the resulting widgets will be input widgets, regardless
        of whether the form fields are for display or whether the
        underlying schema fields are read only.  This is so that one
        can easily build an input form, such as an add form from an
        existing schema.

        The widgets will have prefixes that combine the given form
        prefix and any form-field prefixes.

        A context argument is provided to allow field binding.

        If ignore_request passed a true value, then the widgets will
        not initialize their values from the request.
        """

    def setUpEditWidgets(form_fields, form_prefix, context, request,
                         adapters=None, for_display=False,
                         ignore_request=False):
        """Set up widgets for editing or displaying content

        An IWidgets is returned based on the give form fields.

        The resulting widgets will be input widgets unless:

        - the corresponding form field was defined with the
          for_display option,

        - the underlying field is read only, or

        - the for_display opetion to setUpEditWidgets was passed a
          true value.

        The widgets fields are bound to the context after it is
        adapted to the field schema.  A mapping object can be passed
        to setUpEditWidgets to capture the adapters created. The
        adapters are placed in the mapping using both interfaces and
        interface names as keys.

        If the ignore_request option is passed a true value, then
        widget's rendered data will be set from the context, and user
        inputs will be ignored.
        """

    def setUpDataWidgets(form_fields, form_prefix, context, request, data=(),
                         for_display=False, ignore_request=False):
        """Set up widgets for input or display

        An IWidgets is returned based on the give form fields.

        The resulting widgets will be input widgets unless:

        - the corresponding form field was defined with the
          for_display option,

        - the underlying field is read only, or

        - the for_display opetion to setUpEditWidgets was passed a
          true value.

        A data mapping argument can be passed to provide initial
        data.

        If the ignore_request option is passed a true value, then
        widget's rendered data will be set from the passed data or
        from field defaults, and user inputs will be ignored.

        """

    def getWidgetsData(widgets, form_prefix, data):
        """Get input data and input errors

        A sequence of input errors are returned.  Any data available
        are added to the data argument, which must be a mapping
        argument.  The keys in the output mapping are
        widget/form-field names without the form prefix.

        """

    def checkInvariants(form_fields, form_data):
        """Check schema invariants for input data

        For each schema that was used to define the form fields and
        that had invariants relevent to the fields, the invariants are
        checked. Invariants that refer to fields not included in the
        form fields are ignored.

        A list of errors is returned.
        """

    def applyChanges(context, form_fields, data, adapters=None):
        """Apply form data to an object

        For each form field that has data, the data are applied to the
        context argument.  The context is adapter to the schema for
        each field.  If an adapters mapping is passed, it will be used
        as a cache.  Typically, it would be a mapping object populated
        when setUpEditWidgets was called.

        """

    def Action(label, **options):
        """Define a submit action

        options:

        condition
          A callable or  name of a method to  call to test whether
          the  action is  applicable.  if  the value  is  a method
          name,  then the method  will be  passed the  action when
          called, otherwise, the callables will be passed the form
          and the action.

        validator
          A callable or name of a method to call to validate and
          collect inputs.  This is called only if the action was
          submitted and if the action either has no condition, or the
          condition evaluates to a true value.  If the validator is
          provided as a method name, the method will be called the
          action and a dictionary in which to save data.  If the
          validator is provided as a callable, the callable will be
          called the form, the action, and a dictionary in which to
          save data.  The validator normally returns a (usually empty)
          list of widget input errors.  It may also return None to behave
          as if the action wasn't submitted.

        success
          A handler, called when the the action was submitted and
          there are no validation errors.  The handler may be provided
          as either a callable or a method name.  If the handler is
          provided as a method name, the method will be called the
          action and a dictionary containing the form data.  If the
          success handler is provided as a callable, the callable will
          be called the form, the action, and a dictionary containing
          the data.  The handler may return a form result (e.g. page),
          or may return None to indicate that the form should generate
          it's own output.

        failure
          A handler, called when the the action was submitted and
          there are validation errors.  The handler may be provided as
          either a callable or a method name.  If the handler is
          provided as a method name, the method will be called the
          action, a dictionary containing the form data, and a list of
          errors.  If the failure handler is provided as a callable,
          the callable will be called the form, the action, a
          dictionary containing the data, and a list of errors.  The
          handler may return a form result (e.g. page), or may return
          None to indicate that the form should generate it's own
          output.

        prefix
          A form prefix for the action.  When generating submit
          actions, the prefix should be combined with the action
          name, separating the two with a dot. The default prefix
          is "actions"form.

        name
          The action name, without a prefix.  If the label is a
          valid Python identifier, then the lowe-case label will
          be used, otherwise, a hex encoding of the label will be
          used.  If for some strange reason the labels in a set of
          actions with the same prefix is not unique, a name will
          have to be given for some actions to get unique names.

        css_class
          The CSS class for the action.  The class defaults to
          "action"form.

        data
          A bag of extra information that can be used by handlers,
          validators, or conditions.

        """

    def action(label, **options):
        """Create an action factory

        This function creates a factory for creating an action from a
        function, using the function as the action success handler.
        The options are the same as for the Action constructor except
        that the options don't include the success option.

        The function is designed to be used as a decorator (Python 2.4
        and later), as in:

        @action("Edit")
        def handle_edit(self, action, data):
            ...


        """

    def validate(form, actions, form_prefix, data, default_validate=None):
        """Process a submitted action, if any

        Check each of the given actions to see if any were submitted.

        If an action was submitted, then validate the input.  The input
        is called by calling the action's validator, ir it has one, or
        by calling the default_validate passed in.

        If the input is validated successfully, and the action has one
        success handler, then the success handler is called.

        If the input was validated and there were errors, then the
        action's failure handler will be called, if it has one.

        If an action was submitted, then the function returns the
        result of validation and the action.  The result of validation
        is normally a boolean, but may be None if no validator was
        provided.

        If no action was submitted, then None is returned for both the
        result of validation and the action.

        """

    FormBase = Attribute("""Base class for creating forms

    The FormBase class provides reuasable implementation for creating
    forms.  It implements ISubPage, IBrowserPage, and IFormBaseCustomization.
    Subclasses will override or use attributes defined by
    IFormBaseCustomization.
    """)

class IFormBaseCustomization(ISubPage, IBrowserPage):
    """Attributes provided by the Form base class

    These attributes may be used or overridden.

    Note that the update and render methods are designed to to work
    together.  If you override one, you probably need to override the
    other, unless you use original versions in your override.

    """

    label = Attribute("A label to display at the top of a form")

    status = Attribute(
        """An update status message

        This is normally generated by success or failure handlers.
        """)

    errors = Attribute(
        """Sequence of errors encountered during validation
        """)

    form_result = Attribute(
        """Return from action result method
        """)

    form_reset = Attribute(
        """Boolean indicating whether the form needs to be reset
        """)

    form_fields = Attribute(
        """The form's form field definitions

        This attribute is used by many of the default methods.
        """)

    widgets = Attribute(
        """The form's widgets

        - set by setUpWidgets

        - used by validate
        """)

    def setUpWidgets(ignore_request=False):
        """Set up the form's widgets.

        The default implementation uses the form definitions in the
        form_fields attribute and setUpInputWidgets.

        The function should set the widgets attribute.
        """

    def validate(action, data):
        """The default form validator

        If an action is submitted and the action doesn't have it's own
        validator then this function will be called.
        """

    template = Attribute(
        """Template used to display the form

        This can be overridden in 2 ways:

        1. You can override the attribute in a subclass

        2. You can register an alternate named template, named
           "default" for your form.

        """)

    def resetForm():
        """Reset any cached data because underlying content may have changed
        """

    def error_views():
        """Return views of any errors.

        The errors are returned as an iterable.
        """



class IFormFields(Interface):
    """A colection of form fields (IFormField objects)
    """

    def __len__():
        """Get the number of fields
        """

    def __iter__():
        """Iterate over the form fields
        """

    def __getitem__(name):
        """Return the form field with the given name

        If the desired firld has a prefix, then the given name should
        be the prefix, a dot, and the unprefixed name.  Otherwise, the
        given name is just the field name.

        Raise a KeyError if a field can't be found for the given name.
        """

    def get(name, default=None):
        """Return the form field with the given name

        If the desired firld has a prefix, then the given name should
        be the prefix, a dot, and the unprefixed name.  Otherwise, the
        given name is just the field name.

        Return the default if a field can't be found for the given name.
        """

    def __add__(form_fields):
        """Add two form fields collections (IFormFields)

        Return a new IFormFields that is the concatination of the two
        IFormFields.
        """

    def select(*names):
        """Select fields with given names in order

        Return a new IFormFields that is a selection from the original
        IFormFields that has the named fields in the specified order.
        """

    def omit(*names):
        """Omit fields with given names
        """

SKIP_UNAUTHORIZED = 2
DISPLAY_UNWRITEABLE = 4

class IFormField(Interface):
    """Definition of a field to be included in a form

    This should not be confused with a schema field.
    """

    __name__ = schema.ASCII(
        constraint=reConstraint('[a-zA-Z][a-zA-Z0-9_]*',
                                "Must be an identifier"),
        title = u"Field name",
        description=u"""\
        This is the name, without any proefix, used for the field.
        It is usually the same as the name of the for field's schem field.
        """
        )

    field = Attribute(
        """Schema field that defines the data of the form field
        """
        )

    prefix = schema.ASCII(
        constraint=reConstraint('[a-zA-Z][a-zA-Z0-9_]*',
                                "Must be an identifier"),
        title=u"Prefix",
        description=u"""\
        Form-field prefix.  The form-field prefix is used to
        disambiguate fields with the same name (e.g. from different
        schema) within a collection of form fields.
        """,
        default="",
        )

    for_display = schema.Bool(
        title=u"Is the form field for display only?",
        description=u"""\
        If this attribute has a true value, then a display widget will be
        used for the field even if it is writable.
        """
        )

    for_input = schema.Bool(
        title=u"Is the form field for input?",
        description=u"""\
        If this attribute has a true value, then an input widget will be
        used for the field even if it is readonly.
        """
        )

    custom_widget = Attribute(
        """Factory to use for widget construction.

        If not set, normal view lookup will be used.
        """
        )

    render_context = schema.Choice(
        title=u"Should the rendered value come from the form context?",
        description=u"""\

        If this attribute has a true value, and there is no other
        source of rendered data, then use data from the form context
        to set the rendered value for the widget.  This attribute is
        ignored if:

        - There is user input and user input is not being ignored, or

        - Data for the value is passed to setUpWidgets.

        If the value is true, then it is evaluated as a collection of bit
        flags with the flags:

        DISPLAY_UNWRITEABLE
            If the field isn't writable, then use a display widget

            TODO untested


        SKIP_UNAUTHORIZED
            If the user is not priviledges to perform the requested
            operation, then omit a widget.

            TODO unimplemented

        """,
        vocabulary=schema.vocabulary.SimpleVocabulary.fromValues((
            False, True,
            DISPLAY_UNWRITEABLE,
            SKIP_UNAUTHORIZED,
            DISPLAY_UNWRITEABLE | SKIP_UNAUTHORIZED,
            )),
        default=False,
        missing_value=False,
        )

    get_rendered = Attribute(
        """Object to call to get a rendered value

        This attribute may be set to a callable object or to
        a form method name to call to get a value to be rendered in a
        widget.

        This attribute is ignored if:

        - There is user input and user input is not being ignored, or

        - Data for the value is passed to setUpWidgets.

        """
        )

class IWidgets(Interface):
    """A widget collection

    IWidgets provide ordered collections of widgets that also support:

    - Name-based lookup

    - Keeping track of whether a widget is being used for input or
      display

    """

    def __iter__():
        """Return an interator in the widgets, in order
        """

    def __getitem__(name):
        """Get the widget with the given name

        Widgets are computed from form fields (IFormField).  If the
        form field used to create a widget has a prefix, then that
        should be reflected in the name passed.
        """

    def __iter_input_and_widget__():
        """Return an iterator of flag/widget pairs

        The flags indicate whether the corresponding widgets are used
        for input.  This is necessary because there is currently no
        way to introspect a widget to determine whether it is being
        used for input.
        """

    def __add__(widgets):
        """Add two widgets collections

        The result has the widgets in the first collection followed by
        the widgets in the second collection.

        Widgets should have different names in the two collections.
        The bahavior is undefined if the names overlap.

        """

class IForm(Interface):
    """Base type for forms

    This exists primarily to provide something for which to register
    form-related conponents.

    """

class ISubPageForm(IForm, ISubPage):
    """A component that displays a part of a page.

    The rendered output must not have a form tag.  It is the
    responsibility of the surrounding page to supply a form tag.

    """

class IPageForm(IForm, IBrowserPage):
    """A component that displays a form as a page.
    """

class IAction(ISubPage):
    """Form submit actions
    """

    label = schema.TextLine(title=u"Action label")

    name = schema.TextLine(title=u"Action name")
    __name__ = schema.TextLine(title=u"Action name with its prefix")

    data = schema.Dict(title=u"Application data")

    condition = Attribute(
        """Action condition

        This is a callable object that will be passed a form and an
        action and that returns a boolean to indicate whether the
        action is available.

        """)

    validator = Attribute(
        """Action validator

        This is a callable object that will be passed a form and an
        action and that returns a (possibly empty) list of widget
        input errors.

        """)

    def available():
        """Return a boolean indicating whether the action is available
        """

    def submitted():
        """Return a boolean indicating whether the action was submitted
        """

    def validate(data):
        """Validate inputs

        If an action was submitted and has a custom validator, then
        the validator result is returned. Otherwise, None is returned.

        Validated inputs, if any, are placed into the mapping object
        passed as an argument,
        """

    def success(data):
        """Handle sucessful submition

        This method is called when the action was submitted and the
        submitted data was valid.
        """

    def failure(data, errors):
        """Handle unsucessful submition

        This method is called when the action was submitted and the
        submitted data was not valid.
        """

    def __get__(form, form_class=None):
        """Bind an action to a form

        Note that the other methods defined in this interface are
        valid only after the action has been bound to a form.
        """

class IActions(Interface):
    """An action collection

    IActions provide ordered collections of actions that also support
    name-based lookup.

    """

    def __iter__():
        """Return an interator in the actions, in order
        """

    def __getitem__(name):
        """Get the action with the given name

        Actions are computed from form fields (IFormField).  If the
        form field used to create an action has a prefix, then that
        should be reflected in the name passed.
        """

    def __add__(actions):
        """Add two actions collections

        The result has the actions in the first collection followed by
        the actions in the second collection.

        Actions should have different names in the two collections.
        The bahavior is undefined if the names overlap.

        """

class IBoundAction(IAction):
    """An action that has been bound to a form
    """

    form = Attribute("The form to which the action is bound")


class IAddFormCustomization(IFormBaseCustomization):
    """Form responsible for adding an object.
    """

    def create(data):
        """Create and return an object to be added to the context.

        The data argument is a dictionary with values supplied by the
        form.

        If any user errors occur, they should be collected into a list
        and raised as a `WidgetsError`.

        """

    def add(object):
        """Add an object to the context.  Returns the added object.
        """

    def createAndAdd(data):
        """Create and return an object that has been added to the context.

        The data argument is a dictionary with values supplied by the
        form.

        If any user errors occur, they should be collected into a list
        and raised as a `WidgetsError`.

        This is normally expected to simply call the create() and
        add() methods.

        """

    def nextURL():
        """Return the URL to be displayed after the add operation.

        This can be relative to the view's context.

        The default implementation returns `self.context.nextURL()`,
        i.e. it delegates to the `IAdding` view.

        """