예제 #1
0
def login(data):
    try:
        return current_app.fauna_client.query(
            q.let(
                {
                    'response':
                    q.login(
                        q.match(q.index('unique_account_username_type'),
                                [data.get('username'), 'EMAIL']),
                        {'password': data.get('password')}),
                    'user':
                    q.select_with_default(
                        ['data', 'user'],
                        q.get(q.select(['instance'], q.var('response'))), None)
                }, {
                    'data': {
                        'token':
                        q.select('secret', q.var('response')),
                        'user':
                        q.if_(
                            q.is_ref(q.var('user')),
                            q.select(['data', 'alias'], q.get(q.var('user'))),
                            None)
                    }
                }))
    except Exception as e:
        print(e)
예제 #2
0
 def test_select(self):
   obj = {"a": {"b": 1}}
   self.assertEqual(self._q(query.select("a", obj)), {"b": 1})
   self.assertEqual(self._q(query.select(["a", "b"], obj)), 1)
   self.assertEqual(self._q(query.select(["a", "c"], obj, 'default')), 'default')
   self.assertIsNone(self._q(query.select_with_default("c", obj, None)))
   self.assertRaises(NotFound, lambda: self._q(query.select("c", obj)))
예제 #3
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)
 def test_select_with_default(self):
     json = (
         '{"default":"no food","from":{"object":{"favorites":{"object":{"foods":["steak"]}}}},'
         '"select":["favorites","foods",0]}')
     self.assertJson(
         query.select_with_default(["favorites", "foods", 0],
                                   {"favorites": {
                                       "foods": ["steak"]
                                   }}, "no food"), json)