Changeset ec70d85 in tailbone


Ignore:
Timestamp:
04/10/19 14:20:36 (11 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
6ae703d
Parents:
2bdcc4f
Message:

Add custom grid filter for phone number fields

and use it in various grid views

Location:
tailbone
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • tailbone/grids/filters.py

    r2bdcc4f rec70d85  
    2727from __future__ import unicode_literals, absolute_import
    2828
     29import re
    2930import datetime
    3031import logging
     
    766767
    767768
     769class AlchemyPhoneNumberFilter(AlchemyStringFilter):
     770    """
     771    Special string filter, with logic to deal with phone numbers.
     772    """
     773
     774    def parse_value(self, value):
     775        newvalue = None
     776
     777        # first we try to split according to typical 7- or 10-digit number
     778        digits = re.sub(r'\D', '', value or '')
     779        if len(digits) == 7:
     780            newvalue = "{} {}".format(digits[:3], digits[3:])
     781        elif len(digits) == 10:
     782            newvalue = "{} {} {}".format(digits[:3], digits[3:6], digits[6:])
     783
     784        # if that didn't work, we can also try to split by grouped digits
     785        if not newvalue and value:
     786            parts = re.split(r'\D+', value)
     787            newvalue = ' '.join(parts)
     788
     789        return newvalue or value
     790
     791    def filter_contains(self, query, value):
     792        """
     793        Try to parse the value into "parts" of a phone number, then do a normal
     794        'ILIKE' query with those parts.
     795        """
     796        value = self.parse_value(value)
     797        return super(AlchemyPhoneNumberFilter, self).filter_contains(query, value)
     798
     799    def filter_does_not_contain(self, query, value):
     800        """
     801        Try to parse the value into "parts" of a phone number, then do a normal
     802        'NOT ILIKE' query with those parts.
     803        """
     804        value = self.parse_value(value)
     805        return super(AlchemyPhoneNumberFilter, self).filter_does_not_contain(query, value)
     806
     807
    768808class GridFilterSet(OrderedDict):
    769809    """
  • tailbone/views/customers.py

    r2bdcc4f rec70d85  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    117117            model.CustomerPhoneNumber.preference == 1)))
    118118        g.sorters['phone'] = lambda q, d: q.order_by(getattr(model.CustomerPhoneNumber.number, d)())
    119         g.set_filter('phone', model.CustomerPhoneNumber.number)#, label="Phone Number")
     119        g.set_filter('phone', model.CustomerPhoneNumber.number,
     120                     # label="Phone Number",
     121                     factory=grids.filters.AlchemyPhoneNumberFilter)
    120122        g.set_label('phone', "Phone Number")
    121123
  • tailbone/views/employees.py

    r2bdcc4f rec70d85  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3636from webhelpers2.html import tags, HTML
    3737
     38from tailbone import grids
    3839from tailbone.db import Session
    3940from tailbone.views import MasterView, AutocompleteView
     
    9192        g.filters['email'] = g.make_filter('email', model.EmployeeEmailAddress.address,
    9293                                           label="Email Address")
    93         g.filters['phone'] = g.make_filter('phone', model.EmployeePhoneNumber.number,
    94                                            label="Phone Number")
     94        g.set_filter('phone', model.EmployeePhoneNumber.number,
     95                     label="Phone Number",
     96                     factory=grids.filters.AlchemyPhoneNumberFilter)
    9597
    9698        # id
  • tailbone/views/members.py

    r2bdcc4f rec70d85  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3030import sqlalchemy as sa
    3131
     32from rattail.db import model
     33
    3234from deform import widget as dfwidget
    3335
     36from tailbone import grids
    3437from tailbone.views import MasterView
    35 
    36 from rattail.db import model
    3738
    3839
     
    9394            model.MemberPhoneNumber.preference == 1)))
    9495        g.sorters['phone'] = lambda q, d: q.order_by(getattr(model.MemberPhoneNumber.number, d)())
    95         g.set_filter('phone', model.MemberPhoneNumber.number)
     96        g.set_filter('phone', model.MemberPhoneNumber.number,
     97                     factory=grids.filters.AlchemyPhoneNumberFilter)
    9698        g.set_label('phone', "Phone Number")
    9799
  • tailbone/views/people.py

    r2bdcc4f rec70d85  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3535from webhelpers2.html import HTML, tags
    3636
     37from tailbone import grids
    3738from tailbone.views import MasterView, AutocompleteView
    3839
     
    9394
    9495        g.filters['email'] = g.make_filter('email', model.PersonEmailAddress.address)
    95         g.filters['phone'] = g.make_filter('phone', model.PersonPhoneNumber.number)
     96        g.set_filter('phone', model.PersonPhoneNumber.number,
     97                     factory=grids.filters.AlchemyPhoneNumberFilter)
    9698
    9799        g.joiners['customer_id'] = lambda q: q.outerjoin(model.CustomerPerson).outerjoin(model.Customer)
  • tailbone/views/stores.py

    r2bdcc4f rec70d85  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3333import colander
    3434
     35from tailbone import grids
    3536from tailbone.views import MasterView
    3637
     
    6667            model.StorePhoneNumber.preference == 1)))
    6768
    68         g.filters['phone'] = g.make_filter('phone', model.StorePhoneNumber.number)
     69        g.set_filter('phone', model.StorePhoneNumber.number,
     70                     factory=grids.filters.AlchemyPhoneNumberFilter)
    6971        g.filters['email'] = g.make_filter('email', model.StoreEmailAddress.address)
    7072        g.filters['name'].default_active = True
Note: See TracChangeset for help on using the changeset viewer.