Пример #1
0
def get_course(code):
    """Returns all information for course identified by course code. Search is
    case sensitive

    """
    with pool.get_connection() as sc:
        # Make sure there is a course with the given code
        triples = sc.query(Triple(code, "rdf:type", "Course"))
        if not triples:
            msg = 'There is no course with code "{0}".'.format(code)
            raise Exception(msg)

        course = fetch_rdf_graph(code, dont_follow=["department"])
        course["code"] = code

        # Fetch department and faculty codes separately, as the structure
        # constructed by fetch_rdf_graph becomes sanely does not
        # contain the codes. These just go and assume that every course
        # has both faculty and department. With Noppa scraping this
        # is necessarily true.
        triples = sc.query(Triple(course["code"], "department", None))
        course["department"] = str(triples[0].object)

        triples = sc.query(Triple(course["department"], "faculty", None))
        course["faculty"] = str(triples[0].object)

        return course
Пример #2
0
def get(user_id):
    """Get the information of the user specified by user_id.

    Arguments:
    user_id -- User's user id

    Exceptions:
    UserDoesNotExist -- User with specified user_id does not exist.

    """
    user_uri = '{0}/ID#{1}'.format(PEOPLE_BASE_URI, user_id)
    user = fetch_rdf_graph(user_uri, dont_follow=[
        '{0}#Friend'.format(PEOPLE_BASE_URI)])
    if not user:
        raise UserDoesNotExist(user_id)
    user['user_id'] = user_id
    return user
Пример #3
0
def get_completed_courses(user_id):
    """Returns the completed courses for the given user sorted by most recent
    completion first.

    """
    user_uri = '{0}/ID#{1}'.format(PEOPLE_BASE_URI, user_id)

    with pool.get_connection() as sc:
        all_completed_course_uris = set(triple.subject
            for triple in sc.query(
                Triple(None, 'rdf:type', 'CompletedCourse')))
        all_user_uris = set(triple.subject
            for triple in sc.query(Triple(None, 'user', uri(user_uri))))
        completed_course_uris = all_completed_course_uris & all_user_uris

        completed_courses = []
        for subject in completed_course_uris:
            completed_course = fetch_rdf_graph(
                subject, dont_follow=['user'])
            del completed_course['user']
            completed_courses.append(completed_course)

        return sorted(completed_courses, key=lambda item: item['date'],
            reverse=True)
Пример #4
0
    def test_fetch_rdf_graph(self):
        expected = {
            'address': None,
            'avatar': {
                'link': {
                    'href': '/people/dn3FNGIomr3OicaaWPEYjL/@avatar',
                    'rel': 'self'
                },
                'status': 'not_set'
            },
            'birthdate': None,
            'description': None,
            'gender': None,
            'irc_nick': None,
            'is_association': None,
            'msn_nick': None,
            'name': {
                'family_name': 'Jannu15',
                'given_name': 'Testi',
                'unstructured': 'Testi Jannu15'
            },
            'phone_number': None,
            'role': None,
            'status': {
                'changed': None,
                'message': None
            },
            'updated_at': '2009-08-14T15:04:46Z',
            'username': '******',
            'website': None
        }

        subject = 'http://cos.alpha.sizl.org/people/ID#dn3FNGIomr3OicaaWPEYjL'
        actual = utils.fetch_rdf_graph(subject)

        self.assertEqual(expected, actual)