Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. /?date_from_day=8&date_from_month=5&date_from_year=1960&date_to_day=11&date_to_month=8&date_to_year=2018
  2.  
  3. ?0=d&1=a&2=t&3=e&4=_&5=f&6=r&7=o&8=m&9=_&10=d&11=a&12=y&13=%3D&14=8....
  4.  
  5. ?date_from_day=8
  6.  
  7. class FilteredProductsListAPIView(LoginRequiredMixin, ListAPIView):
  8. authentication_classes = (authentication.SessionAuthentication,)
  9. permission_classes = (permissions.IsAuthenticated,)
  10.  
  11. queryset = Product.objects.all()
  12. serializer_class = ProductSerializer
  13. filter_backends = (filters.DjangoFilterBackend,)
  14. filterset_class = ProductFilter
  15.  
  16. def get_queryset(self):
  17. qs = Product.objects.filter(user=self.request.user).order_by("-timestamp")
  18. date_from_day = self.request.query_params.get("date_from_day", None)
  19. date_from_month = self.request.query_params.get("date_from_month", None)
  20. date_from_year = self.request.query_params.get("date_from_year", None)
  21.  
  22. if date_from_day and date_from_month and date_from_year:
  23. date_from = datetime.date(int(date_from_year), int(date_from_month), int(date_from_day))
  24. qs = qs.filter(date__gte=date_from)
  25.  
  26. # some more ugly code
  27.  
  28. return qs
  29.  
  30. def list(self, request, *args, **kwargs):
  31. queryset = self.get_queryset()
  32. total_count = queryset.count()
  33.  
  34. draw = int(self.request.GET["draw"], 0)
  35. start = int(self.request.GET["start"], 0)
  36. length = int(self.request.GET["length"], 0)
  37.  
  38. queryset = queryset[start:start+length]
  39. serializer = ProductSerializer(queryset, many=True)
  40. result = {"draw": draw,
  41. "recordsTotal": total_count,
  42. "recordsFiltered": total_count,
  43. "data": serializer.data}
  44. print("result:", result)
  45. return Response(result)
  46.  
  47. url(r"^my-filtered-products/",
  48. views.FilteredProductsListAPIView.as_view(), name="filter-products"),
  49.  
  50. <table class="table table-striped table-sm table-bordered" id="exampleAjax3">
  51. <thead>
  52. <tr>
  53. <th></th>
  54. <th>Date</th>
  55. <th>Time</th>
  56. <th>Title</th>
  57. <th>Description</th>
  58. <th>Price</th>
  59. <th></th>
  60. </tr>
  61. </thead>
  62. <tbody></tbody>
  63. </table>
  64.  
  65. function applyFilters(event) {
  66. event.preventDefault();
  67. var productFiltersForm = $(".my-product-filters-form");
  68. $('#exampleAjax3').dataTable( {
  69. "autoWidth": true,
  70. "displayLength": 10,
  71. "lengthChange": false,
  72. "ordering": false,
  73. "processing": false, // Show/Hide Processing message
  74. "searching": false,
  75. "serverSide": true,
  76. "language": {
  77. "zeroRecords": "Nothing to display",
  78. "info": "Showing _START_ to _END_ of _TOTAL_ products",
  79. "infoEmpty": ""
  80. },
  81. "ajax": {
  82. "processing": false, // Show/Hide Processing message
  83. "type": 'GET',
  84. "url": '/my-filtered-products/',
  85. "data": $(".my-product-filters-form").serialize(),
  86. "dataSrc": "data"
  87. },
  88.  
  89. "columns": [
  90. { "data": "date" },
  91. { "data": "time" },
  92. { "data": "description" },
  93. { "data": "price" }],
  94.  
  95. retrieve: true
  96. });
  97. }
  98.  
  99. date_from_day=8&date_from_month=5&date_from_year=1960&date_to_day=11&date_to_month=8&date_to_year=2018&odds_from=1.100&odds_to=10.000&stake_from=1.000&stake_to=234.340&sport_multi=4
  100.  
  101. /?date_from_day=8&date_from_month=5&date_from_year=1960&date_to_day=11&date_to_month=8&date_to_year=2018,
Add Comment
Please, Sign In to add comment