Change search to POST and add sequence numbers

This commit is contained in:
Tanner Collin 2020-01-11 04:20:07 +00:00
parent 4e78087338
commit 8c1f1ab3de
6 changed files with 19 additions and 19 deletions

View File

@ -24,9 +24,12 @@ class UserSerializer(serializers.ModelSerializer):
# member viewing member list or other member # member viewing member list or other member
class OtherMemberSerializer(serializers.ModelSerializer): class OtherMemberSerializer(serializers.ModelSerializer):
q = serializers.CharField(write_only=True, max_length=64)
seq = serializers.IntegerField(write_only=True, )
class Meta: class Meta:
model = models.Member model = models.Member
fields = ['preferred_name', 'last_name', 'status', 'current_start_date'] fields = ['q', 'seq', 'preferred_name', 'last_name', 'status', 'current_start_date']
# member viewing himself # member viewing himself
class MemberSerializer(serializers.ModelSerializer): class MemberSerializer(serializers.ModelSerializer):

View File

@ -13,10 +13,6 @@ class AllowMetadata(permissions.BasePermission):
return request.method in ['OPTIONS', 'HEAD'] return request.method in ['OPTIONS', 'HEAD']
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('-date_joined')
serializer_class = serializers.UserSerializer
search_strings = {} search_strings = {}
def gen_search_strings(): def gen_search_strings():
@ -33,17 +29,17 @@ def gen_search_strings():
print('Generated search strings in {} s'.format(time.time() - start)) print('Generated search strings in {} s'.format(time.time() - start))
gen_search_strings() gen_search_strings()
class SearchViewSet(viewsets.ReadOnlyModelViewSet): class SearchViewSet(viewsets.ViewSet):
permission_classes = [AllowMetadata | permissions.IsAuthenticated]
serializer_class = serializers.OtherMemberSerializer serializer_class = serializers.OtherMemberSerializer
def get_queryset(self): def get_queryset(self):
NUM_SEARCH_RESULTS = 10 NUM_SEARCH_RESULTS = 10
queryset = models.Member.objects.all() queryset = models.Member.objects.all()
params = self.request.query_params search = self.request.data.get('q', '').lower()
if 'q' in params and len(params['q']): if len(search):
search = params['q'].lower()
choices = search_strings.keys() choices = search_strings.keys()
# get exact starts with matches # get exact starts with matches
@ -68,9 +64,10 @@ class SearchViewSet(viewsets.ReadOnlyModelViewSet):
return queryset return queryset
def list(self, request): # must POST so query string doesn't change so preflight request is cached
def create(self, request):
try: try:
seq = int(request.query_params.get('seq', 0)) seq = int(request.data.get('seq', 0))
except ValueError: except ValueError:
seq = 0 seq = 0

View File

@ -6,7 +6,7 @@ from rest_framework import routers
from .api import views from .api import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet) #router.register(r'users', views.UserViewSet)
router.register(r'members', views.MemberViewSet, basename='member') router.register(r'members', views.MemberViewSet, basename='member')
router.register(r'courses', views.CourseViewSet, basename='course') router.register(r'courses', views.CourseViewSet, basename='course')
router.register(r'sessions', views.SessionViewSet, basename='session') router.register(r'sessions', views.SessionViewSet, basename='session')

View File

@ -150,7 +150,7 @@ function MemberInfo(props) {
</Table.Row> </Table.Row>
) )
: :
<p>None</p> <Table.Row><Table.Cell>None</Table.Cell></Table.Row>
} }
</Table.Body> </Table.Body>
</Table> </Table>

View File

@ -8,11 +8,11 @@ import { NotFound, PleaseLogin } from './Misc.js';
export function Members(props) { export function Members(props) {
const [members, setMembers] = useState(false); const [members, setMembers] = useState(false);
const [search, setSearch] = useState({t: 0, v: ''}); const [search, setSearch] = useState({seq: 0, q: ''});
const { token } = props; const { token } = props;
useEffect(() => { useEffect(() => {
requester('/search/?seq='+search.t+'&q='+search.v, 'GET', '') requester('/search/', 'POST', token, search)
.then(res => { .then(res => {
if (!members || res.seq > members.seq) { if (!members || res.seq > members.seq) {
setMembers(res); setMembers(res);
@ -30,12 +30,12 @@ export function Members(props) {
<Input autoFocus focus icon='search' <Input autoFocus focus icon='search'
placeholder='Search...' placeholder='Search...'
value={search.v} value={search.v}
onChange={(e, v) => setSearch({t: e.timeStamp, v: v.value})} onChange={(e, v) => setSearch({seq: e.timeStamp, q: v.value})}
aria-label='search products' aria-label='search products'
/> />
<Header size='medium'> <Header size='medium'>
{search.length ? 'Search Results' : 'Recently Vetted'} {search.q.length ? 'Search Results' : 'Recently Vetted'}
</Header> </Header>
{members ? {members ?
@ -58,7 +58,7 @@ export function Members(props) {
</Table.Row> </Table.Row>
) )
: :
<p>No Results</p> <Table.Row><Table.Cell>No Results</Table.Cell></Table.Row>
} }
</Table.Body> </Table.Body>
</Table> </Table>

View File

@ -36,7 +36,7 @@ export function Transactions(props) {
</Table.Row> </Table.Row>
) )
: :
<p>None</p> <Table.Row><Table.Cell>None</Table.Cell></Table.Row>
} }
</Table.Body> </Table.Body>
</Table> </Table>