def clean_players(self): lines = self.cleaned_data['players'].splitlines() lineno, ok, players = -1, True, [] self.messages = [] for line in lines: lineno += 1 if line.strip() == '': continue if line.strip() == '-' or line.strip().lower() == 'bye': players.append(None) continue pls = find_player(query=line, make=False) if not pls.exists(): # Translators: Matches as in search matches self.messages.append( Message(_("No matches found: '%s'.") % line.strip(), type=Message.ERROR)) ok = False elif pls.count() > 1: self.messages.append( NotUniquePlayerMessage(line.strip(), pls, update=self['players'].auto_id, updateline=lineno, type=Message.ERROR)) ok = False else: players.append(pls.first()) if not ok: raise ValidationError( _('One or more errors found in player list.')) return players
def search(self, adm): # {{{ Check validity (lol) if not self.is_valid(): msgs = [] msgs.append( Message(_('Entered data was invalid, no changes made.'), type=Message.ERROR)) for field, errors in self.errors.items(): for error in errors: msgs.append( Message(error=error, field=self.fields[field].label)) return {'messages': msgs} # }}} matches = (Match.objects.all().prefetch_related( 'message_set').prefetch_related('pla', 'plb', 'period', 'eventobj').annotate( Count('eventobj__match'))) # {{{ All the easy filtering if self.cleaned_data['after'] is not None: matches = matches.filter(date__gte=self.cleaned_data['after']) if self.cleaned_data['before'] is not None: matches = matches.filter(date__lte=self.cleaned_data['before']) if self.cleaned_data['unassigned'] and adm: matches = matches.filter(eventobj__isnull=True) if self.cleaned_data['bestof'] == '3': matches = matches.filter(Q(sca__gte=2) | Q(scb__gte=2)) elif self.cleaned_data['bestof'] == '5': matches = matches.filter(Q(sca__gte=3) | Q(scb__gte=3)) if self.cleaned_data['offline'] != 'both': matches = matches.filter( offline=(self.cleaned_data['offline'] == 'offline')) if self.cleaned_data['game'] != 'all': matches = matches.filter(game=self.cleaned_data['game']) # }}} # {{{ Filter by event if self.cleaned_data['event'] != None: lex = shlex.shlex(self.cleaned_data['event'], posix=True) lex.wordchars += "'" lex.quotes = '"' terms = [s.strip() for s in list(lex) if s.strip() != ''] no_eventobj_q = Q(eventobj__isnull=True) for term in terms: no_eventobj_q &= Q(event__icontains=term) matches = matches.filter(no_eventobj_q | Q(eventobj__isnull=False, eventobj__fullname__iregex=( r"\s".join(r".*{}.*".format(term) for term in terms)))) # }}} ret = {'messages': []} # {{{ Filter by players lines = self.cleaned_data['players'].splitlines() lineno, ok, players = -1, True, [] for line in lines: lineno += 1 if line.strip() == '': continue pls = find_player(query=line, make=False) if not pls.exists(): ret['messages'].append( Message( # Translators: Matches here as in search matches. _("No matches found: '%s'.") % line.strip(), type=Message.ERROR)) ok = False else: if pls.count() > 1: ret['messages'].append( NotUniquePlayerMessage(line.strip(), pls, update=self['players'].auto_id, updateline=lineno, type=Message.WARNING)) players.append(list(pls)) if not ok: return ret pls = [] for p in players: pls += p if len(pls) > 1: matches = matches.filter(pla__in=pls, plb__in=pls) elif len(pls) == 1: matches = matches.filter(Q(pla__in=pls) | Q(plb__in=pls)) # }}} # {{{ Collect data ret['count'] = matches.count() if ret['count'] > 1000: ret['messages'].append( Message(_('Too many results (%i). Please add restrictions.') % ret['count'], type=Message.ERROR)) return ret matches = matches.order_by('-eventobj__latest', '-eventobj__idx', '-date', 'event', 'id') if 1 <= len(pls) <= 2: ret['matches'] = display_matches(matches, date=True, fix_left=pls[0], eventcount=True) ret['sc_my'], ret['sc_op'] = ( sum([m['pla']['score'] for m in ret['matches']]), sum([m['plb']['score'] for m in ret['matches']]), ) ret['msc_my'], ret['msc_op'] = ( sum([ 1 if m['pla']['score'] > m['plb']['score'] else 0 for m in ret['matches'] ]), sum([ 1 if m['plb']['score'] > m['pla']['score'] else 0 for m in ret['matches'] ]), ) ret['left'] = pls[0] if len(pls) == 2: ret['right'] = pls[1] else: ret['matches'] = display_matches(matches, date=True, eventcount=True) return ret