def test_student_model_created(self): """Make sure the student model auto_add_now works as expected for the created field""" now = datetime.now(tz=utc) ptype = PatronType.objects.get(ptype='1') student = Student(student_id = '3025768', first_name = 'john', last_name = 'smith', ptype=ptype) assert student.created is None student.save() assert student.created > now
def test_student_model_update(self): """Make sure the student model update works as expected""" ptype = PatronType.objects.get(ptype='1') student = Student(student_id = '3025768', first_name = 'john', last_name = 'smith', ptype=ptype) assert student.first_name == 'john' student.save() st2 = Student(student_id = '3025768', first_name = 'bill', last_name = 'smith', ptype=ptype) st2.save() bill = Student.objects.get(student_id='3025768') assert bill.first_name == 'bill'
def test_student_model_modified(self): """Make sure the student model auto_add_now works as expected for the created field""" ptype = PatronType.objects.get(ptype='1') student = Student(student_id = '3025768', first_name = 'john', last_name = 'smith', ptype=ptype) student.save() t1 = student.created st2 = Student(student_id = '3025768', first_name = 'bill', last_name = 'smith', ptype=ptype, created=t1) st2.save() bill = Student.objects.get(student_id='3025768') assert bill.modified > t1
def normalize_students(ptype=None): """Normalize the data from the import file. Pre: multiple records with the same student_id, one for each different email and phone. Post: Email and Phone have been saved to related records and student_id is unique""" if not ptype: students = ProtoStudent.objects.values('student_id') else: subset = ProtoStudent.objects.filter(ptype=ptype) students = subset.values('student_id') stlist = [s['student_id'] for s in students] stlist = list(set(stlist)) stlist.sort() for student in stlist: recs = ProtoStudent.objects.filter(student_id=student) if not ptype: # if we're not specifying a ptype, it is possible that # recs contains records with different patron types rank = recs.aggregate(rank=Max('ptype__rank'))['rank'] ptype = recs.filter(ptype__rank=rank)[0].ptype proto = recs[0] barcodes = ProtoBarcode.objects.filter(person_num=student).aggregate(Max('barcode')) barcode = barcodes['barcode__max'] phones = [(r.telephone_type, r.telephone1) for r in recs if not r.telephone1.isspace()] emails = [(r.email_type, r.email_address, r.preferred_email) for r in recs] phones = list(set(phones)) emails = list(set(emails)) # Construct MARC record base s = Student() s.student_id = student s.first_name = proto.first_name s.last_name = proto.last_name s.street_address = proto.street_address s.city = proto.city s.province = proto.province s.postal_code = proto.postal_code log.debug("%s, %s" % (student,barcode)) try: s.telephone_1 = phones[0][1] s.telephone_2 = phones[1][1] except IndexError: if s.telephone_1 is None: s.telephone_1 = u'' if s.telephone_2 is None: s.telephone_2 = u'' for t,e,p in emails: if t.upper() == 'UWE': uwe = e s.email_address = uwe s.barcode = barcode s.ptype = ptype try: s.clean_fields(exclude=['created']) except ValidationError, e: for key in e.message_dict: log.info("%s: %s" % (key, e.message_dict[key],)) s.save()
def test_student_model_insert(self): """Make sure the student model insert works as expected""" ptype = PatronType.objects.get(ptype='1') student = Student(student_id = '3025768', first_name = 'john', last_name = 'smith', ptype=ptype) assert student.first_name == 'john' student.save()
def normalize_students(ptype=None): """Normalize the data from the import file. Pre: multiple records with the same student_id, one for each different email and phone. Post: Email and Phone have been saved to related records and student_id is unique""" if not ptype: students = ProtoStudent.objects.values('student_id') else: subset = ProtoStudent.objects.filter(ptype=ptype) students = subset.values('student_id') stlist = [s['student_id'] for s in students] stlist = list(set(stlist)) stlist.sort() for student in stlist: recs = ProtoStudent.objects.filter(student_id=student) if not ptype: # if we're not specifying a ptype, it is possible that # recs contains records with different patron types rank = recs.aggregate(rank=Max('ptype__rank'))['rank'] ptype = recs.filter(ptype__rank=rank)[0].ptype proto = recs[0] barcodes = ProtoBarcode.objects.filter(person_num=student).aggregate( Max('barcode')) barcode = barcodes['barcode__max'] phones = [(r.telephone_type, r.telephone1) for r in recs if not r.telephone1.isspace()] emails = [(r.email_type, r.email_address, r.preferred_email) for r in recs] phones = list(set(phones)) emails = list(set(emails)) # Construct MARC record base s = Student() s.student_id = student s.first_name = proto.first_name s.last_name = proto.last_name s.street_address = proto.street_address s.city = proto.city s.province = proto.province s.postal_code = proto.postal_code log.debug("%s, %s" % (student, barcode)) try: s.telephone_1 = phones[0][1] s.telephone_2 = phones[1][1] except IndexError: if s.telephone_1 is None: s.telephone_1 = u'' if s.telephone_2 is None: s.telephone_2 = u'' for t, e, p in emails: if t.upper() == 'UWE': uwe = e s.email_address = uwe s.barcode = barcode s.ptype = ptype try: s.clean_fields(exclude=['created']) except ValidationError, e: for key in e.message_dict: log.info("%s: %s" % ( key, e.message_dict[key], )) s.save()