示例#1
0
def edit_discussion(request, id):
    """edit a discussion"""
    # prepare messages
    mm = MessageManager(request)
    d = get_object_or_404(Discussion, pk=id)
    if request.user.is_authenticated() and request.user.username == d.user.username:
        if request.POST:
            form = CreateDiscussionForm(request.POST, request.FILES, user=request.user)
            if form.is_valid():
                form.update(request, d)
                mm.set_success("Your discussion has been updated")
                return redirect(d.get_absolute_url())
            else:
                # form had errors
                return render(request, 'main/discussion/edit.html', mm.messages(), {'form':form, 'discussion':d})
        else:
            # prepare new form for user
            dpk = None
            if d.group():
                dpk = d.group().pk
            term_str = delim_list(object_taxon(d), ',')
            form = CreateDiscussionForm(user=request.user, initial={'group': dpk, 'title':d.title, 'body':d.body, 'type':d.type, 'terms':term_str})
            
            return render(request, 'main/discussion/edit.html', mm.messages(), {'form':form, 'discussion':d})
    else:
        return redirect(request.META.get('HTTP_REFERER','/'))
示例#2
0
def edit_link(request, id):
    """edit a link"""
    # prepare messages
    mm = MessageManager(request)
    link = get_object_or_404(Link, pk=id)
    if request.user.is_authenticated() and request.user.username == link.user.username:
        if request.POST:
            form = CreateLinkForm(request.POST, request.FILES, user=request.user)
            if form.is_valid():
                form.update(request, link)
                mm.set_success("Your link has been updated")
                return redirect(link.get_absolute_url())
            else:
                # form had errors
                return render(request, 'main/link/edit.html', mm.messages(), {'form':form, 'link':link})
        else:
            # prepare new form for user
            lpk = None
            if link.group():
                lpk = link.group().pk
            term_str = delim_list(object_taxon(link), ',')
            form = CreateLinkForm(user=request.user, initial={'group': lpk, 'title':link.title, 'url':link.url, 'terms':term_str})
            
            return render(request, 'main/link/edit.html', mm.messages(), {'form':form, 'link':link})
    else:
        return redirect(request.META.get('HTTP_REFERER','/'))
示例#3
0
def edit_code_package(request, id):
    """edit a code package"""
    # prepare messages
    mm = MessageManager(request)
    cp = get_object_or_404(CodePackage, pk=id)
    if request.user.is_authenticated() and request.user.username == cp.user.username:
        if request.POST:
            form = EditCodePackageForm(request.POST, user=request.user)
            if form.is_valid():
                form.save(request, cp)
                mm.set_success("Your code package has been updated")
                return redirect(cp.get_absolute_url())
            else:
                # form had errors
                return render(request, 'main/code_package/edit.html', mm.messages(), {'form':form, 'code_package':cp})
        else:
            # prepare new form for user
            cpk = None
            if cp.group():
                cpk = cp.group().pk
            term_str = delim_list(object_taxon(cp), ',')
            form = EditCodePackageForm(user=request.user, initial={'group': cpk, 'title':cp.title, 'description':cp.description, 'terms':term_str})
            
            return render(request, 'main/code_package/edit.html', mm.messages(), {'form':form, 'code_package':cp})
    else:
        return redirect(request.META.get('HTTP_REFERER','/'))
