I have a table in which I display information about organizations and I have a mechanism who is triggering next action date on the organization, that is manually set on the organizations.
Now I've added a custom datepicker
field on top of my table so I can filter it for the wanted date and show all next action date
for that organization on selected date, so I'm passing a value to Controller
so I can catch in the restAPI
, and in the rest
I want to filter the date to be before or equal to now so I can show organizations by next action date.
My question is how to filter the date to be before or equal to now, I really lost here so can someone help me and explain how can I do that in my ModelViewSet
.
ModelViewSet:
from rest_framework import viewsets, permissions, filters
from cms.restapi.pagination import StandardResultsOffsetPagination
from cms_sales.models import LeadContact
from cms_sales.restapi.permissions.lead_contact_permissions import LeadContactPermissions
from cms_sales.restapi.serializers.lead_contact_serializer import LeadContactSerializer
class LeadContactViewSet(viewsets.ModelViewSet):
def get_queryset(self):
queryset = LeadContact.objects.none()
user = self.request.user
if user.has_perm('vinclucms_sales.can_view_full_lead_contact_list'):
queryset = LeadContact.objects.all()
elif user.has_perm('vinclucms_sales.can_view_lead_contact'):
queryset = LeadContact.objects.filter(account_handler=user)
return queryset
serializer_class = LeadContactSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_fields = ('account_handler',)
ordering_fields = (
'first_name', 'last_name', 'account_handler__first_name', 'account_handler__last_name',
'sub_organization_name', 'organization_name', 'next_action_date', 'serial_number',
'next_action_date', 'status_text', 'select_date')
search_fields = (
'first_name', 'last_name', 'account_handler__first_name', 'account_handler__last_name',
'sub_organization_name', 'organization_name', 'next_action_date', 'serial_number',
'next_action_date', 'status_text', 'select_date')
pagination_class = StandardResultsOffsetPagination
permission_classes = [permissions.IsAuthenticated, LeadContactPermissions]
Cotroller where I pass the datepicker
field:
app = angular.module 'cms.sales'
app.controller 'LeadContactListCtrl', ['$scope', '$rootScope', '$compile', 'LeadContacts'
($scope, $rootScope, $compile, LeadContacts) ->
savedSuccessMessage = "Lead Contact List was updated"
savedFailMessage = "Failed to update lead contact list"
$scope.init = ()->
fetchLeadContacts()
fetchLeadContacts = () ->
$('#expensesListTable').DataTable(
createdRow: (row, data, index) ->
$compile(row)($scope)
sDom: 'lfrtip'
processing: true
serverSide: true
searchDelay: 1000
orderMulti: false
pageLength: 25
ajax:
url: '/api/sales/lead_contact/'
data: (data) ->
data.limit = data.length
data.offset = data.start
data.search = data.search['value']
if data.order[0]['dir'] == 'asc'
data.ordering = data.columns[data.order[0]['column']].name
else
data.ordering = '-' + data.columns[data.order[0]['column']].name
return 0
dataFilter: (data) ->
json = jQuery.parseJSON(data)
json.recordsTotal = json.count
json.recordsFiltered = json.count
json.data = json.results
return JSON.stringify json
columns: [
{
data: 'serial_number'
name: 'serial_number'
render: (data, type, row, meta) ->
return '<a href="{{ site.domain }}' + row.absolute_url + '">' + data + '</a>'
}
{
name: 'first_name,last_name'
render: (data, type, row, meta)->
return row['first_name'] + ' ' + row['last_name']
}
{
data: 'organization_name'
name: 'organization_name'
}
{
data: 'sub_organization_name'
name: 'sub_organization_name'
}
{
data: 'account_handler'
name: 'account_handler__first_name, account_handler__last_name'
render: (data, type, row, meta)->
return data['first_name'] + ' ' + data['last_name']
}
{
data: 'status_text'
name: 'status_text'
render: (data, type, row, meta)->
return "<span class='" + row['status_display_class'] + "'>" + data + "</span>"
}
{
data: "next_action_date"
name: "next_action_date"
render: (data, type, row, meta)->
initValue = data
if data
initValue = "'" + data + "'"
return '<div class="input-control text" data-role="datepicker"
ng-controller="ContactDateCtrl"
ng-init="init(' + row['pk'] + ', ' + initValue + ')"
data-format="mmmm d, yyyy">
<input type="text" ng-model="contactDate" ng-change="onChange()">
<button class="button"><span class="mif-calendar"></span></button>
</div>'
}
])
$scope.SelectLeadContacts = () ->
params = {}
if $scope.lead_contact.id
params['id'] = $scope.lead_contact.id
LeadContacts.update(params).$promise.then saveSuccessCallback, saveFailedCallback
else
LeadContacts.save(params).$promise.then saveSuccessCallback, saveFailedCallback
saveSuccessCallback = (response) ->
ClientNotifications.showNotification("Success", savedSuccessMessage, "success")
saveFailedCallback = (error) ->
ClientNotifications.showNotification("Alert", savedFailMessage, "alert")
$scope.showAddNew = ()->
initNewLeadContacts()
initNewLeadContacts = ()->
$scope.lead_contact = {}
$scope.lead_contact.select_date = 'Jan 1, 2200'
]
Template:
{% extends "site_base.html" %}
{% load i18n static %}
{% block head_title %}Lead contact list{% endblock %}
{% block ng_app %}cms.sales{% endblock %}
{% block body %}
<div class="grid">
<div class="row">
<h2 class="align-center">
<strong>
{% trans "Contact leads List" %}
</strong>
</h2>
</div>
</div>
<div class="flex-grid"
ng-controller="LeadContactListCtrl"
ng-init="init()">
<div class="row">
<div class="cell size-p20 padding10">
<label for="id_select_date">Select Date: *</label>
<div class="full-size">
<div class="input-control full-size text"
data-role="datepicker" date-format="mmmm d, yyyy">
<input id="id_select_date" ng-model="lead_contact.select_date"/>
<button class="button"><span class="mif-calendar"></span></button>
</div>
</div>
</div>
</div>
<div class="row">
<div class="cell size-p20 padding10">
<button class="button primary" ng-click="SelectLeadContacts()">
{% trans "Submit" %}
</button>
</div>
</div>
<div class="row">
<div class="cell size-p100 padding10">
<table title="Contact leads List" class="dataTable" id="contactLeadsList">
<thead>
<tr>
<th>{% trans 'Serial Number' %}</th>
<th>{% trans 'Lead name' %}</th>
<th>{% trans 'Organization' %}</th>
<th>{% trans 'Sub-organization' %}</th>
<th>{% trans 'Handler' %}</th>
<th>{% trans 'Sale status' %}</th>
<th>{% trans 'Next communication date' %}</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
{% endblock %}