Exemplo n.º 1
0
Arquivo: qa.py Projeto: todun/ksweb
    def post(self,
             title,
             workspace,
             question,
             tooltip,
             link,
             answer_type,
             precondition=None,
             answers=None,
             **kw):
        if not self._are_answers_valid(answer_type, answers):
            response.status_code = 412
            return dict(
                errors={'answers': _('Please add at least one more answer')})

        user = request.identity['user']

        qa = Qa(_owner=user._id,
                _workspace=ObjectId(workspace),
                _parent_precondition=to_object_id(precondition),
                title=title,
                question=question,
                tooltip=tooltip,
                link=link,
                type=answer_type,
                answers=answers,
                auto_generated=False,
                public=True,
                visible=True)
        DBSession.flush(qa)
        qa.generate_output_from()

        return dict(errors=None, _id=ObjectId(qa._id))
Exemplo n.º 2
0
    def _original_edit(self):
        params = session.get('entity')
        params['_workspace'] = to_object_id(params.get('_workspace'))
        params['_precondition'] = to_object_id(params.get('_precondition'))
        entity = entity_from_id(params['_id'])
        if (type(entity) is model.Precondition):
            if entity.is_advanced:
                params['condition'] = [to_object_id(__) if __ not in model.Precondition.PRECONDITION_OPERATOR else __ for __ in params['condition'] ]
            else:
                params['condition'][0] = ObjectId(params['condition'][0])

        old_hash = entity['hash']
        params.pop('entity', None)
        for k, v in params.items():
            setattr(entity, k, v)
        DBSession.flush(entity)
        entity.update_dependencies(old_hash)
        return entity
Exemplo n.º 3
0
Arquivo: qa.py Projeto: todun/ksweb
    def put(self,
            _id,
            title,
            workspace,
            question,
            tooltip,
            link,
            answer_type,
            precondition=None,
            answers=None,
            **kw):
        if not self._are_answers_valid(answer_type, answers):
            response.status_code = 412
            return dict(
                errors={'answers': _('Please add at least one more answer')})

        check = self.get_related_entities(_id)

        if check.get("entities"):
            entity = dict(_id=_id,
                          _workspace=workspace,
                          title=title,
                          entity='qa',
                          question=question,
                          tooltip=tooltip,
                          link=link,
                          auto_generated=False,
                          type=answer_type,
                          _parent_precondition=precondition,
                          answers=answers)

            session.data_serializer = 'pickle'
            session[
                'entity'] = entity  # overwrite always same key for avoiding conflicts
            session.save()

            return dict(redirect_url=tg.url('/resolve',
                                            params=dict(workspace=workspace)))

        qa = Qa.upsert({'_id': ObjectId(_id)},
                       dict(_workspace=ObjectId(workspace),
                            _parent_precondition=to_object_id(precondition),
                            title=title,
                            question=question,
                            auto_generated=False,
                            tooltip=tooltip,
                            link=link,
                            type=answer_type,
                            answers=answers))
        DBSession.flush(qa)
        qa.generate_output_from()
        return dict(errors=None)
Exemplo n.º 4
0
    def _original_edit(self):

        # fetch params from session
        params = session.get('entity')

        # transform to ObjectId here because ObjectId is not JSON serializable
        params['_category'] = to_object_id(params.get('_category'))
        params['_precondition'] = to_object_id(params.get('_precondition'))

        # retrieve original object
        entity = self._get_entity(params['entity'], params['_id'])

        # popping non-related values
        params.pop('entity', None)

        # true edit
        for k, v in params.items():
            setattr(entity, k, v)

        # TODO: update..
        # self._find_and_modify(kw)
        return entity
Exemplo n.º 5
0
Arquivo: qa.py Projeto: fossabot/ksweb
    def put(self,
            _id,
            title,
            category,
            question,
            tooltip,
            link,
            answer_type,
            precondition=None,
            answers=None,
            **kw):
        if not self._are_answers_valid(answer_type, answers):
            response.status_code = 412
            return dict(
                errors={'answers': _('Please add at least one more answer')})

        check = self.get_related_entities(_id)

        if check.get("entities"):
            entity = dict(_id=_id,
                          _category=category,
                          title=title,
                          entity='qa',
                          question=question,
                          tooltip=tooltip,
                          link=link,
                          type=answer_type,
                          _parent_precondition=precondition,
                          answers=answers)

            session.data_serializer = 'pickle'
            session[
                'entity'] = entity  # overwrite always same key for avoiding conflicts
            session.save()

            return dict(redirect_url=tg.url('/resolve',
                                            params=dict(workspace=category)))

        qa = model.Qa.query.get(_id=ObjectId(_id))
        qa._category = ObjectId(category)
        qa._parent_precondition = to_object_id(precondition)
        qa.title = title
        qa.question = question
        qa.tooltip = tooltip
        qa.question = question
        qa.link = link
        qa.type = answer_type
        qa.answers = answers
        self._autofill_qa_filters(qa)

        return dict(errors=None)
Exemplo n.º 6
0
 def test_to_object_id_none(self):
     o = to_object_id(None)
     assert o is None, o
Exemplo n.º 7
0
 def test_to_object_id(self):
     o = to_object_id("507f1f77bcf86cd799439011")
     assert isinstance(o, ObjectId), type(o)