Advertisement
Regex404

views.py

May 26th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. def products(request):
  2.     categories = Category.objects.all()
  3.     if request.method == 'POST':
  4.         pagination_content = ""
  5.         page_number = request.POST['data[page]'] if request.POST['data[page]'] else 1
  6.         page = int(page_number)
  7.         name = request.POST['data[name]']
  8.         sort = '-' if request.POST['data[sort]'] == 'DESC' else ''
  9.         search = request.POST['data[search]']
  10.         max = int(request.POST['data[max]'])
  11.                
  12.         cur_page = page
  13.         page -= 1
  14.         per_page = max # Set the number of results to display
  15.         start = page * per_page
  16.        
  17.         # If search keyword is not empty, we include a query for searching
  18.         # the "content" or "name" fields in the database for any matched strings.
  19.         if search:       
  20.             all_posts = Product.objects.filter(Q(content__contains = search) | Q(name__contains = search)).exclude(available = False).order_by(sort + name)[start:per_page]
  21.             count = Product.objects.filter(Q(content__contains = search) | Q(name__contains = search)).exclude(available = False).count()
  22.            
  23.         else:
  24.             all_posts = Product.objects.exclude(available = False).order_by(sort + name)[start:cur_page * max]
  25.             count = Product.objects.exclude(available = False).count()
  26.        
  27.         if all_posts:
  28.             for post in all_posts:
  29.                 pagination_content += '''
  30.                     <div class='col-sm-3'>
  31.                         <div class='panel panel-default'>
  32.                             <div class='panel-heading'>%s</div>
  33.                             <div class='panel-body p-0 p-b'>
  34.                                 <a href='%s'>
  35.                                     <img src='%s' width='%s' class='img-responsive'>
  36.                                 </a>
  37.                                 <div class='list-group m-0'>
  38.                                     <div class='list-group-item b-0 b-t'>
  39.                                         <i class='fa fa-calendar-o fa-2x pull-left ml-r'></i>
  40.                                         <p class='list-group-item-text'>Price</p>
  41.                                         <h4 class='list-group-item-heading'>$%s</h4>
  42.                                     </div>
  43.                                     <div class='list-group-item b-0 b-t'>
  44.                                         <i class='fa fa-calendar fa-2x pull-left ml-r'></i>
  45.                                         <p class='list-group-item-text'>On Stock</p>
  46.                                         <h4 class='list-group-item-heading'>%d</h4>
  47.                                     </div>
  48.                                 </div>
  49.                             </div>
  50.                             <div class='panel-footer'>
  51.                                 <a href='%s' class='btn btn-primary btn-block'>View Item</a>
  52.                             </div>
  53.                         </div>
  54.                     </div>
  55.                 ''' %(post.name, Helpers.get_path('product/' + str(post.id)), Helpers.get_path(post.featured_image), '100%', post.price, post.quantity, Helpers.get_path('product/' + str(post.id)))
  56.         else:
  57.             pagination_content += "<p class='bg-danger p-d'>No results</p>"
  58.        
  59.         return JsonResponse({
  60.             'content': pagination_content,
  61.             'navigation': Helpers.nagivation_list(count, per_page, cur_page),
  62.         })
  63.     else:  
  64.         return render(request, Helpers.get_url('product/index.html'), {'categories': categories})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement