diefenbach / django-lfs

An online-shop based on Django
http://www.getlfs.com
BSD 3-Clause "New" or "Revised" License
622 stars 222 forks source link

Filtering customers in control panel by first and last name is broken #64

Closed emilian closed 11 years ago

emilian commented 11 years ago

It looks like filtering customers is broken. The master branch shows that the filter code was commented out in lfs/manage/views/customers.py file on line ~350:

# Filter
name = customer_filters.get("name", "")
#if name != "":
#    f = Q(sa_object_id__lastname__icontains=name)
#    f |= Q(sa_object_id__firstname__icontains=name)
#    customers = customers.filter(f)

The issue here is that you cannot filter based on the sa_object_id.

My solution is to store the first and last name values in the Customer model in addition to storing that information in the generic address model. The first and last name values in the address should only be used when printing the invoice / packing slip because it is possible that the recipient has a different name than the customer account (for example, when sending a gift to a different person).

What are your thoughts on this solution?

rjacks commented 11 years ago

Here's what I did:

def _get_filtered_customers(request, customer_filters): """ """

Process filter settings

customers = Customer.objects.all()
customer_ordering = request.session.get("customer-ordering", "id")
customer_ordering_order = request.session.get("customer-ordering-order", "")
name = customer_filters.get("name", "")
if name != "":
    f = Q(user__last_name__icontains=name)
    f |= Q(user__first_name__icontains=name)
    customers = customers.filter(f)

# Ordering
customers = customers.order_by("%s%s" % (customer_ordering_order, customer_ordering))

return customers

Works for me

rjacks commented 11 years ago

Not sure why Github mangled my code... First comment I have submitted here...

emilian commented 11 years ago

The only issue with that code is that the Customer model does not require a User object. In that case it will probably throw an error if the customer does not have a User object linked to the customer.

pigletto commented 11 years ago

rjacks solution seems to be OK. It will work even for null user objects