Esempio n. 1
0
    def handle(self, *args, **options):
        host = options.get('host', None)
        port = options.get('port', None)
        user = options.get('user', None)
        password = options.get('password', None)
        encoding = options.get('encoding', 'cp949')
        studentid = options.get('studentid', None)
        try:
            if password is None:
                password = getpass.getpass()
        except (KeyboardInterrupt, EOFError):
            print
            return

        if studentid is None:
            query = 'SELECT * FROM view_report_e_degree_k'
        else:
            query = 'SELECT * FROM view_report_e_degree_k WHERE student_no=%d' % int(
                studentid)
        user_dept = execute(host, port, user, password, query)

        if studentid is None:
            query = 'SELECT * FROM view_kds_students_other_major'
        else:
            query = 'SELECT * FROM view_kds_students_other_major WHERE student_no=%d' % int(
                studentid)
        user_major_minor = execute(host, port, user, password, query)

        for a in user_dept:
            user = UserProfile.objects.filter(student_id=a[0])
            try:
                dept = Department.objects.get(id=a[1])
            except ObjectDoesNotExist:
                if len(user) > 0:
                    print("No department with id %d\n%s" % (a[1], a))
                continue

            for u in user:
                u.department = dept
                u.save()

        for a in user_major_minor:
            user = UserProfile.objects.filter(student_id=a[0])
            dept = Department.objects.filter(name=a[2].decode("cp949"))

            for u in user:
                for d in dept:
                    if a[1].decode('cp949') == u"부전공신청":
                        u.minors.add(d)
                    elif a[1].decode('cp949') == u"복수전공신청":
                        u.majors.add(d)
                    else:
                        print("Major/minor type not matching : " %
                              (a.decode('cp959')))
    def handle(self, *args, **options):
        print("start")
        host = options.get("host", None)
        port = options.get("port", None)
        user = options.get("user", None)
        password = options.get("password", None)
        student_no = options.get("student_no", None)
        try:
            if password is None:
                password = getpass.getpass()
        except (KeyboardInterrupt, EOFError):
            print()
            return

        if not UserProfile.objects.filter(student_id=student_no).exists():
            return

        query = "SELECT * FROM view_OTL_attend WHERE student_no = %s" % student_no
        rows = execute(host, port, user, password, query)

        cleared_semester_list = []

        userprofile = UserProfile.objects.filter(student_id=student_no).first()
        lectures = Lecture.objects.filter(deleted=False)
        for a in rows:
            if (a[0], a[1]) not in cleared_semester_list:
                cleared_semester_list.append((a[0], a[1]))
                userprofile.taken_lectures.remove(*userprofile.taken_lectures.filter(year=a[0], semester=a[1]))
            try:
                lecture = lectures.get(year=a[0], semester=a[1], code=a[2], class_no=a[3].strip(), deleted=False)
                userprofile.taken_lectures.add(lecture)
            except (Lecture.DoesNotExist, Lecture.MultipleObjectsReturned) as exception:
                print(f"error on getting lecture for {str(a[0])} {str(a[1])} {a[2]} {a[3]}", file=sys.stderr)
                print(exception, file=sys.stderr)
    def handle(self, *args, **options):
        print "start"
        host = options.get('host', None)
        port = options.get('port', None)
        user = options.get('user', None)
        password = options.get('password', None)
        encoding = options.get('encoding', 'cp949')
        student_no = options.get('student_no', None)
        try:
            if password is None:
                password = getpass.getpass()
        except (KeyboardInterrupt, EOFError):
            print
            return

        if not UserProfile.objects.filter(student_id=student_no).exists():
            return

        query = 'SELECT * FROM view_OTL_attend WHERE student_no = %s' % student_no
        rows = execute(host, port, user, password, query)

        cleared_semester_list = []

        user = UserProfile.objects.filter(student_id=student_no).first()
        lectures = Lecture.objects.filter(deleted=False)
        for a in rows:
            if (a[0], a[1]) not in cleared_semester_list:
                cleared_semester_list.append((a[0], a[1]))
                user.take_lecture_list.remove(
                    *user.take_lecture_list.filter(year=a[0], semester=a[1]))
            lecture = lectures.filter(year=a[0],
                                      semester=a[1],
                                      code=a[2],
                                      class_no=a[3].strip())
            if len(lecture) == 1:
                user.take_lecture_list.add(lecture[0])
            else:
                print >> sys.stderr, str(a[0]) + " " + str(
                    a[1]) + " " + a[2] + " " + a[3] + "는 왜 개수가 " + str(
                        len(lecture)) + " 지?"
