예제 #1
0
파일: views.py 프로젝트: varunarora/OC
def create_textbook_reference(textbook, scope, title, username,
    description, license, collection):
    DEFAULT_COST = 0

    try:
        from django.contrib.auth.models import User

        title = title if title else 'Untitled reference'
        user = User.objects.get(username=username)

        from django.template.defaultfilters import slugify

        # Create a new resource
        from oer.models import Resource as OEResource
        new_resource = OEResource(
            title=title,
            cost=DEFAULT_COST,
            user=user,
            slug=slugify(title),
            visibility='public',
            description=description,
            license=license
        )

        new_reference = Reference(source_type='textbook', textbook=textbook, scope=scope)
        new_reference.save()

        from oer.models import ResourceRevision
        new_resource_revision = ResourceRevision()
        new_resource_revision.content = new_reference
        new_resource_revision.user = user
        new_resource_revision.save()

        new_resource.revision = new_resource_revision
        new_resource.save()

        # Assign this resource to the revision created.
        new_resource.revision.resource = new_resource
        new_resource.revision.save()

        # Now add this resource to the collection it belongs to
        collection.resources.add(new_resource)
        collection.save()

        new_curriculum_resource = Resource(resource=new_resource)
        new_curriculum_resource.save()

        return (new_resource.id, new_curriculum_resource.id)

    except Exception, exception:
        print exception
예제 #2
0
def build_resource_from_exercise(exercise_raw, tag):
    exercise_name = os.path.basename(exercise_raw)

    api_url = '/api/v1/exercises/' + exercise_name

    req.connect()
    req.request('GET', api_url)
    response = req.getresponse()

    result = response.read()

    if result != '' and 'There is no' not in result:
        try:
            exercise = json.loads(result)
        except:
            print exercise_name
            return None
    else:
        return None

    # Fetch URL.
    number_of_items = 0
    for problem_types in exercise['problem_types']:
        number_of_items += len(problem_types['items'])

    final_image = get_image(exercise['image_url'])

    description = exercise['description_html']
    if description == '':
        if exercise['author_name'] == '':
            description = 'A set of ' + str(len(
                exercise['author_name'])) + ' questions to enhance student understanding on the topic and make them assessment ready.'
        else:
            description = 'A set of ' +  str(len(
                exercise['author_name'])) + '+ questions created by Khan Academy\'s ' +  exercise['author_name'] + '.'

    created = dateutil.parser.parse(exercise['creation_date'])
    resource = Resource(
        title=exercise['title'],
        cost=0,
        user=user,
        created=created,
        image=ImageFile(open(final_image)),
        license=license,
        description=description,
        slug=slugify(exercise['title']),
        visibility='public',
        source='KhanAcademy API'
    )

    url = Link(url=exercise['ka_url'])
    url.save()

    resource_revision = ResourceRevision()
    resource_revision.content = url
    resource_revision.created = created
    resource_revision.user = user
    resource_revision.save()

    resource.revision = resource_revision
    resource.save()

    user.get_profile().collection.resources.add(resource)
    user.get_profile().collection.save()

    # Delete the image from disk.
    os.remove(final_image)

    resource.tags.add(Tag.objects.get(title=(
        'Assessment' if exercise['is_quiz'] else 'Exercise'), category=TagCategory.objects.get(title='Resource type')))
    resource.tags.add(Tag.objects.filter(title=tag, category=TagCategory.objects.get(title='Standards'))[0])

    # CREATE A MOFO NOTIFICATION.
    Resource.resource_created.send(
        sender="Resources", resource=resource,
        context_type='user profile', context=user.get_profile()
    )

    req.close()

    # Create a video Resource from each of the video tags if doesn't exist
    for video_id in exercise['related_video_readable_ids']:
        video_url = '/api/v1/videos/' + video_id

        req.connect()
        req.request('GET', video_url)
        response = req.getresponse()

        result = response.read()

        if result != '' and 'null' not in result:
            try:
                video_response = json.loads(result)
            except:
                print video_id
                return None
        else:
            continue

        try:
            link = Link.objects.get(url__icontains=video_response['youtube_id'])
            video = Resource.objects.get(user__username='******',
                revision__content_id=link.id, revision__content_type=link_content_type.id)
        except:
            video_image = get_image(video_response['image_url'])
            
            date_added = dateutil.parser.parse(video_response['date_added'])
            
            req.close()

            if video_response['description']:
                description = video_response['description']
            else:
                description = ''

            video = Resource(
                title=video_response['title'],
                cost=0,
                user=user,
                created=date_added,
                image=ImageFile(open(video_image)),
                license=license,
                description=description,
                slug=slugify(video_response['title']),
                visibility='public'
            )

            url = Link(url='http://www.youtube.com/watch?v=' + video_response['youtube_id'])
            url.save()

            video_revision = ResourceRevision()
            video_revision.content = url
            resource_revision.created = created
            video_revision.user = user
            video_revision.save()

            video.revision = video_revision
            video.save()

            user.get_profile().collection.resources.add(video)
            user.get_profile().collection.save()

            # Delete the image from disk.
            os.remove(video_image)

        # Add tag to video.
        video.tags.add(Tag.objects.get(
            title='Lecture', category=TagCategory.objects.get(title='Resource type')))
        video.tags.add(Tag.objects.filter(title=tag, category=TagCategory.objects.get(title='Standards'))[0])

        # CREATE A MOFO NOTIFICATION.
        Resource.resource_created.send(
            sender="Resources", resource=video,
            context_type='user profile', context=user.get_profile()
        )
