示例#1
0
文件: object.py 项目: alejo8591/maker
def comments_likes(context, object, expand=True):
    "Comments and Likes/Dislikes box for an object"
    
    request = context['request']
    
    response_format = 'html'
    if 'response_format' in context:
        response_format = context['response_format']
    
    update = isinstance(object, UpdateRecord)
    profile  = request.user.get_profile()
    
    if request.POST.get('like', 0) == unicode(object.id):
        object.likes.add(profile)
        if hasattr(object, 'score'):
            object.score += 1
            object.save()
    
    elif request.POST.get('unlike', 0) == unicode(object.id):
        object.likes.remove(profile)
        if hasattr(object, 'score'):
            object.score -= 1
            object.save()
        
    elif request.POST.get('dislike', 0) == unicode(object.id):
        object.dislikes.add(profile)
        if hasattr(object, 'score'):
            object.score += 1
            object.save()
        
    elif request.POST.get('undislike', 0) == unicode(object.id):
        object.dislikes.remove(profile)
        if hasattr(object, 'score'):
            object.score -= 1
            object.save()
    
    elif request.POST.get('commentobject', 0) == unicode(object.id) and 'comment' in request.POST:
        comment = Comment(author=profile,
                          body=request.POST.get('comment'))
        comment.save()
        if hasattr(object, 'score'):
            object.score += 1
            object.save()
        object.comments.add(comment)
    
    likes      = object.likes.all()
    dislikes   = object.dislikes.all()
    comments   = object.comments.all()
    
    ilike      = profile in likes
    idislike   = profile in dislikes
    icommented = comments.filter(author=profile).exists() or \
                 comments.filter(author__default_group__in=[profile.default_group_id]+[i.id for i in profile.other_groups.all().only('id')]).exists()

    return Markup(render_to_string('core/tags/comments_likes',
                                   {'object': object,
                                    'is_update': update,
                                    'profile': profile,
                                    'likes': likes,
                                    'dislikes': dislikes,
                                    'comments': comments,
                                    'ilike': ilike,
                                    'idislike': idislike,
                                    'icommented': icommented,
                                    'expand': expand },
                                   context_instance=RequestContext(request),
                                   response_format=response_format))
示例#2
0
文件: ajax.py 项目: alejo8591/maker
def comments_likes(request, target, form, expand=True):
    dajax = Dajax()
        
    response_format = 'html'
    
    object_id = form.get('object_id', 0)
    update = form.get('update', 0)
    object = None
    if update:
        object = UpdateRecord.objects.get(pk=object_id)
    else:
        object = Object.objects.get(pk=object_id)
    
    profile  = request.user.get_profile()
    
    if object:
        if form.get('like', 0) == unicode(object.id):
            object.likes.add(profile)
            if hasattr(object, 'score'):
                object.score += 1
                object.save()
        
        elif form.get('unlike', 0) == unicode(object.id):
            object.likes.remove(profile)
            if hasattr(object, 'score'):
                object.score -= 1
                object.save()
            
        elif form.get('dislike', 0) == unicode(object.id):
            object.dislikes.add(profile)
            if hasattr(object, 'score'):
                object.score += 1
                object.save()
            
        elif form.get('undislike', 0) == unicode(object.id):
            object.dislikes.remove(profile)
            if hasattr(object, 'score'):
                object.score -= 1
                object.save()
        
        elif form.get('commentobject', 0) == unicode(object.id) and 'comment' in form:
            comment = Comment(author=profile,
                              body=form.get('comment'))
            comment.save()
            if hasattr(object, 'score'):
                object.score += 1
                object.save()
            object.comments.add(comment)
    
    likes      = object.likes.all()
    dislikes   = object.dislikes.all()
    comments   = object.comments.all()
    
    ilike      = profile in likes
    idislike   = profile in dislikes
    icommented = comments.filter(author=profile).exists() or \
                 comments.filter(author__default_group__in=[profile.default_group_id]+[i.id for i in profile.other_groups.all().only('id')]).exists()

    output = render_to_string('core/tags/comments_likes',
                               {'object': object,
                                'is_update': update,
                                'profile': profile,
                                'likes': likes,
                                'dislikes': dislikes,
                                'comments': comments,
                                'ilike': ilike,
                                'idislike': idislike,
                                'icommented': icommented,
                                'expand': expand },
                               context_instance=RequestContext(request),
                               response_format=response_format)
    
    dajax.add_data({'target': target, 'content': output}, 'maker.add_data')
    return dajax.json()