def prepare_exam_scores(applicant): try: niets_scores = applicant.NIETS_scores except: niets_scores = None if not niets_scores: return None cal_scores = niets_scores.as_calculated_list_by_exam_round() scores = [] test_names = ['gat','pat1','pat3'] test_norm_scores = dict([(n,[]) for n in test_names]) for i in range(len(cal_scores)): scores.append({'date': GPExamDate.get_by_id(i+1), 'scores': cal_scores[i]}) for n in test_names: test_norm_scores[n].append(cal_scores[i][n]['normalized']) best_scores = dict([(n,max(test_norm_scores[n])) for n in test_names]) final_score = niets_scores.get_score() return { 'scores': scores, 'best_scores': best_scores, 'final_score': final_score }
def extract_data(f): NUM_SKIP = 14 ROUND_FIELDS = (12, 13) ROW_FIELDS = ( ('pat1', 14), ('pat3', 28), ('gat', 48), ) GROUPING_FIELD = 'nat_id' DATA_FIELDS = ( ('nat_id', 4), ('first_name', 6), ('last_name', 9), ) source = f.read() lines = source.split("\n")[NUM_SKIP:] scores = [] old_key = '' for l in lines: items = l.split(",") if len(items) < 30: continue round_key = extract_round(tuple([items[i] for i in ROUND_FIELDS])) row = dict([(f[0], convert_data_field(items[f[1]])) for f in ROW_FIELDS]) data = dict([(f[0], items[f[1]]) for f in DATA_FIELDS]) k = data[GROUPING_FIELD] if k != old_key: app_data = data app_data['scores'] = [] scores.append(app_data) #print k old_key = k app_data = scores[-1] if GPExamDate.get_by_year_and_number(round_key[0], round_key[1]) != None: app_data['scores'].append((round_key, row)) return scores
def prepare_exam_scores(applicant): try: niets_scores = applicant.NIETS_scores except: niets_scores = None if not niets_scores or not niets_scores.is_request_successful: return None cal_scores = niets_scores.as_calculated_list_by_exam_round() scores = [] test_names = ['gat','pat1','pat3'] test_norm_scores = dict([(n,[]) for n in test_names]) for i in range(len(cal_scores)): scores.append({'date': GPExamDate.get_by_id(i+1), 'scores': cal_scores[i]}) for n in test_names: test_norm_scores[n].append(cal_scores[i][n]['normalized']) best_scores = dict([(n,max(test_norm_scores[n])) for n in test_names]) final_score = niets_scores.get_score() final_score = niets_scores.get_score() score_complete = True for n in test_names: score_available = False for i in range(len(cal_scores)): if cal_scores[i][n]['raw'] != 0: score_available = True break if not score_available: best_scores[n] = 0 score_complete = False return { 'scores': scores, 'best_scores': best_scores, 'final_score': final_score }
def score_import(f): from datetime import datetime count = 0 scores = extract_data(f) for s in scores: try: a = Applicant.objects.get(national_id=s['nat_id']) except Applicant.DoesNotExist: a = None if a == None: continue try: niets_scores = a.NIETS_scores except: niets_scores = NIETSScores(applicant=a) niets_scores.is_request_successful = True niets_scores.requested_at = datetime.now() score_list = initialialize_empty_score_list() for scr in s['scores']: y = scr[0][0] num = scr[0][1] d = GPExamDate.get_by_year_and_number(y, num) i = d.rank - 1 score_list[i * 3] = scr[1]['gat'] score_list[i * 3 + 1] = scr[1]['pat1'] score_list[i * 3 + 2] = scr[1]['pat3'] niets_scores.score_list = ','.join([str(x) for x in score_list]) niets_scores.save() count += 1 return u'นำเข้าข้อมูลทั้งสิ้น %d คน' % count
def initialialize_empty_score_list(): excount = len(GPExamDate.get_all()) return [0] * excount * 3