def employer_questions(request, employer_id): employer = get_object_or_404(Employer, pk=employer_id) questions_qs = Question.objects.filter(employer=employer).order_by('-created') questions = questions_qs qids = [x.id for x in questions] all_answers = Answer.objects.filter(question__in=qids).order_by('question') extract_uid = lambda x: x.user_id # TODO - see if this is going to be useful or not... all_user_ids = set(map(extract_uid, questions) + map(extract_uid, all_answers)) all_users = dict((x.id, x) for x in User.objects.filter(id__in=all_user_ids)) for item in itertools.chain(questions, all_answers): if not item.is_anonymous: item.user = all_users[item.user_id] questions_dict = dict((q.id, q) for q in questions) # aggregate all the answers by question_id. # all_answers is sorted by question, so this should return unique groupings by question_id. for qid, answers in groupby(all_answers, lambda x: x.question_id): # set the mapping question -> list of answers. questions_dict[int(qid)]._answers = list(answers) # hide all questions that are # -deleted # -has no answer. questions = [q for q in questions if not(q.deleted and not q.answers)] num_unanswered = Question.objects.get_unanswered(employer).count() num_subscribed = Subscription.objects.filter( type=SUBSCRIPTIONS.QNA, employer=employer, is_active=True).count() # already subscribed or not. is_subscribed = Subscription.is_user_subscribed_to_qna(request.user, employer) ctx = dict( employer=employer, # forms question_form=QuestionForm({'employer':employer}, auto_id=None), # data to be displayed questions=questions, # questions_paginator=questions_paginator, num_unanswered=num_unanswered, num_subscribed=num_subscribed, is_subscribed=is_subscribed, ) return partial_html_response('main/employer_discussions.html', ctx, request)
def employer_rankings(request, employer_id): 'Display the tab for the employer rankings screen.' employer = get_object_or_404(Employer, id=employer_id) qs = UserPostings.objects.filter(employer=employer).select_related('user')\ .order_by('-created') averages = average_rankings(qs) helpful_ids = UserPostingsHelpful.objects.filter(user=request.user, posting__employer=employer).values('posting_id') helpfuls = set(x['posting_id'] for x in helpful_ids) user_postings = process_rankings(qs, helpfuls) ctx = dict( employer=employer, averages=averages, user_postings=user_postings, sort_bys=[SortBy(col, SORT_BY_HUMAN_READABLE[col], 'asc') for col in ['overall', 'created', 'year']] ) return partial_html_response('main/employer_ranking.html', ctx, request)
def employer_summary(request, employer_id): emp = Employer.objects.get(id=employer_id) # todo - we might neeed 'magic function' to order questions. unanswered = Question.objects.get_unanswered(emp).filter(deleted=False).order_by('-created')[:3] user_postings = UserPostings.objects.filter(employer=emp)[:3] def valid_location(loc): return loc.lat is not None and loc.lng is not None and loc.formatted_address.strip() def filter_dupe_locs(locations): existing_addr = set() for loc in locations: if loc.formatted_address not in existing_addr: existing_addr.add(loc.formatted_address) yield loc locations = filter(valid_location, emp.location_set.all().order_by('-created')) # filter out duplicates locations = list(filter_dupe_locs(locations)) def location_to_json(loc): 'Convert the given locations to json, to be displayed by' res = loc.coord._asdict() res.update(address=loc.formatted_address) return res posting_dist = list(UWPostingsAggregate.objects.for_employer(emp)) TERM_DICT = {1:'W', 2:'S', 3:'F'} def aggr_to_json(aggregate): return ("%02d'%s" % (aggregate.year % 100, TERM_DICT[aggregate.term]), aggregate.cnt) show_posting_distribution = len(posting_dist) > 1 if show_posting_distribution: posting_dist = map(aggr_to_json, posting_dist) else: posting_dist = [] request.json_output['posting_dist'] = posting_dist request.json_output['locations'] = map(location_to_json, locations) ctx = dict( locations=locations, user_postings=user_postings, unanswered=unanswered, employer=emp, show_posting_distribution=show_posting_distribution, ) return partial_html_response('main/employer_summary.html', ctx, request)