Пример #1
0
    def create(cls, user, name):
        id_ = str(user.id) + standard_name(name)

        self = yield super().create(id_)
        yield self.store_dict(
            {'name': name, 'user_id': user.id})
        return self
Пример #2
0
    def create(cls, user, data):
        """Create a new slideshow document in the database.

        :param src.bd.User user:
            The user that will own the slideshow.

        :param dict data:
            The data that should be used to create the new
            slideshow object.

            ``data`` should have the following format:

            .. code-block:: python

                {
                    'name': 'Slideshow Name',
                    'slides': [
                        {
                            'url': 'my.slides.com#slide1',
                            'question': {
                                'type': 'alternatives',
                                'wording': 'A question?',
                                'answers': [
                                    'Answer 1.',
                                    'Answer 2.',
                                    ...
                                ]
                            }
                        },
                        ...
                    ]
                }

            Where ``'Slideshow Name'`` should be the name of
            the slideshow, ``'my.slides.com#slide1'`` can be
            any valid URL, ``'A question?'`` should be an
            associated question to be asked using this slide
            and ``'Answer #.'`` are the different answer
            alternatives for the question.

            Currently the only supported question type is
            ``'alternatives'``.

            A presentation can have any number of slides and
            a question can have any number of answers.

            The object associated to the key ``'question'``
            can be ``None`` (``'question': None``).

        :return:
            A new slideshow object.
        :rtype: :class:`Slide`

        :raises AttributeError:
            If ``user`` has no attribute ``id``.

        :raises NotDictError:
            If ``data`` is not a dictionary.

        :raises NotStringError:
            If ``user.id`` or ``data['name']`` is not a
            string.

        :raises KeyError:
            If ``data`` has no key ``name``.

        :raises pymongo.errors.OperationFailure:
            If an database error occurred during creation.

        :raises pymongo.errors.DuplicateKeyError:
            If an object with the same id alredy exists in
            the database.
            :class:`~pymongo.errors.DuplicateKeyError` is a
            subclass of
            :class:`~pymongo.errors.OperationFailure`.

        :raises ConditionNotMetError:
            If the just created slide document no longer
            exists in the database. This should never
            happen!
        """
        try:
            id_ = user.id + standard_name(data['name'])
            self = yield super().create(id_)
            data['user_id'] = user.id
            yield self.store_dict(data)
            return self

        except AttributeError as e:
            if not hasattr(user, 'id'):
                ae = AttributeError(
                    "'user' has no attribute 'id'")
                raise ae from e

            else:
                raise

        except TypeError as te:
            if not isinstance(data, dict):
                raise NotDictError('data') from te

            elif not isinstance(data['name'], str):
                raise NotStringError("data['name']") from te

            elif not isinstance(user.id, str):
                raise NotStringError('user.id') from te

            else:
                raise

        except KeyError as e:
            if 'name' not in data:
                ke = KeyError("'data' has no key 'name'")
                raise ke from e

            else:
                raise

        except:
            raise
Пример #3
0
    def create(cls, user, data):
        """Create a new slideshow document in the database.

        :param src.bd.User user:
            The user that will own the slideshow.

        :param dict data:
            The data that should be used to create the new
            slideshow object.

            ``data`` should have the following format:

            .. code-block:: python

                {
                    'name': 'Slideshow Name',
                    'slides': [
                        {
                            'url': 'my.slides.com#slide1',
                            'question': {
                                'type': 'alternatives',
                                'wording': 'A question?',
                                'answers': [
                                    'Answer 1.',
                                    'Answer 2.',
                                    ...
                                ]
                            }
                        },
                        ...
                    ]
                }

            Where ``'Slideshow Name'`` should be the name of
            the slideshow, ``'my.slides.com#slide1'`` can be
            any valid URL, ``'A question?'`` should be an
            associated question to be asked using this slide
            and ``'Answer #.'`` are the different answer
            alternatives for the question.

            Currently the only supported question type is
            ``'alternatives'``.

            A presentation can have any number of slides and
            a question can have any number of answers.

            The object associated to the key ``'question'``
            can be ``None`` (``'question': None``).

        :return:
            A new slideshow object.
        :rtype: :class:`Slide`

        :raises AttributeError:
            If ``user`` has no attribute ``id``.

        :raises NotDictError:
            If ``data`` is not a dictionary.

        :raises NotStringError:
            If ``user.id`` or ``data['name']`` is not a
            string.

        :raises KeyError:
            If ``data`` has no key ``name``.

        :raises pymongo.errors.OperationFailure:
            If an database error occurred during creation.

        :raises pymongo.errors.DuplicateKeyError:
            If an object with the same id alredy exists in
            the database.
            :class:`~pymongo.errors.DuplicateKeyError` is a
            subclass of
            :class:`~pymongo.errors.OperationFailure`.

        :raises ConditionNotMetError:
            If the just created slide document no longer
            exists in the database. This should never
            happen!
        """
        try:
            id_ = user.id + standard_name(data['name'])
            self = yield super().create(id_)
            data['user_id'] = user.id
            yield self.store_dict(data)
            return self

        except AttributeError as e:
            if not hasattr(user, 'id'):
                ae = AttributeError("'user' has no attribute 'id'")
                raise ae from e

            else:
                raise

        except TypeError as te:
            if not isinstance(data, dict):
                raise NotDictError('data') from te

            elif not isinstance(data['name'], str):
                raise NotStringError("data['name']") from te

            elif not isinstance(user.id, str):
                raise NotStringError('user.id') from te

            else:
                raise

        except KeyError as e:
            if 'name' not in data:
                ke = KeyError("'data' has no key 'name'")
                raise ke from e

            else:
                raise

        except:
            raise
Пример #4
0
    def create(cls, user, name):
        id_ = str(user.id) + standard_name(name)

        self = yield super().create(id_)
        yield self.store_dict({'name': name, 'user_id': user.id})
        return self