示例#4
0
def SearchCore(query=None, start=0, len=RESULTS_PER_PAGE, sort_by=0, object_type=None, tags=list()):
    """
    query the database for objects, and sort by either search rank (0) or user rating (1).
    query: query string, start: starting node in results list, len: number of nodes in results, sort_by: sorting method, object_type: object type filter
    """
    # TODO: organize this better
    
    # return tag results
    if tags:
        results = list()
        if object_type:
            results.extend(Taxon.objects.get_objects_of_type_for_term(term_list=tags, object_type=object_type))
        else:
            results.extend(Taxon.objects.get_objects_of_type_for_term(term_list=tags, object_type=Discussion))
            results.extend(Taxon.objects.get_objects_of_type_for_term(term_list=tags, object_type=CodePackage))
            results.extend(Taxon.objects.get_objects_of_type_for_term(term_list=tags, object_type=Link))
          
        return SearchMeta(results=results[start:start+len], node_count=results.__len__(), query=query, page_len=len, start=start, prev=start-1, next=start+len)
        
        
        
    # default query
    if query == None or query == "":
        if object_type is None:
            return None
        else:
            results = list()
            # TODO: validate the class type
            results.extend(object_type._default_manager.all().reverse())
            return SearchMeta(results=results[start:start+len], node_count=results.__len__(), query=query, page_len=len, start=start, prev=start-1, next=start+len)
        
        #results = list()
        #
        #if object_type is None:
        #    results.extend(CodePackage._default_manager.all().reverse())
        #    results.extend(Discussion._default_manager.all().reverse())
        #    results.extend(Link._default_manager.all().reverse())
        #else:
        #    results.extend(object_type._default_manager.all().reverse())
        #sMeta = SearchMeta(results=results[start:start+len], node_count=results.__len__(), query=None, page_len=len, start=start, prev=start-1, next=start+len)
        #return sMeta
    
    # setup
    ranker = tfidf()
    ranker.set_query(query)
    ranker.is_strict(False)

    # add discussions
    for d in Discussion.objects.iterator():
        mylist = (t.term for t in object_taxon(d))
        doc = tfidf_doc("%s %s" % (d.body, d.title), d.pk, d.__class__.__name__)#, (t.term.term for t in object_taxon(d)))
        ranker.add_document(doc)
    
    # add links
    for d in Link.objects.iterator():
        doc = tfidf_doc(d.title, d.pk, d.__class__.__name__)#, (t.term.term for t in object_taxon(d)))
        ranker.add_document(doc)
        
    # add code packages
    for d in CodePackage.objects.iterator():
        doc = tfidf_doc("%s %s" % (d.description, d.title), d.pk, d.__class__.__name__)#, (t.term.term for t in object_taxon(d)))
        ranker.add_document(doc)
        
    # add comments
    for c in Comment.objects.iterator():
        doc = tfidf_doc(c.comment, c.pk, c.__class__.__name__)
        ranker.add_document(doc)
    
    # add groups
    for g in cGroup.objects.iterator():
        doc = tfidf_doc("%s %s" % (g.description, g.name), g.pk, g.__class__.__name__)
        ranker.add_document(doc)
        
    # add curriculums
    for c in Curriculum.objects.iterator():
        doc = tfidf_doc("%s %s" % (c.description, c.title), c.pk, c.__class__.__name__)
        ranker.add_document(doc)
        
    # add lessons
    for l in Lesson.objects.iterator():
        doc = tfidf_doc("%s %s" % (l.description + ' ' + l.body , l.title), l.pk, l.__class__.__name__)
        ranker.add_document(doc)
        
    # rank it!
    ranks = ranker.rank_it()
    
    # collect objects
    results = list()
    for r in ranks:
        if r.class_name == "Discussion":
            try:
                d = Discussion.objects.get(id=r.id)
                d.search_rank = r.key()
                results.append(d)
            except Discussion.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought Discussion id %s but found none." % (r.id))
        elif r.class_name == "CodePackage":
            try:
                d = CodePackage.objects.get(id=r.id)
                d.search_rank = r.key()
                results.append(d)
            except CodePackage.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought CodePackage id %s but found none." % (r.id))
        elif r.class_name == "Link":
            try:
                d = Link.objects.get(id=r.id)
                d.search_rank = r.key()
                results.append(d)
            except Link.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought Link id %s but found none." % (r.id))
        elif r.class_name == "Comment":
            try:
                d = Comment.objects.get(id=r.id)
                d.search_rank = r.key()
                results.append(d)
            except Comment.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought Comment id %s but found none." % (r.id))
        elif r.class_name == "cGroup":
            try:
                d = cGroup.objects.get(id=r.id)
                d.search_rank = r.key()
                results.append(d)
            except cGroup.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought cGroup id %s but found none." % (r.id))
        elif r.class_name == "Curriculum":
            try:
                d = Curriculum.objects.get(id=r.id)
                results.append(d)
            except Curriculum.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought Curriculum id %s but found none." % (r.id))
        elif r.class_name == "Lesson":
            try:
                d = Lesson.objects.get(id=r.id)
                results.append(d)
            except Lesson.DoesNotExist:
                raise Exception("TF-IDF: tfidf_doc.id is invalid or corrupt. Sought Lesson id %s but found none." % (r.id))
    sMeta = SearchMeta(results=results[start:start+len], node_count=results.__len__(), query=query, page_len=len, start=start, prev=start-1, next=start+len)
    return sMeta
