Beispiel #1
0
def find_player(lst, make=False, soft=False):
    qset = Player.objects.all()
    
    for s in [s.strip() for s in lst if s.strip() != '']:
        # If numeric, assume it's a restriction on ID and nothing else
        if s.isdigit():
            qset = qset.filter(id=int(s))
            continue

        # Always search by player tag, team and aliases
        if soft:
            q = Q(tag__icontains=s) | Q(alias__name__icontains=s) |\
                    Q(teammembership__current=True, teammembership__team__name__icontains=s) |\
                    Q(teammembership__current=True, teammembership__team__alias__name__icontains=s)
        else:
            q = Q(tag__iexact=s) | Q(alias__name__iexact=s) |\
                    Q(teammembership__current=True, teammembership__team__name__iexact=s) |\
                    Q(teammembership__current=True, teammembership__team__alias__name__iexact=s)

        # Race query
        if len(s) == 1 and s.upper() in 'PTZSR':
            q |= Q(race=s.upper())

        # Country codes
        if len(s) == 2 and s.upper() in data.cca2_to_ccn:
            q |= Q(country=s.upper())
        if len(s) == 3 and s.upper() in data.cca3_to_ccn:
            q |= Q(country=ccn_to_cca2(cca3_to_ccn(s.upper())))
        renorm = s[0].upper() + s[1:].lower()
        if renorm in data.cn_to_ccn:
            q |= Q(country=ccn_to_cca2(cn_to_ccn(renorm)))

        qset = qset.filter(q)

    # Make player if needed and allowed
    if not qset.exists() and make:
        tag, country, race = None, None, None

        for s in [s.strip() for s in lst if s.strip() != '']:
            if s.isdigit():
                continue

            if len(s) == 1 and s.upper() in 'PTZSR':
                race = s.upper()
                continue

            if len(s) == 2 and s.upper() in data.cca2_to_ccn:
                country = s.upper()
                continue
            if len(s) == 3 and s.upper() in data.cca3_to_ccn:
                country = ccn_to_cca2(cca3_to_ccn(s.upper()))
                continue
            renorm = s[0].upper() + s[1:].lower()
            if renorm in data.cn_to_ccn:
                country = ccn_to_cca2(cn_to_ccn(renorm))
                continue

            tag = s

        if tag == None:
            raise Exception('Player \'' + ' '.join(lst) + '\' was not found and could not be made'\
                    + ' (missing player tag)')

        if race == None:
            raise Exception('Player \'' + ' '.join(lst) + '\' was not found and could not be made'\
                    + ' (missing race)')

        p = Player()
        p.tag = tag
        p.country = country
        p.race = race
        p.save()

        return Player.objects.filter(id=p.id)

    return qset.distinct()
