def student_search(request): # check permissions roles = Role.all_roles(request.user.username) allowed = set(['ADVS', 'ADMN', 'GRAD', 'FUND', 'SYSA']) if not(roles & allowed) and not has_formgroup(request): # doesn't have any allowed roles return ForbiddenResponse(request, "Not permitted to do student search.") if 'term' not in request.GET: return ForbiddenResponse(request, "Must provide 'term' query.") term = request.GET['term'] response = HttpResponse(content_type='application/json') # do the query with Haystack # experimentally, score >= 1 seems to correspond to useful things student_qs = SearchQuerySet().models(Person).filter(text=term)[:20] data = [{'value': r.emplid, 'label': r.search_display} for r in student_qs if r and r.score >= 1 and unicode(r.emplid) not in EXCLUDE_EMPLIDS] # non-haystack version of the above query if len(student_qs) == 0: studentQuery = get_query(term, ['userid', 'emplid', 'first_name', 'last_name']) students = Person.objects.filter(studentQuery)[:20] data = [{'value': s.emplid, 'label': s.search_label_value()} for s in students if unicode(s.emplid) not in EXCLUDE_EMPLIDS] if 'nonstudent' in request.GET and 'ADVS' in roles: nonStudentQuery = get_query(term, ['first_name', 'last_name', 'pref_first_name']) nonStudents = NonStudent.objects.filter(nonStudentQuery)[:10] data.extend([{'value': n.slug, 'label': n.search_label_value()} for n in nonStudents]) #data.sort(key = lambda x: x['label']) json.dump(data, response, indent=1) return response
def artifact_search(request): if 'text-search' not in request.GET: return ForbiddenResponse(request, "must send search query") search = request.GET['text-search'] query = get_query(search, ('text', )) artifact_notes = ArtifactNote.objects.filter( query, unit__in=request.units, hidden=False).order_by("-created_at")[:100] for a in artifact_notes: if a.course: a.url = reverse('advising:view_course_notes', kwargs={'unit_course_slug': a.course.slug}) a.description = a.course elif a.course_offering: a.url = reverse('advising:view_offering_notes', kwargs={'course_slug': a.course_offering.slug}) a.description = a.course_offering else: a.url = reverse('advising:view_artifact_notes', kwargs={'artifact_slug': a.artifact.slug}) a.description = a.artifact artifact_form = ArtifactSearchForm(prefix="text", initial={'search': search}) context = { 'artifact_notes': artifact_notes, 'artifact_form': artifact_form } return render(request, 'advisornotes/artifact_search.html', context)
def student_search(request): # check permissions roles = Role.all_roles(request.user.username) allowed = set(['ADVS', 'ADMN', 'GRAD', 'FUND', 'SYSA']) if not (roles & allowed) and not has_formgroup(request): # doesn't have any allowed roles return ForbiddenResponse(request, "Not permitted to do student search.") if 'term' not in request.GET: return ForbiddenResponse(request, "Must provide 'term' query.") term = request.GET['term'] response = HttpResponse(content_type='application/json') # do the query with Haystack # experimentally, score >= 1 seems to correspond to useful things student_qs = SearchQuerySet().models(Person).filter(text=term)[:20] data = [{ 'value': r.emplid, 'label': r.search_display } for r in student_qs if r and r.score >= 1 and unicode(r.emplid) not in EXCLUDE_EMPLIDS] # non-haystack version of the above query if len(student_qs) == 0: studentQuery = get_query( term, ['userid', 'emplid', 'first_name', 'last_name']) students = Person.objects.filter(studentQuery)[:20] data = [{ 'value': s.emplid, 'label': s.search_label_value() } for s in students if unicode(s.emplid) not in EXCLUDE_EMPLIDS] if 'nonstudent' in request.GET and 'ADVS' in roles: nonStudentQuery = get_query( term, ['first_name', 'last_name', 'pref_first_name']) nonStudents = NonStudent.objects.filter(nonStudentQuery)[:10] data.extend([{ 'value': n.slug, 'label': n.search_label_value() } for n in nonStudents]) #data.sort(key = lambda x: x['label']) json.dump(data, response, indent=1) return response
def note_search(request): if 'text-search' not in request.GET: return ForbiddenResponse(request, "must send search query") search = request.GET['text-search'] query = get_query(search, ('text',)) notes = AdvisorNote.objects.filter(query, unit__in=request.units) \ .select_related('student', 'advisor').order_by("-created_at")[:100] note_form = NoteSearchForm(prefix="text", initial={'search': search}) context = {'notes': notes, 'note_form': note_form} return render(request, 'advisornotes/note_search.html', context)
def _get_query(term): # if "Withdrawn" in search term, search on "WIDR" for status, display in STATUS_CHOICES: if display in term: term = term.replace(display, status) return get_query(term, ['person__userid', 'person__emplid', 'person__first_name', 'person__last_name', 'person__pref_first_name', 'program__label', 'program__description', 'current_status'], startonly=True)
def index(request): if 'q' in request.GET: q = request.GET.get('q') query = get_query(q, ['userid', 'description', 'comment']) logs = LogEntry.objects.order_by('-datetime').filter(query)[:200] else: q = '' keywords = "" logs = None reg = "" return render(request, "log/index.html", {"logs": logs, 'q': q})
def _get_query(term): # if "Withdrawn" in search term, search on "WIDR" for status, display in STATUS_CHOICES: if display in term: term = term.replace(display, status) return get_query(term, [ 'person__userid', 'person__emplid', 'person__first_name', 'person__last_name', 'person__pref_first_name', 'program__label', 'program__description', 'current_status' ], startonly=True)
def course_search(request): if 'term' not in request.GET: return ForbiddenResponse(request, "Must provide 'term' query.") term = request.GET['term'] response = HttpResponse(content_type='application/json') data = [] query = get_query(term, ['subject', 'number', 'title']) courses = Course.objects.filter(query) for c in courses: label = "%s %s" % (c.subject, c.number) d = {'value': c.id, 'label': label} data.append(d) json.dump(data, response, indent=1) return response
def offerings_search(request): if 'term' not in request.GET: return ForbiddenResponse(request, "Must provide 'term' query.") term = request.GET['term'] response = HttpResponse(content_type='application/json') data = [] query = get_query(term, ['subject', 'number', 'section', 'semester__name', 'title']) offerings = CourseOffering.objects.filter(query).exclude(component="CAN").select_related('semester') for o in offerings: label = o.search_label_value() d = {'value': o.id, 'label': label} data.append(d) json.dump(data, response, indent=1) return response
def found(request): """ View to handle the enter-search/press-enter behaviour in the autocomplete box """ if 'search' not in request.GET: return ForbiddenResponse(request, 'must give search in query') search = request.GET['search'] studentQuery = get_query(search, ['userid', 'emplid', 'first_name', 'last_name']) people = Person.objects.filter(studentQuery)[:200] for p in people: # decorate with RAAppointment count p.ras = RAAppointment.objects.filter(unit__in=Unit.sub_units(request.units), person=p, deleted=False).count() context = {'people': people} return render(request, 'ra/found.html', context)
def found(request): """ View to handle the enter-search/press-enter behaviour in the autocomplete box """ if 'search' not in request.GET: return ForbiddenResponse(request, 'must give search in query') search = request.GET['search'] studentQuery = get_query(search, ['userid', 'emplid', 'first_name', 'last_name']) people = Person.objects.filter(studentQuery)[:200] for p in people: # decorate with RAAppointment count p.ras = RAAppointment.objects.filter(person=p, deleted=False).count() context = {'people': people} return render(request, 'ra/found.html', context)
def offerings_search(request): if 'term' not in request.GET: return ForbiddenResponse(request, "Must provide 'term' query.") term = request.GET['term'] response = HttpResponse(content_type='application/json') data = [] query = get_query( term, ['subject', 'number', 'section', 'semester__name', 'title']) offerings = CourseOffering.objects.filter(query).exclude( component="CAN").select_related('semester') for o in offerings: label = o.search_label_value() d = {'value': o.id, 'label': label} data.append(d) json.dump(data, response, indent=1) return response
def artifact_search(request): if 'text-search' not in request.GET: return ForbiddenResponse(request, "must send search query") search = request.GET['text-search'] query = get_query(search, ('text',)) artifact_notes = ArtifactNote.objects.filter(query, unit__in=request.units, hidden=False).order_by("-created_at")[:100] for a in artifact_notes: if a.course: a.url = reverse('advising:view_course_notes', kwargs={'unit_course_slug': a.course.slug}) a.description = a.course elif a.course_offering: a.url = reverse('advising:view_offering_notes', kwargs={'course_slug': a.course_offering.slug}) a.description = a.course_offering else: a.url = reverse('advising:view_artifact_notes', kwargs={'artifact_slug': a.artifact.slug}) a.description = a.artifact artifact_form = ArtifactSearchForm(prefix="text", initial={'search': search}) context = {'artifact_notes': artifact_notes, 'artifact_form': artifact_form} return render(request, 'advisornotes/artifact_search.html', context)