Пример #1
0
    def __init__(self, request, user):
        newRequest = request.copy()

        default_cost = 0

        if not newRequest.get('title'):
            newRequest.__setitem__('title', 'Untitled Video')

        newRequest.setdefault('user', user.id)
        newRequest.setdefault('cost', default_cost)

        from django.template.defaultfilters import slugify
        newRequest.setdefault('slug', slugify(newRequest.get('title')))

        # Create and save new Link.
        new_video_link = Link(url=sanitize_url(newRequest.get('url', None)))
        new_video_link.save()

        new_resource_revision = ResourceRevision()
        new_resource_revision.content = new_video_link
        new_resource_revision.user = user
        new_resource_revision.save()

        newRequest.setdefault('revision', new_resource_revision.id);

        from meta.models import TagCategory
        tag_category = TagCategory.objects.get(title='Resources')
        newRequest.setlist('tags', FormUtilities.get_taglist(
            newRequest.getlist('tags'), tag_category))

        super(NewVideoForm, self).__init__(newRequest)
Пример #2
0
    def __init__(self, request, user):
        newRequest = request.copy()

        default_cost = 0

        newRequest.setdefault('user', user.id)
        newRequest.setdefault('cost', default_cost)

        title = newRequest.get('title', None)

        # Fetch the URL page by making Http requests
        # using BeautifulSoup, and find meta tags in the DOM.
        try:
            from BeautifulSoup import BeautifulSoup
            from urllib import urlopen
            # Open the resource and build its DOM into a BeautifulSoup
            #     object.
            source = urlopen(newRequest.get('url'))
            soup = BeautifulSoup(source)

            # Extract the page title, and the description from its meta
            #     tags in <head>
            if not title:
                title = soup.find('title').text

            description = soup.findAll(
                'meta', attrs={'name': "description"}
            )[0]

            # If a description was found, set it on the resource.
            if description.hasattr('title'):
                newRequest.setdefault('description', description['content'])
            else:
                newRequest.setdefault('description', '')
        except:
            if not title:
                title = 'Untitled website'

        newRequest.__setitem__('title', title)

        from django.template.defaultfilters import slugify
        newRequest.setdefault('slug', slugify(newRequest.get('title')))

        # Create and save new Link.
        new_video_link = Link(url=sanitize_url(newRequest.get('url', None)))
        new_video_link.save()

        new_resource_revision = ResourceRevision()
        new_resource_revision.content = new_video_link
        new_resource_revision.user = user
        new_resource_revision.save()

        newRequest.setdefault('revision', new_resource_revision.id);

        from meta.models import TagCategory
        tag_category = TagCategory.objects.get(title='URL')
        newRequest.setlist('tags', FormUtilities.get_taglist(
            newRequest.getlist('tags'), tag_category))

        super(NewURLForm, self).__init__(newRequest)
Пример #3
0
    def __init__(self, request, user, instance):
        newRequest = request.copy()

        # For now, default to the previous values of user, slug & cost
        newRequest.setdefault('user', instance.user.id)
        newRequest.setdefault('cost', instance.cost)
        newRequest.setdefault('slug', instance.slug)

        # Create and save new Document.
        new_document = Document()
        new_document.save()

        new_resource_revision = ResourceRevision()
        new_resource_revision.content = new_document
        new_resource_revision.resource = instance
        new_resource_revision.user = user
        new_resource_revision.save()

        newRequest.setdefault('revision', new_resource_revision.id);

        from meta.models import TagCategory
        tag_category = TagCategory.objects.get(title='Document')
        newRequest.setlist('tags', FormUtilities.get_taglist(
            newRequest.getlist('tags'), tag_category))

        super(DocumentEditForm, self).__init__(newRequest, instance=instance)
Пример #4
0
    def __init__(self, request, user, instance):
        newRequest = request.copy()

        # For now, default to the previous values of user, slug & cost
        newRequest.setdefault('user', instance.user.id)
        newRequest.setdefault('cost', instance.cost)        
        newRequest.setdefault('slug', instance.slug)

        # Create and save new Link, if the link URL changed.
        url_submitted = sanitize_url(newRequest.get('url', None))
        if instance.revision.content.url == url_submitted:
            new_video_link = Link(url=url_submitted)
            new_video_link.save()
            content = new_video_link
        else:
            content = instance.revision.content

        new_resource_revision = ResourceRevision()
        new_resource_revision.content = content
        new_resource_revision.resource = instance
        new_resource_revision.user = user        
        new_resource_revision.save()

        newRequest.setdefault('revision', new_resource_revision.id);

        from meta.models import TagCategory
        tag_category = TagCategory.objects.get(title='Resources')
        newRequest.setlist('tags', FormUtilities.get_taglist(
            newRequest.getlist('tags'), tag_category))

        super(VideoEditForm, self).__init__(newRequest, instance=instance)
Пример #5
0
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
Пример #6
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()
        )