def add_matches(request): # This view is the default redirect for login requests if 'username' in request.POST and 'password' in request.POST: user = authenticate(username=request.POST['username'], password=request.POST['password']) if user != None and user.is_active: login(request, user) base = base_ctx('Submit', 'Matches', request) add_login_message(base, extra='Submitted results will be pending review before inclusion.') # If the user is logged in, the template needs a username and a list of event objects if base['adm']: base['events'] = Event.objects.filter(closed=False, rgt=F('lft')+1).order_by('lft') if 'matches' in request.POST: # Collect various information from the form try: event = request.POST['event'].strip() except: event = None date = request.POST['date'] matches = request.POST['matches'].splitlines() offline = not (request.POST['type'] != 'offline') game = request.POST['game'] if game not in ['WoL', 'HotS']: game = 'WoL' base['game'] = game base['type'] = 'offline' if offline else 'online' base['matches'] = '\n'.join(matches) base['date'] = date base['event'] = event # Get event object. If not admin, an exception will be thrown and eventobj = None. try: eventobj = Event.objects.get(id=int(request.POST['eobj'])) if eventobj.id == 2: eventobj = None except: eventobj = None if eventobj is not None: base['eobj'] = eventobj.id # Get extra data needed if not admin. try: source = request.POST['source'] contact = request.POST['contact'] notes = request.POST['notes'] base['source'] = source base['contact'] = contact base['notes'] = notes except: pass # Check various requirements for non-admins if not base['adm'] and len(matches) > 100: base['messages'].append(Message('Please do not submit more than 100 results at a time.', 'Too many entries', Message.ERROR)) base.update(csrf(request)) return render_to_response('add.html', base) if not base['adm'] and source.strip() == '': base['messages'].append(Message('Please include a source.', 'Source missing', Message.ERROR)) base.update(csrf(request)) return render_to_response('add.html', base) # If not admin, make a PreMatchGroup. if not base['adm']: pmgroup = PreMatchGroup(date=date, event=event, source=source, contact=contact, notes=notes,\ game=game, offline=offline) pmgroup.save() # Lists of successes and failures success = [] failure = [] # Loop through match entries for s in matches: if s.strip() == '': continue try: # Parse and collect the components collect = parse_match(s.strip()) pla = collect[0] plb = collect[1] sca = int(collect[2][0]) scb = int(collect[2][1]) # Check for !DUP and !MAKE switches if user is logged in dup_switch = False make_switch = False while collect[2][-1][0] == '!': if collect[2][-1] == '!MAKE': make_switch = True elif collect[2][-1] == '!DUP': dup_switch = True collect[2] = collect[2][:-1] if not base['adm']: make_switch = False # Check for race overrides def get_race(lst): if lst[-1][:2].upper() == 'R:': r = lst[-1][2:].upper() if lst[-1][2:].upper() in 'PTZR' else None return r, lst[:-1] else: return None, lst rca, pla = get_race(pla) rcb, plb = get_race(plb) # Find players def get_player(lst, failure, base, make, adm): #try: pls = find_player(lst, make=make) #except Exception as e: #failure.append(s) #base['messages'].append(Message('Could not parse: ' + e.message, #s, Message.ERROR)) #return None if not pls.exists() and adm: # Player not found, and user logged in. Add failure message and return None. failure.append(s) base['messages'].append(Message( 'Could not find player \'%s\', add !MAKE switch to create.' % ' '.join(lst), s, Message.ERROR)) return None if pls.count() > 1 and adm: # Too many players found, and used logged in. Add failure message and return None. failure.append(s) base['messages'].append(NotUniquePlayerMessage(' '.join(lst), pls)) return None if not pls.exists() or pls.count() > 1: # Too many or too few players found, and user not logged in. Just return None. return None return pls[0] # If the user is logged in and some players were not found, abort. pla_obj = get_player(pla, failure, base, make_switch, base['adm']) if pla_obj == None and base['adm']: continue plb_obj = get_player(plb, failure, base, make_switch, base['adm']) if plb_obj == None and base['adm']: continue # If both players are known, check for duplicates if pla_obj and plb_obj: n = find_duplicates(pla_obj, plb_obj, sca, scb, date) if n > 0 and not dup_switch: failure.append(s) base['messages'].append(Message( '%i possible duplicate(s) found, add !DUP switch to force.' % n, s, Message.ERROR)) continue # If the user is not logged in, we now have enough information to create a prematch. if not base['adm']: pm = PreMatch(group=pmgroup, sca=sca, scb=scb, pla=pla_obj, plb=plb_obj,\ pla_string=' '.join(pla), plb_string=' '.join(plb), date=pmgroup.date) if rca: pm.rca = rca elif pla_obj: pm.rca = pla_obj.race else: pm.rca = None if rcb: pm.rcb = rcb elif plb_obj: pm.rcb = plb_obj.race else: pm.rcb = None pm.save() success.append(pm) continue # Abort if race information is incorrect if pla_obj.race == 'S' and rca == None: failure.append(s) base['messages'].append(Message( '%s is Random or Switcher, need race information.' % pla_obj.tag, s, Message.ERROR)) continue if plb_obj.race == 'S' and rcb == None: failure.append(s) base['messages'].append(Message( '%s is Random or Switcher, need race information.' % plb_obj.tag, s, Message.ERROR)) continue # Add match m = Match() m.pla = pla_obj m.plb = plb_obj m.sca = sca m.scb = scb m.rca = pla_obj.race if rca == None else rca m.rcb = plb_obj.race if rcb == None else rcb m.date = date m.submitter = request.user m.set_period() m.eventobj = eventobj m.offline = offline m.game = game m.save() success.append(m) except Exception as e: failure.append(s) base['messages'].append(Message('Could not parse: ' + e.message, s, Message.ERROR)) continue success = display_matches(success, messages=False) if len(success) > 0: base['messages'].append(Message('Added %i match(es).' % len(success), type=Message.SUCCESS)) base.update({'matches': '\n'.join(failure), 'success': success}) elif 'eventid' in request.GET: try: event = Event.objects.get(id=int(request.GET['eventid'])) if event.closed: event.closed = False event.save() base['messages'].append(Message('Reopened \'%s\'.' % event.fullname, type=Message.SUCCESS)) base['eobj'] = event.id except: base['messages'].append(Message('Couldn\'t find event ID %s.' % request.GET['eventid'], type=Message.ERROR)) try: if base['adm'] and request.POST['action'] == 'Add and close event' and eventobj is not None: if len(failure) == 0: eventobj.close() base['messages'].append(Message('Event \'%s\' closed.' % eventobj.fullname, type=Message.SUCCESS)) else: base['messages'].append(Message('Event \'%s\' was NOT closed.' % eventobj.fullname, type=Message.WARNING)) except: pass base.update(csrf(request)) return render_to_response('add.html', base)
def review(request): base = base_ctx('Submit', 'Review', request) if not base['adm']: base.update(csrf(request)) return render_to_response('login.html', base) add_login_message(base) base['events'] = Event.objects.filter(closed=False, rgt=F('lft')+1).order_by('lft') if 'act' in request.POST and base['adm'] == True: if int(request.POST['eobj']) != 2: eobj = Event.objects.get(id=int(request.POST['eobj'])) base['eobj'] = eobj.id delete = True if request.POST['act'] == 'reject' else False success = [] ndel = 0 for key in sorted(request.POST.keys()): if request.POST[key] != 'y': continue if key[0:6] == 'match-': pm = PreMatch.objects.get(id=int(key.split('-')[-1])) if delete: group = pm.group pm.delete() if not group.prematch_set.all().exists(): group.delete() ndel += 1 continue if pm.pla == None: pm.pla_string = request.POST['match-%i-pla' % pm.id] if pm.plb == None: pm.plb_string = request.POST['match-%i-plb' % pm.id] if pm.pla == None or pm.plb == None: review_treat_players(pm, base) if pm.pla and not pm.rca: pm.pla = None if pm.plb and not pm.rcb: pm.plb = None pm.save() if pm.pla and pm.plb and pm.rca and pm.rcb: m = Match() m.pla = pm.pla m.plb = pm.plb m.sca = pm.sca m.scb = pm.scb m.rca = pm.rca m.rcb = pm.rcb if request.POST['date'].strip() == '': m.date = pm.group.date else: m.date = request.POST['date'] m.event = pm.group.event m.eventobj = eobj m.submitter = request.user m.set_period() m.offline = pm.group.offline m.game = pm.group.game m.save() success.append(m) group = pm.group pm.delete() if not group.prematch_set.all().exists(): group.delete() base['success'] = display_matches(success, messages=False) if len(success) > 0: base['messages'].append(Message('Approved %i match(es).' % len(success), type=Message.SUCCESS)) if ndel > 0: base['messages'].append(Message('Rejected %i match(es).' % ndel, type=Message.SUCCESS)) groups = PreMatchGroup.objects.filter(prematch__isnull=False)\ .select_related('prematch').order_by('id', 'event').distinct() for g in groups: g.prematches = display_matches(g.prematch_set.all(), messages=False) base['groups'] = groups base.update(csrf(request)) return render_to_response('review.html', base)