コード例 #1
0
def get_courses_view(request):
    '''
    Get course data for the given course and section and return it as an HTTP response
    containing a JSON string
    '''
    course = request.GET.get('course')
    section = request.GET.get('section')
    s = Scraper()
    response_data = s.get_courses(course, section, request.user.id)
    return response_data
コード例 #2
0
ファイル: views.py プロジェクト: unscsprt/umagellan
def add_course(request):
    '''
    add new course object to the database
    '''
    course = request.GET.get('course').upper()
    section = request.GET.get('section')
    response_data = {}
    course_info = {}
    response_data['error'] = False
    response_data['error_msg'] = ''

    # add the course to the user's list of courses
    user = UserProfile.objects.get(user=request.user)
    profile = request.user.profile

    # print course
    # print section

    if len(section) != 4:
        if len(section) == 3:
            section = "0" + section
        else:
            response_data['error'] = True
            response_data['error_msg'] = 'Section ID is invalid!'
            return HttpResponse(json.dumps(response_data), mimetype="application/json")

    # Set URL for testudo course search, term, and other query data
    page_url = "https://ntst.umd.edu/soc/search?courseId=" + course + "&sectionId=" + section + "&termId=201308&_openSectionsOnly=on&courseLevelFilter=ALL&classStartTime=&_classDays=on&teachingCenter=ALL"
    page = urllib2.urlopen(page_url).read()
    soup = BeautifulSoup(page)

    if soup.find("div", {"class" : "no-courses-message"}) != None:
        response_data['error'] = True
        response_data['error_msg'] = 'That course does not exist!'
        return HttpResponse(json.dumps(response_data), mimetype="application/json")

    if len(course) <= 4:
        response_data['error'] = True
        response_data['error_msg'] = 'That course does not exist!'
        return HttpResponse(json.dumps(response_data), mimetype="application/json")

    course_container = soup.find("div", {"class" : "courses-container"})
    first_block = course_container.find("div", {"class" : "course"}, {"id": course})

    if first_block == None:
        response_data['error'] = True
        response_data['error_msg'] = 'That course does not exist!'
        return HttpResponse(json.dumps(response_data), mimetype="application/json")

    class_block = first_block.find('div', {'class' : 'class-days-container'})
    classes = class_block.findAll('div', {'class' : 'row'})
    response_data['courses'] = []
    
    # create the new course objects and add them to database
    for i in range(0, len(classes)):
        try:
            # set c to the matching course if it exists in DB
            room = classes[i].find('span', {'class' : 'class-room'}).text
            c = Course.objects.get(name=course, section=section, room_number=room)
        except: 
            # course does not exist in DB, create new course and add it to DB
            c = Course()
            c.name = course.upper()
            c.section = section
    
            room = classes[i].find('span', {'class' : 'class-room'}).text
            
            if room != None:
                if room == 'ONLINE':
                    response_data['error'] = True
                    response_data['error_msg'] = 'You cannot add online classes!'
                    return HttpResponse(json.dumps(response_data), mimetype="application/json")
                else:
                    c.room_number = room
            
            c.build_code = classes[i].find('span', {'class' : 'building-code'}).text
            
            class_start = classes[i].find('span', {'class' : 'class-start-time'}).text
            c.start_time =  parser.parse(class_start)
            
            class_end = classes[i].find('span', {'class' : 'class-end-time'}).text
            c.end_time = parser.parse(class_end)
            
            c.section_days = classes[i].find('span', {'class' : 'section-days'}).text
            c.link = page_url

            if classes[i].find('span', {'class' : 'class-type'}) != None:
                c.tag = classes[i].find('span', {'class' : 'class-type'}).text
            
            c.save()
        
        # add course to user's list of courses
        if not c in profile.courses.all():
            user.courses.add(c)
            user.save()
    
            course_info = {}
            course_info['name']         = c.name
            course_info['section']      = c.section
            course_info['build_code']   = c.build_code
            course_info['room_number']  = c.room_number
            course_info['start_time']   = c.start_time.strftime("%H:%M")
            course_info['end_time']     = c.end_time.strftime("%H:%M")
            course_info['section_days'] = []
            s = Scraper()
            s.split_days(course_info['section_days'], c.section_days)
            course_info['link']         = c.link
            course_info['tag']          = '' if c.tag == None else c.tag
            course_info['id']           = c.id
            response_data['courses'].append(course_info)
            response_data['error'] = False
            response_data['error_msg'] = ''
        else:
            response_data['error'] = True
            response_data['error_msg'] = 'Course already added!'
            errorResponse = HttpResponse(json.dumps(response_data), mimetype="application/json")

    if response_data['error']:
        if course_info == {}:
            return errorResponse

    response_data['error'] = False
    response_data['error_msg'] = ''
    return HttpResponse(json.dumps(response_data), mimetype="application/json")
