Exemplo n.º 1
0
    def build_breadcrumb(self):
        # Build breadcrumb for selected category.
        breadcrumb = category_util.build_breadcrumb(self.selected_category)[1:-1]
        breadcrumb.reverse()

        for breadcrumb_category in breadcrumb:
            breadcrumb_category.url = reverse(
                'browse', kwargs={
                    'category_slug': breadcrumb_category.url
                }
            )

        self.breadcrumb = breadcrumb
Exemplo n.º 2
0
    def get_serialized_category(self, category):
        # TODO(Varun): Cache this call.
        if category.id not in self.categories:
            breadcrumb = category_util.build_breadcrumb(category)
            self.categories[category.id] = {
                'id': category.id,
                'title': category.title,
                'slug': category.slug,
                'url': reverse(
                    'browse', kwargs={
                        'category_slug': breadcrumb[0].url
                    }
                )
            }

        return self.categories[category.id]
Exemplo n.º 3
0
def get_nested_child_tags_from_category(request, category_id):
    from meta.models import Category, TagCategory
    category = Category.objects.get(pk=category_id)

    import meta.CategoryUtilities as catU
    (browse_tree, flattened_tree) = catU.build_child_categories(
        {'root': [category]}, [])

    tags = {}
    standards_categories = TagCategory.objects.filter(title__in=['Standards', 'Objectives'])

    for descendant_category in flattened_tree:
        descendant_category_tags = descendant_category.tags.filter(category__in=standards_categories).order_by('position')
        if descendant_category_tags.count() > 0:
            if descendant_category.parent == category:
                tags[descendant_category] = descendant_category_tags
            else:
                if descendant_category.parent not in tags:
                    tags[descendant_category.parent] = descendant_category_tags
                else:
                    tags[descendant_category.parent] |= descendant_category_tags

    serialized_tags = []
    for tag_category, tag_set in tags.items():
        for tag in tag_set:
            serialized_tags.append({
                'id': tag.id,
                'title': tag.title,
                'domain': tag_category.title,
                'description': tag.description,
                'position': tag.position,
                'url': reverse(
                    'meta:standard', kwargs={
                        'tag_title': tag.title
                })
            })

    context = {
        'tags': serialized_tags
    }
    return APIUtilities._api_success(context)
Exemplo n.º 4
0
    def suggestions(self):
        # Fetch the resources in the current category and everything nested within.
        (current_browse_tree, current_flattened_tree) = category_util.build_child_categories(
            {'root': [self.selected_category]}, [])

        from oer.models import Suggestion
        unfiltered_suggestions = Suggestion.objects.filter(category=self.current_category).order_by('-created')

        suggestions = []
        for unfiltered_suggestion in unfiltered_suggestions:
            for tag in unfiltered_suggestion.suggested.tags.all():
                if tag.category == self.resource_type_tag_category:
                    suggestions.append(unfiltered_suggestion)

        for suggestion in suggestions:
            # Set types on suggestions.
            if suggestion.suggested_type.name == 'collection':
                suggestion.suggested.type = 'Folder'
            else:
                suggestion.suggested.type = suggestion.suggested.tags.get(
                    category=self.resource_type_tag_category)
            
            if not hasattr(suggestion.suggested, 'filtered_tags'):
                suggestion.suggested.filtered_tags = suggestion.suggested.tags.exclude(
                    category=self.resource_type_tag_category).exclude(
                    category=self.resources_category)

        context = {
            'title': 'Review suggestions',
            'selected_category': self.selected_category,
            'child_categories': current_flattened_tree,
            'items': suggestions,
            'current_category': self.current_category,
            'is_catalog': False,
            'breadcrumb': self.breadcrumb,
            'return_url': self.return_url,
            'browse_mode': 'suggestions'
        }
        return render(self.request, 'browse.html', context)
Exemplo n.º 5
0
def standard(request, tag_title):
    # Get the tag.
    try:
        tag = Tag.objects.get(title=tag_title)
    except:
        raise Http404

    # Get public resources associated with the tag.
    from oer.models import Resource
    resources = Resource.objects.filter(tags=tag)[:20]

    category = Category.objects.filter(tags=tag)[0]

    breadcrumb = catU.build_breadcrumb(category)
    breadcrumb_urlized = []
    for breadcrumb_category in breadcrumb:
        if breadcrumb_category.parent.slug == 'standards':
            break

        try:
            breadcrumb_urlized.append(standard_urlize(breadcrumb_category.parent))
        
        # HACK(Varun): For practice standards whose parent has been ripped away.
        except:
            breadcrumb_category.url = ''    
            breadcrumb_urlized.append(breadcrumb_category)

    breadcrumb_urlized.reverse()

    context = {
        'tag': tag,
        'resources': resources,
        'breadcrumb': breadcrumb_urlized,
        'title': tag.title + " ‹ OpenCurriculum"
    }
    return render(request, 'standard.html', context)
