Esempio n. 1
0
    def submit_async(self, **kwargs):
        """Ajax form validation and/or submission.

        This is the save handler for :class:`~mediacore.forms.media.UploadForm`.

        When ajax is enabled this action is called for each field as the user
        fills them in. Although the entire form is validated, the JS only
        provides the value of one field at a time,

        :param validate: A JSON list of field names to check for validation
        :parma \*\*kwargs: One or more form field values.
        :rtype: JSON dict
        :returns:
            :When validating one or more fields:

            valid
                bool
            err
                A dict of error messages keyed by the field names

            :When saving an upload:

            success
                bool
            redirect
                If valid, the redirect url for the upload successful page.

        """
        if "validate" in kwargs:
            # we're just validating the fields. no need to worry.
            fields = json.loads(kwargs["validate"])
            err = {}
            for field in fields:
                if field in tmpl_context.form_errors:
                    err[field] = tmpl_context.form_errors[field]

            data = dict(valid=len(err) == 0, err=err)
        else:
            # We're actually supposed to save the fields. Let's do it.
            if len(tmpl_context.form_errors) != 0:
                # if the form wasn't valid, return failure
                tmpl_context.form_errors["success"] = False
                data = tmpl_context.form_errors
            else:
                # else actually save it!
                kwargs.setdefault("name")

                media_obj = self.save_media_obj(
                    kwargs["name"],
                    kwargs["email"],
                    kwargs["title"],
                    kwargs["description"],
                    None,
                    kwargs["file"],
                    kwargs["url"],
                )
                email.send_media_notification(media_obj)
                data = dict(success=True, redirect=url_for(action="success"))

        return data
Esempio n. 2
0
    def submit_async(self, **kwargs):
        """Ajax form validation and/or submission.

        This is the save handler for :class:`~mediacore.forms.media.UploadForm`.

        When ajax is enabled this action is called for each field as the user
        fills them in. Although the entire form is validated, the JS only
        provides the value of one field at a time,

        :param validate: A JSON list of field names to check for validation
        :parma \*\*kwargs: One or more form field values.
        :rtype: JSON dict
        :returns:
            :When validating one or more fields:

            valid
                bool
            err
                A dict of error messages keyed by the field names

            :When saving an upload:

            success
                bool
            redirect
                If valid, the redirect url for the upload successful page.

        """
        if 'validate' in kwargs:
            # we're just validating the fields. no need to worry.
            fields = json.loads(kwargs['validate'])
            err = {}
            for field in fields:
                if field in tmpl_context.form_errors:
                    err[field] = tmpl_context.form_errors[field]

            data = dict(valid=len(err) == 0, err=err)
        else:
            # We're actually supposed to save the fields. Let's do it.
            if len(tmpl_context.form_errors) != 0:
                # if the form wasn't valid, return failure
                tmpl_context.form_errors['success'] = False
                data = tmpl_context.form_errors
            else:
                # else actually save it!
                kwargs.setdefault('name')

                media_obj = self.save_media_obj(
                    kwargs['name'],
                    kwargs['email'],
                    kwargs['title'],
                    kwargs['description'],
                    None,
                    kwargs['file'],
                    kwargs['url'],
                )
                email.send_media_notification(media_obj)
                data = dict(success=True, redirect=url_for(action='success'))

        return data
Esempio n. 3
0
    def submit(self, **kwargs):
        """
        """
        kwargs.setdefault("name")

        # Save the media_obj!
        media_obj = self.save_media_obj(
            kwargs["name"], kwargs["email"], kwargs["title"], kwargs["description"], None, kwargs["file"], kwargs["url"]
        )
        email.send_media_notification(media_obj)

        # Redirect to success page!
        redirect(action="success")
Esempio n. 4
0
    def submit(self, **kwargs):
        """
        """
        kwargs.setdefault('name')

        # Save the media_obj!
        media_obj = self.save_media_obj(
            kwargs['name'], kwargs['email'],
            kwargs['title'], kwargs['description'],
            None, kwargs['file'], kwargs['url'],
        )
        email.send_media_notification(media_obj)

        # Redirect to success page!
        redirect(action='success')
Esempio n. 5
0
    def submit(self, **kwargs):
        """
        """
        kwargs.setdefault('name')

        # Save the media_obj!
        media_obj = self.save_media_obj(
            kwargs['name'], kwargs['email'],
            kwargs['title'], kwargs['description'],
            None, kwargs['file'], kwargs['url'],
        )
        email.send_media_notification(media_obj)

        # Redirect to success page!
        redirect(action='success')
Esempio n. 6
0
    def upload_submit(self, **kwargs):
        """
        """
        kwargs.setdefault('name')
        kwargs.setdefault('tags')

        # Save the media_obj!
        media_obj = self._save_media_obj(
            kwargs['name'], kwargs['email'],
            kwargs['title'], kwargs['description'],
            kwargs['tags'], kwargs['file']
        )
        email.send_media_notification(media_obj)

        # Redirect to success page!
        redirect(action='upload_success')
Esempio n. 7
0
    def upload_submit_async(self, **kwargs):
        """Ajax form validation and/or submission.

        This is the save handler for :class:`~mediacore.forms.media.UploadForm`.

        When ajax is enabled this action is called for each field as the user
        fills them in. Although the entire form is validated, the JS only
        provides the value of one field at a time,

        :param validate: A JSON list of field names to check for validation
        :parma \*\*kwargs: One or more form field values.
        :rtype: JSON dict
        :returns:
            :When validating one or more fields:

            valid
                bool
            err
                A dict of error messages keyed by the field names

            :When saving an upload:

            success
                bool
            redirect
                If valid, the redirect url for the upload successful page.

        .. note::

            This method returns incorrect Content-Type headers: Content-Type
            is set to ``text/html`` even though the returned data is really
            of type ``application/json``.

            This is because this method is used from the flash based uploader;
            Swiff.Uploader (which we use) uses Flash's FileReference.upload()
            method, which doesn't allow overriding the HTTP headers.

            On windows, the default headers have an "Accept: text/\*" line.
            This means that it won't accept "application/json".

            TG honours that, and, when returning, will throw an error rather
            than respond with an unacceptable Content-Type.

            It would perhaps be more correct to set Content-Type to
            ``text/plain`` or ``text/x-json``, but there seems to be a bug in
            the current TG 2.0.3 + Pylons 0.9.7 stack w.r.t. overriding the
            Content-Type headers.
        """
        # TODO: look into the bug outlined in the note above.

        if 'validate' in kwargs:
            # we're just validating the fields. no need to worry.
            fields = json.loads(kwargs['validate'])
            err = {}
            for field in fields:
                if field in tmpl_context.form_errors:
                    err[field] = tmpl_context.form_errors[field]

            return json.dumps(dict(
                valid = len(err) == 0,
                err = err
            ))
        else:
            # We're actually supposed to save the fields. Let's do it.
            if len(tmpl_context.form_errors) != 0:
                # if the form wasn't valid, return failure
                return json.dumps(dict(
                    success = False
                ))

            # else actually save it!
            kwargs.setdefault('name')
            kwargs.setdefault('tags')

            media_obj = self._save_media_obj(
                kwargs['name'], kwargs['email'],
                kwargs['title'], kwargs['description'],
                kwargs['tags'], kwargs['file']
            )
            email.send_media_notification(media_obj)

            return json.dumps(dict(
                success = True,
                redirect = url_for(action='upload_success')
            ))