def count(self): params = [] count = 0 for item in self.items.all(): for criteria in item.criteria.all(): carrier_id = None if criteria.carrier: carrier_id = criteria.carrier.id params.append( ( criteria.nature, criteria.state, carrier_id, criteria.areacode, criteria.city, criteria.neighborhood ), ) return PersonCounter.count(params)
def get(self, request, format=None): params = ( ( request.GET.get('nature', None), request.GET.get('state', None), request.GET.get('carrier', None), request.GET.get('areacode', None), request.GET.get('city', None), request.GET.get('neighborhood', None) ), ) wrapper = namedtuple('PersonCountWrapper', ('count',))(PersonCounter.count(params)) return Response(PersonCountSerializer(wrapper).data, status=status.HTTP_200_OK)
def post(self, request, format=None): cart, created = Cart.objects.get_or_create(account=request.user.account, status=Cart.STATUS_CHOICES_CREATED[0]) params = ( ( request.data.get('nature', None), request.data.get('state', None), request.data.get('carrier', None), request.data.get('areacode', None), request.data.get('city', None), request.data.get('neighborhood', None) ), ) #import pdb; pdb.set_trace() if not cart.items.exists(): checkout = Checkout() checkout.save() cart.items.add(checkout) cart.save() checkout = cart.items.first() carrier = None carrier_id = request.data.get('carrier', None) if carrier_id: carrier = Carrier.objects.get(pk=carrier_id) criteria, created = CheckoutCriteria.objects.get_or_create( checkout=checkout, nature = request.data.get('nature', None), state = request.data.get('state', None), areacode = request.data.get('areacode', None), city = request.data.get('city', None), carrier = carrier, count = PersonCounter.count(params) ) return Response(CartSerializer(cart).data, status=status.HTTP_200_OK)
def handle(self, *args, **options): combinations_count = 0 natures = ['P', 'L', None] for nature in natures: if not nature: combinations_count += 1 params = ((None, None, None, None, None, None),) print '%s - %s - %s' % (combinations_count, params, PersonCounter.count(params),) continue states = list(Filter.objects.filter(nature=nature).values_list('state', flat=True).distinct('state').order_by('state').all(),) states.append(None) for state in states: if not state: combinations_count += 1 params = ((nature, None, None, None, None, None),) print '%s - %s - %s' % (combinations_count, params, PersonCounter.count(params),) continue carriers = list(Filter.objects.filter(nature=nature, state=state).values_list('carrier_id', flat=True).distinct('carrier__name').order_by('carrier__name').all(),) carriers.append(None) for carrier in carriers: if not carrier: combinations_count += 1 params = ((nature, state, None, None, None, None),) print '%s - %s - %s' % (combinations_count, params, PersonCounter.count(params),) continue areacodes = list(Filter.objects.filter(nature=nature, carrier=carrier).values_list('areacode', flat=True).distinct('areacode').order_by('areacode').all(),) areacodes.append(None) for areacode in areacodes: if not areacode: combinations_count += 1 params = ((nature, state, carrier, None, None, None),) print '%s - %s - %s' % (combinations_count, params, PersonCounter.count(params),) continue cities = list(Filter.objects.filter(nature=nature, carrier=carrier, areacode=areacode).values_list('city', flat=True).distinct('city').order_by('city').all(),) cities.append(None) for city in cities: if not city: combinations_count += 1 params = ((nature, state, carrier, areacode, None, None),) print '%s - %s - %s' % (combinations_count, params, PersonCounter.count(params),) continue neighborhoods = list(Filter.objects.filter(nature=nature, carrier=carrier, areacode=areacode, city=city).values_list( 'neighborhood', flat=True).distinct('neighborhood').order_by('neighborhood').all(),) neighborhoods.append(None) for neighborhood in neighborhoods: combinations_count += 1 params = ((nature, state, carrier, areacode, city, neighborhood),) print '%s - %s - %s' % (combinations_count, params, PersonCounter.count(params),)