示例#1
0
文件: views.py 项目: kpx13/libfic
def statistics_page(request):
    import datetime
    c = get_common_context(request)
    c['title'] = u'Статистика аккаунта'
    if request.method == 'POST':
        try:
            date_from = datetime.datetime.strptime(request.POST.get('date_from', ''),'%d.%m.%Y')
            date_to = datetime.datetime.strptime(request.POST.get('date_to', ''),'%d.%m.%Y')
        except:
            messages.error(request, u'Необходимо выбрать даты')
            return HttpResponseRedirect('/cabinet/statistics/')
        if date_from > date_to:
            dd = date_from
            date_from = date_to
            date_to = dd
        funfics = Work.get_by_author(request.user)
        c['date_from'] = date_from.strftime('%d.%m.%Y')
        c['date_to'] = date_to.strftime('%d.%m.%Y')
        td = datetime.timedelta(days=1)
        result = []
        curr = date_from
        while curr <= date_to:
            stat = []
            for f in funfics:
                print curr, f.id, Review.get_count_by_work_date_range(f, curr, curr+td)
                curr_stat = {
                                'funfic': f,
                                'reviews': Review.get_count_by_work_date_range(f, curr, curr+td),
                                'comments': Comment.get_count_by_work_date_range(f, curr, curr+td),
                                'bookmarks': Bookmark.get_count_by_work_date_range(f, curr, curr+td),
                             }
                curr_stat.update(Rating.get_count_by_work_date_range(f, curr, curr+td))
                stat.append(curr_stat)
            result.append((curr.date(), stat))
            curr = curr + td
        c['statistics'] = result
    return render_to_response('cabinet/statistics.html', c, context_instance=RequestContext(request))
示例#2
0
文件: views.py 项目: kpx13/libfic
def get_common_context(request):
    c = {}
    c['request_url'] = request.path
    c['categories'] = Category.get_all()
    c['registration_form'] = RegistrationForm()
    c['reviews'] = Review.get_recent(COMMENTS_COUNT_ON_PAGE)
    c['comments'] = Comment.get_recent(COMMENTS_COUNT_ON_PAGE)
    c['marks'] = [x for x in range(1, 10)]
    c['genres'] = Genre.get_all()
    c['fandoms'] = Fandom.get_all()
    c['warnings'] = Warning.get_all()
    c['categoriest_first_part'], c['categoriest_second_part'] = Category.get_two_parts()
    c['genre_f_part'], c['genre_s_part'], c['genre_t_part'], c['genre_fo_part'] = Genre.get_four_parts()
    c.update(csrf(request))
    return c
示例#3
0
文件: views.py 项目: kpx13/libfic
def search(request):
    c = get_common_context(request)
    c['title'] = u'Результаты поиска'
    c['search_objects'] = None

    if request.GET.get('search', None):
        search_text = request.GET.get('search')
        search_results = watson.search(search_text, ranking=True)

        work_ids = []
        search_objects = {}
        for res in search_results:
            res_class = res.object.__class__.__name__
            if res_class == 'Work':
                work_id = res.object.id
            elif res_class == 'Review':
                work_id = res.object.work.id
            elif res_class == 'Comment':
                work_id = res.object.work_part.work.id

            if not work_id in work_ids:
                work_ids.append(work_id)
                search_objects[work_id] = {}

            reviews = search_objects[work_id].get('reviews',[])
            comments = search_objects[work_id].get('comments',[])
            if res_class  == 'Review':
                reviews.append(res.object)
                search_objects[work_id]['reviews'] = reviews
            elif res_class == 'Comment':
                comments.append(res.object)
                search_objects[work_id]['comments'] = comments

        funfics = Work.objects.filter(id__in=work_ids)



        sort_par = request.GET.get('sort', 'time')
        if sort_par == 'vizits':
            funfics = funfics.order_by('-vizits')
        elif sort_par == 'comments':
            funfics = funfics.order_by('-commetns')
        elif sort_par == 'rating':
            funfics = funfics.order_by('-mark')
        else:
            funfics = funfics.order_by('-last_editing_time')
            sort_par = 'time'
        c['sort'] = sort_par

        paginator = Paginator(funfics, FUNCICS_COUNT_ON_PAGE)
        page = int(request.GET.get('page', '1'))
        try:
            funfics = paginator.page(page)
        except PageNotAnInteger:
            page = 1
            funfics = paginator.page(page)
        except EmptyPage:
            page = paginator.num_pages
            funfics = paginator.page(page)
        c['page'] = page
        c['page_range'] = paginator.page_range
        if len(c['page_range']) > 1:
            c['need_pagination'] = True

        c['funfics'] = funfics
        c['old_args'] = request.META['QUERY_STRING'].replace('page', 'o').replace('sort', 'o')
        c['reviews'] = Review.get_recent(COMMENTS_COUNT_ON_PAGE)
        c['comments'] = Comment.get_recent(COMMENTS_COUNT_ON_PAGE)
        c['search_text'] = search_text
        c['search_objects'] = search_objects
        c['fun_title'] = u'Результаты поиска по "%s"' % search_text

        return render_to_response('search.html', c, context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect('/')