Exemplo n.º 6
0
def _get_category_url(node):
    import meta.CategoryUtilities as catU

    breadcrumb = catU.build_breadcrumb(node)
    return reverse("browse", kwargs={"category_slug": breadcrumb[0].url})
Exemplo n.º 7
0
    def search(self, query):
        from haystack.query import SearchQuerySet

        current_category = Category.objects.get(pk=self.current_category_id)
        (browse_tree, flattened_tree) = category_util.build_child_categories(
            {'root': [current_category]}, [])

        all_resources = []
        all_raw_resources = []

        child_categories = Category.objects.filter(parent=current_category)

        # Remove the current category as this a downward searchself.
        current_category_in_tree = next(current for current in flattened_tree if current.id == current_category.id)
        flattened_tree.remove(current_category_in_tree)
        
        categories = [category.title for category in flattened_tree]
        categories_tags = []
        for category in flattened_tree:
            categories_tags += category.tags.all()

        def set_child_category(resource, immediate_category):
            while True:
                if immediate_category in child_categories:
                    resource.category = immediate_category
                    return None
                else:
                    immediate_category = immediate_category.parent

        def get_child_category_from_resource_categories(resource):
            for resource_category in resource.object.categories.all():
                if resource_category in flattened_tree:
                    return resource_category

        def set_category_on_categorized_resource(resource):
            a = get_child_category_from_resource_categories(resource)
            set_child_category(resource.object, a)
            return resource

        def categorize_resource(searched_resource):
            # Find the child category this is a descendant in.
            filtered_tags = searched_resource.object.tags.exclude(
                category=self.resource_type_category).exclude(
                category=self.resources_category)

            for tag in filtered_tags:
                if tag in categories_tags:
                    for category in flattened_tree:
                        if tag in category.tags.all():
                            immediate_category = category
                            break

            set_child_category(searched_resource.object, immediate_category)

            return searched_resource

        # TODO(Varun): Rope in collection search.
        """if len(categories) > 0 and len(categories_tags) > 0:
            sqs = SearchQuerySet().filter(content_auto=query, visibility='public', categories__in=categories) | SearchQuerySet(
                ).filter(content_description=query, visibility='public', categories__in=categories)
            sqs_tags = SearchQuerySet().filter(content_auto=query, visibility='public', tags__in=categories_tags) | SearchQuerySet(
                ).filter(content_description=query, visibility='public', tags__in=categories_tags)

            all_raw_resources += sorted(
                set(map(set_category_on_categorized_resource, sqs) + map(
                    categorize_resource, sqs_tags)), key=lambda searched_resource: searched_resource.object.created, reverse=True)
        elif len(categories) > 0:
            sqs_tags = SearchQuerySet().filter(content_auto=query, visibility='public', tags__in=categories_tags) | SearchQuerySet(
                    ).filter(content_description=query, visibility='public', tags__in=categories_tags)
            all_raw_resources += map(set_category_on_categorized_resource, sqs_tags)
        elif len(categories_tags) > 0:"""
        sqs_title_categories = SearchQuerySet().filter(content_auto=query, visibility='public', categories__in=categories)
        sqs_description_categories = SearchQuerySet(
            ).filter(content_description=query, visibility='public', categories__in=categories)
        
        sqs = set()
        if len(sqs_title_categories) > 0:
            sqs |= set(sqs_title_categories)

        if len(sqs_description_categories) > 0:
            sqs |= set(sqs_description_categories)

        all_raw_resources += list(map(set_category_on_categorized_resource, sqs))

        # Setup each resource's favorites count and type.
        from meta.models import Tag
        for resource in all_raw_resources:
            try:
                self.build_browse_resource(resource.object)
                all_resources.append(resource)
            except Tag.DoesNotExist:
                pass

        unserialized_resources = []
        for resource in all_resources:
            unserialized_resources.append(resource.object)

        serialized_resources = serialize(unserialized_resources)

        context = {
            'resources': serialized_resources
        }
        return APIUtilities._api_success(context)
