File: carrier.py

package info (click to toggle)
tryton-modules-carrier-subdivision 7.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 356 kB
  • sloc: python: 325; xml: 48; makefile: 11; sh: 3
file content (81 lines) | stat: -rw-r--r-- 3,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import re

from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval


class Selection(metaclass=PoolMeta):
    __name__ = 'carrier.selection'

    from_subdivision = fields.Many2One(
        'country.subdivision', "From Subdivision", ondelete='RESTRICT',
        domain=[
            ('country', '=', Eval('from_country', -1)),
            ],
        states={
            'invisible': ~Eval('from_country'),
            },
        help="The subdivision the carrier collects from.\n"
        "Leave empty to allow collection from any subdivision.")
    from_postal_code = fields.Char("From Postal Code",
        help=""
        "The regular expression to match the postal codes "
        "the carrier collects from.\n"
        "Leave empty to allow collection from any postal code.")
    to_subdivision = fields.Many2One(
        'country.subdivision', "To Subdivision", ondelete='RESTRICT',
        domain=[
            ('country', '=', Eval('to_country', -1)),
            ],
        states={
            'invisible': ~Eval('to_country'),
            },
        help="The subdivision the carrier delivers to.\n"
        "Leave empty to allow delivery to any subdivision.")
    to_postal_code = fields.Char("To Postal Code",
        help=""
        "The regular expression to match the postal codes "
        "the carrier delivers to.\n"
        "Leave empty to allow delivery to any postal code.")

    def match(self, pattern, match_none=False):
        pool = Pool()
        Subdivision = pool.get('country.subdivision')

        def parents(subdivision):
            if subdivision is None:
                return []
            subdivision = Subdivision(subdivision)
            while subdivision:
                yield subdivision
                subdivision = subdivision.parent

        if 'from_subdivision' in pattern:
            pattern = pattern.copy()
            from_subdivision = pattern.pop('from_subdivision')
            if (self.from_subdivision is not None
                    and self.from_subdivision not in parents(
                        from_subdivision)):
                return False
        if 'from_postal_code' in pattern:
            pattern = pattern.copy()
            from_postal_code = pattern.pop('from_postal_code') or ''
            if (self.from_postal_code is not None
                    and not re.match(self.from_postal_code, from_postal_code)):
                return False
        if 'to_subdivision' in pattern:
            pattern = pattern.copy()
            to_subdivision = pattern.pop('to_subdivision')
            if (self.to_subdivision is not None
                    and self.to_subdivision not in parents(to_subdivision)):
                return False
        if 'to_postal_code' in pattern:
            pattern = pattern.copy()
            to_postal_code = pattern.pop('to_postal_code') or ''
            if (self.to_postal_code is not None
                    and not re.search(self.to_postal_code, to_postal_code)):
                return False
        return super().match(pattern, match_none=match_none)