def perform_search(self): # We make filter queries from the defer queries = [Q(**{f: self.search_query}) for f in self.search_defer] queries.append(Q(**{"%s__in" % self.key: self.search_uid_defer})) # We instantiate a variable called QS from Q class qs = Q() # for every query in filter queries for query in queries: # we make or query for every search defer with same value qs = qs | query # Then we try to try: # append the filter methode to the queryset object with qs object as parameter self.posts = self.obj.filter(qs) # also the record filtered rows count need to passed to the return self.data['recordsFiltered'] = self.obj.filter(qs).count() # if we failed to try except Exception as e: # we sent an error response to client return JSONResponse({ 'error': 'error in search parameter', 'error detail': str(e), 'suggestion': 'Only enable varchar data type only for search' })
def post(self, request): ''' O post salva o objeto Task (INSERT | UPDATE), caso a Task tiver id será buscado o objeto altual no banco de dados executar Update, Não é identificado que é um novo objeto a ser inserido no banco de dados ''' if 'id' in request.data and request.data['id'] is not None: task = Task.objects.filter(id=request.data['id']).first() if task is None: self.logger.debug('Task não encontrada para atualizar') return Response('Tarefa não encontrada para atualizar', status=status.HTTP_404_NOT_FOUND) task = TaskSerializer(data=request.data, instance=Task.objects.get(pk=request.data['id'])) self.logger.debug('Alterando Task') else: task = TaskSerializer(data=request.data) self.logger.debug('Nova Task') if task.is_valid(): task.save() self.logger.debug('Task salva como sucesso') return JSONResponse(task.data) self.logger.debug('Task requisição mal formada') return Response('Tarefa inválida, falta informação na tarefa', status=status.HTTP_400_BAD_REQUEST)
def get_data(self): # if there is an error in this instance if self.error: # then we return the error message return self.error_messages # Finally we return the data on this instace return JSONResponse(self.data)
def get(self, request,id): task = Task.objects.filter(id=int(id)).first() self.logger.debug('Buscando Task nº: %s' % (id)) if task: task = TaskSerializer(task) self.logger.debug('Task encontrada') return JSONResponse(task.data) self.logger.debug('Task não encontrada') return Response('Tarefa não encontrada', status=status.HTTP_404_NOT_FOUND)
def search(self, filter_qry): search_defer = [] # for every defer in this instance for n in range(len(self.defer)): # we check, is it searchable or not? searchable parameter sent by client in request url if self.request.GET.get('columns[' + str(n) + '][searchable]', 'false') == 'true': # if it is searchable, we append it to search defer list search_defer.append(self.defer[n] + "__icontains") # We make filter queries from the defer queries = [Q(**{f: filter_qry}) for f in search_defer] # We instantiate a variable called QS from Q class qs = Q() # for every query in filter queries for query in queries: # we make or query for every search defer with same value qs = qs | query # Then we try to try: # append the filter methode to the queryset object with qs object as parameter self.posts = self.obj.filter(qs) # also the record filtered rows count need to passed to the return self.data['recordsFiltered'] = self.obj.filter(qs).count() # if we failed to try except Exception as e: # we sent an error response to client return JSONResponse({ 'error': 'error in search parameter', 'error detail': str(e), 'suggestion': 'Only enable varchar data type only for search' })
def get(self, request): tasks = Task.objects.all() tasks = TaskSerializer(tasks, many=True) self.logger.debug('Buscando Tasks') return JSONResponse(tasks.data)