Ejemplo n.º 1
0
 def process_students(self, row, teacher):
     if not self.student: return
     first_name = row.get('first_name').strip()
     last_name = row.get('last_name').strip()
     gender = row.get('gender', 'F').strip()
     full_name = "{0}, {1}".format(last_name, first_name)
     if full_name in self.students:
         return self.students[full_name]
     try:
         student = Student.objects.get(first_name=first_name, last_name=last_name, teacher=teacher)
     except ObjectDoesNotExist as err:
         student = Student(
             first_name=first_name,
             last_name=last_name,
             teacher=teacher
         )
     finally:
         student.identifier = student.get_identifier
         student.gender = gender
         if self.save:
             try:
                 student.save()
             except Exception as err:
                 print '==== student.save.err [{0}]'.format(err)
         print '==== student.save [{0}]'.format(student)
     self.students[full_name] = student
     return student
Ejemplo n.º 2
0
    def handle(self, *args, **options):

        for key in ['all', 'save']:
            setattr(self, key, options.get(key, None))

        try:
            count = 0
            for filename in args:
                with open(filename, 'r') as csv_file:
                    reader = csv.DictReader(csv_file)
                    for row in reader:
                        teacher_name = regex.sub(r'\d+', '', row['teacher'])
                        grade = Grade.objects.get(grade=int(row['grade']))
                        try:
                            teacher = Teacher.objects.get(last_name__icontains=teacher_name, grade=grade)
                        except ObjectDoesNotExist as e:
                            print '==== teacher.exist.e [{0}][{1}][{2}]'.format(teacher_name, grade, e)
                            continue

                        try:
                            student = Student.objects.get(first_name=row['first_name'], last_name=row['last_name'], teacher=teacher)
                        except ObjectDoesNotExist as e:
                            student = Student(
                                first_name=row['first_name'],
                                last_name=row['last_name'],
                                gender=row['gender'],
                                teacher=teacher
                            )
                        finally:
                            student.identifier = student.get_identifier
                            student.gender = row['gender']
                            if self.save:
                                try:
                                    student.save()
                                    count += 1
                                except Exception as e:
                                    print '==== student.save.e [{0}]'.format(e)
                            print '==== student.save [{0}]'.format(student)


            self.stdout.write('Successfully Imported {0} Students from "{1}"'.format(count, filename))
        except Exception, e:
            raise CommandError('Could not parse file "{0}"'.format(e))
Ejemplo n.º 3
0
    def process_all(self, row):
        teacher_name = regex.sub(r'\d+', '', row.get('teacher'))
        try:
            grade = Grade.objects.get(grade=int(row.get('grade', '0')))
        except ObjectDoesNotExist as err:
            print '==== grade.exist.err [{0}][{1}]'.format(teacher_name, err)
            try:
                teacher = Teacher.objects.get(last_name=teacher_name)
            except:
                print '==== teacher.exist.err [{0}][{1}]'.format(teacher_name, err)
        else:
            try:
                teacher = Teacher.objects.get(last_name__icontains=teacher_name, grade=grade)
            except ObjectDoesNotExist as err:
                print '==== teacher.exist.err [{0}][{1}][{2}]'.format(teacher_name, grade, err)
                try:
                    teacher = Teacher.objects.get(last_name=teacher_name)
                except:
                    print '==== teacher.exist.err [{0}][{1}]'.format(teacher_name, err)

        try:
            student = Student.objects.get(first_name=row.get('first_name'), last_name=row.get('last_name'), teacher=teacher)
        except ObjectDoesNotExist as err:
            student = Student(
                first_name=row.get('first_name'),
                last_name=row.get('last_name'),
                gender=row.get('gender', 'F'),
                teacher=teacher
            )
        finally:
            student.identifier = student.get_identifier
            student.gender = row.get('gender', 'F')
            if self.save:
                try:
                    student.save()
                    count += 1
                except Exception as err:
                    print '==== student.save.err [{0}]'.format(err)
            print '==== student.save [{0}]'.format(student)
Ejemplo n.º 4
0
    def handle(self, *args, **options):

        for key in ['all', 'save']:
            option = options.get(key, None)
            if option:
                setattr(self, key, option)

        for filename in args:
            try:
                soup = BeautifulSoup(open(filename))
                teacher = None
                student = None
                temp    = None
                start_student = False
                count = 0
                for div in soup.find_all('div'):
                    span_1 = div.find('span')
                    if span_1 and span_1['class'][0] not in ['cls_005', 'cls_006', 'cls_007', 'cls_008']:
                        start_student = False
                        continue
                    if span_1 and span_1['class'][0] in ['cls_007', 'cls_008']:
                        start_student = True
                    if span_1 and span_1['class'][0] == 'cls_005':
                        match = regex.compile(r'(?P<total>(\d+)) Total (?P<type>(Males|Females|Students))')
                        match_total = match.match(span_1.string)
                        if match_total:
                            print '==== Total {1} [{0}]'.format(match_total.group('total'), match_total.group('type'))

                    ## Save Teacher Information ##
                    if span_1 and not start_student:
                        span_2 = span_1.next_sibling
                        if span_2 and span_2['class'][0] == 'cls_006':
                            teacher = self.create_teacher(teacher, span_2)
                            count = 1
                            continue
                        elif span_1['class'][0] == 'cls_006':
                            temp = self.create_teacher(None, span_1)
                            temp.room_number = temp.room_number
                            temp.grade = temp.grade
                            temp.save()
                            teacher = temp
                            continue
                        elif span_1.string == 'Teacher#':
                            if teacher:
                                print '==== Total Added [{0}]'.format(teacher.students.count())
                            teacher = Teacher()
                            count = -2
                        if count == 1:
                            room = div.find('span').string
                            match = regex.compile(r'Room#\s*(?P<room>(\d+))')
                            match_room = match.match(room)
                            if match_room:
                                teacher.room_number = match_room.group('room')
                            count = 2
                        if count == 4:
                            grade_number = div.find('span')
                            grade = Grade.objects.get(grade=grade_number.string)
                            teacher.grade = grade
                            if not temp:
                                teacher.save()
                        count += 1

                    if teacher and start_student:
                        ## Save Student Information ##
                        match = regex.compile('^\d+$')
                        if match.match(span_1.string):
                            count = 1
                        if count == 2:
                            student = Student()
                            student.last_name = span_1.string
                            student.teacher = teacher
                        if count == 3:
                            student.first_name = span_1.string
                            student.identifier = '{0}-{1}-{2}'.format(replace_space(student.first_name), replace_space(student.last_name), teacher.room_number)
                        if count == 4:
                            if len(span_1.string) == 1:
                                student.gender = span_1.string
                                if self.save:
                                    student.save()
                        if count == 5:
                            student.gender = span_1.string
                            if self.save:
                                student.save()
                            print '==== student [{0}][{1}]'.format(student, student.gender)
                        count += 1
                print '==== Total Added [{0}]'.format(teacher.students.count())
                self.stdout.write('Successfully Imported Data for "{0}"'.format(filename))
            except Exception, e:
                raise CommandError('Could not parse file "{0}"'.format(e))