def browse(request, project_slug): try: project = Project.objects.get(slug=project_slug) except: raise Http404 # Get the root collection of the project. root_collection = project.collection # Get all the assets of the root collection. root_assets = root_collection.resources import oer.CollectionUtilities as cu child_collections = cu._get_child_collections(root_collection) (browse_tree, flattened_tree) = _get_browse_tree(root_collection) resources = root_assets.all() cu.set_resources_type(resources) cu.preprocess_collection_listings(resources) context = { 'project': project, 'resources': resources, 'collections': child_collections, 'collection': root_collection, 'browse_tree': browse_tree, 'title': (_(settings.STRINGS['projects']['BROWSE_TITLE']) + ' ‹ ' + project.title) } return render(request, 'project/browse.html', context)
def list_collection(request, project_slug, collection_slug): try: project = Project.objects.get(slug=project_slug) except: raise Http404 (browse_tree, flattened_tree) = _get_browse_tree(project.collection) collection = next( tree_item for tree_item in flattened_tree if tree_item.slug == collection_slug) root_assets = collection.resources import oer.CollectionUtilities as cu child_collections = cu._get_child_collections(collection) resources = root_assets.all() cu.set_resources_type(resources) cu.preprocess_collection_listings(resources) context = { 'project': project, 'resources': resources, 'collections': child_collections, 'collection': collection, 'browse_tree': browse_tree, # TODO(Varun): Make this a custom title. 'title': (_(settings.STRINGS['projects']['BROWSE_TITLE']) + ' ‹ ' + project.title) } return render(request, 'project/browse.html', context)
def collaborator_add_notification(sender, **kwargs): collection = kwargs.get('collection', None) user = kwargs.get('user', None) request = kwargs.get('request', None) notification = Notification() notification.user = user # Get root host of the collection. import oer.CollectionUtilities as cu (collection_root_type, collection_root) = cu.get_collection_root(collection) if collection_root_type.name == 'project': notification.url = reverse( 'projects:list_collection', kwargs={ 'project_slug': collection_root.slug, 'collection_slug': collection.slug } ) notification.description = 'You have been added as a collaborator on the collection "%s"' % ( collection.title) notification.save() # Send an email about this notification. nu.notify_by_email(notification, request.get_host())
def _hasImmediateChildren(collection): """Adapted from _hasImmediateChildren() in articles.views""" import oer.CollectionUtilities as cu childCollections = list(cu._get_child_collections(collection)) if len(childCollections) > 0: return {collection: childCollections} else: return None
def add_existing_to_section_item_resources(request): section_item_resources_id = request.POST.get('section_item_resources_id', None) #is_resource = request.POST.get('is_resource', None) == 'true' resource_collection_id = request.POST.get('resource_collection_ID', None) try: section_item_resources = SectionItemResources.objects.get(pk=section_item_resources_id) except: section_item_resources = create_resource_set(request, section_item_resources) if not section_item_resources: return APIUtilities._api_not_found() from oer.models import Resource as OEResource resource = OEResource.objects.get(pk=resource_collection_id) new_objective_resource = Resource(resource=resource) new_objective_resource.save() section_item_resources.resources.add(new_objective_resource) import oer.CollectionUtilities as cu cu.set_resources_type([resource]) cu.preprocess_collection_listings([resource]) context = { 'resource': { 'id': new_objective_resource.id, 'url': reverse( 'read', kwargs={ 'resource_id': resource.id, 'resource_slug': resource.slug } ), 'thumbnail': settings.MEDIA_URL + resource.image.name if resource.image else '', 'title': resource.title, 'type': resource.type } } return APIUtilities.success(context)
def get_resource_root(resource): from django.core.exceptions import MultipleObjectsReturned try: collection = Collection.objects.get(resources__id=resource.id) except MultipleObjectsReturned: collection = Collection.objects.filter( resources__id=resource.id)[0] try: import oer.CollectionUtilities as cu return cu.get_collection_root(collection) except: return None
def project_home(request, project_slug): try: project = Project.objects.get(slug=project_slug) # TODO(Varun): Check if this is a private project, and if it is, check # if the requestee of the page is a member of the project. If not, # have a flag that blocks the page contents from being listed, or has # an alternative view. if project.visibility == 'private': if request.user not in project.confirmed_members: return redirect('projects:project_about', project_slug=project.slug) # Get activity feed related to this project. from django.contrib.contenttypes.models import ContentType project_ct = ContentType.objects.get_for_model(Project) posts = Comment.objects.filter( parent_id=project.id, parent_type=project_ct).order_by('-created') # For each of the comments, get its child comments recursively comments_ct = ContentType.objects.get_for_model(Comment) build_posts_social(posts, request.user) categories = GroupCategory.objects.filter(parent=project) import oer.CollectionUtilities as cu # Setup attachments on posts. for post in posts: if post.attachment: if post.attachment_type.name == 'collection': (root_host_type, root_host) = cu.get_collection_root( post.attachment) post.attachment.host_type = root_host_type post.attachment.host = root_host context = { 'title': project.title + ' ‹ OpenCurriculum', 'project': project, 'posts': posts, 'categories': categories, 'host_content_type': comments_ct, 'comments_content_type': comments_ct, } return render(request, 'project/project.html', context) except: raise Http404
def project_navigation_tree(value, user): import oer.CollectionUtilities as cu return cu.build_project_collection_navigation(value, user)
def _get_fields(item): serialized_resource_sets = [] from os.path import splitext from oer.models import Link, Attachment from django.contrib.contenttypes.models import ContentType link_content_type = ContentType.objects.get_for_model(Link) reference_content_type = ContentType.objects.get_for_model(Reference) attachment_content_type = ContentType.objects.get_for_model(Attachment) from meta.models import Tag document = [".doc", ".docx", ".rtf", "odt"] for resource_set in item.resource_sets.all(): serialized_resources = [] for resource in resource_set.resources.all(): import oer.CollectionUtilities as cu cu.set_resources_type([resource.resource]) cu.preprocess_collection_listings([resource.resource]) if resource.resource.revision.content_type == attachment_content_type: name, extension = splitext( resource.resource.revision.content.file.name) if extension in document: resource.resource.type = 'pdf' thumbnail = settings.MEDIA_URL + resource.resource.revision.content.textbook.thumbnail.name if ( resource.resource.revision.content_type == reference_content_type) else settings.MEDIA_URL + resource.resource.image.name serialized_resources.append({ 'id': resource.id, 'url': resource.resource.revision.content.url if ( resource.resource.revision.content_type == link_content_type) else reverse( 'read', kwargs={ 'resource_id': resource.resource.id, 'resource_slug': resource.resource.slug } ), 'title': resource.resource.title, 'thumbnail': thumbnail, 'user_thumbnail': settings.MEDIA_URL + resource.resource.user.get_profile().profile_pic.name, 'type': resource.resource.type }) serialized_resource_sets.append({ 'id': resource_set.id, 'position': resource_set.position, 'title': resource_set.title, 'resources': serialized_resources }) """if item.resource_sets.count() == 0: serialized_resource_sets.append({ 'id': None, 'resources': [] })""" if item.meta: for meta_item in item.meta: if 'standards' in meta_item: standards = [] for std_id in meta_item['standards']: standard = Tag.objects.get(pk=std_id) standards.append({ 'title': standard.title, 'description': standard.description }) meta_item['standards'] = standards return (serialized_resource_sets, item.meta)
def new_resource_favorite_activity(sender, **kwargs): favorite = kwargs.get('favorite', None) import oer.CollectionUtilities as cu import oer.ResourceUtilities as ru if favorite.parent_type.name == 'resource': from django.core.exceptions import MultipleObjectsReturned try: collection = Collection.objects.get(resources__id=favorite.parent.id) except MultipleObjectsReturned: collection = Collection.objects.filter( resources__id=favorite.parent.id)[0] else: collection = favorite.parent.host (collection_host_type, collection_host) = ru.get_resource_root( favorite.parent) new_activity = Activity( actor=favorite.user, action=favorite, target=favorite.parent, context=collection_host ) new_activity.save() recipients = None # Get all people subscribed to resource creator. subscriptions = Subscription.objects.filter( subscribee=favorite.user.get_profile()) if subscriptions.count() >= 1: if (collection_host_type.name == 'project' and ( collection_host.visibility == 'public') and ( favorite.parent.visibility != 'private')): recipients = [x.subscriber.user for x in subscriptions] elif (collection_host_type.name == 'project' and ( collection_host.visibility == 'private')): if favorite.parent.visibility == 'project': recipients = [x.subscriber.user for x in subscriptions if x in collection_host.confirmed_members] elif favorite.parent.visibility == 'private': recipients = [x.subscriber.user for x in subscriptions if x in favorite.parent.collaborators.all()] elif favorite.parent.visibility == 'collection': root_private_collection = cu.get_root_private_collection(collection) recipients = [x.subscriber.user for x in subscriptions if x in root_private_collection.collaborators.all()] elif favorite.parent.visibility == 'public': recipients = [x.subscriber.user for x in subscriptions] elif favorite.parent.visibility == 'private': recipients = [x.subscriber.user for x in subscriptions if x in favorite.parent.collaborators.all()] elif favorite.parent.visibility == 'collection': root_private_collection = cu.get_root_private_collection(collection) recipients = [x.subscriber.user for x in subscriptions if x in root_private_collection.collaborators.all()] if recipients: for recipient in recipients: new_activity.recipients.add(recipient)