From 7de796c1f3021d93fc87f4ea56f595b38936c68a Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Mon, 11 Mar 2024 14:42:27 -0400 Subject: [PATCH] add dyes to the graphql api --- backend/fpbase/forms.py | 4 +++- backend/proteins/models/spectrum.py | 7 ++++-- backend/proteins/schema/query.py | 34 ++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/backend/fpbase/forms.py b/backend/fpbase/forms.py index b1dba3e5..2b933672 100644 --- a/backend/fpbase/forms.py +++ b/backend/fpbase/forms.py @@ -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"] diff --git a/backend/proteins/models/spectrum.py b/backend/proteins/models/spectrum.py index 0abf2cb8..2e771169 100644 --- a/backend/proteins/models/spectrum.py +++ b/backend/proteins/models/spectrum.py @@ -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"] @@ -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: diff --git a/backend/proteins/schema/query.py b/backend/proteins/schema/query.py index 0a9d3e48..cac3a209 100644 --- a/backend/proteins/schema/query.py +++ b/backend/proteins/schema/query.py @@ -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 @@ -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") @@ -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