Exemplo n.º 1
0
def followees(request):
    user = request.user
    if user is None:
        raise HTTPForbidden
    if request.method == 'GET':
        followees = FollowRecordService.by_follower_id(user.id, request)
        return list_to_json(followees, 'followees')
    if request.method == 'POST':
        data = json.loads(json.dumps(request.json))
        if 'followee_id' in data:
            follow = Follow(follower_id=user.id, followee_id=int(data['followee_id']))
            
            with transaction.manager:
                zope.sqlalchemy.register(request.dbsession, transaction_manager=transaction.manager)
                request.dbsession.add(follow)
            
            responseCreated = HTTPCreated()
            responseCreated.body = "followed"
            return responseCreated
    
    if request.method == 'DELETE':
        data = json.loads(json.dumps(request.json))
        if 'followee_id' in data:
            with transaction.manager:
                zope.sqlalchemy.register(request.dbsession, transaction_manager=transaction.manager)
                request.dbsession.query(Follow).filter_by(followee_id=int(data['followee_id'])).filter_by(follower_id=user.id).delete()
                transaction.commit()
            responseDeleted = HTTPOk()
            responseDeleted.body = "unfollowed"
            return responseDeleted
Exemplo n.º 2
0
def categories(request):
    user = request.user
    if user is None:
        raise HTTPForbidden
    if request.method == 'GET':
        data = request.params
        if 'creator_id' in data:
            categories = CategoryRecordService.by_creator_id(int(data['creator_id']), request)
            return list_to_json(categories, 'categories')
        categories = CategoryRecordService.by_creator_id(user.id, request)
        json_obj = list_to_json(categories, 'categories')
        return json_obj

    if request.method == 'POST':
        data = json.loads(json.dumps(request.json))
        category_name = ''
        category_type_id = 0
        if 'category_name' in data:
            category_name = data['category_name']
        if 'category_type_id' in data:
            category_type_id = data['category_type_id']
        if len(category_name) > 0 and category_type_id > 0:
            newcategory = Category(name=category_name, type_id=category_type_id, creator_id=user.id)
            with transaction.manager:
                zope.sqlalchemy.register(request.dbsession, transaction_manager=transaction.manager)
                request.dbsession.add(newcategory)
                transaction.commit()
            responseCreated = HTTPCreated()
            responseCreated.body = "category created"
            return responseCreated
Exemplo n.º 3
0
    def put(self):
        # we never accept id, it doesn't conform to jsonapi format
        data = self.request.json_body['data']

        klass = get_content_factory(self.request, data['type'])

        add_permission = klass.type_info.add_permission
        if not self.request.has_permission(add_permission, self.context):
            raise HTTPForbidden()

        schema_name = _schema_factory_name(type_name=data['type'])
        schema_factory = self.request.registry.getUtility(ISchemaFactory,
                                                          name=schema_name)
        schema = schema_factory(None, self.request)
        validated = schema.deserialize(data['attributes'])

        name = title_to_name(validated['title'], blacklist=self.context.keys())
        new_item = self.context[name] = klass(**validated)

        response = HTTPCreated()
        response.body = render('kotti_jsonp', new_item, self.request)
        return response
Exemplo n.º 4
0
def categoryquestions(request):
    user = request.user
    if user is None:
        raise HTTPForbidden
    if request.method == 'GET':
        data = request.params
        if 'category_id' in data:
            categoryquestions = CategoryQuestionRecordService.by_category_id(int(data['category_id']), request)
        elif 'question_id' in data:
            categoryquestions = CategoryQuestionRecordService.by_question_id(int(data['question_id']), request)
        return list_to_json(categoryquestions, 'categoryquestions')

    elif request.method == 'POST':
        data = json.loads(json.dumps(request.json))
        if 'category_id' and 'question_id' in data:
            categoryquestion = CategoryQuestion(category_id=data['category_id'], question_id=data['question_id'])
            with transaction.manager:
                zope.sqlalchemy.register(request.dbsession, transaction_manager=transaction.manager)
                request.dbsession.add(categoryquestion)
                transaction.commit()
            responseCreated = HTTPCreated()
            responseCreated.body = "categoryquestion created"
            return responseCreated
Exemplo n.º 5
0
def options(request):
    user = request.user
    if user is None:
        raise HTTPForbidden
    if request.method == 'GET':
        data = request.params
        if 'option_id' in data:
            option = OptionRecordService.by_id(int(data['option_id']), request)
            return json.dumps(option.as_dict())
        if 'question_id' in data:
            options = OptionRecordService.by_question_id(int(data['question_id']), request)
            return list_to_json(options, 'options')
        if 'correct_answer_question_id' in data:
            options = OptionRecordService.correct_answer_by_question_id(int(data['correct_answer_question_id']), request)
            return list_to_json(options, 'correctoptions')
        # Return all options if no parameter specified
        options = OptionRecordService.all(request)
        return list_to_json(options)

    if request.method == 'POST':
        data = json.loads(json.dumps(request.json))
        if 'options' and 'question_id' in data:
            question_id = data['question_id']
            optionsJsonArray = data['options']
            for optionJson in optionsJsonArray:
                option = Option(
                    option=optionJson['option'],
                    isCorrectAnswer=optionJson['isCorrectAnswer'],
                    question_id=question_id,
                    )
                with transaction.manager:
                    zope.sqlalchemy.register(request.dbsession, transaction_manager=transaction.manager)
                    request.dbsession.add(option)
                    transaction.commit()
            responseCreated = HTTPCreated()
            responseCreated.body = "options created"
            return responseCreated