示例#5
0
def HTMLWrapper(obj):
    """encapsulate searchable objects into template compatible nodes that can be displayed on the website."""
    SNIPPET_LENGTH = 180
    class Encapsulator():
        """create a compatable template node"""
        def __init__(self, obj_class="", human_class="", url="", title="", rank=0, rating=0, snippet="", tags=list(), group=None, object=None):
            self.obj_class = obj_class #machine readable class name
            self.human_class = human_class #human readable class name
            self.url = url
            self.title = title
            self.rank = rank
            self.rating = rating
            self.snippet = snippet
            self.tags = tags
            self.group = group
            self.object = object
        
        def get_absolute_url():
            return self.url
    
    # display more link.
    # display rank details.
    # display tags
    # display quick links?? like download, comment, etc.
    if obj.__class__.__name__ == 'CodePackage':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'code package',
                            url=obj.get_absolute_url(),
                            title=obj.title,
                            rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.description, SNIPPET_LENGTH, "..."),
                            tags=object_taxon(obj),
                            group=obj.group,
                            object=obj)
    elif obj.__class__.__name__ == 'Discussion':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'discussion',
                            url=obj.get_absolute_url(),
                            title=obj.title,
                            rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.body, SNIPPET_LENGTH, "..."),
                            tags=object_taxon(obj),
                            group=obj.group,
                            object=obj)
    elif obj.__class__.__name__ == 'Link':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'link',
                            url=obj.url,
                            title=obj.title,
                            rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.url, SNIPPET_LENGTH, "..."),
                            tags=object_taxon(obj),
                            group=obj.group,
                            object=obj)
    elif obj.__class__.__name__ == 'Comment':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'comment',
                            url=obj.get_absolute_url(),
                            title=shorten(obj.comment, SNIPPET_LENGTH, "..."),
                            # TODO: find a more generic way to keep track of the search_rank
                            #rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.comment, SNIPPET_LENGTH, "..."),
                            tags=list())
    elif obj.__class__.__name__ == 'cGroup':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'group',
                            url=obj.get_absolute_url(),
                            title=shorten(obj.name, SNIPPET_LENGTH, "..."),
                            rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.description, SNIPPET_LENGTH, "..."),
                            tags=list())
    elif obj.__class__.__name__ == 'Curriculum':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'curriculum',
                            url=obj.get_absolute_url(),
                            title=shorten(obj.title, SNIPPET_LENGTH, "..."),
                            #rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.description, SNIPPET_LENGTH, "..."),
                            tags=list())
    elif obj.__class__.__name__ == 'Lesson':
        return Encapsulator(obj_class=obj.__class__.__name__.lower(),
                            human_class = 'lesson',
                            url=obj.get_absolute_url(),
                            title=shorten(obj.title, SNIPPET_LENGTH, "..."),
                            #rank=obj.search_rank,
                            rating=0,
                            snippet=shorten(obj.description + ' ' + obj.body, SNIPPET_LENGTH, "..."),
                            tags=list())