예제 #3
0
        attachment.save()

        created_local_attachment.close()

        attachment_revision = ResourceRevision(
            content=attachment,
            user=user
        )
        attachment_revision.save()

        #(attachment_name, attachment_name) = os.path.split(attachment.file.name)
        attachment_resource = Resource(
            revision=attachment_revision,
            title=filename,
            license=license,
            user=user,
            description="",
            visibility='public',
            cost=0.0,
            slug=slugify(filename)
        )
        attachment_resource.save()

        attachment_revision.resource = attachment_resource
        attachment_revision.save()

        user.get_profile().collection.resources.add(attachment_resource)

        attachments.append(attachment_resource)
        os.remove(attachment_path)

    img_web = urllib2.urlopen(value['image_url']).read()
    revision = ResourceRevision(
        content=content,
        user=user
    )
    revision.save()

    tag_category = TagCategory.objects.get(title='Resources')

    from django.template.defaultfilters import slugify
    resource = Resource(
        revision=revision,
        title=value['title'],
        license=license,
        user=user,
        created=value['published'],
        description="",
        visibility='public',
        cost=0.0,
        source="IllustrativeMathematics.org" + value['url'],
        slug=slugify(value['title'])
    )
    resource.save()

    tags=FormUtilities.get_taglist(
                value['standards'], tag_category)
    for tag in tags:
        resource.tags.add(tag)

    revision.resource = resource
    revision.save()
예제 #5
0
파일: gooru.py 프로젝트: varunarora/OC
	#searchTerm = article.title
	searchTerm = article.title
	print searchTerm

	#try:
	req.request('POST', '/gooru-search/rest/search/resource?sessionToken=' + token + '&query=' + searchTerm)
	searchOutput = req.getresponse().read()
	#print searchOutput

	result = json.loads(searchOutput)

	searchResults = result['searchResults']
	oerSet = []

	for sr in searchResults:
		oer = Resource()
		# If resourcetype is video/url, set it to be that way
		oer.title = sr['title']
		print sr['title']
		if sr['resourceType']['name'] == "resource/url":
			oer.type = "url"
		elif sr['resourceType']['name'] == "video/youtube" or sr['resourceType']['name'] == "video/vimeo":
			oer.type = "video"

		oer.url = sr['url']

		if sr['license']['name'] == "Other":
			lo = License.objects.get(title="Unknown")
			oer.license = lo

		oer.cost = 0.0