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

add dyes to the graphql api #198

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion backend/fpbase/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ def send_email(self):


class CustomSignupForm(SignupForm):
captcha = ReCaptchaField(label="", widget=ReCaptchaV3(attrs={"required_score": 0.85}))
# only if not in debug mode
if not settings.DEBUG:
captcha = ReCaptchaField(label="", widget=ReCaptchaV3(attrs={"required_score": 0.85}))
field_order = ["username", "email", "password1", "password2", "captcha"]
7 changes: 5 additions & 2 deletions backend/proteins/models/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def dye_slugs(self):
self.get_queryset().filter(category=self.DYE).values_list("owner_dye__slug", "owner_dye__name").distinct()
)

def sluglist(self):
def sluglist(self, filters: dict | None = None):
"""probably using this one going forward for spectra page"""

owners = ["state", "dye", "filter", "light", "camera"]
Expand All @@ -107,7 +107,10 @@ def sluglist(self):
for suffix in ["slug", "id", "name"]:
for owner in owners:
vals.append(f"owner_{owner}__{suffix}")
Q = self.get_queryset().values(*vals)
Q = self.get_queryset()
if filters:
Q = Q.filter(**filters)
Q = Q.values(*vals)

out = []
for v in Q:
Expand Down
34 changes: 31 additions & 3 deletions backend/proteins/schema/query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import graphene
from django.core.cache import cache
from django.utils.text import slugify
from graphene_django.filter import DjangoFilterConnectionField
from graphql import FieldNode, GraphQLError, GraphQLResolveInfo

Expand Down Expand Up @@ -91,14 +92,25 @@ def resolve_protein(self, info, **kwargs):
return None

# spectra = graphene.List(Spectrum)
spectra = graphene.List(types.SpectrumInfo)
spectra = graphene.List(types.SpectrumInfo, subtype=graphene.String(), category=graphene.String())
spectrum = graphene.Field(types.Spectrum, id=graphene.Int())

def resolve_spectra(self, info, **kwargs):
requested_fields = get_requested_fields(info)

fkwargs = {}
if subtype := kwargs.get("subtype"):
fkwargs["subtype"] = str(subtype).lower()
if cat := kwargs.get("category"):
fkwargs["category"] = str(cat).lower()

if "owner" in requested_fields:
return models.Spectrum.objects.sluglist()
return models.Spectrum.objects.all().values(*requested_fields)
# owner is complicated ... need to do our own thing
return models.Spectrum.objects.sluglist(filters=fkwargs)
elif fkwargs:
return models.Spectrum.objects.filter(**fkwargs).values(*requested_fields)
else:
return models.Spectrum.objects.all().values(*requested_fields)

def resolve_spectrum(self, info, **kwargs):
_id = kwargs.get("id")
Expand Down Expand Up @@ -129,3 +141,19 @@ def resolve_opticalConfig(self, info, **kwargs):
if _id is not None:
return gdo.query(models.OpticalConfig.objects.filter(id=_id), info).get()
return None

dyes = graphene.List(types.Dye)
dye = graphene.Field(types.Dye, id=graphene.Int(), name=graphene.String())

def resolve_dyes(self, info, **kwargs):
return gdo.query(models.Dye.objects.all(), info)

def resolve_dye(self, info, **kwargs):
name = kwargs.get("name")
if name is not None:
slug = slugify(name)
return gdo.query(models.Dye.objects.filter(slug=slug), info).get()
_id = kwargs.get("id")
if _id is not None:
return gdo.query(models.Dye.objects.filter(id=_id), info).get()
return None
Loading