Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] New filters #33

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions gasoline/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Schemas for gasoline app """
# Django
import django_filters
from django.db.models import Avg

# Models
from gasoline.models import Price, Station
Expand All @@ -11,6 +12,7 @@
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

#-------------Main types-----------
class StationType(DjangoObjectType):
"""Type for Station Model"""
class Meta:
Expand Down Expand Up @@ -40,7 +42,7 @@ class Meta:
'station',
'gas_type',
'price',
'date',
'date',
)

#-----------NODE-QUERIES----------
Expand All @@ -62,7 +64,6 @@ class Meta:
}
interfaces = (relay.Node, Node)


class StationConnection(Connection):
"""Station Connection"""
class Meta:
Expand All @@ -81,10 +82,11 @@ class Meta:
'gas_type': ['exact', 'icontains','istartswith'],
'price':['exact', 'icontains','istartswith'],
'date':['exact'],
'station__state':['exact','icontains','istartswith'],
'station__town':['exact','icontains','istartswith'],
}
interfaces = (relay.Node,Node)


class PriceConnection(Connection):
"""Price Connection"""
class Meta:
Expand All @@ -97,14 +99,18 @@ class Query(graphene.ObjectType):
"""Gasoline Query class"""
all_stations = ConnectionField(StationConnection)
all_prices = ConnectionField(PriceConnection)
avg_price_by_state = graphene.Float(PriceType,state=graphene.String(required=True))

def resolve_all_stations(root, info, **kwargs):
def resolve_all_stations(root, info, **kwargs):
""" Return all the stations """
return Station.objects.filter(is_active=True)

def resolve_all_prices(root, info, **kwargs):
""" Return all prices """
return Price.objects.filter(station__is_active=True)

def resolve_avg_price_by_state(root,info,state):
return Price.objects.filter(station__state=state).aggregate(Avg('price'))

# Node Query class
station = relay.Node.Field(StationNode)
Expand Down