Esempio n. 4
0
    def handle(self, *args, **options):
        host = options.get('host', None)
        port = options.get('port', None)
        user = options.get('user', None)
        password = options.get('password', None)
        encoding = options.get('encoding', 'cp949')

        if options['year']!=None and options['semester']!=None:
            year = int(options['year'])
            semester = int(options['semester'])
        else:
            year = settings.CURRENT_YEAR
            semester = settings.CURRENT_SEMESTER

        try:
            if password is None:
                password = getpass.getpass()
        except (KeyboardInterrupt, EOFError):
            print
            return

        print year, semester
        query = 'SELECT * FROM view_OTL_attend WHERE lecture_year = %d AND lecture_term = %d' % (year, semester)
        rows = execute(host, port, user, password, query)

        cleared_user_list = []

        lectures = Lecture.objects.filter(year = year, semester = semester,deleted=False)
        for a in rows:
            users = UserProfile.objects.filter(student_id = a[5])
            for user in users:
                if user not in cleared_user_list:
                    cleared_user_list.append(user)
                    user.take_lecture_list.remove(*user.take_lecture_list.filter(year=year, semester=semester))
                lecture = lectures.filter(code = a[2], class_no = a[3].strip())
                if len(lecture) == 1:
                    user.take_lecture_list.add(lecture[0])
                else:
                    print>>sys.stderr, str(a[0]) + " " + str(a[1]) + " " + a[2] + " " + a[3] + "는 왜 개수가 " + str(len(lecture)) + " 지?"
    def _import_taken_lecture(self, target_year, target_semester,
                              db_specification):
        print(target_year, target_semester)

        host = db_specification["host"]
        port = db_specification["port"]
        user = db_specification["user"]
        password = db_specification["password"]
        encoding = db_specification["encoding"]

        query = "SELECT * FROM view_OTL_attend WHERE lecture_year = %d AND lecture_term = %d" % (
            target_year, target_semester)
        rows = execute(host, port, user, password, query)

        cleared_user_list = []

        lectures = Lecture.objects.filter(year=target_year,
                                          semester=target_semester,
                                          deleted=False)
        for a in rows:
            users = UserProfile.objects.filter(student_id=a[5])
            for u in users:
                if u not in cleared_user_list:
                    cleared_user_list.append(u)
                    u.taken_lectures.remove(*u.taken_lectures.filter(
                        year=target_year, semester=target_semester))
                try:
                    lecture = lectures.get(code=a[2],
                                           class_no=a[3].strip(),
                                           deleted=False)
                    u.taken_lectures.add(lecture)
                except (Lecture.DoesNotExist,
                        Lecture.MultipleObjectsReturned) as exception:
                    print(
                        f"error on getting lecture for {str(a[0])} {str(a[1])} {a[2]} {a[3]}",
                        file=sys.stderr)
                    print(exception, file=sys.stderr)
Esempio n. 6
0
    def handle(self, *args, **options):
        host = options.get("host", None)
        port = options.get("port", None)
        user = options.get("user", None)
        password = options.get("password", None)
        encoding = options.get("encoding")
        all_ = options.get("all", None)
        student_id = options.get("studentid", None)
        try:
            if password is None:
                password = getpass.getpass()
        except (KeyboardInterrupt, EOFError):
            print()
            return

        if all_:
            query = "SELECT * FROM view_report_e_degree_k"
        elif student_id is not None:
            query = "SELECT * FROM view_report_e_degree_k WHERE student_no=%d" % int(
                student_id)
        else:
            print(
                "Target user not specified. Use argument [--studentid STUDENTID] or [--all]."
            )
            return
        user_dept = execute(host, port, user, password, query)

        if all_:
            query = "SELECT * FROM view_kds_students_other_major"
        elif student_id is not None:
            query = "SELECT * FROM view_kds_students_other_major WHERE student_no=%d" % int(
                student_id)
        else:
            print(
                "Target user not specified. Use argument [--studentid STUDENTID] or [--all]."
            )
            return
        user_major_minor = execute(host, port, user, password, query)

        for a in user_dept:
            profile_matches = UserProfile.objects.filter(student_id=a[0])
            try:
                department = Department.objects.get(id=a[1])
            except Department.DoesNotExist:
                if len(profile_matches) > 0:
                    print("No department with id %d\n%s" % (a[1], a))
                continue

            for user in profile_matches:
                user.department = department
                user.save()

        for a in user_major_minor:
            profile_matches = UserProfile.objects.filter(student_id=a[0])
            departments = Department.objects.filter(name=a[2])

            for user in profile_matches:
                for d in departments:
                    application_type = a[1]
                    if application_type == "부전공신청":
                        user.minors.add(d)
                    elif application_type == "복수전공신청":
                        user.majors.add(d)
                    else:
                        print("Major/minor type not matching : " %
                              (a.decode("cp959")))
