|
1 | | -import urllib |
| 1 | +import re |
2 | 2 |
|
| 3 | +from django.http import Http404 |
3 | 4 | from django.utils.decorators import method_decorator |
4 | 5 | from django.views.decorators.cache import cache_page |
5 | 6 | from rest_framework.response import Response |
|
14 | 15 | from ontology.ontology_matching import OntologyMatching |
15 | 16 | from ontology.serializers import OntologyTermRelationSerializer |
16 | 17 |
|
| 18 | +_GENE_SYMBOL_RE = re.compile(r'^[A-Za-z0-9\-]+$') |
| 19 | + |
17 | 20 |
|
18 | 21 | class SearchMondoText(APIView): |
19 | 22 | def get(self, request, **kwargs) -> Response: |
20 | 23 |
|
21 | 24 | search_term = request.GET.get('search_term') or '' |
22 | 25 | gene_symbol = request.GET.get('gene_symbol') |
23 | 26 |
|
24 | | - urllib.parse.quote(search_term).replace('/', '%252F') # a regular escape / gets confused for a URL divider |
25 | 27 | selected = [term.strip() for term in (request.GET.get('selected') or '').split(",") if term.strip()] |
26 | 28 |
|
27 | 29 | ontology_matches = OntologyMatching.from_search(search_text=search_term, gene_symbol=gene_symbol, selected=selected) |
@@ -58,8 +60,11 @@ def get(self, request, *args, **kwargs): |
58 | 60 | @method_decorator(cache_page(WEEK_SECS), name='get') |
59 | 61 | class GeneDiseaseRelationshipView(APIView): |
60 | 62 | def get(self, request, *args, **kwargs): |
| 63 | + gene_symbol = self.kwargs['gene_symbol'] |
| 64 | + if not _GENE_SYMBOL_RE.match(gene_symbol): |
| 65 | + raise Http404 |
61 | 66 | data = [] |
62 | 67 | ontology_version = OntologyVersion.latest() |
63 | | - for otr in ontology_version.gene_disease_relations(self.kwargs['gene_symbol'], quality_filter=ONTOLOGY_RELATIONSHIP_MEDIUM_QUALITY_FILTER): |
| 68 | + for otr in ontology_version.gene_disease_relations(gene_symbol, quality_filter=ONTOLOGY_RELATIONSHIP_MEDIUM_QUALITY_FILTER): |
64 | 69 | data.append(OntologyTermRelationSerializer(otr).data) |
65 | 70 | return Response(data) |
0 commit comments