Revision d220292f snf-astakos-app/astakos/im/views.py

b/snf-astakos-app/astakos/im/views.py
56 56
                                                get_model_and_form_class)
57 57
from django.views.generic.list_detail import object_list, object_detail
58 58
from django.http import HttpResponseBadRequest
59
from django.core.paginator import Paginator, InvalidPage
59 60

  
60 61
from astakos.im.models import (
61 62
    AstakosUser, ApprovalTerms, AstakosGroup, Resource,
......
75 76
                                  SendNotificationError)
76 77
from astakos.im.settings import (
77 78
    COOKIE_NAME, COOKIE_DOMAIN, SITENAME, LOGOUT_NEXT,
78
    LOGGING_LEVEL
79
)
79
    LOGGING_LEVEL, PAGINATE_BY)
80 80
from astakos.im.tasks import request_billing
81 81

  
82 82
logger = logging.getLogger(__name__)
......
716 716
@signed_terms_required
717 717
@login_required
718 718
def group_list(request):
719
    q = request.user.astakos_groups.none()
719 720
    list = request.user.astakos_groups.select_related().all()
720
    return object_list(request, queryset=list,
721
                       extra_context=dict(
722
                       is_search=False
723
                       )
724
                       )
721
    d = {}
722
    d['own'] = [g for g in list if request.user in g.owner.all()]
723
    d['other'] = list.exclude(id__in=(g.id for g in d['own']))
724
    for k, queryset in d.iteritems():
725
        paginator = Paginator(queryset, PAGINATE_BY)
726
        page = request.GET.get('%s_page' % k, 1)
727
        try:
728
            page_number = int(page)
729
        except ValueError:
730
            if page == 'last':
731
                page_number = paginator.num_pages
732
            else:
733
                # Page is not 'last', nor can it be converted to an int.
734
                raise Http404
735
        try:
736
            page_obj = locals()['%s_page_obj' % k] = paginator.page(page_number)
737
        except InvalidPage:
738
            raise Http404
739
    return object_list(request, queryset=q,
740
                       extra_context={'is_search':False,
741
                                      'mine': locals()['own_page_obj'],
742
                                      'other': locals()['other_page_obj']})
725 743

  
726 744

  
727 745
@signed_terms_required
......
765 783
@signed_terms_required
766 784
@login_required
767 785
def group_search(request, extra_context=None, **kwargs):
786
    q = request.GET.get('q')
768 787
    if request.method == 'GET':
769
        form = AstakosGroupSearchForm()
788
        form = AstakosGroupSearchForm({'q': q} if q else None)
770 789
    else:
771 790
        form = AstakosGroupSearchForm(get_query(request))
772 791
        if form.is_valid():
773 792
            q = form.cleaned_data['q'].strip()
774
            queryset = AstakosGroup.objects.select_related(
775
            ).filter(name__contains=q)
776
            return object_list(
777
                request,
778
                queryset,
779
                template_name='im/astakosgroup_list.html',
780
                extra_context=dict(form=form,
781
                                   is_search=True))
782
    return render_response(
783
        template='im/astakosgroup_list.html',
784
        form=form,
785
        context_instance=get_context(request, extra_context),
786
        is_search=False
787
    )
793
    if q:
794
        queryset = AstakosGroup.objects.select_related(
795
        ).filter(name__contains=q)
796
    else:
797
        queryset = AstakosGroup.objects.none()
798
    return object_list(
799
        request,
800
        queryset,
801
        paginate_by=PAGINATE_BY,
802
        page=request.GET.get('page') or 1,
803
        template_name='im/astakosgroup_list.html',
804
        extra_context=dict(form=form,
805
                           is_search=True,
806
                           q=q))
788 807

  
789 808
@signed_terms_required
790 809
@login_required
791 810
def group_all(request, extra_context=None, **kwargs):
792
    if request.method != 'POST':
793
        return HttpResponseBadRequest(_('Bad method'))
794 811
    return object_list(
795 812
                request,
796 813
                AstakosGroup.objects.select_related().all(),
814
                paginate_by=PAGINATE_BY,
815
                page=request.GET.get('page') or 1,
797 816
                template_name='im/astakosgroup_list.html',
798 817
                extra_context=dict(form=AstakosGroupSearchForm(),
799 818
                                   is_search=True))

Also available in: Unified diff