Exemple #1
0
def add_data():
    """Fill db with some data objects"""
    for category, themes in data.items():
        category_obj = Category(name=category)
        category_obj.save()
        for theme, attributes in themes.items():
            level_obj = Level.objects.filter(code=attributes['level']).first()
            theme_obj = Theme(name=theme,
                              level=level_obj,
                              category=category_obj)
            theme_obj.save()
            for word in attributes['words']:
                word_obj = Word(name=word[0],
                                translation=word[1],
                                theme=theme_obj)
                word_obj.save()
Exemple #2
0
 async def to_json(self, result):
     json = await super(MaterialCategorySerializer, self).to_json(result)
     async with self.context['request'].app['db'].acquire() as conn:
         query = Category.select().where(Category.c.id == json['category'])
         result = await conn.execute(query)
         serializer = CategorySerializer()
         category = await serializer.to_json(result)
         json = category
         return json
async def search_categories(request):
    async with request.app['db'].acquire() as conn:
        text = request.query.get('text')
        if text is None:
            raise ValidationError(dict(text='This query parameters is required'))

        query = Category.select().where(Category.c.name.like('%{}%'.format(text)))
        result = await conn.execute(query)

        serializer = CategorySerializer(many=True)
        data = await serializer.to_json(result)
        return web.json_response(data)
 def test_should_call_overriden_save(self):
     category = Category(name='Python', description='We got your whitespace.')
     category.save()
     assert_equal('python', category.slug)
 def test_should_call_save(self):
     category = Category(name='Python', slug='python', description='We got your whitespace.')
     category.save()
     c = Category.objects.get(name='Python')
     assert_equal(c.name, category.name)