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
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() )
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() resource_response = urllib2.urlopen( 'http://map.mathshell.org/materials/' + value['lessons'][0]) content_disposition = resource_response.headers.get('Content-Disposition')
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() # Add the resource to a collection. user.get_profile().collection.resources.add(resource) print resource.id # Generate thumbnail image if not generated by default.
oer.user = User.objects.get(username="******") if sr['description'] != "": oer.body_markdown = sr['description'] else: if sr['resourceType']['name'] == "resource/url" or sr['resourceType']['name'] == "video/youtube": try: source = urlopen(sr['url']) soup = BeautifulSoup(source) description = soup.findAll('meta', attrs={'name' : "description"})[0] if description: oer.body_markdown = description['content'] except: pass oer.save() oerSet.append(oer) print oerSet article.resources = oerSet article.save() except: for oer in oerSet: oer.delete() print "ended up here" req, token = connect()