Ejemplo n.º 1
0
def create_user(data):
    try:
        current_identity = get_current_identity()
        email_hash = md5(
            current_identity['data']['username'].encode('utf-8')).hexdigest()
        return current_app.fauna_client.query(
            q.if_(
                q.is_ref(
                    q.select_with_default(['data', 'user'],
                                          q.get(q.current_identity()), None)),
                q.abort('exists'),
                q.let(
                    {
                        'userMetaRef': q.new_id(),
                        'userRef': q.new_id()
                    },
                    q.do(
                        q.create(
                            q.ref(q.collection('user_metas'),
                                  q.var('userMetaRef')),
                            {
                                'data': {
                                    'name':
                                    data.get('name'),
                                    'email':
                                    q.select(['data', 'username'],
                                             q.get(q.current_identity())),
                                    'dob':
                                    parser.parse(data.get('dob')).date()
                                }
                            }),
                        q.create(
                            q.ref(q.collection('users'),
                                  q.var('userRef')),
                            {
                                'data': {
                                    'alias':
                                    data.get('alias'),
                                    'avatar':
                                    f'https://www.gravatar.com/avatar/{email_hash}',
                                    'public':
                                    False,
                                    'meta':
                                    q.ref(q.collection('user_metas'),
                                          q.var('userMetaRef')),
                                }
                            }),
                        q.update(
                            q.current_identity(), {
                                'data': {
                                    'user':
                                    q.ref(q.collection('users'),
                                          q.var('userRef'))
                                }
                            }), q.call('current_user', [])))))
    except Exception as e:
        if str(e) == 'exists':
            abort(409, 'User for current identity already exists.')
        print(e)
Ejemplo n.º 2
0
def create_product(secret, name, price, quantity, categories):
    client = FaunaClient(secret=secret)
    return client.query(
        q.if_(
            q.call('check_if_categories_exists', categories),
            q.create(q.collection('products'), {"data": { 
                "name": name,
                "price": price,
                "quantity":quantity,
                "createdAt": q.now(),
                "categories": list(map(
                    lambda category: q.ref(q.collection("categories"), category),
                    categories
                ))
             }}),
            q.abort('Categories not found'),
        )
    )
Ejemplo n.º 3
0
def update_product(secret, ref, data):
    client = FaunaClient(secret=secret)
    if(data.get("categories") == None):
        return client.query(q.update(
            q.ref(q.collection('products'), ref),
            { "data": data }
        ))
    
    return client.query(
        q.if_(
            q.call('check_if_categories_exists', data.get("categories")),
            q.update(
                q.ref(q.collection('products'), ref),
                { "data": {
                    **data,
                    "categories": list(map(
                        lambda category: q.ref(q.collection("categories"), category),
                        data.get("categories")
                    ))
                } }
            ),
            q.abort('Categories not found'),
        )
    ) 
 def test_abort(self):
     self.assertJson(query.abort("aborting"), '{"abort":"aborting"}')
Ejemplo n.º 5
0
 def test_abort(self):
     self._assert_bad_query(query.abort("aborting"))