Exemple #1
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)
Exemple #2
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)
Exemple #3
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)
Exemple #4
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,
        }