Esempio n. 7
0
    def _import_scholardb(self, target_year, target_semester, exclude_lecture,
                          db_specification):
        print(f"Importing scholardb for {target_year}-{target_semester}")

        host = db_specification["host"]
        port = db_specification["port"]
        user = db_specification["user"]
        password = db_specification["password"]
        encoding = db_specification["encoding"]

        lecture_count = 0

        def _extract_department_code(lecture_old_code):
            return re.compile(r"([a-zA-Z]+)(\d+)").match(
                lecture_old_code).group(1)

        if not exclude_lecture:
            professor_lecture_charge_query = "SELECT * FROM view_OTL_charge WHERE lecture_year = %d AND lecture_term = %d" % (
                target_year,
                target_semester,
            )
            professor_lecture_charge_rows = execute(
                host, port, user, password, professor_lecture_charge_query)

            lecture_query = "SELECT * FROM view_OTL_lecture WHERE lecture_year = %d AND lecture_term = %d ORDER BY dept_id" % (
                target_year,
                target_semester,
            )
            lecture_rows = execute(host, port, user, password, lecture_query)
            departments = {}
            lectures_not_updated = set()

            for lecture in Lecture.objects.filter(year=target_year,
                                                  semester=target_semester):
                lectures_not_updated.add(lecture.id)
            # Make Staff Professor with ID 830
            staff_professor, _ = Professor.objects.get_or_create(
                professor_id=Professor.STAFF_ID,
                defaults={
                    "professor_name": "Staff",
                    "professor_name_en": "Staff",
                })

            prev_department = None
            for row in lecture_rows:
                myrow = row[:]

                # Extract department info.
                lecture_no = myrow[2]
                lecture_code = myrow[20]
                lecture_class_no = myrow[3].strip()
                department_no = lecture_no[0:2]
                department_id = int(myrow[4])
                department_code = _extract_department_code(lecture_code)

                # Update department info.
                if prev_department != department_id:
                    new_flag = False
                    try:
                        department = Department.objects.get(id=department_id)
                        print(f"Updating department: {department}")
                    except Department.DoesNotExist:
                        department = Department(id=department_id)
                        new_flag = True
                        print(
                            f"Adding department: {department_code}({department_id})..."
                        )
                    department.num_id = department_no
                    department.code = department_code
                    department.name = myrow[5]
                    department.name_en = myrow[6]
                    department.save()

                    if new_flag:
                        departments = Department.objects.filter(
                            code=department_code, visible=True)
                        for dept in departments:
                            if dept.id != department.id:
                                dept.visible = False
                                dept.save()

                prev_department = department_id

                # Extract lecture info.
                # try:
                # print 'Retrieving %s: %s [%s]...' % (lecture_code, myrow[7].encode('utf-8'), lecture_class_no)
                # except UnicodeDecodeError:
                # print 'Retrieving %s: ??? [%s]...' % (lecture_code, lecture_class_no)
                # myrow[7] = u'???'
                lecture_key = {
                    "code": lecture_no,
                    "year": int(myrow[0]),
                    "semester": int(myrow[1]),
                    "deleted": False,
                    "class_no": lecture_class_no,
                }
                # Convert the key to a hashable object
                lecture_key_hashable = -1
                try:
                    lecture = Lecture.objects.get(**lecture_key)
                    lecture_key_hashable = lecture.id
                    print(f"Updating existing lecture {lecture}")
                except Lecture.DoesNotExist:
                    lecture = Lecture(**lecture_key)
                    lecture.num_people = 0
                    print(f"Creating new lecture {lecture}")

                # Update lecture info.
                lecture.department = department
                lecture.old_code = myrow[20]
                lecture.title = myrow[7]
                lecture.title_en = myrow[8]
                lecture.type = myrow[10]  # 과목구분 (한글)
                lecture.type_en = myrow[11]  # 과목구분 (영문)
                lecture.audience = int(myrow[12])  # 학년 구분
                lecture.limit = myrow[17]  # 인원제한
                lecture.credit = myrow[16]  # 학점
                lecture.credit_au = myrow[13]  # AU
                lecture.num_classes = int(myrow[14])  # 강의시수
                lecture.num_labs = int(myrow[15])  # 실험시수
                """
                if myrow[19] != None and len(myrow[19]) >= 190:
                    myrow[19] = myrow[19][:190]
                lecture.notice = myrow[19]          # 비고
                """
                lecture.is_english = True if myrow[
                    21] == "Y" else False  # 영어강의 여부
                lecture.deleted = False
                # Course save
                try:
                    course = Course.objects.get(old_code=lecture.old_code)
                    course.department = department
                    course.type = lecture.type
                    course.type_en = lecture.type_en
                    course.title = lecture.title.split("<")[0].split("[")[0]
                    course.title_en = lecture.title_en.split("<")[0].split(
                        "[")[0]
                    course.save()
                #                    print "Updating Course ... %s" % course.title
                except Course.DoesNotExist:
                    course = Course()
                    course.old_code = lecture.old_code
                    course.department = department
                    course.type = lecture.type
                    course.type_en = lecture.type_en
                    course.title = lecture.title.split("<")[0].split("[")[0]
                    course.title_en = lecture.title_en.split("<")[0].split(
                        "[")[0]

                    course.grade_average = 0.0
                    course.load_average = 0.0
                    course.speech_average = 0.0
                    # course total score
                    """
                    course.score_average = 0
                    course.load_average = 0
                    course.gain_average = 0
                    """
                    course.save()
                #                    print "Making new Course ... %s" % course.title
                lecture.course = course
                lecture.save()
                lecture_count += 1
                # professor save
                match_scholar = list(
                    filter(
                        lambda p:
                        (lecture.year == p[0] and lecture.semester == p[1] and
                         lecture.code == p[2] and lecture.class_no.strip(
                         ) == p[3].strip() and lecture.department_id == p[4]),
                        professor_lecture_charge_rows,
                    ))
                if len(match_scholar) != 0:
                    professors_not_updated = set()
                    for prof in lecture.professors.all():
                        professors_not_updated.add(prof.id)
                    for i in match_scholar:
                        try:
                            prof_id = i[5]
                            prof_name = i[6]
                            if i[8] is None or i[8] == "":
                                prof_name_en = ""
                            else:
                                prof_name_en = i[8].strip()
                            if i[4] is None or i[4] == "":
                                prof_major = ""
                            else:
                                prof_major = i[4]
                            professor = Professor.objects.get(
                                professor_id=prof_id)
                            if professor.professor_name != prof_name and prof_id != Professor.STAFF_ID:
                                professor.professor_name = prof_name
                                professor.save()
                            if professor.professor_name_en != prof_name_en and prof_id != Professor.STAFF_ID and prof_name_en != "":
                                professor.professor_name_en = prof_name_en
                                professor.save()
                            if professor.major != prof_major and prof_id != Professor.STAFF_ID:
                                professor.major = prof_major
                                professor.save()
                            professors_not_updated.remove(professor.id)
                        except Professor.DoesNotExist:
                            professor = Professor.objects.create(
                                professor_id=prof_id)
                            professor.professor_name = prof_name
                            professor.professor_name_en = prof_name_en
                            professor.major = prof_major
                            professor.save()
                        #                            print "Making new Professor ... %s" % professor.professor_name
                        except KeyError:
                            pass
                        lecture.professors.add(professor)
                        if professor.professor_id != Professor.STAFF_ID:
                            lecture.course.professors.add(professor)

                    for key in professors_not_updated:
                        professor = Professor.objects.get(id=key)
                        lecture.professors.remove(professor)
                else:
                    lecture.professors.add(staff_professor)

                try:
                    lectures_not_updated.remove(lecture_key_hashable)
                except KeyError:
                    pass

        # Extract exam-time, class-time info.

        print("Extracting exam time information...")
        exam_time_query = "SELECT * FROM view_OTL_exam_time WHERE lecture_year = %d AND lecture_term = %d" % (
            target_year,
            target_semester,
        )
        exam_time_rows = execute(host, port, user, password, exam_time_query)
        print("exam_times")
        ExamTime.objects.filter(lecture__year__exact=target_year,
                                lecture__semester=target_semester).delete()
        for row in exam_time_rows:
            print(row)
            myrow = row[:]
            lecture_key = {
                "deleted": False,
                "code": myrow[2],
                "year": int(myrow[0]),
                "semester": int(myrow[1]),
                "department": Department.objects.filter(id=int(myrow[4]))[0],
                "class_no": myrow[3].strip(),
            }
            try:
                lecture = Lecture.objects.get(**lecture_key)
                exam_time = ExamTime(lecture=lecture)
                exam_time.day = int(myrow[5]) - 1
                exam_time.begin = time(hour=myrow[6].hour,
                                       minute=myrow[6].minute)
                exam_time.end = time(hour=myrow[7].hour,
                                     minute=myrow[7].minute)
                print(f"Updating exam time for {lecture}")
                exam_time.save()
            except Lecture.DoesNotExist:
                print(
                    f"Exam-time for non-existing lecture {myrow[2]}; skip it..."
                )

        # Extract class time.

        print("Extracting class time information...")
        class_time_query = "SELECT * FROM view_OTL_time WHERE lecture_year = %d AND lecture_term = %d" % (
            target_year, target_semester)
        class_time_rows = execute(host, port, user, password, class_time_query)
        # print class_times
        ClassTime.objects.filter(lecture__year__exact=target_year,
                                 lecture__semester=target_semester).delete()
        for row in class_time_rows:
            print(row)
            myrow = row[:]
            lecture_key = {
                "deleted": False,
                "code": myrow[2],
                "year": int(myrow[0]),
                "semester": int(myrow[1]),
                "department": Department.objects.filter(id=int(myrow[4]))[0],
                "class_no": myrow[3].strip(),
            }
            try:
                print(myrow)
                lecture = Lecture.objects.get(**lecture_key)
                class_time = ClassTime(lecture=lecture)
                class_time.day = int(myrow[5]) - 1
                class_time.begin = time(hour=myrow[6].hour,
                                        minute=myrow[6].minute)
                class_time.end = time(hour=myrow[7].hour,
                                      minute=myrow[7].minute)
                class_time.type = myrow[8]
                class_time.building_id = myrow[9]
                class_time.room_name = myrow[10]
                class_time.building_full_name = myrow[12]
                class_time.building_full_name_en = myrow[13]
                try:
                    class_time.unit_time = int(myrow[11])
                except (ValueError, TypeError):
                    class_time.unit_time = 0
                print(f"Updating class time for {lecture}")
                class_time.save()
            except Lecture.DoesNotExist:
                print(
                    f"Class-time for non-existing lecture {myrow[2]}; skip it..."
                )

        # Extract Syllabus info.
        """
        syllabus_query = 'SELECT * FROM view_OTL_syllabus WHERE lecture_year = %d AND lecture_term = %d'
            % (target_year, target_semester)
        syllabus_rows = execute(host, port, user, password, syllabus_query)
        Syllabus.objects.filter(lecture__year__exact=target_year, lecture__semester=target_semester).delete()

        for row in syllabus_rows:
            myrow = row[:]
            lecture_key = {
                'code': myrow[2],
                'year': int(myrow[0]),
                'semester': int(myrow[1]),
                'department': Department.objects.filter(id = int(myrow[4]))[0],
                'class_no': myrow[3].strip(),
            }
            try:
                lecture = Lecture.objects.get(**lecture_key)
                syllabus = Syllabus(lecture=lecture)
                syllabus.professor_info = myrow[5]
                syllabus.abstract = myrow[6]
                syllabus.evluation = myrow[7]
                syllabus.materials = myrow[8]
                syllabus.plan = myrow[9]
                syllabus.etc = myrow[10]
                syllabus.url = myrow[11]
                syllabus.attachment = myrow[12]

                print 'Updating syllabus information for %s' % lecture
                syllabus.save()
            except Lecture.DoesNotExist:
                print 'Syllabus information for non-existing lecture %s; skip it...' % myrow[2]
        """
        if not exclude_lecture:
            # Mark deleted lectures to notify users.
            print("Marking deleted lectures...")
            for key in lectures_not_updated:
                lecture = Lecture.objects.get(id=key)
                lecture.deleted = True
                #                print '%s is marked as deleted...' % lecture
                lecture.save()

        print(f"\nTotal number of departments : {Department.objects.count()}")
        print(f"Total number of lectures newly added : {lecture_count}")