Exemplo n.º 8
0
    def process_category_slug(self):
        categories_slugs = self.category_slug.split('/')

        # Determine the depth to figure out what level of page needs to be displayed.
        try:
            host_category = Category.objects.filter(slug=categories_slugs[0])
            if host_category.count() == 0:
                raise Http404
            else:
                host_category = host_category[0]

            if categories_slugs[0] != 'common-core':
                self.is_common_core_hosted = False
            else:
                self.is_common_core_hosted = True

            (host_browse_tree, host_flattened_tree) = category_util.build_child_categories(
                {'root': [host_category]}, [])

            # Assume that there is a unique pair of child/parent relationship in any
            #     host category. Breaks on the edge case where this pair is found twice.
            try:
                self.current_category = Category.objects.get(
                    slug=categories_slugs[-1], parent__slug=categories_slugs[-2])
            
            except IndexError:
                try:
                    self.current_category = Category.objects.get(slug=categories_slugs[-1])
                except MultipleObjectsReturned:
                    self.current_category = Category.objects.filter(slug=categories_slugs[-1])[0]
            
            except MultipleObjectsReturned:
                # Find the unique combinator inside the host tree.
                self.current_category = next(category for category in host_flattened_tree if(
                    category.slug == categories_slugs[-1] and category.parent.slug == categories_slugs[-2]))

            except Category.DoesNotExist:
                raise Http404

            # Determine subject if subject.
            try:
                self.subject_category = Category.objects.get(
                    slug=categories_slugs[1], parent__slug=categories_slugs[0])
            except:
                pass
            
            for category in host_flattened_tree:
                if category == self.current_category:
                    self.selected_category = category

            if not self.selected_category:
                raise Http404

            if len(categories_slugs) == 1:
                self.return_url = None
            else:            
                return_category_slug = category_util.build_breadcrumb(self.selected_category.parent.parent)[0].url
                if return_category_slug != '':
                    self.return_url = reverse(
                        'browse', kwargs={
                            'category_slug': return_category_slug
                        }
                    )
                else:
                    self.return_url = reverse('browse_default')

            if len(categories_slugs) == 2:
                self.is_subject_home = True

            # Build the breadcrumb associated with this slug.
            self.build_breadcrumb()

        except""" Exception, e""":
            # from django.core.mail import mail_admins
            # mail_admins('Browse failed to render category', str(e) + category_slug)

            # Happens either when the slug is not found.
            raise Http404


        return {
            'host_category': host_category,
        }
Exemplo n.º 9
0
def build_child_tree(root_node):
    # Get the list of child of this node.
    nodes = list(itertools.chain.from_iterable(root_node.values()))
    node_elements = []

    # Create <li> nodes for each.
    for node in nodes:
        node_element = ElementTree.Element('li')

        if type(node) is dict:
            current_node = get_root_key(node)
        else:
            current_node = node

        # Determine whether the node is a tag or category.
        if type(current_node).__name__.lower() == 'category':
            node_type = 'category'
        else:
            node_type = 'tag'

        # If this child has other children, build child tree.
        # Has to be the case of our tag category.
        if type(node) is dict:
            # Set a class to indicate that this element has child collections.
            node_element.set('class', 'parent-category')

            node_toggler = ElementTree.SubElement(node_element, 'span')
            node_toggler.set('class', 'toggle-category')
            node_toggler.text = ' '

            node_href = ElementTree.SubElement(node_element, 'a')
    
            node_href.set('href', reverse('browse', kwargs={
                    'category_slug': catU.build_breadcrumb(current_node)[0].url }))
            node_href.set('id', 'category-' + str(current_node.id))            
            node_href.text = current_node.title

            nodeList = ElementTree.SubElement(node_element, 'ul')

            child_nodes = build_child_tree(node)
            for child_node in child_nodes:
                nodeList.append(child_node)

        # Otherwise, append a child <a> element as is.
        # Can be a tag category or a tag.
        else:
            # Set a class to indicate that this element does not have child collections.
            node_element.set('class', 'empty-category' if node_type == 'category' else 'empty-tag')

            node_toggler = ElementTree.SubElement(node_element, 'span')
            node_toggler.set(
                'class', 'toggle-category' if node_type == 'category' else 'toggle-tag')
            node_toggler.text = ' '

            node_href = ElementTree.SubElement(node_element, 'a')

            if node_type == 'category':
                node_href.set('href', reverse('browse', kwargs={
                    'category_slug': catU.build_breadcrumb(current_node)[0].url }))
            else:
                node_href.set('href', reverse('meta:standard', kwargs={
                    'tag_title': current_node.title }))

            node_href.set(
                'id', ('category-' if node_type == 'category' else 'tag-') + str(current_node.id))
            node_href.text = current_node.title if node_type == 'category' else (
                current_node.title + ': ' + (current_node.description if current_node.description else ''))

        node_elements.append(node_element)

    return node_elements