Пример #1
0
def new_search(query, org='', _type=None, cid=0, limit=10, offset=0, started='false', hasTA='false', exclude_ids=[]):
    parameters = {
        'query': query,
        'org': org,
        'cid': cid,
        'limit': limit,
        'offset': offset,
        'started': cmp(started, 'true') == 0,
        'hasTA': cmp(hasTA, 'true') == 0,
        'exclude_ids': exclude_ids,
        '_type': _type,
    }

    search_res = {}
    if parameters['cid']:
        # Get course query by category id
        queryResult = CourseCategory.objects.get(
            id=parameters['cid']).course_set
    elif parameters['query']:
        # Get course query by search api
        course_id_dict = search_all_course_id(parameters['query'])
        queryResult = Course.objects.filter(course_id__in=course_id_dict.keys())
    elif parameters['org']:
        if cmp(parameters['org'], 'edx') == 0:
            queryResult = Course.objects.filter(owner='edX')
        else:
            queryResult = Course.objects.filter(org=parameters['org'])
    else:
        # Get all the courses
        queryResult = Course.objects

    if parameters['started']:
        queryResult = queryResult.filter(serialized__gt=0)

    if parameters['hasTA']:
        now = datetime.now(UTC())
        queryResult = queryResult.filter(owner='xuetangX').filter(
            start__lt=now).filter(end__gt=now)

    if parameters['_type'] == 'recent_course':
        recent_time = datetime.now(UTC()) - timedelta(days=90)
        queryResult = queryResult.filter(start__gt=recent_time)

    # Filter and order by status
    visible_owner = microsite.get_visible_owner()
    verified_courses_ids = CourseMode.get_verified_option_courses()
    queryResult = queryResult.filter(status__gte=0, owner__in=visible_owner, course_type=0).exclude(
        course_id__in=verified_courses_ids).order_by('-status', '-id')

    total_length = queryResult.count()
    courses = sorted(queryResult, key=lambda x: course_id_dict.get(x.course_id, 0))[
              parameters['offset']:parameters['offset'] + parameters['limit']]

    return courses, total_length
Пример #2
0
 def _exclude_vpc_and_selfpaced(enrollment):
     return Course.objects.filter(
         course_id=enrollment.course_id, owner__in=microsite.get_visible_owner(), course_type=0).exists()