Esempio n. 8
0
    def handle(self, *args, **options):
        rx_dept_code = re.compile(ur'([a-zA-Z]+)(\d+)')
        host = options.get('host', None)
        port = options.get('port', None)
        user = options.get('user', None)
        password = options.get('password', None)
        encoding = options.get('encoding', 'cp949')
        exclude_lecture = options.get('exclude_lecture', False)
        lecture_count = 0

        if options['year'] != None and options['semester'] != None:
            next_year = int(options['year'])
            next_semester = int(options['semester'])
        else:
            next_year = settings.CURRENT_YEAR
            next_semester = settings.CURRENT_SEMESTER

        try:
            if password is None:
                password = getpass.getpass()
        except (KeyboardInterrupt, EOFError):
            print
            return

        if not exclude_lecture:
            query = 'SELECT * FROM view_OTL_charge WHERE lecture_year = %d AND lecture_term = %d' % (
                next_year, next_semester)
            professors = execute(host, port, user, password, query)

            query = 'SELECT * FROM view_OTL_lecture WHERE lecture_year = %d AND lecture_term = %d ORDER BY dept_id' % (
                next_year, next_semester)
            rows = execute(host, port, user, password, query)
            departments = {}
            lectures_not_updated = set()

            for lecture in Lecture.objects.filter(year=next_year,
                                                  semester=next_semester):
                lectures_not_updated.add(lecture.id)
            # Make Staff Professor with ID 830
            try:
                staff_professor = Professor.objects.get(professor_id=830)
            except Professor.DoesNotExist:
                staff_professor = Professor.objects.create(professor_id=830)
                staff_professor.professor_name = 'Staff'
                staff_professor.professor_name_en = 'Staff'
                staff_professor.save()

            prev_department = None
            for row in rows:
                myrow = []
                for elem in row:
                    if isinstance(elem, str):
                        try:
                            elem = elem.decode(encoding)
                        except UnicodeDecodeError:
                            elem = u'%s (???)' % row[20]
                            print >> sys.stderr, 'ERROR: parsing error on lecture %s' % row[
                                20]
                            print >> sys.stderr, '       cannot read "%s" in cp949.' % elem
                    myrow.append(elem)

                # Extract department info.
                lecture_no = myrow[2]
                lecture_code = myrow[20]
                lecture_class_no = myrow[3].strip()
                department_no = lecture_no[0:2]
                department_id = int(myrow[4])
                department_code = rx_dept_code.match(lecture_code).group(1)

                # Update department info.
                if prev_department != department_id:
                    new_flag = False
                    try:
                        department = Department.objects.get(id=department_id)
                        print 'Updating department: %s' % department
                    except Department.DoesNotExist:
                        department = Department(id=department_id)
                        new_flag = True
                        print 'Adding department: %s(%d)...' % (
                            department_code, department_id)
                    department.num_id = department_no
                    department.code = department_code
                    department.name = myrow[5]
                    department.name_en = myrow[6]
                    department.save()

                    if new_flag:
                        departments = Department.objects.filter(
                            code=department_code, visible=True)
                        for dept in departments:
                            if dept.id != department.id:
                                dept.visible = False
                                dept.save()

                prev_department = department_id

                # Extract lecture info.
                #try:
                #print 'Retreiving %s: %s [%s]...' % (lecture_code, myrow[7].encode('utf-8'), lecture_class_no)
                #except UnicodeDecodeError:
                #print 'Retreiving %s: ??? [%s]...' % (lecture_code, lecture_class_no)
                #myrow[7] = u'???'
                lecture_key = {
                    'code': lecture_no,
                    'year': int(myrow[0]),
                    'semester': int(myrow[1]),
                    'deleted': False,
                    'class_no': lecture_class_no,
                }
                # Convert the key to a hashable object
                lecture_key_hashable = -1
                try:
                    lecture = Lecture.objects.get(**lecture_key)
                    lecture_key_hashable = lecture.id
                    print 'Updating existing lecture...'
                except Lecture.DoesNotExist:
                    lecture = Lecture(**lecture_key)
                    lecture.num_people = 0
                    print 'Creating new lecture...'

                # Update lecture info.
                lecture.department = department
                lecture.old_code = myrow[20]
                lecture.title = myrow[7]
                lecture.title_en = myrow[8]
                lecture.type = myrow[10]  # 과목구분 (한글)
                lecture.type_en = myrow[11]  # 과목구분 (영문)
                lecture.audience = int(myrow[12])  # 학년 구분
                lecture.limit = myrow[17]  # 인원제한
                lecture.credit = myrow[16]  # 학점
                lecture.credit_au = myrow[13]  # AU
                lecture.num_classes = int(myrow[14])  # 강의시수
                lecture.num_labs = int(myrow[15])  # 실험시수
                '''
                if myrow[19] != None and len(myrow[19]) >= 190:
                    myrow[19] = myrow[19][:190]
                lecture.notice = myrow[19]          # 비고
                '''
                lecture.is_english = True if myrow[
                    21] == 'Y' else False  # 영어강의 여부
                lecture.deleted = False
                # Course save
                try:
                    course = Course.objects.get(old_code=lecture.old_code)
                    course.department = department
                    course.type = lecture.type
                    course.type_en = lecture.type_en
                    course.title = lecture.title.split("<")[0].split("[")[0]
                    course.title_en = lecture.title_en.split("<")[0].split(
                        "[")[0]
                    course.save()
