コード例 #1
0
def course_grab_start(request):
    grabber = cache.get(request.session.session_key + '_grabber')
    if grabber:
        try:
            grabber.setUp(**request.POST.dict())
        except LookupError:
            return {'error_code': 'SyntaxError'}, 400

        try:
            grabber.run()
            user_profile = request.user.get_profile()
            CourseStatus.objects.filter(
                user_profile=user_profile,
                status=CourseStatus.SELECT).update(status=CourseStatus.CANCEL)
            semester = Semester.objects.get(pk=grabber.semester_id)

            course_added = False
            for c in grabber.courses:
                course = find_in_db(c)
                if not course:
                    course = add_to_db(c, semester)
                    course_added = True
                try:
                    course_status = CourseStatus.objects.get(
                        user_profile=user_profile, course=course)
                except CourseStatus.DoesNotExist:
                    CourseStatus.objects.create(user_profile=user_profile,
                                                course=course,
                                                status=CourseStatus.SELECT)
                else:
                    course_status.status = CourseStatus.SELECT
                    course_status.save()
            if course_added:
                cache_name = 'semester_{0}_courses'.format(semester.pk)
                cache.delete(cache_name)

            UserAction.objects.create(user=request.user,
                                      semester=semester,
                                      action_type=UserAction.COURSE_IMPORTED)

            return {'semester_id': grabber.semester_id}
        except LoginError as e:
            if e.error == 'auth':
                return {'error_code': 'AuthError'}
            elif e.error == 'captcha':
                return {'error_code': 'CaptchaError'}
            else:
                return {'error_code': 'UnknownLoginError'}
        except GrabError as e:
            return {
                'error_code': 'GrabError',
                'error': e.error,
            }
    else:
        return {
            'error_code': 'GrabberExpired',
        }, 503
コード例 #2
0
def course_grab_start(request):
    grabber = cache.get(request.session.session_key+'_grabber')
    if grabber:
        try:
            grabber.setUp(**request.POST.dict())
        except LookupError:
            return {'error_code': 'SyntaxError'}, 400

        try:
            grabber.run()
            user_profile = request.user.get_profile()
            CourseStatus.objects.filter(user_profile=user_profile, status=CourseStatus.SELECT).update(status=CourseStatus.CANCEL)
            semester = Semester.objects.get(pk=grabber.semester_id)

            course_added = False
            for c in grabber.courses:
                course = find_in_db(c)
                if not course:
                    course = add_to_db(c, semester)
                    course_added = True
                try:
                    course_status = CourseStatus.objects.get(user_profile=user_profile, course=course)
                except CourseStatus.DoesNotExist:
                    CourseStatus.objects.create(user_profile=user_profile,
                      course=course, status=CourseStatus.SELECT)
                else:
                    course_status.status=CourseStatus.SELECT
                    course_status.save()
            if course_added:
                cache_name = 'semester_{0}_courses'.format(semester.pk)
                cache.delete(cache_name)

            UserAction.objects.create(user=request.user, semester=semester, action_type=UserAction.COURSE_IMPORTED)

            return {'semester_id': grabber.semester_id}
        except LoginError as e:
            if e.error == 'auth':
                return {'error_code': 'AuthError'}
            elif e.error == 'captcha':
                return {'error_code': 'CaptchaError'}
            else:
                return {'error_code': 'UnknownLoginError'}
        except GrabError as e:
            return {
                'error_code': 'GrabError',
                'error': e.error,
            }
    else:
        return {
            'error_code': 'GrabberExpired',
        }, 503
コード例 #3
0
    def handle(self, *args, **options):
        try:
            semester_id = int(args[0])
            dir = args[1]
        except (IndexError, ValueError):
            raise CommandError('Invalid syntax.')

        try:
            files = os.listdir(dir)
        except OSError:
            raise CommandError('Directory not exist.')
        files = [
            f for f in files if os.path.isfile(os.path.join(dir, f))
            and os.path.splitext(f)[1] == ".yaml"
        ]
        files = [os.path.join(dir, f) for f in files]
        log_file = os.path.join(dir, 'import.log')

        semester = Semester.objects.get(pk=semester_id)
        total_import = 0
        id = 1
        for file_i in files:
            self.stdout.write('#{0} {1}\n'.format(id, file_i))
            with open(file_i) as f:
                courses = yaml.load(f)
            count_import = 0
            count_all = 0
            for c in courses:
                if find_in_db(c):
                    self.stdout.write('-')
                    with open(log_file, 'a') as log:
                        log.write('Duplicated Item:\n')
                        log.write(pretty_format(c))
                        log.write('\n')
                else:
                    self.stdout.write('+')
                    count_import += 1
                    add_to_db(c, semester)
                self.stdout.flush()
                count_all += 1
                if count_all % 100 == 0:
                    self.stdout.write(' {0}\n'.format(count_all))
            self.stdout.write(' {0}'.format(count_all))
            total_import += count_import
            id += 1
            self.stdout.write(
                '\n{filename}: {count} courses successfully imported.\n'.
                format(filename=file_i, count=count_import))
        self.stdout.write(
            'Total: {0} courses imported.\n'.format(total_import))
コード例 #4
0
    def handle(self, *args, **options):
        try:
            semester_id = int(args[0])
            dir = args[1]
        except (IndexError, ValueError):
            raise CommandError('Invalid syntax.')

        try:
            files = os.listdir(dir)
        except OSError:
            raise CommandError('Directory not exist.')
        files = [f for f in files if
          os.path.isfile(os.path.join(dir, f)) and os.path.splitext(f)[1] == ".yaml"]
        files = [os.path.join(dir, f) for f in files]
        log_file = os.path.join(dir, 'import.log')

        semester = Semester.objects.get(pk=semester_id)
        total_import = 0
        id = 1
        for file_i in files:
            self.stdout.write('#{0} {1}\n'.format(id, file_i))
            with open(file_i) as f:
                courses = yaml.load(f)
            count_import = 0
            count_all = 0
            for c in courses:
                if find_in_db(c):
                    self.stdout.write('-')
                    with open(log_file, 'a') as log:
                        log.write('Duplicated Item:\n')
                        log.write(pretty_format(c))
                        log.write('\n')
                else:
                    self.stdout.write('+')
                    count_import += 1
                    add_to_db(c, semester)
                self.stdout.flush()
                count_all += 1
                if count_all % 100 == 0:
                    self.stdout.write(' {0}\n'.format(count_all))
            self.stdout.write(' {0}'.format(count_all))
            total_import += count_import
            id += 1
            self.stdout.write('\n{filename}: {count} courses successfully imported.\n'.format(filename=file_i, count=count_import))
        self.stdout.write('Total: {0} courses imported.\n'.format(total_import))