Ejemplo n.º 1
0
def show_text(request, text_id):
    text = get_object_or_404(Text, id=text_id)
    if request.user.is_anonymous() or (not text.user == request.user and not request.user.is_staff):
    # only text owner can see own hidden texts
        if text.is_hidden == 1:
           return HttpResponseForbidden("403 Forbidden")
    else:
        text.can_change = 1


    if request.GET.get('rate', None):
        text.rate(request.user, request.GET.get('rate', None))

    client_address = request.META['REMOTE_ADDR']
    text.visit(client_address)
    text.visit_count = text.get_visit_count()

    text.can_rate = text._can_rate(request.user)

    # if current user is subscribed on this author then set text as read
    # check if current user is not anonymous
    if not request.user.is_anonymous():
        # check if user is subscribed on author of current text
        if Subscription.objects.filter(subscriber=request.user, publisher=text.user, is_active=True).count() > 0:
            # check if user already read current text
            if Read_text.objects.filter(text=text, read_by=request.user).count() == 0:
                # create record that current text was read by user
                read_text = Read_text(text=text, read_by=request.user)
                read_text.save()
        # ------------------------------------------------------------------

    # compose title
    user = User.objects.get(id=text.user.id)

    # use unicode(text.get_title(), 'UTF-8')
    try:
        pageTitle = text.get_title() + ", " + user.get_profile().name + " ( " + user.username + " )"
    except:
        pageTitle = text.get_title()
        pass

    if not request.user.is_anonymous() and text.type in [1, 2, 3, 5]:
        is_favorite_enabled = True
        is_favorite = Favorite.objects.filter(text=text, user=request.user).count() > 0
    else:
        is_favorite_enabled = False
        is_favorite = False

    return render_to_response('texts/text_detail.html',
                              {'pagetitle': pageTitle, 'u': request.user, 'text': text, 'type': str(text.type),
                               'is_favorite': is_favorite, 'is_favorite_enabled': is_favorite_enabled},
                              context_instance=RequestContext(request))