#                    print "Updating Course ... %s" % course.title
                except Course.DoesNotExist:
                    course = Course()
                    course.old_code = lecture.old_code
                    course.department = department
                    course.type = lecture.type
                    course.type_en = lecture.type_en
                    course.title = lecture.title.split("<")[0].split("[")[0]
                    course.title_en = lecture.title_en.split("<")[0].split(
                        "[")[0]

                    course.grade_average = 0.0
                    course.load_average = 0.0
                    course.speech_average = 0.0
                    course.total_average = 0.0
                    ################################### course total score
                    '''
                    course.score_average = 0
                    course.load_average = 0
                    course.gain_average = 0
                    '''
                    course.save()
#                    print "Making new Course ... %s" % course.title
                lecture.course = course
                lecture.save()
                lecture_count += 1
                # professor save
                match_scholar = filter(
                    lambda a: lecture.year == a[0] and lecture.semester == a[
                        1] and lecture.code == a[2] and lecture.class_no.strip(
                        ) == a[3].strip() and lecture.department_id == a[4],
                    professors)
                if len(match_scholar) != 0:
                    professors_not_updated = set()
                    for prof in lecture.professor.all():
                        professors_not_updated.add(prof.id)
                    for i in match_scholar:
                        try:
                            prof_id = i[5]
                            prof_name = unicode(i[6], 'cp949')
                            if i[8] is None or i[8] == '':
                                prof_name_en = ''
                            else:
                                prof_name_en = unicode(i[8].strip(), 'cp949')
                            if i[4] is None or i[4] == '':
                                prof_major = ''
                            else:
                                prof_major = i[4]
                            professor = Professor.objects.get(
                                professor_id=prof_id)
                            if professor.professor_name != prof_name and prof_id != 830:
                                professor.professor_name = prof_name
                                professor.save()
                            if professor.professor_name_en != prof_name_en and prof_id != 830 and prof_name_en != '':
                                professor.professor_name_en = prof_name_en
                                professor.save()
                            if professor.major != prof_major and prof_id != 830:
                                professor.major = prof_major
                                professor.save()
                            professors_not_updated.remove(professor.id)
                        except Professor.DoesNotExist:
                            professor = Professor.objects.create(
                                professor_id=prof_id)
                            professor.professor_name = prof_name
                            professor.professor_name_en = prof_name_en
                            professor.major = prof_major
                            professor.save()
