def index(request, list_kind=1, page=1, direction="dsc", order_by="id"): """index""" if request.user.is_superuser: account_type = 1 else: account_type = request.session["user_filter"]["account_type"] list_kind = int(list_kind) ordering = order_by filter_active = False if direction == "dsc": ordering = order_by order_by = "-%s" % order_by listing = List.objects.values("id", "to_address", "from_address").filter(list_type=list_kind).order_by(order_by) if not request.user.is_superuser: query = Q() addresses = request.session["user_filter"]["addresses"] if account_type == 2: if addresses: for domain in addresses: kwarg = {"to_address__iendswith": domain} query = query | Q(**kwarg) listing = listing.filter(query) else: listing = listing.filter(user=request.user.id) if account_type == 3: # if addresses: # for email in addresses: # kw = {'to_address__exact':email} # q = q | Q(**kw) # kw = {'to_address__exact':request.user.username} # q = q | Q(**kw) # listing = listing.filter(q) # else: listing = listing.filter(user=request.user.id) if request.method == "POST": filter_form = FilterForm(request.POST) if filter_form.is_valid(): request.session["query_type"] = int(filter_form.cleaned_data["query_type"]) request.session["search_for"] = filter_form.cleaned_data["search_for"] request.session.modified = True search_for = request.session.get("search_for", "") query_type = request.session.get("query_type", 1) if search_for != "": filter_active = True if query_type == 1: if ordering == "to_address": listing = listing.filter(to_address__icontains=search_for) elif ordering == "from_address": listing = listing.filter(from_address__icontains=search_for) else: if ordering == "to_address": listing = listing.exclude(to_address__icontains=search_for) elif ordering == "from_address": listing = listing.exclude(from_address__icontains=search_for) if request.is_ajax(): p = Paginator(listing, 15) if page == "last": page = p.num_pages po = p.page(page) listing = po.object_list listing = map(jsonify_list, listing) page = int(page) ap = 2 sp = max(page - ap, 1) if sp <= 3: sp = 1 ep = page + ap + 1 pn = [n for n in range(sp, ep) if n > 0 and n <= p.num_pages] pg = { "page": page, "pages": p.num_pages, "page_numbers": pn, "next": po.next_page_number(), "previous": po.previous_page_number(), "has_next": po.has_next(), "has_previous": po.has_previous(), "show_first": 1 not in pn, "show_last": p.num_pages not in pn, "app": "lists", "list_kind": list_kind, "direction": direction, "order_by": ordering, "filter_active": filter_active, } json = simplejson.dumps({"items": listing, "paginator": pg}) return HttpResponse(json, mimetype="application/javascript") return object_list( request, template_name="lists/index.html", queryset=listing, paginate_by=15, page=page, extra_context={ "app": "lists", "list_kind": list_kind, "direction": direction, "order_by": ordering, "filter_active": filter_active, "list_all": 0, }, )
def index(request, list_kind=1, page=1, direction='dsc', order_by='id'): """index""" if request.user.is_superuser: account_type = 1 else: account_type = request.session['user_filter']['account_type'] list_kind = int(list_kind) ordering = order_by filter_active = False if direction == 'dsc': ordering = order_by order_by = "-%s" % order_by listing = List.objects.values( 'id', 'to_address', 'from_address').filter(list_type=list_kind).order_by(order_by) if not request.user.is_superuser: query = Q() addresses = request.session['user_filter']['addresses'] if account_type == 2: if addresses: for domain in addresses: kwarg = {'to_address__iendswith': domain} query = query | Q(**kwarg) listing = listing.filter(query) else: listing = listing.filter(user=request.user.id) if account_type == 3: listing = listing.filter(user=request.user.id) if request.method == 'POST': filter_form = FilterForm(request.POST) if filter_form.is_valid(): request.session['query_type'] = int( filter_form.cleaned_data['query_type']) request.session['search_for'] = ( filter_form.cleaned_data['search_for']) request.session.modified = True search_for = request.session.get('search_for', '') query_type = request.session.get('query_type', 1) if search_for != "": filter_active = True if query_type == 1: if ordering == 'to_address': listing = listing.filter(to_address__icontains=search_for) elif ordering == 'from_address': listing = listing.filter(from_address__icontains=search_for) else: if ordering == 'to_address': listing = listing.exclude(to_address__icontains=search_for) elif ordering == 'from_address': listing = listing.exclude(from_address__icontains=search_for) if request.is_ajax(): p = Paginator(listing, 15) if page == 'last': page = p.num_pages po = p.page(page) listing = po.object_list listing = map(jsonify_list, listing) page = int(page) ap = 2 sp = max(page - ap, 1) if sp <= 3: sp = 1 ep = page + ap + 1 pn = [n for n in range(sp, ep) if n > 0 and n <= p.num_pages] pg = { 'page': page, 'pages': p.num_pages, 'page_numbers': pn, 'next': po.next_page_number(), 'previous': po.previous_page_number(), 'has_next': po.has_next(), 'has_previous': po.has_previous(), 'show_first': 1 not in pn, 'show_last': p.num_pages not in pn, 'app': 'lists', 'list_kind': list_kind, 'direction': direction, 'order_by': ordering, 'filter_active': filter_active } json = anyjson.dumps({'items': listing, 'paginator': pg}) return HttpResponse(json, mimetype='application/javascript') return object_list(request, template_name='lists/index.html', queryset=listing, paginate_by=15, page=page, extra_context={ 'app': 'lists', 'list_kind': list_kind, 'direction': direction, 'order_by': ordering, 'filter_active': filter_active, 'list_all': 0 })
def index(request, list_kind=1, page=1, direction='dsc', order_by='id'): """index""" if request.user.is_superuser: account_type = 1 else: account_type = request.session['user_filter']['account_type'] list_kind = int(list_kind) ordering = order_by filter_active = False if direction == 'dsc': ordering = order_by order_by = "-%s" % order_by listing = List.objects.values('id', 'to_address', 'from_address').filter( list_type=list_kind).order_by(order_by) if not request.user.is_superuser: query = Q() addresses = request.session['user_filter']['addresses'] if account_type == 2: if addresses: for domain in addresses: kwarg = {'to_address__iendswith': domain} query = query | Q(**kwarg) listing = listing.filter(query) else: listing = listing.filter(user=request.user.id) if account_type == 3: listing = listing.filter(user=request.user.id) if request.method == 'POST': filter_form = FilterForm(request.POST) if filter_form.is_valid(): request.session['query_type'] = int( filter_form.cleaned_data['query_type']) request.session['search_for'] = ( filter_form.cleaned_data['search_for']) request.session.modified = True search_for = request.session.get('search_for', '') query_type = request.session.get('query_type', 1) if search_for != "": filter_active = True if query_type == 1: if ordering == 'to_address': listing = listing.filter(to_address__icontains=search_for) elif ordering == 'from_address': listing = listing.filter(from_address__icontains=search_for) else: if ordering == 'to_address': listing = listing.exclude(to_address__icontains=search_for) elif ordering == 'from_address': listing = listing.exclude(from_address__icontains=search_for) if request.is_ajax(): p = Paginator(listing, 15) if page == 'last': page = p.num_pages po = p.page(page) listing = po.object_list listing = map(jsonify_list, listing) page = int(page) ap = 2 sp = max(page - ap, 1) if sp <= 3: sp = 1 ep = page + ap + 1 pn = [n for n in range(sp, ep) if n > 0 and n <= p.num_pages] pg = {'page': page, 'pages': p.num_pages, 'page_numbers': pn, 'next': po.next_page_number(), 'previous': po.previous_page_number(), 'has_next': po.has_next(), 'has_previous': po.has_previous(), 'show_first': 1 not in pn, 'show_last': p.num_pages not in pn, 'app': 'lists', 'list_kind': list_kind, 'direction': direction, 'order_by': ordering, 'filter_active': filter_active} json = anyjson.dumps({'items': listing, 'paginator': pg}) return HttpResponse(json, mimetype='application/javascript') return object_list(request, template_name='lists/index.html', queryset=listing, paginate_by=15, page=page, extra_context={'app': 'lists', 'list_kind': list_kind, 'direction': direction, 'order_by': ordering, 'filter_active': filter_active, 'list_all': 0})