コード例 #1
0
ファイル: views.py プロジェクト: campherkurt/glowmob
def chapter(request, chapter_id):
        
    chapter = get_object_or_404(Chapter, id=chapter_id)
    content_type = ContentType.objects.get_for_model(Chapter)
    
    if request.user.is_authenticated():
        profile = request.user.profile_set.latest('pk')
        profile.last_story = chapter.story
        profile.last_chapter = chapter
        profile.save()
        stat = Stat(content_type=content_type, object_id=chapter.pk, user=request.user)
    else:
        stat = Stat(content_type=content_type, object_id=chapter.pk)
    stat.save()
    

    comments = ThreadedComment.objects.filter(
        content_type=content_type,
        object_id=chapter.id
        ).exclude(status=threadedcomments.models.STATUS_MODERATION)

    comments = comments.order_by("-date_submitted")

    variables = {
        "chapter": chapter,
        "comments": comments,
    }
    
    if request.GET.has_key("p"):
        p = int(request.GET["p"] or 1)
        if p == 1:
            start = 0
        else:
            start = p - 2
        chapters = Chapter.objects.filter(story=chapter.story).filter(publish_date__lte=datetime.now()).order_by('order','created','id')
        total_count = chapters.count()
        chapters = chapters[start : p + 1]

        if p > 1:
            variables["prev_chapter"] = Chapter.objects.get(id=chapters[0].id)
            
        if p + 1 <= total_count:
            if chapters.count() == 3:
                variables["next_chapter"] = Chapter.objects.get(id=chapters[2].id)
            elif chapters.count() == 2:
                variables["next_chapter"] = Chapter.objects.get(id=chapters[1].id)
            
        variables["next_page"] = p + 1
        variables["prev_page"] = p - 1
        variables["p"] = p

    return render_to_response("stories/chapter.html", variables, context_instance=RequestContext(request))
コード例 #2
0
ファイル: views.py プロジェクト: campherkurt/glowmob
def story(request, story_id):
#    if not request.META.has_key(MXIT_HEADER):
#        if not request.user.is_authenticated():
#            return HttpResponseRedirect("/account/login/")
    
    story = Story.public.get(id=story_id)
    
    chapters = Chapter.objects.filter(story=story).filter(publish_date__lte=datetime.now()).order_by('order','created','id')
    
    chapter = chapters[:1]
    try:
        chapter = chapter[0]
    except:
        chapter = None
    
    content_type = ContentType.objects.get_for_model(Story)
    
    comments = ThreadedComment.objects.filter(
        content_type=content_type, 
        object_id=story.id,
        ).exclude(status=threadedcomments.models.STATUS_MODERATION)

    comments = comments.order_by("-date_submitted")

    
    if request.user.is_authenticated():
        stat = Stat(content_type=content_type, object_id=story.id, user=request.user)
    else:
        stat = Stat(content_type=content_type, object_id=story.id)
        
    stat.save()

    from apps.story.models import Announcement

    announcements = Announcement.objects.filter(story=story).order_by("-created")[:5]
    
    try:
        back = request.META.get('HTTP_REFERER', None)
    except:
        back = None

    competition = Competition.visible.filter(story=story).order_by("-created")[:1]
    if len(competition)> 0:
        competition = competition[0]
    #print '\\n\n\n\n\n\n\n\nComp %s\n\n\n\n\n' % competition[0].title
    pages = Chapter.objects.filter(story=story)[:1]
    
    static_pages = story.page_set.all()

    reviews = Review.objects.filter(
        content_type=content_type,
        object_id=story.id,
        is_approved=True
    )
    review_count = reviews.count()

    try:
        page = int(request.GET['page'])
    except:
        page = 1
    
    data = {
        "story": story,
        "chapters": chapters,
        "comments": comments,
        "chapter": chapter,
        "announcements": announcements,
        "competition": competition,
        #"page": page,
        'static_pages': static_pages,
        'review_count': review_count,
        'page': page,
        "back": back
    }

    try:
        page = int(request.GET['review_page'])
    except:
        page = 1

    data["review_page"] = page

    data["has_review_permission"] = False

    if has_permission_to_review(request, story):
        data["has_review_permission"] = True

    if review_count > 0:
        limit = 6
        page_count = int(math.ceil(review_count/float(limit)))

        reviews = reviews[(page - 1) * limit: page * limit]

        prev = False
        prev_page = 1
        next = False
        next_page = page_count

        if page > 1:
            prev = True
            prev_page = page - 1

        if page < page_count:
            next = True
            next_page = page + 1

        if page * limit > review_count:
            to = review_count
        else:
            to = page * limit

        data["start"] = ((page - 1) * limit)

        data['reviews'] = reviews

        data["indicator"] = "%s - %s of %s" % ((((page - 1) * limit) + 1), to, review_count)
        data["prev"] = prev
        data["prev_page"] = prev_page
        data["next"] = next
        data["next_page"] = next_page

    return render_to_response("stories/story.html", data, context_instance=RequestContext(request))