Beispiel #2
0
def find_player(query=None, lst=None, make=False, soft=False, strict=False):
    queryset = Player.objects.all()

    if not lst:
        try:
            lst = [s.strip() for s in shlex.split(query) if s.strip() != '']
        except:
            return []

    tag, country, race = None, None, None

    # {{{ Build filter
    for s in lst:
        # If numeric, assume a restriction on ID
        if type(s) is int or s.isdigit():
            queryset = queryset.filter(id=int(s))
            continue

        # If only one character, assume a restriction on race
        if len(s) == 1 and s.upper() in 'PTZSR':
            race = s.upper()
            queryset = queryset.filter(race=s.upper())
            continue

        # Otherwise, always search by player tag, team and aliases
        filter_type = "iexact"
        if soft:
            filter_type = "icontains"

        # Helper function that formats the filter to use `filter_type`
        def format_filter(**kwargs):
            ret = dict()
            for k in kwargs:
                if k.endswith("MATCHES"):
                    ret[k.replace("MATCHES", filter_type)] = kwargs[k]
                else:
                    ret[k] = kwargs[k]
            return ret

        tag_filter = format_filter(tag__MATCHES=s)
        alias_filter = format_filter(alias__name__MATCHES=s)
        full_name_filter = format_filter(name__MATCHES=s)
        romanized_name_filter = format_filter(romanized_name__MATCHES=s)

        q = (
            Q(**tag_filter) |
            Q(**alias_filter) |
            Q(**full_name_filter) |
            Q(**romanized_name_filter)
        )
        if not strict or len(lst) > 1:
            group_name_filter = format_filter(
                groupmembership__current=True,
                groupmembership__group__name__MATCHES=s,
                groupmembership__group__is_team=True)

            group_alias_filter = format_filter(
                groupmembership__current=True,
                groupmembership__group__alias__name__MATCHES=s,
                groupmembership__group__is_team=True)

            q |= Q(**group_name_filter) | Q(**group_alias_filter)

            # ...and perhaps country codes
            found_country = False
            if len(s) == 2 and s.upper() in data.cca2_to_ccn:
                country = s.upper()
                found_country = True
                q |= Q(country=s.upper())

            if len(s) == 3 and s.upper() in data.cca3_to_ccn:
                country = ccn_to_cca2(cca3_to_ccn(s.upper()))
                found_country = True
                q |= Q(country=ccn_to_cca2(cca3_to_ccn(s.upper())))

            renorm = s[0].upper() + s[1:].lower()
            if renorm in data.cn_to_ccn:
                country = ccn_to_cca2(cn_to_ccn(renorm))
                found_country = True
                q |= Q(country=ccn_to_cca2(cn_to_ccn(renorm)))

            if not found_country:
                tag = s

        queryset = queryset.filter(q)
    # }}}

    # {{{ If no results, make player if allowed
    if not queryset.exists() and make:
        # {{{ Raise exceptions if missing crucial data
        if tag == None:
            msg = _("Player '%s' was not found and cound not be made (missing player tag)") % ' '.join(lst)
            raise Exception(msg)

        if race == None:
            msg = _("Player '%s' was not found and cound not be made (missing race)") % ' '.join(lst)
            raise Exception(msg)
        # }}}

        p = Player(tag=tag, country=country, race=race)
        p.save()

        return Player.objects.filter(id=p.id)
    # }}}

    return queryset.distinct()
Beispiel #3
0
def find_player(query=None, lst=None, make=False, soft=False, strict=False):
    queryset = Player.objects.all()

    if not lst:
        try:
            lst = [s.strip() for s in shlex.split(query) if s.strip() != ""]
        except:
            return []

    tag, country, race = None, None, None

    # {{{ Build filter
    for s in lst:
        # If numeric, assume a restriction on ID
        if s.isdigit():
            queryset = queryset.filter(id=int(s))
            continue

        # If only one character, assume a restriction on race
        if len(s) == 1 and s.upper() in "PTZSR":
            race = s.upper()
            queryset = queryset.filter(race=s.upper())
            continue

        # Otherwise, always search by player tag, team and aliases
        filter_type = "iexact"
        if soft:
            filter_type = "icontains"

        # Helper function that formats the filter to use `filter_type`
        def format_filter(**kwargs):
            ret = dict()
            for k in kwargs:
                if k.endswith("MATCHES"):
                    ret[k.replace("MATCHES", filter_type)] = kwargs[k]
                else:
                    ret[k] = kwargs[k]
            return ret

        tag_filter = format_filter(tag__MATCHES=s)
        alias_filter = format_filter(alias__name__MATCHES=s)

        q = Q(**tag_filter) | Q(**alias_filter)
        if not strict or len(lst) > 1:
            group_name_filter = format_filter(
                groupmembership__current=True,
                groupmembership__group__name__MATCHES=s,
                groupmembership__group__is_team=True,
            )

            group_alias_filter = format_filter(
                groupmembership__current=True,
                groupmembership__group__alias__name__MATCHES=s,
                groupmembership__group__is_team=True,
            )

            q |= Q(**group_name_filter) | Q(**group_alias_filter)

            # ...and perhaps country codes
            found_country = False
            if len(s) == 2 and s.upper() in data.cca2_to_ccn:
                country = s.upper()
                found_country = True
                q |= Q(country=s.upper())

            if len(s) == 3 and s.upper() in data.cca3_to_ccn:
                country = ccn_to_cca2(cca3_to_ccn(s.upper()))
                found_country = True
                q |= Q(country=ccn_to_cca2(cca3_to_ccn(s.upper())))

            renorm = s[0].upper() + s[1:].lower()
            if renorm in data.cn_to_ccn:
                country = ccn_to_cca2(cn_to_ccn(renorm))
                found_country = True
                q |= Q(country=ccn_to_cca2(cn_to_ccn(renorm)))

            if not found_country:
                tag = s

        queryset = queryset.filter(q)
    # }}}

    # {{{ If no results, make player if allowed
    if not queryset.exists() and make:
        # {{{ Raise exceptions if missing crucial data
        if tag == None:
            msg = _("Player '%s' was not found and cound not be made (missing player tag)") % " ".join(lst)
            raise Exception(msg)

        if race == None:
            msg = _("Player '%s' was not found and cound not be made (missing race)") % " ".join(lst)
            raise Exception(msg)
        # }}}

        p = Player(tag=tag, country=country, race=race)
        p.save()

        return Player.objects.filter(id=p.id)
    # }}}

    return queryset.distinct()