コード例 #3
0
def add_course(request):
    '''
    add new course object to the database
    '''
    course = request.GET.get('course').upper()
    section = request.GET.get('section')
    response_data = {}
    course_info = {}
    response_data['error'] = False
    response_data['error_msg'] = ''

    # add the course to the user's list of courses
    user = UserProfile.objects.get(user=request.user)
    profile = request.user.profile

    # print course
    # print section

    if len(section) != 4:
        if len(section) == 3:
            section = "0" + section
        else:
            response_data['error'] = True
            response_data['error_msg'] = 'Section ID is invalid!'
            return HttpResponse(json.dumps(response_data),
                                mimetype="application/json")

    # Set URL for testudo course search, term, and other query data
    page_url = "https://ntst.umd.edu/soc/search?courseId=" + course + "&sectionId=" + section + "&termId=201308&_openSectionsOnly=on&courseLevelFilter=ALL&classStartTime=&_classDays=on&teachingCenter=ALL"
    page = urllib2.urlopen(page_url).read()
    soup = BeautifulSoup(page)

    if soup.find("div", {"class": "no-courses-message"}) != None:
        response_data['error'] = True
        response_data['error_msg'] = 'That course does not exist!'
        return HttpResponse(json.dumps(response_data),
                            mimetype="application/json")

    if len(course) <= 4:
        response_data['error'] = True
        response_data['error_msg'] = 'That course does not exist!'
        return HttpResponse(json.dumps(response_data),
                            mimetype="application/json")

    course_container = soup.find("div", {"class": "courses-container"})
    first_block = course_container.find("div", {"class": "course"},
                                        {"id": course})

    if first_block == None:
        response_data['error'] = True
        response_data['error_msg'] = 'That course does not exist!'
        return HttpResponse(json.dumps(response_data),
                            mimetype="application/json")

    class_block = first_block.find('div', {'class': 'class-days-container'})
    classes = class_block.findAll('div', {'class': 'row'})
    response_data['courses'] = []

    # create the new course objects and add them to database
    for i in range(0, len(classes)):
        try:
            # set c to the matching course if it exists in DB
            room = classes[i].find('span', {'class': 'class-room'}).text
            c = Course.objects.get(name=course,
                                   section=section,
                                   room_number=room)
        except:
            # course does not exist in DB, create new course and add it to DB
            c = Course()
            c.name = course.upper()
            c.section = section

            room = classes[i].find('span', {'class': 'class-room'}).text

            if room != None:
                if room == 'ONLINE':
                    response_data['error'] = True
                    response_data[
                        'error_msg'] = 'You cannot add online classes!'
                    return HttpResponse(json.dumps(response_data),
                                        mimetype="application/json")
                else:
                    c.room_number = room

            c.build_code = classes[i].find('span', {
                'class': 'building-code'
            }).text

            class_start = classes[i].find('span', {
                'class': 'class-start-time'
            }).text
            c.start_time = parser.parse(class_start)

            class_end = classes[i].find('span', {
                'class': 'class-end-time'
            }).text
            c.end_time = parser.parse(class_end)

            c.section_days = classes[i].find('span', {
                'class': 'section-days'
            }).text
            c.link = page_url

            if classes[i].find('span', {'class': 'class-type'}) != None:
                c.tag = classes[i].find('span', {'class': 'class-type'}).text

            c.save()

        # add course to user's list of courses
        if not c in profile.courses.all():
            user.courses.add(c)
            user.save()

            course_info = {}
            course_info['name'] = c.name
            course_info['section'] = c.section
            course_info['build_code'] = c.build_code
            course_info['room_number'] = c.room_number
            course_info['start_time'] = c.start_time.strftime("%H:%M")
            course_info['end_time'] = c.end_time.strftime("%H:%M")
            course_info['section_days'] = []
            s = Scraper()
            s.split_days(course_info['section_days'], c.section_days)
            course_info['link'] = c.link
            course_info['tag'] = '' if c.tag == None else c.tag
            course_info['id'] = c.id
            response_data['courses'].append(course_info)
            response_data['error'] = False
            response_data['error_msg'] = ''
        else:
            response_data['error'] = True
            response_data['error_msg'] = 'Course already added!'
            errorResponse = HttpResponse(json.dumps(response_data),
                                         mimetype="application/json")

    if response_data['error']:
        if course_info == {}:
            return errorResponse

    response_data['error'] = False
    response_data['error_msg'] = ''
    return HttpResponse(json.dumps(response_data), mimetype="application/json")