Example #1
0
def index(request):

    if request.method == 'POST':
        bform = FileUploadForm(request.POST, request.FILES)
        bchat = ChatForm(request.POST)
        if bform.is_valid():
            new_file = bform.save(commit=False)
            new_file.save()
        if bchat.is_valid():
            new_chat = bchat.save(commit=False)
            new_chat.chat_user = Profile.objects.get(user=request.user)
            new_chat.save()
    else:
        bform = FileUploadForm()
        bchat = ChatForm()

    mentor_count = Profile.objects.filter(is_mentor=True).count()
    reply_count = Reply.objects.all().count()
    question_count = Question.objects.all().count()
    mentor_list = Profile.objects.filter(is_mentor=True).annotate(review_count=Count('review_mentor__id')).order_by("-review_count")[0:4]
    notice_list = Notice.objects.order_by('-created_at')[0:12]
    freeboard_list = Freeboard.objects.order_by('-created_at')[0:12]
    file_list = Files.objects.order_by('-created_at')
    sitehits = Sitehits.objects.all()[0]
    sitehits.hits = sitehits.hits + 1
    sitehits.save()
    currentpoll = Poll.objects.order_by('-poll_date')[0]
    currentpoll_choices = Choice.objects.filter(question=currentpoll).order_by('choice_text')
    chat = Chat.objects.order_by('-chat_time')[0:10]
    context = {
        'question_count': question_count,
        'reply_count': reply_count,
        'mentor_count': mentor_count,
        'mentor_list': mentor_list,
        'notice_list': notice_list,
        'freeboard_list': freeboard_list,
        'hits' : sitehits.hits,
        'poll' : currentpoll,
        'poll_choices' : currentpoll_choices,
        'file_list' : file_list,
        'bchat' : bchat,
        'chat' : chat,
    }
    return render(request, 'blog/index.html', context)