Fixed Flake8

This commit is contained in:
Markus Padourek 2015-11-26 09:39:49 +00:00
parent 2feb62f597
commit 21e9f7a977

View File

@ -64,10 +64,12 @@ USER_DATA = [
}, },
] ]
def fetch_user_from_database(user_id): def fetch_user_from_database(user_id):
user, = [user for user in USER_DATA if user['user_id'] == user_id] user, = [user for user in USER_DATA if user['user_id'] == user_id]
return user return user
def fetch_users_from_database(args): def fetch_users_from_database(args):
full_name = args.get('fullName', None) full_name = args.get('fullName', None)
if full_name: if full_name:
@ -78,8 +80,9 @@ def fetch_users_from_database(args):
if sort_direction and sort_direction == 'DESC': if sort_direction and sort_direction == 'DESC':
reversed = True reversed = True
filtered_users = [user for user in USER_DATA if full_name is None or full_name in filtered_users = [user for user in USER_DATA if full_name is None or
user['first_name'].upper() + ' ' + user['last_name'].upper()] full_name in user['first_name'].upper() + ' ' +
user['last_name'].upper()]
if sort_key and sort_direction: if sort_key and sort_direction:
filtered_users.sort(lambda item: item[sort_key], reverse=reversed) filtered_users.sort(lambda item: item[sort_key], reverse=reversed)
@ -87,30 +90,37 @@ def fetch_users_from_database(args):
schema = graphene.Schema() schema = graphene.Schema()
class User(relay.Node): class User(relay.Node):
"""A User fetched from the database""" """A User fetched from the database"""
user_id = graphene.ID() user_id = graphene.ID()
first_name = graphene.String() first_name = graphene.String()
last_name = graphene.String() last_name = graphene.String()
full_name = graphene.String(description='A field created by setting the first and last name.') full_name = graphene.String(
description='A field created by setting the first and last name.')
email_address = graphene.String() email_address = graphene.String()
age = graphene.Int() age = graphene.Int()
@classmethod @classmethod
def get_node(cls, user_id, info): def get_node(cls, user_id, info):
user_dict = fetch_user_from_database(user_id) user_dict = fetch_user_from_database(user_id)
# user_dict will contain the fields, user_id, first_name, last_name, email_address and age # user_dict will contain the fields, user_id, first_name, last_name,
user_dict['id'] = user_dict['user_id'] # will be used to set the global ID used by relay # email_address and age
# will be used to set the global ID used by relay
user_dict['id'] = user_dict['user_id']
return User(**user_dict) return User(**user_dict)
def resolve_full_name(self, *args): def resolve_full_name(self, *args):
return ' '.join([self.first_name, self.last_name]) return ' '.join([self.first_name, self.last_name])
def _user_container(user): def _user_container(user):
# user_dict will contain the fields, user_id, first_name, last_name, email_address and age # user_dict will contain the fields, user_id, first_name, last_name,
# email_address and age
user['id'] = user['user_id'] user['id'] = user['user_id']
return User(**user) return User(**user)
# This will be our root query # This will be our root query
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
users = graphene.List(User, users = graphene.List(User,
@ -138,7 +148,9 @@ Then, we can start querying our schema:
```python ```python
result = schema.execute('query { users { fullName } }') result = schema.execute('query { users { fullName } }')
# result.data should be {'users': [{fullName: 'Peter Cabbage'}, {fullName: 'Lukas Chart'}, {fullName: 'Marie Cauliflower'}]} # result.data should be
# {'users': [{fullName: 'Peter Cabbage'}, {fullName: 'Lukas Chart'},
# {fullName: 'Marie Cauliflower'}]}
users = result.data['users'] users = result.data['users']
print(users) print(users)