Пример #1
0
class WikiPageForm(Form):
    title = StringField(label=_l(u"Title"),
                        filters=(strip, ),
                        validators=[required()])
    body_src = TextAreaField(
        label=_l("Body"),
        filters=(strip, clean_up),
        validators=[required()],
        widget=TextArea(rows=10, resizeable='vertical'),
    )

    message = StringField(label=_l("Commit message"))
    page_id = HiddenField(filters=(int_or_none, ), validators=[flaghidden()])
    last_revision_id = HiddenField(filters=(int_or_none, ),
                                   validators=[flaghidden()])

    def validate_title(self, field):
        title = field.data
        if title != field.object_data and page_exists(title):
            raise ValidationError(
                _(u"A page with this name already exists. Please use another name."
                  ))

    def validate_last_revision_id(self, field):
        val = field.data
        current = field.object_data

        if val is None or current is None:
            return

        if val != current:
            raise ValidationError(_(u'this page has been edited since'))
Пример #2
0
class CommentForm(Form):

    body = TextAreaField(
        label=_l(u'Comment'),
        validators=[required()],
        filters=(strip, ),
        widget=TextArea(rows=5, resizeable='vertical'),
    )

    class Meta:
        model = Comment
        include_primary_keys = True
        assign_required = False  # for 'id': allow None, for new records
Пример #3
0
class CommunityForm(Form):
    name = StringField(label=_l(u"Name"), validators=[required()])
    description = TextAreaField(label=_l(u"Description"),
                                validators=[required(),
                                            length(max=500)],
                                widget=TextArea(resizeable="vertical"))

    linked_group = Select2Field(
        label=_l(u'Linked to group'),
        description=_l(
            u'Manages a group of users through this community members.'),
        choices=_group_choices)

    image = FileField(label=_l('Image'),
                      widget=ImageInput(width=65, height=65),
                      validators=[optional()])

    type = Select2Field(label=_(u"Type"),
                        validators=[required()],
                        filters=(strip, ),
                        choices=[(_l(u'informative'), 'informative'),
                                 (_l(u'participative'), 'participative')])

    has_documents = BooleanField(label=_l(u"Has documents"),
                                 widget=BooleanWidget(on_off_mode=True))
    has_wiki = BooleanField(label=_l(u"Has a wiki"),
                            widget=BooleanWidget(on_off_mode=True))
    has_forum = BooleanField(label=_l(u"Has a forum"),
                             widget=BooleanWidget(on_off_mode=True))

    def validate_name(self, field):
        name = field.data = field.data.strip()

        if name and field.object_data:
            # form is bound to an existing object, name is not empty
            if name != field.object_data:
                # name changed: check for duplicates
                if Community.query.filter(Community.name == name).count() > 0:
                    raise ValidationError(
                        _(u"A community with this name already exists"))

    def validate_description(self, field):
        field.data = field.data.strip()

    # FIXME: code duplicated from the user edit form (UserProfileForm).
    # Needs to be refactored.
    def validate_image(self, field):
        data = request.form.get('image')
        if not data:
            return

        data = field.data
        filename = data.filename
        valid = any(map(filename.lower().endswith, ('.png', '.jpg', '.jpeg')))

        if not valid:
            raise ValidationError(
                _(u'Only PNG or JPG image files are accepted'))

        img_type = imghdr.what('ignored', data.read())

        if img_type not in ('png', 'jpeg'):
            raise ValidationError(
                _(u'Only PNG or JPG image files are accepted'))

        data.seek(0)
        try:
            # check this is actually an image file
            im = PIL.Image.open(data)
            im.load()
        except:
            raise ValidationError(_(u'Could not decode image file'))

        data.seek(0)
        field.data = data