Beispiel #1
0
def account_profile_view(request, pk):
    user = get_object_or_404(User, pk=pk)
    blogs = user.blog_set.filter(draft=False, trash=False).select_related(depth=1)
    blog_count = blogs.count()

    pager = Paginator(blogs, 8)
    p = request.GET.get('page') or 1

    try:
        pagination = pager.page(p)
        blogs = pagination.object_list
    except (PageNotAnInteger, EmptyPage):
        raise Http404

    p = int(p)

    page_range = get_page_range(pagination)

    context = {'blogs': blogs,
               'has_pager': len(page_range) > 1,
               'pagination': pagination,
               'page': p,
               'pager': pager,
               'page_range': page_range,
               'viewed_user': user,
               'blog_count': blog_count}

    return render(request, 'account/account_profile_view.html', context)
Beispiel #2
0
def account_manage_users(request):
    if not request.user.is_staff:
        return render(request, '403.html', status=403)

    users = User.objects.all()

    if request.GET.get('sort') and request.GET.get('order'):
        sort = request.GET.get('sort')
        order = request.GET.get('order')

        if sort == 'role':
            sort = ['is_staff']
        elif sort == 'name':
            sort = ['first_name', 'last_name']
        else:
            sort = [sort]

        if order == 'desc':
            users = users.order_by(*['-%s' % sort_col for sort_col in sort])
        else:
            users = users.order_by(*['%s' % sort_col for sort_col in sort])
    else:
        order = 'desc'
        users = users.order_by('-date_joined')

    pager = Paginator(users, 20)
    p = request.GET.get('page') or 1

    try:
        pagination = pager.page(p)
        users = pagination.object_list
    except (PageNotAnInteger, EmptyPage):
        raise Http404

    p = int(p)

    page_range = get_page_range(pagination)

    context = {
        'users': users,
        'has_pager': len(page_range) > 1,
        'pagination': pagination,
        'page': p,
        'pager': pager,
        'page_range': page_range,
        'order': order == 'desc' and 'asc' or 'desc',
        }

    return render(request, 'account/account_manage_users.html', context)
Beispiel #3
0
def blog_all(request, title='Latest Stories', filter={}, filter_text={}, url=None, param='', color='blue'):
    items = Blog.objects.filter(draft=False, 
                                trash=False
                            ).select_related(
                                depth=1
                            ).filter(
                                **filter
                            ).order_by('-published')
    
    page_size = request.GET.get('page_size') or 8
   
    pager = Paginator(items, int(page_size))
    p = request.GET.get('page') or 1
    
    try:
        pagination = pager.page(p)
        blogs = pagination.object_list
    except (PageNotAnInteger, EmptyPage):
        raise Http404
    
    p = int(p)
    
    filter = filter_text if filter_text else filter
    
    if not url:
        url = reverse('blog_all')
    
    page_range = get_page_range(pagination)
    context = {
        'title': title,
        'blogs': blogs,
        'has_pager': len(page_range) > 1,
        'pagination': pagination,
        'page': p,
        'pager': pager,
        'page_range': page_range,
        'filter': filter,
        'url': url,
        'param': param,
        'color': color,
        'total_blogs': items.count(),
        'page_size': page_size,
    }
    
    return render(request, 'blog/blog_list.html', context)
Beispiel #4
0
def blog_search(request):
    """Search blogs by title and description"""
    keyword = request.REQUEST.get("keyword", '')

    context = {'title': 'keyword: %s' % keyword,
               'keyword': keyword,
               'param': 'keyword=%s' % keyword}

    keyword = keyword.strip()
    if keyword:

        blogs = Blog.objects.filter(
            Q(title__icontains=keyword) |
            Q(description__icontains=keyword)
        ).order_by('-published')

        pager = Paginator(blogs, 8)
        p = request.GET.get('page') or 1

        try:
            pagination = pager.page(p)
            blogs = pagination.object_list
        except (PageNotAnInteger, EmptyPage):
            raise Http404

        p = int(p)

        page_range = get_page_range(pagination)

        context.update({'blogs': blogs,
                        'has_pager': len(page_range) > 1,
                        'pagination': pagination,
                        'page': p,
                        'pager': pager,
                        'page_range': page_range,})

    return render(request, 'blog/blog_search.html', context)
Beispiel #5
0
def blog_manage(request, section):
    if request.user.is_superuser:
        blogs = Blog.objects.annotate(num_loves=Count('love'))
    else:
        blogs = Blog.objects.filter(user=request.user).annotate(num_loves=Count('love'))

    if request.GET.get('sort') and request.GET.get('order'):
        sort = request.GET.get('sort')
        order = request.GET.get('order')
        if sort == 'num_views':
            sort = 'view_summary__totalcount'
        if order == 'desc':
            blogs = blogs.order_by('-%s' % sort)
        else:
            blogs = blogs.order_by('%s' % sort)
    else:
        order = 'desc'
        blogs = blogs.order_by('-published')

    blog_all = blogs.filter(trash=False)
    blog_published = blogs.filter(draft=False, trash=False)
    blog_draft = blogs.filter(draft=True, trash=False)
    blog_trash = blogs.filter(trash=True)

    if section == 'published':
        url = reverse('blog_manage_published')
    elif section == 'draft':
        url = reverse('blog_manage_draft')
    elif section == 'trash':
        url = reverse('blog_manage_trash')
    else:
        url = reverse('blog_manage')

    can_restore = False
    if section == 'published':
        blogs = blog_published
    elif section == 'draft':
        blogs = blog_draft
    elif section == 'trash':
        blogs = blog_trash
        can_restore = True
    else:
        blogs = blog_all

    pager = Paginator(blogs, 20)
    p = request.GET.get('page') or 1

    try:
        pagination = pager.page(p)
        blogs = pagination.object_list
    except (PageNotAnInteger, EmptyPage):
        raise Http404

    p = int(p)

    page_range = get_page_range(pagination)

    context = {
        'blogs': blogs,
        'can_restore': can_restore,
        'num_all': blog_all.count(),
        'num_published': blog_published.count(),
        'num_draft': blog_draft.count(),
        'num_trash': blog_trash.count(),
        'has_pager': len(page_range) > 1,
        'pagination': pagination,
        'page': p,
        'pager': pager,
        'page_range': page_range,
        'url': url,
        'order': order == 'desc' and 'asc' or 'desc',
        'section': section,
    }

    return render(request, 'blog/blog_manage.html', context)