def users(request, known_only=False): if not request.user.is_authenticated(): return http.HttpResponseForbidden('Must be logged in') query = request.GET.get('term').strip() if len(query) < 2: return [] results = [] # I chose a limit of 30 because there are about 20+ 'peter' # something in mozilla for each in ldap_lookup.search_users(query, 30, autocomplete=True): if not each.get('givenName'): logging.warn("Skipping LDAP entry %s" % each) continue if known_only: if not User.objects.filter(email__iexact=each['mail']).exists(): continue full_name_and_email = '%s %s <%s>' % (each['givenName'], each['sn'], each['mail']) result = {'id': each['uid'], 'label': full_name_and_email, 'value': full_name_and_email} results.append(result) return results
def save_following(request): search = request.POST.get('search') if not search: return http.HttpResponseBadRequest('Missing search') if (-1 < search.rfind('<') < search.rfind('@') < search.rfind('>')): try: email = re.findall('<([\w\.\-]+@[\w\.\-]+)>', search)[0] email = email.strip() validate_email(email) except (ValidationError, IndexError): email = None elif search.isdigit(): try: email = User.objects.get(pk=search).email except User.DoesNotExist: email = None # will deal with this later else: found = [] result = ldap_lookup.search_users(search, 30, autocomplete=True) for each in result: try: found.append(User.objects.get(email__iexact=each['mail'])) except User.DoesNotExist: pass if len(found) > 1: return http.HttpResponseBadRequest('More than one user found') elif not found: return http.HttpResponseBadRequest('No user found') else: email = found[0].email # if no email is found in the search, it's an error if not email: return http.HttpResponseBadRequest('No email found') try: user = User.objects.get(email__iexact=email) except User.DoesNotExist: return http.HttpResponseBadRequest('No user by that email found') FollowingUser.objects.get_or_create( follower=request.user, following=user, ) # find a reason why we're following this user _minions_1 = get_minions(request.user, depth=1, max_depth=1) _minions_2 = get_minions(request.user, depth=1, max_depth=2) if user in _minions_1: reason = 'direct manager of' elif user in _minions_2: reason = 'indirect manager of' elif user == request.user.get_profile().manager_user: reason = 'your manager' elif (request.user.get_profile().manager_user and user in _minions_1): reason = 'teammate' else: reason = 'curious' name = ('%s %s' % (user.first_name, user.last_name)).strip() if not name: name = user.username data = { 'id': user.pk, 'name': name, 'reason': reason, } return data