#                            print "Making new Professor ... %s" % professor.professor_name
                        except KeyError:
                            pass
                        lecture.professor.add(professor)
                        if professor.professor_id != 830:
                            lecture.course.professors.add(professor)

                    for key in professors_not_updated:
                        professor = Professor.objects.get(id=key)
                        lecture.professor.remove(professor)
                else:
                    lecture.professor.add(staff_professor)

                try:
                    lectures_not_updated.remove(lecture_key_hashable)
                except KeyError:
                    pass

        # Extract exam-time, class-time info.

        print 'Extracting exam time information...'
        query = 'SELECT * FROM view_OTL_exam_time WHERE lecture_year = %d AND lecture_term = %d' % (
            next_year, next_semester)
        exam_times = execute(host, port, user, password, query)
        print exam_times
        ExamTime.objects.filter(lecture__year__exact=next_year,
                                lecture__semester=next_semester).delete()
        for row in exam_times:
            print row
            myrow = []
            for elem in row:
                if isinstance(elem, str):
                    try:
                        elem = elem.decode(encoding)
                    except UnicodeDecodeError:
                        elem = u'???'
                        print >> sys.stderr, 'ERROR: parsing error on lecture. cannot read in cp949.'
                myrow.append(elem)
            lecture_key = {
                'deleted': False,
                'code': myrow[2],
                'year': int(myrow[0]),
                'semester': int(myrow[1]),
                'department': Department.objects.filter(id=int(myrow[4]))[0],
                'class_no': myrow[3].strip(),
            }
            try:
                lecture = Lecture.objects.get(**lecture_key)
                exam_time = ExamTime(lecture=lecture)
                exam_time.day = int(myrow[5]) - 1
                exam_time.begin = time(hour=myrow[6].hour,
                                       minute=myrow[6].minute)
                exam_time.end = time(hour=myrow[7].hour,
                                     minute=myrow[7].minute)
                print 'Updating exam time for %s' % lecture
                exam_time.save()
            except Lecture.DoesNotExist:
                print 'Exam-time for non-existing lecture %s; skip it...' % myrow[
                    2]

        # Extract class time.

        print 'Extracting class time information...'
        query = 'SELECT * FROM view_OTL_time WHERE lecture_year = %d AND lecture_term = %d' % (
            next_year, next_semester)
        class_times = execute(host, port, user, password, query)
        # print class_times
        ClassTime.objects.filter(lecture__year__exact=next_year,
                                 lecture__semester=next_semester).delete()
        for row in class_times:
            print row
            myrow = []
            for elem in row:
                if isinstance(elem, str):
                    try:
                        elem = elem.decode(encoding)
                    except UnicodeDecodeError:
                        elem = u'???'
                        print >> sys.stderr, 'ERROR: parsing error on lecture. cannot read in cp949.'
                myrow.append(elem)
            lecture_key = {
                'deleted': False,
                'code': myrow[2],
                'year': int(myrow[0]),
                'semester': int(myrow[1]),
                'department': Department.objects.filter(id=int(myrow[4]))[0],
                'class_no': myrow[3].strip(),
            }
            try:
                print(myrow)
                lecture = Lecture.objects.get(**lecture_key)
                class_time = ClassTime(lecture=lecture)
                class_time.day = int(myrow[5]) - 1
                class_time.begin = time(hour=myrow[6].hour,
                                        minute=myrow[6].minute)
                class_time.end = time(hour=myrow[7].hour,
                                      minute=myrow[7].minute)
                class_time.type = myrow[8]
                class_time.building = myrow[9]
                class_time.roomNum = myrow[10]
                class_time.roomName = myrow[12]
                class_time.roomName_en = myrow[13]
                try:
                    class_time.unit_time = int(myrow[11])
                except (ValueError, TypeError):
                    class_time.unit_time = 0
                print 'Updating class time for %s' % lecture
                class_time.save()
            except Lecture.DoesNotExist:
                print 'Class-time for non-existing lecture %s; skip it...' % myrow[
                    2]

        # Extract Syllabus info.
        '''
        query = 'SELECT * FROM view_OTL_syllabus WHERE lecture_year = %d AND lecture_term = %d' % (next_year, next_semester)
        syllabuses = execute(host, port, user, password, query)
        Syllabus.objects.filter(lecture__year__exact=next_year, lecture__semester=next_semester).delete()

        for row in syllabuses:
            myrow = []
            for elem in row:
                if isinstance(elem, str):
                    try:
                        elem = elem.decode(encoding)
                    except UnicodeDecodeError:
                        eleme = u'%s (???)' % row[2]
                        print>>sys.stderr, 'ERROR: parsing error on lecture %s' % row[2]
                        print>>sys.stderr, '       cannot read "%s" in cp949.' % elem
                myrow.append(elem)
            lecture_key = {
                'code': myrow[2],
                'year': int(myrow[0]),
                'semester': int(myrow[1]),
                'department': Department.objects.filter(id = int(myrow[4]))[0],
                'class_no': myrow[3].strip(),
            }
            try:
                lecture = Lecture.objects.get(**lecture_key)
                syllabus = Syllabus(lecture=lecture)
                syllabus.professor_info = myrow[5]
                syllabus.abstract = myrow[6]
                syllabus.evluation = myrow[7]
                syllabus.materials = myrow[8]
                syllabus.plan = myrow[9]
                syllabus.etc = myrow[10]
                syllabus.url = myrow[11]
                syllabus.attachment = myrow[12]

                print 'Updating syllabus information for %s' % lecture
                syllabus.save()
            except Lecture.DoesNotExist:
                print 'Syllabus information for non-existing lecture %s; skip it...' % myrow[2]
        '''
        if not exclude_lecture:
            # Mark deleted lectures to notify users.
            print 'Marking deleted lectures...'
            for key in lectures_not_updated:
                lecture = Lecture.objects.get(id=key)
                lecture.deleted = True
                #                print '%s is marked as deleted...' % lecture
                lecture.save()

        print '\nTotal number of departments : %d' % Department.objects.count()
        print 'Total number of lectures newly added : %d' % lecture_count