def compress(self, data_list): """ Compress data from the hidden input fields. The input value is usually the result of the client-side DartDM lookup, however a user can hit 'submit' with a self-entered name, or change the autocompleted name. If this is the case we do another DartDm lookup to verify the name. `data_list` is of the form `[NAME_WITH_YEAR, NETID, NAME_WITH_AFFIL]` """ logger.info('compress: %r' % data_list) if len(data_list) == 0: # Empty field return None if len(data_list) == 1 or not data_list[2].startswith(data_list[0]): # User did not wait for the typeahead autocomplete, # or changed the autocompleted name after the lookup. # Try and lookup the given name. results = lookup.dartdm_lookup(data_list[0]) if len(results) == 0: raise ValidationError('User not found') elif len(results) == 1: return results[0] # Lookup results are already formatted else: raise ValidationError("Ambiguous name %r" % data_list[0]) return { lookup.NAME_WITH_YEAR: data_list[0], lookup.NETID: data_list[1], lookup.NAME_WITH_AFFIL: data_list[2] }
def dartdm_lookup_view(request): """ Dartmouth Name Directory connector. The dartdm netid lookup doesn't allow cross-site requests, hence no AJAX. This view allows us to do DND lookups by acting as an endpoint for typeahead. """ try: query = request.GET['term'] except KeyError: results = [] else: results = dartdm_lookup(query) # setting safe=False allows us to return the JSON array return JsonResponse(results, safe=False)