-
Notifications
You must be signed in to change notification settings - Fork 1
Allow users to add reference materials #297
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
297c198
Allow users to add reference materials
naglepuff 7a7cf6a
Model vetting details per user
naglepuff a1400c0
Update vetting details from front end
naglepuff 2dff11e
Use separate component for reference materials
naglepuff 821c17d
Add length check and test to API layer
naglepuff 7f25dc5
Swap position of close/save for consistency
naglepuff 438d507
Use correct max length value for vuetify rule
naglepuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from bats_ai.core.models import VettingDetails | ||
|
|
||
|
|
||
| @admin.register(VettingDetails) | ||
| class VettingDetailsAdmin(admin.ModelAdmin): | ||
| list_display = [ | ||
| 'pk', | ||
| 'user', | ||
| # 'reference_materials', | ||
| ] | ||
| search_fields = ('user',) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Generated by Django 4.2.23 on 2026-01-08 18:57 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ('core', '0024_configuration_mark_annotations_completed_enabled_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='VettingDetails', | ||
| fields=[ | ||
| ( | ||
| 'id', | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name='ID' | ||
| ), | ||
| ), | ||
| ('reference_materials', models.TextField(blank=True)), | ||
| ( | ||
| 'user', | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name='vettingdetails', | ||
| constraint=models.CheckConstraint( | ||
| check=models.Q(('reference_materials__length__lte', 2000)), | ||
| name='reference_materials_max_2000', | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from django.contrib.auth.models import User | ||
| from django.db import models | ||
| from django.db.models import Q | ||
| from django.db.models.functions import Length | ||
|
|
||
| models.TextField.register_lookup(Length, 'length') | ||
|
|
||
|
|
||
| class VettingDetails(models.Model): | ||
| user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
| reference_materials = models.TextField(blank=True) | ||
|
|
||
| class Meta: | ||
| constraints = [ | ||
| models.CheckConstraint( | ||
| # TODO change to 'condition' in Django v6 | ||
| check=Q(reference_materials__length__lte=2000), | ||
| name='reference_materials_max_2000', | ||
| ) | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import pytest | ||
|
|
||
| from .factories import UserFactory, VettingDetailsFactory | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code', | ||
| [ | ||
| ('client', 401), | ||
| ('authenticated_client', 200), | ||
| ('authorized_client', 200), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_get_vetting_details(client_fixture, status_code, user, vetting_details, request): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| resp = api_client.get(f'/api/v1/vetting/user/{user.id}') | ||
| assert resp.status_code == status_code | ||
| if status_code == 200: | ||
| assert resp.json()['reference_materials'] == vetting_details.reference_materials | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_get_vetting_details_other_user(authenticated_client): | ||
| other_user = UserFactory() | ||
| VettingDetailsFactory(user=other_user) | ||
| resp = authenticated_client.get(f'/api/v1/vetting/user/{other_user.id}') | ||
| assert resp.status_code == 404 | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_vetting_details(client): | ||
| test_text = 'foo' | ||
| data = {'reference_materials': test_text} | ||
| test_user = UserFactory() | ||
| client.force_login(user=test_user) | ||
| resp = client.post( | ||
| f'/api/v1/vetting/user/{test_user.id}', data=data, content_type='application/json' | ||
| ) | ||
| assert resp.status_code == 200 | ||
| assert resp.json()['user_id'] == test_user.id | ||
|
Comment on lines
+31
to
+41
Collaborator
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. There is a DB constraint for the text size, should we be testin that in the tests?
Collaborator
Author
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. |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code', | ||
| [ | ||
| ('authenticated_client', 404), | ||
| ('authorized_client', 200), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_create_vetting_details_other_user(client_fixture, status_code, request): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| test_text = 'foo' | ||
| data = {'reference_materials': test_text} | ||
| other_user = UserFactory() | ||
| resp = api_client.post( | ||
| f'/api/v1/vetting/user/{other_user.id}', data=data, content_type='application/json' | ||
| ) | ||
| assert resp.status_code == status_code | ||
| if status_code == 200: | ||
| assert resp.json()['reference_materials'] == test_text | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_vetting_details(client): | ||
| test_text = 'bar' | ||
| data = {'reference_materials': 'bar'} | ||
| test_user = UserFactory() | ||
| VettingDetailsFactory(user=test_user, reference_materials='foo') | ||
| client.force_login(test_user) | ||
|
|
||
| initial_resp = client.get(f'/api/v1/vetting/user/{test_user.id}') | ||
| assert initial_resp.status_code == 200 | ||
|
|
||
| resp = client.post( | ||
| f'/api/v1/vetting/user/{test_user.id}', data=data, content_type='application/json' | ||
| ) | ||
| assert resp.status_code == 200 | ||
|
|
||
| new_details_response = client.get(f'/api/v1/vetting/user/{test_user.id}') | ||
| assert new_details_response.status_code == 200 | ||
| assert new_details_response.json()['reference_materials'] == test_text | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'client_fixture,status_code', | ||
| [ | ||
| ('authenticated_client', 404), | ||
| ('authorized_client', 200), | ||
| ], | ||
| ) | ||
| @pytest.mark.django_db | ||
| def test_update_vetting_details_other_user( | ||
| client_fixture, status_code, random_user_vetting_details, request | ||
| ): | ||
| api_client = request.getfixturevalue(client_fixture) | ||
| resp = api_client.post( | ||
| f'/api/v1/vetting/user/{random_user_vetting_details.user.id}', | ||
| data={'reference_materials': 'foo'}, | ||
| content_type='application/json', | ||
| ) | ||
| assert resp.status_code == status_code | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_update_vetting_details_length_constraint(authorized_client, random_user_vetting_details): | ||
| data = {'reference_materials': 'a' * 2001} | ||
| resp = authorized_client.post( | ||
| f'/api/v1/vetting/user/{random_user_vetting_details.user.id}', | ||
| data=data, | ||
| content_type='application/json', | ||
| ) | ||
| assert resp.status_code == 400 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from django.http import Http404, HttpRequest, HttpResponseBadRequest | ||
| from ninja import Schema | ||
| from ninja.pagination import RouterPaginated | ||
|
|
||
| from bats_ai.core.models import VettingDetails | ||
|
|
||
| router = RouterPaginated() | ||
|
|
||
|
|
||
| class VettingDetailsSchema(Schema): | ||
| id: int | ||
| user_id: int | ||
| reference_materials: str | ||
|
|
||
| @classmethod | ||
| def from_orm(cls, obj): | ||
| print(obj) | ||
| return cls(id=obj.id, reference_materials=obj.reference_materials, user_id=obj.user_id) | ||
|
|
||
|
|
||
| class UpdateVettingDetailsSchema(Schema): | ||
| reference_materials: str | ||
|
|
||
|
|
||
| @router.get('/user/{user_id}', response=VettingDetailsSchema) | ||
| def get_vetting_details_for_user(request: HttpRequest, user_id: int): | ||
| details = VettingDetails.objects.filter(user_id=user_id).first() | ||
|
|
||
| if not details: | ||
| raise Http404() | ||
|
|
||
| if details.user != request.user and not request.user.is_staff: | ||
| # Don't leak user IDs, prefer to return a 404 over a 403 | ||
| raise Http404 | ||
|
|
||
| return details | ||
|
|
||
|
|
||
| @router.post('/user/{user_id}', response=VettingDetailsSchema) | ||
| def update_or_create_vetting_details_for_user( | ||
| request: HttpRequest, | ||
| payload: UpdateVettingDetailsSchema, | ||
| user_id: int, | ||
| ): | ||
| if not (request.user.pk == user_id or request.user.is_staff): | ||
| raise Http404 | ||
|
|
||
BryonLewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if len(payload.reference_materials) > 2000: | ||
| return HttpResponseBadRequest( | ||
| 'reference_materials exceeds maximum length of 2000 characters' | ||
| ) | ||
|
|
||
| details = VettingDetails.objects.filter(user_id=user_id).first() | ||
|
|
||
| if not details: | ||
| details = VettingDetails(user=request.user, reference_materials=payload.reference_materials) | ||
| else: | ||
| details.reference_materials = payload.reference_materials | ||
|
|
||
| details.save() | ||
|
|
||
| return details | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.