-
Notifications
You must be signed in to change notification settings - Fork 0
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
Create API endpoint for proximity analysis #53
base: main
Are you sure you want to change the base?
Changes from 11 commits
f3114ec
4d88605
efdee64
c48ca53
6a5b6f9
adf0f5d
0f8b7aa
4da0311
e4ee358
191daa6
30ef395
e2c5448
2130b24
41382a6
bfa0060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from ..models import Document, Corpus | ||
from rest_framework.test import APIRequestFactory | ||
from app.views import add_proximity_analysis | ||
|
||
|
||
def proximity_view_test(text): | ||
"""A function for testing the proximity analysis posting, | ||
check api/all_proximity to see the updates list | ||
""" | ||
c1 = Corpus(title="Corpus Test", description="This is the testing corpus") | ||
c1.save() | ||
Document.objects.create_document(title='document_1', year=2021, text=text) | ||
d1 = Document.objects.get(title='document_1') | ||
c1.documents.add(d1) | ||
factory = APIRequestFactory() | ||
request = factory.post('api/all_proximity', {'word_window': '2', 'corpus_id': c1.id}) | ||
add_proximity_analysis(request) | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,23 +20,24 @@ | |
'component_name': 'ExampleId' | ||
} | ||
""" | ||
import json | ||
|
||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from django.shortcuts import render | ||
from .models import ( | ||
Document, | ||
Gender, | ||
Corpus | ||
Corpus, | ||
ProximityAnalysis | ||
) | ||
from .serializers import ( | ||
DocumentSerializer, | ||
SimpleDocumentSerializer, | ||
GenderSerializer, | ||
CorpusSerializer | ||
CorpusSerializer, | ||
ProximityAnalysisSerializer, | ||
) | ||
|
||
from .analysis.proximity import run_analysis | ||
|
||
@api_view(['GET']) | ||
def get_example(request, example_id): | ||
|
@@ -243,6 +244,43 @@ def get_corpus(request, corpus_id): | |
return Response(serializer.data) | ||
|
||
|
||
@api_view(['GET']) | ||
def all_proximity(request): | ||
prox_objs = ProximityAnalysis.objects.all() | ||
serializer = ProximityAnalysisSerializer(prox_objs, many=True) | ||
return Response(serializer.data) | ||
|
||
|
||
@api_view(['POST']) | ||
def add_proximity_analysis(request): | ||
""" | ||
API endpoint for posting the proximity analysis | ||
""" | ||
attributes = request.data | ||
corpus_id = int(attributes['corpus_id']) | ||
word_window = int(attributes['word_window']) | ||
|
||
proximity_query = ProximityAnalysis.objects.filter(corpus__id=corpus_id, word_window=word_window) | ||
|
||
if proximity_query.exists(): | ||
proximity_obj = proximity_query.get() | ||
|
||
else: | ||
results = run_analysis(corpus_id, word_window) | ||
fields = { | ||
'corpus': Corpus.objects.get(pk=corpus_id), | ||
'word_window': word_window, | ||
'results': results, | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ultimately, we may want to change this up so that you call a ProximityAnalysisManager or similar (like we do with the DocumentManager) when we create this new ProximityAnalysis, and have the ProximityAnalysisManager call the run_analysis function. That is: since a ProximityAnalysis necessarily relies on the run_analysis function, we could use the ProximityAnalysis itself (through a Manager) to ensure that run_analysis is run when a new instance is created. That would more tightly couple associated functionality. That said, since this is working and is well tested, we can leave it be for now and come back to it later. |
||
proximity_obj = ProximityAnalysis.objects.create(**fields) | ||
gender_ids = list(Gender.objects.values_list('pk', flat=True)) | ||
proximity_obj.genders.add(*gender_ids) | ||
|
||
serializer = ProximityAnalysisSerializer(proximity_obj) | ||
return Response(serializer.data) | ||
|
||
|
||
def corpora(request): | ||
""" | ||
Corpora page | ||
|
@@ -274,4 +312,3 @@ def corpus(request, corpus_id): | |
} | ||
|
||
return render(request, 'index.html', context) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this test used simply to manually ensure that the full ProximityAnalysis creation occurred as expected? Would it be possible to add something to our larger test suite so we can check that the model instance is saved as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, this was just a manual test. I will try and add a more comprehensive test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just added a test to the tests.py file that makes sure the post request is successful