Beispiel #4
0
def find_player(query=None, lst=None, make=False, soft=False):
    queryset = Player.objects.all()

    if not lst:
        try:
            lst = [s.strip() for s in shlex.split(query) if s.strip() != '']
        except:
            return []

    tag, country, race = None, None, None

    # {{{ Build filter
    for s in lst:
        # If numeric, assume a restriction on ID
        if s.isdigit():
            queryset = queryset.filter(id=int(s))
            continue

        # If only one character, assume a restriction on race
        if len(s) == 1 and s.upper() in 'PTZSR':
            race = s.upper()
            queryset = queryset.filter(race=s.upper())
            continue

        # Otherwise, always search by player tag, team and aliases
        if soft:
            q = (
                Q(
                    groupmembership__current=True,
                    groupmembership__group__name__icontains=s,
                    groupmembership__group__is_team=True,
                ) | Q(
                    groupmembership__current=True,
                    groupmembership__group__alias__name__icontains=s,
                    groupmembership__group__is_team=True,
                ) | Q(tag__icontains=s) | Q(alias__name__icontains=s)
            )
        else:
            q = (
                Q(
                    groupmembership__current=True,
                    groupmembership__group__name__iexact=s,
                    groupmembership__group__is_team=True,
                ) | Q(
                    groupmembership__current=True,
                    groupmembership__group__alias__name__iexact=s,
                    groupmembership__group__is_team=True,
                ) | Q(tag__iexact=s) | Q(alias__name__iexact=s)
            )

        # ...and perhaps country codes
        found_country = False
        if len(s) == 2 and s.upper() in data.cca2_to_ccn:
            country = s.upper()
            found_country = True
            q |= Q(country=s.upper())

        if len(s) == 3 and s.upper() in data.cca3_to_ccn:
            country = ccn_to_cca2(cca3_to_ccn(s.upper()))
            found_country = True
            q |= Q(country=ccn_to_cca2(cca3_to_ccn(s.upper())))

        renorm = s[0].upper() + s[1:].lower()
        if renorm in data.cn_to_ccn:
            country = ccn_to_cca2(cn_to_ccn(renorm))
            found_country = True
            q |= Q(country=ccn_to_cca2(cn_to_ccn(renorm)))

        if not found_country:
            tag = s

        queryset = queryset.filter(q)
    # }}}

    # {{{ If no results, make player if allowed
    if not queryset.exists() and make:
        # {{{ Raise exceptions if missing crucial data
        if tag == None:
            msg = "Player '%s' was not found and cound not be made (missing player tag)" % ' '.join(lst)
            raise Exception(msg)

        if race == None:
            msg = "Player '%s' was not found and cound not be made (missing race)" % ' '.join(lst)
            raise Exception(msg)
        # }}}

        p = Player(tag=tag, country=country, race=race)
        p.save()

        return Player.objects.filter(id=p.id)
    # }}}

    return queryset.distinct()