def login(request): c = all_page_infos(request) nexturl = request.GET.get("next", "/") if request.method == 'POST': form = AuthenticationForm(data=request.POST) form.fields["password"].max_length = 4096 if form.is_valid(): user = form.get_user() django_contrib_auth_login(request, user) logger.info("Logged in user '%s' (%s) a.k.a '%s'", user.username, user.last_name, user.userprofile.aliases) if request.user.userprofile.game_pref_fixed or request.session.get("game_pref", 0) == 0: # if pref was set explicitely, always set cookie according to profile # or if no cookie set, use profiles setting (even if 0) request.session["game_pref"] = request.user.userprofile.game_pref else: # cookie is already set (!=0), overwrite users pref as it's not fixed try: request.user.userprofile.game_pref = int(request.session["game_pref"]) request.user.userprofile.save() except ValueError: # not int, ignore pass return HttpResponseRedirect(nexturl) else: logger.info("login error: %s", form.errors) else: form = AuthenticationForm() c["next"] = nexturl c['form'] = form form.fields["password"].max_length = 4096 return render(request, 'login.html', c)
def login(request): c = all_page_infos(request) nexturl = request.GET.get("next", "/") if request.method == 'POST': form = AuthenticationForm(data=request.POST) form.fields["password"].max_length = 4096 if form.is_valid(): user = form.get_user() django_contrib_auth_login(request, user) logger.info("Logged in user '%s' (%s) a.k.a '%s'", user.username, user.last_name, user.userprofile.aliases) if request.user.userprofile.game_pref_fixed or request.session.get( "game_pref", 0) == 0: # if pref was set explicitely, always set cookie according to profile # or if no cookie set, use profiles setting (even if 0) request.session[ "game_pref"] = request.user.userprofile.game_pref else: # cookie is already set (!=0), overwrite users pref as it's not fixed try: request.user.userprofile.game_pref = int( request.session["game_pref"]) request.user.userprofile.save() except ValueError: # not int, ignore pass return HttpResponseRedirect(nexturl) else: logger.info("login error: %s", form.errors) else: form = AuthenticationForm() c["next"] = nexturl c['form'] = form form.fields["password"].max_length = 4096 return render(request, 'login.html', c)
def hall_of_fame(request, abbreviation): c = all_page_infos(request) game = get_object_or_404(Game, abbreviation=abbreviation) if game.sldb_name == "ZK": pass elif game.sldb_name != "": try: c["leaderboards"] = get_sldb_leaderboards(game) except SLDBConnectionError as exc: logger.error("get_sldb_leaderboards(%r): %s", game, exc) else: c["errmsg"] = "No ratings available for this game. Please choose one from the menu." logger.error("%s (%s)", c["errmsg"], game) if abbreviation == "ZK": c["intro_text"] = ['<b>The official Hall of Fame of Zero-K is at <a ' 'href="http://zero-k.info/Ladders">http://zero-k.info/Ladders</a>.</b><br/>No rating ' 'records are kept on this site anymore.'] c["games"] = Game.objects.exclude(sldb_name="") c["ladders"] = [x[1] for x in RatingBase.MATCH_TYPE_CHOICES] # games_with_bawards = Game.objects.filter(gamerelease__name__in=BAwards.objects.values_list("replay__gametype", flat=True).distinct()).distinct() # FIXME: show awards that belong to each game separately. for now only BA. games_with_bawards = Game.objects.filter(name="Balanced Annihilation") if game in games_with_bawards: try: sist = SiteStats.objects.get(id=1) except ObjectDoesNotExist: update_stats() sist = SiteStats.objects.get(id=1) c["bawards"] = sist.bawards c["bawards_lu"] = sist.last_modified c["thisgame"] = game return render(request, "hall_of_fame.html", c)
def modlinks_modal(request, gameID): c = all_page_infos(request) mml = mapmodlinks(gameID) for k, v in mml.items(): c[k] = v return render(request, 'modal_modlinks.html', c)
def ratinghistorygraph_modal(request, game_abbr, accountid, match_type): c = all_page_infos(request) c["game_abbr"] = game_abbr c["game_verbose"] = Game.objects.get(sldb_name=game_abbr).name c["accountid"] = accountid c["match_type"] = match_type c["match_type_verbose"] = SldbPlayerTSGraphCache.match_type2sldb_name[match_type] return render(request, 'modal_rating_history_graph.html', c)
def upload(request): global timer c = all_page_infos(request) UploadFormSet = formset_factory(UploadFileForm, extra=5) if request.method == 'POST': formset = UploadFormSet(request.POST, request.FILES) replays = [] if formset.is_valid(): for form in formset: if form.cleaned_data: logger.info("upload by request.user=%r form.cleaned_data=%r", request.user, form.cleaned_data) ufile = form.cleaned_data['file'] short = form.cleaned_data['short'] long_text = form.cleaned_data['long_text'] tags = form.cleaned_data['tags'] timer = SrsTiming() timer.start("upload()") (path, written_bytes) = save_uploaded_file(ufile.read(), ufile.name) logger.info("User '%s' uploaded file '%s' with title '%s', parsing it now.", request.user, os.path.basename(path), short[:20]) if written_bytes != ufile.size: logger.warn("written_bytes=%r != ufile.size=%r", written_bytes, ufile.size) try: replay, msg = parse_uploaded_file(path, timer, tags, short, long_text, request.user) logger.debug("replay=%r msg=%r", replay, msg) replays.append((True, replay)) except parse_demo_file.BadFileType: form._errors = {'file': [u'Not a spring demofile: %s.' % ufile.name]} replays.append((False, 'Not a spring demofile: %s.' % ufile.name)) continue except AlreadyExistsError as exc: form._errors = {'file': [u'Uploaded replay already exists: "{}"'.format(exc.replay.title)]} replays.append((False, 'Uploaded replay already exists: <a href="{}">{}</a>.'.format( exc.replay.get_absolute_url(), exc.replay.title))) continue finally: timer.stop("upload()") logger.info("timings:\n%s", timer) if len(replays) == 0: logger.error("no replay created, this shouldn't happen") elif len(replays) == 1: if replays[0][0]: return HttpResponseRedirect(replays[0][1].get_absolute_url()) else: # fall through to get back on page with error msg pass else: c['replays'] = replays c["replay_details"] = True return render(request, 'multi_upload_success.html', c) else: # form = UploadFileForm() formset = UploadFormSet() c['formset'] = formset return render(request, 'upload.html', c)
def upload_media(request, gameID): c = all_page_infos(request) replay = get_object_or_404(Replay, gameID=gameID) if not request.user in get_owner_list(replay.uploader): return HttpResponseRedirect(replay.get_absolute_url()) UploadMediaFormSet = formset_factory(UploadMediaForm, extra=5) c["replay"] = replay c["replay_details"] = True if request.method == 'POST': logger.debug("request.FILES: %s", request.FILES) formset = UploadMediaFormSet(request.POST, request.FILES) if formset.is_valid(): c["media_files"] = list() for form in formset: if form.cleaned_data: logger.info("form.cleaned_data=%s", form.cleaned_data) media = form.cleaned_data['media_file'] image = form.cleaned_data['image_file'] comment = form.cleaned_data['comment'] if media: media.seek(0) media_magic_text = magic.from_buffer(media.read(1024)) media.seek(0) media_magic_mime = magic.from_buffer(media.read(1024), mime=True) media.seek(0) else: media_magic_text = None media_magic_mime = None erm = ExtraReplayMedia.objects.create( replay=replay, uploader=request.user, comment=comment, media=media, image=image, media_magic_text=media_magic_text, media_magic_mime=media_magic_mime) c["media_files"].append(erm) logger.info( "User '%s' uploaded for replay:'%s' media:'%s' img:'%s' with comment:'%s'.", request.user, replay, erm.media, erm.image, erm.comment[:20]) return render(request, 'upload_media_success.html', c) else: logger.error("formset.errors: %s", formset.errors) logger.error("request.FILES: %s", request.FILES) logger.error("request.POST: %s", request.POST) else: formset = UploadMediaFormSet() c["formset"] = formset return render(request, 'upload_media.html', c)
def edit_replay(request, gameID): c = all_page_infos(request) try: replay = Replay.objects.prefetch_related().get(gameID=gameID) c["replay"] = replay except ObjectDoesNotExist: raise Http404("No replay with ID '{}' found.".format( strip_tags(gameID))) if request.user != replay.uploader: return render(request, 'edit_replay_wrong_user.html', c) if request.method == 'POST': form = EditReplayForm(request.POST) if form.is_valid(): short = request.POST['short'] long_text = request.POST['long_text'] tags = request.POST['tags'] for tag in replay.tags.all(): if tag.replay_count == 1 and tag.pk > 10: # this tag was used only by this replay and is not one of the default ones (see srs/sql/tag.sql) tag.delete() replay.tags.clear() autotag = set_autotag(replay) save_tags(replay, tags) save_desc(replay, short, long_text, autotag) replay.save() logger.info( "User '%s' modified replay '%s': short: '%s' title:'%s' long_text:'%s' tags:'%s'", request.user, replay.gameID, replay.short_text, replay.title, replay.long_text, ", ".join( Tag.objects.filter(replay=replay).values_list("name", flat=True))) return HttpResponseRedirect(replay.get_absolute_url()) else: form = EditReplayForm({ 'short': replay.short_text, 'long_text': replay.long_text, 'tags': ", ".join( Tag.objects.filter(replay=replay).values_list("name", flat=True)) }) c['form'] = form c["replay_details"] = True return render(request, 'edit_replay.html', c)
def index(request): c = all_page_infos(request) if "game_pref_obj" in c: replays = Replay.objects.filter(published=True, gametype__in=c["game_pref_obj"].gamerelease_set.values_list("name", flat=True)) else: replays = Replay.objects.filter(published=True) c["replays"] = replays.order_by("-upload_date")[:settings.INDEX_REPLAY_RANGE] c["range"] = settings.INDEX_REPLAY_RANGE c["range_end"] = settings.INDEX_REPLAY_RANGE c["popular_replays"] = Replay.objects.order_by("-download_count")[:8] c["news"] = NewsItem.objects.filter(show=True).order_by('-pk')[:6] c["replay_details"] = False c["latest_comments"] = Comment.objects.filter(is_removed=False).order_by("-submit_date")[:5] return render(request, 'index.html', c)
def media(request, mediaid): media = get_object_or_404(ExtraReplayMedia, id=mediaid) if media.media_magic_mime == "image/svg+xml": c = all_page_infos(request) c["media"] = media return render(request, 'show_svg.html', c) else: try: response = HttpResponse(media.media.read(), content_type=media.media_magic_mime) response['Content-Disposition'] = 'attachment; filename="%s"' % media.media_basename return response except IOError, e: logger.error("Cannot read media from ExtraReplayMedia(%d) of Replay(%d): media '%s'. Exception: %s" % ( media.id, media.replay.id, media.media_basename, str(e))) raise Http404("Error reading '%s', please contact 'Dansan' in the springrts forum." % media.media_basename)
def upload_media(request, gameID): c = all_page_infos(request) replay = get_object_or_404(Replay, gameID=gameID) if not request.user in get_owner_list(replay.uploader): return HttpResponseRedirect(replay.get_absolute_url()) UploadMediaFormSet = formset_factory(UploadMediaForm, extra=5) c["replay"] = replay c["replay_details"] = True if request.method == 'POST': logger.debug("request.FILES: %s", request.FILES) formset = UploadMediaFormSet(request.POST, request.FILES) if formset.is_valid(): c["media_files"] = list() for form in formset: if form.cleaned_data: logger.info("form.cleaned_data=%s", form.cleaned_data) media = form.cleaned_data['media_file'] image = form.cleaned_data['image_file'] comment = form.cleaned_data['comment'] if media: media.seek(0) media_magic_text = magic.from_buffer(media.read(1024)) media.seek(0) media_magic_mime = magic.from_buffer(media.read(1024), mime=True) media.seek(0) else: media_magic_text = None media_magic_mime = None erm = ExtraReplayMedia.objects.create(replay=replay, uploader=request.user, comment=comment, media=media, image=image, media_magic_text=media_magic_text, media_magic_mime=media_magic_mime) c["media_files"].append(erm) logger.info("User '%s' uploaded for replay:'%s' media:'%s' img:'%s' with comment:'%s'.", request.user, replay, erm.media, erm.image, erm.comment[:20]) return render(request, 'upload_media_success.html', c) else: logger.error("formset.errors: %s", formset.errors) logger.error("request.FILES: %s", request.FILES) logger.error("request.POST: %s", request.POST) else: formset = UploadMediaFormSet() c["formset"] = formset return render(request, 'upload_media.html', c)
def user_settings(request): # TODO: c = all_page_infos(request) up = request.user.userprofile if request.method == 'POST': game_pref_form = GamePref(request.POST) if game_pref_form.is_valid(): up.game_pref_fixed = not game_pref_form.cleaned_data["auto"] if game_pref_form.cleaned_data["game_choice"]: up.game_pref = int(game_pref_form.cleaned_data["game_choice"]) else: up.game_pref = request.session.get("game_pref", None) up.save() request.session["game_pref"] = up.game_pref else: game_pref_form = GamePref(initial={"auto": not up.game_pref_fixed, "game_choice": up.game_pref}) c["game_pref_form"] = game_pref_form return render(request, 'settings.html', c)
def index_replay_range(request, range_end, game_pref): c = all_page_infos(request) try: game_pref = int(game_pref) except (TypeError, ValueError): pass else: if "game_pref_obj" not in c and game_pref > 0: c["game_pref"] = game_pref c["game_pref_obj"] = Game.objects.get(id=game_pref) c["range"] = settings.INDEX_REPLAY_RANGE c["range_end"] = int(range_end) + settings.INDEX_REPLAY_RANGE if "game_pref_obj"in c: replays = Replay.objects.filter(published=True, gametype__in=c["game_pref_obj"].gamerelease_set.values_list("name", flat=True)) else: replays = Replay.objects.filter(published=True) c["replays"] = replays.order_by("-upload_date")[int(range_end):c["range_end"]] return render(request, 'replay_index_boxes.html', c)
def index(request): c = all_page_infos(request) if "game_pref_obj" in c: replays = Replay.objects.filter( published=True, gametype__in=c["game_pref_obj"].gamerelease_set.values_list( "name", flat=True)) else: replays = Replay.objects.filter(published=True) c["replays"] = replays.order_by( "-upload_date")[:settings.INDEX_REPLAY_RANGE] c["range"] = settings.INDEX_REPLAY_RANGE c["range_end"] = settings.INDEX_REPLAY_RANGE c["popular_replays"] = Replay.objects.order_by("-download_count")[:8] c["news"] = NewsItem.objects.filter(show=True).order_by('-pk')[:6] c["replay_details"] = False c["latest_comments"] = Comment.objects.filter( is_removed=False).order_by("-submit_date")[:5] return render(request, 'index.html', c)
def player(request, accountid): c = all_page_infos(request) accountid = int(accountid) pa = get_object_or_404(PlayerAccount, accountid=accountid) c['pagetitle'] = "Player " + pa.preffered_name c["pagedescription"] = "Statistics and match history of player %s" % pa.preffered_name c["playeraccount"] = pa c["PA"] = pa c["all_names"] = pa.get_names() bawards = pa.bawards if any(pa.bawards.values()): c["bawards"] = bawards xtawards = pa.xtawards if any(xtawards): c["xtawards"] = xtawards if not respect_privacy(request, accountid): c["ts_history_games"] = pa.get_all_games_no_bots().exclude(sldb_name="") return render(request, "player.html", c)
def player(request, accountid): c = all_page_infos(request) accountid = int(accountid) pa = get_object_or_404(PlayerAccount, accountid=accountid) c['pagetitle'] = "Player " + pa.preffered_name c["pagedescription"] = "Statistics and match history of player %s" % pa.preffered_name c["playeraccount"] = pa c["PA"] = pa c["all_names"] = pa.get_names() bawards = pa.bawards if any(pa.bawards.values()): c["bawards"] = bawards xtawards = pa.xtawards if any(xtawards): c["xtawards"] = xtawards if not respect_privacy(request, accountid): c["ts_history_games"] = pa.get_all_games_no_bots().exclude( sldb_name="") return render(request, "player.html", c)
def media(request, mediaid): media = get_object_or_404(ExtraReplayMedia, id=mediaid) if media.media_magic_mime == "image/svg+xml": c = all_page_infos(request) c["media"] = media return render(request, 'show_svg.html', c) else: try: response = HttpResponse(media.media.read(), content_type=media.media_magic_mime) response[ 'Content-Disposition'] = 'attachment; filename="%s"' % media.media_basename return response except IOError, e: logger.error( "Cannot read media from ExtraReplayMedia(%d) of Replay(%d): media '%s'. Exception: %s" % (media.id, media.replay.id, media.media_basename, str(e))) raise Http404( "Error reading '%s', please contact 'Dansan' in the springrts forum." % media.media_basename)
def user_settings(request): # TODO: c = all_page_infos(request) up = request.user.userprofile if request.method == 'POST': game_pref_form = GamePref(request.POST) if game_pref_form.is_valid(): up.game_pref_fixed = not game_pref_form.cleaned_data["auto"] if game_pref_form.cleaned_data["game_choice"]: up.game_pref = int(game_pref_form.cleaned_data["game_choice"]) else: up.game_pref = request.session.get("game_pref", None) up.save() request.session["game_pref"] = up.game_pref else: game_pref_form = GamePref(initial={ "auto": not up.game_pref_fixed, "game_choice": up.game_pref }) c["game_pref_form"] = game_pref_form return render(request, 'settings.html', c)
def sldb_privacy_mode(request): c = all_page_infos(request) accountid = request.user.userprofile.accountid try: c["current_privacy_mode"] = get_sldb_pref(accountid, "privacyMode") except SLDBConnectionError as exc: logger.error("get_sldb_pref(%r, \"privacyMode\"): %s", accountid, exc) c["current_privacy_mode"] = -1 logger.debug("current_privacy_mode: %s (user: %s)", str(c["current_privacy_mode"]), request.user) if request.method == 'POST': # # POST # form = SLDBPrivacyForm(request.POST) if form.is_valid(): c["in_post"] = True new_mode = int(form.cleaned_data["mode"]) logger.debug("new_mode: %d (user: %s)", new_mode, request.user) try: sldb_pref = set_sldb_pref(accountid, "privacyMode", str(new_mode)) except: logger.exception("FIXME: to broad exception handling.") c["new_privacy_mode"] = -1 else: c["new_privacy_mode"] = new_mode logger.debug("new_privacy_mode: %d (user: %s)", new_mode, request.user) else: # # GET # form = SLDBPrivacyForm({'mode': c["current_privacy_mode"]}) c['form'] = form return render(request, 'sldb_privacy_mode.html', c)
def index_replay_range(request, range_end, game_pref): c = all_page_infos(request) try: game_pref = int(game_pref) except (TypeError, ValueError): pass else: if "game_pref_obj" not in c and game_pref > 0: c["game_pref"] = game_pref c["game_pref_obj"] = Game.objects.get(id=game_pref) c["range"] = settings.INDEX_REPLAY_RANGE c["range_end"] = int(range_end) + settings.INDEX_REPLAY_RANGE if "game_pref_obj" in c: replays = Replay.objects.filter( published=True, gametype__in=c["game_pref_obj"].gamerelease_set.values_list( "name", flat=True)) else: replays = Replay.objects.filter(published=True) c["replays"] = replays.order_by( "-upload_date")[int(range_end):c["range_end"]] return render(request, 'replay_index_boxes.html', c)
def edit_replay(request, gameID): c = all_page_infos(request) try: replay = Replay.objects.prefetch_related().get(gameID=gameID) c["replay"] = replay except ObjectDoesNotExist: raise Http404("No replay with ID '{}' found.".format(strip_tags(gameID))) if request.user != replay.uploader: return render(request, 'edit_replay_wrong_user.html', c) if request.method == 'POST': form = EditReplayForm(request.POST) if form.is_valid(): short = request.POST['short'] long_text = request.POST['long_text'] tags = request.POST['tags'] for tag in replay.tags.all(): if tag.replay_count == 1 and tag.pk > 10: # this tag was used only by this replay and is not one of the default ones (see srs/sql/tag.sql) tag.delete() replay.tags.clear() autotag = set_autotag(replay) save_tags(replay, tags) save_desc(replay, short, long_text, autotag) replay.save() logger.info("User '%s' modified replay '%s': short: '%s' title:'%s' long_text:'%s' tags:'%s'", request.user, replay.gameID, replay.short_text, replay.title, replay.long_text, ", ".join(Tag.objects.filter(replay=replay).values_list("name", flat=True))) return HttpResponseRedirect(replay.get_absolute_url()) else: form = EditReplayForm({'short': replay.short_text, 'long_text': replay.long_text, 'tags': ", ".join(Tag.objects.filter(replay=replay).values_list("name", flat=True))}) c['form'] = form c["replay_details"] = True return render(request, 'edit_replay.html', c)
def hall_of_fame(request, abbreviation): c = all_page_infos(request) game = get_object_or_404(Game, abbreviation=abbreviation) if game.sldb_name == "ZK": pass elif game.sldb_name != "": try: c["leaderboards"] = get_sldb_leaderboards(game) except SLDBConnectionError as exc: logger.error("get_sldb_leaderboards(%r): %s", game, exc) else: c["errmsg"] = "No ratings available for this game. Please choose one from the menu." logger.error("%s (%s)", c["errmsg"], game) if abbreviation == "ZK": c["intro_text"] = [ '<b>The official Hall of Fame of Zero-K is at <a ' 'href="http://zero-k.info/Ladders">http://zero-k.info/Ladders</a>.</b><br/>No rating ' 'records are kept on this site anymore.' ] c["games"] = Game.objects.exclude(sldb_name="") c["ladders"] = [x[1] for x in RatingBase.MATCH_TYPE_CHOICES] # games_with_bawards = Game.objects.filter(gamerelease__name__in=BAwards.objects.values_list("replay__gametype", flat=True).distinct()).distinct() # FIXME: show awards that belong to each game separately. for now only BA. games_with_bawards = Game.objects.filter(name="Balanced Annihilation") if game in games_with_bawards: try: sist = SiteStats.objects.get(id=1) except ObjectDoesNotExist: update_stats() sist = SiteStats.objects.get(id=1) c["bawards"] = sist.bawards c["bawards_lu"] = sist.last_modified c["thisgame"] = game return render(request, "hall_of_fame.html", c)
def replay(request, gameID): c = all_page_infos(request) if not gameid_re.findall(gameID): raise Http404("Malformed gameID: %r." % gameID) try: replay = Replay.objects.prefetch_related().get(gameID=gameID) c["replay"] = replay c["comment_obj"] = replay except ObjectDoesNotExist: raise Http404("No replay with gameID '" + strip_tags(gameID) + "' found.") if not replay.published: return render(request, 'replay_unpublished.html', c) game = replay.game_release.game match_type = replay.match_type_short try: match_skills = get_sldb_match_skills([replay.gameID]) if match_skills: match_skills = match_skills[0] except SLDBConnectionError as exc: logger.error("get_sldb_match_skills(%s): %s", [replay.gameID], exc) match_skills = {"status": 3} # ignore, we'll just use the old values from the DB in the view else: if match_skills and match_skills["status"] == 0: # update skill data in DB logger.debug("got match data for %s from sldb", replay) for player in match_skills["players"]: pa = player["account"] pa_skill = pa.get_rating(game, match_type) mu, si = player["skills"][1] if pa_skill.trueskill_mu != mu or pa_skill.trueskill_sigma != si: try: playername = Player.objects.get(account=pa, replay=replay, spectator=False).name except ObjectDoesNotExist: playername = "??" pa_skill.trueskill_mu = mu pa_skill.trueskill_sigma = si if pa_skill.playername == "" or pa_skill.playername == "??": pa_skill.playername = playername pa_skill.save() defaults = dict( match_date=replay.unixTime, playername=playername, trueskill_mu=mu, trueskill_sigma=si ) RatingHistory.objects.update_or_create(match=replay, game=game, match_type=match_type, playeraccount=pa, defaults=defaults) if pa.sldb_privacy_mode != player["privacyMode"]: pa.sldb_privacy_mode = player["privacyMode"] pa.save() if not replay.rated: replay.rated = True replay.save() else: # ignore, we'll just use the old values from the DB in the view logger.debug("no match data from SLDB") pass # fill cache prefetching all entries from DB in one call all_players = Player.objects.filter(replay=replay) allyteams = Allyteam.objects.filter(replay=replay) if not allyteams.filter(winner=True).exists() or ( replay.upload_date.year >= 2016 and allyteams.filter(winner=True, num=0).exists()): # workaround for issue #89: guess winner from ratings fix_missing_winner(replay) c["allyteams"] = [] match_rating_history = RatingHistory.objects.filter(match=replay, match_type=match_type) [entry for entry in match_rating_history] # prefetch all ratings of this match by hitting the DB only once for at in allyteams: playeraccounts = PlayerAccount.objects.filter(player__team__allyteam=at).order_by("player__team__num") teams = Team.objects.filter(allyteam=at) players = all_players.filter(account__in=playeraccounts) players_w_rating = list() old_rating = 0 new_rating = 0 lobby_rank_sum = 0 if replay.rated == False or replay.notcomplete or not players.exists() or not replay.game_release.game.sldb_name or all_players.filter( account__accountid=0).exists(): # notcomplete, no SLDB rating or bot present -> no rating players_w_rating = [(player, None, None) for player in players] else: # TrueSkill ratings for pa in playeraccounts: if match_skills and match_skills["status"] == 0: # use SLDB-provided values def _get_players_skills(pa): for pl in match_skills["players"]: if pl["account"] == pa: return pl pl = _get_players_skills(pa) pl_new = pl["skills"][1][0] pl_old = pl["skills"][0][0] else: # use old method of DB lookups for current and previous matchs DB entries try: pl_new = match_rating_history.get(playeraccount=pa).trueskill_mu except ObjectDoesNotExist: # no rating on this replay pl_new = None try: # find previous TS value pl_old = RatingHistory.objects.filter(playeraccount=pa, game=game, match_type=match_type, match__unixTime__lt=replay.unixTime).order_by( "-match__unixTime")[0].trueskill_mu except IndexError: pl_old = 25 # default value for new players # privatize? if playeraccounts.count() > 2 or pa.sldb_privacy_mode == 0: new_rating += pl_new if pl_new else 0 old_rating += pl_old if pl_old else 0 else: new_rating += privatize_skill(pl_new) if pl_new else 0 old_rating += privatize_skill(pl_old) if pl_old else 0 if pa.sldb_privacy_mode != 0 and ( not request.user.is_authenticated() or pa.accountid != request.user.userprofile.accountid): if pl_new: pl_new = privatize_skill(pl_new) if pl_old: pl_old = privatize_skill(pl_old) players_w_rating.append((all_players.get(account=pa, spectator=False), pl_old, pl_new)) if teams: lobby_rank_sum = reduce(lambda x, y: x + y, [pl.rank for pl in all_players.filter(team__allyteam=at)], 0) c["allyteams"].append((at, players_w_rating, old_rating, new_rating, lobby_rank_sum)) c["has_bot"] = replay.tags.filter(name="Bot").exists() c["specs"] = all_players.filter(replay=replay, spectator=True).order_by("id") c["upload_broken"] = UploadTmp.objects.filter(replay=replay).exists() c["mapoptions"] = MapOption.objects.filter(replay=replay).order_by("name") c["modoptions"] = ModOption.objects.filter(replay=replay).order_by("name") c["replay_details"] = True c["was_stopped"] = not allyteams.filter(winner=True).exists() if c["was_stopped"]: logger.info("was_stopped=True: allyteams=%r replay=%r", allyteams, replay) c["is_draw"] = allyteams.filter(winner=True).count() > 1 c["pagedescription"] = "%s %s %s match on %s (%s)" % ( replay.num_players, replay.match_type, replay.game_release.game.name, replay.map_info.name, replay.unixTime) c["replay_owners"] = get_owner_list(replay.uploader) c["extra_media"] = ExtraReplayMedia.objects.filter(replay=replay) c["known_video_formats"] = ["video/webm", "video/mp4", "video/ogg", "video/x-flv", "application/ogg"] c["has_video"] = c["extra_media"].filter(media_magic_mime__in=c["known_video_formats"]).exists() c["metadata"] = list() if replay.map_info.width > 128: # api.springfiles.com returnd pixel size map_px_x = replay.map_info.width / 512 map_px_y = replay.map_info.height / 512 else: # api.springfiles.com returnd Spring Map Size map_px_x = replay.map_info.width map_px_y = replay.map_info.height try: c["metadata"].append(("Size", "{} x {}".format(map_px_x, map_px_y))) try: c["metadata"].append(("Wind", "{} - {}".format(replay.map_info.metadata["metadata"]["MinWind"], replay.map_info.metadata["metadata"]["MaxWind"]))) except KeyError: pass try: c["metadata"].append(("Tidal", str(replay.map_info.metadata["metadata"]["TidalStrength"]))) except KeyError: pass for k, v in replay.map_info.metadata["metadata"].items(): if type(v) == str and not v.strip(): continue elif type(v) == list and not v: continue elif k.strip() in ["", "Width", "TidalStrength", "MapFileName", "MapMinHeight", "Type", "MapMaxHeight", "Resources", "Height", "MinWind", "MaxWind", "StartPos"]: # either already added above, or ignore uninteresting data continue else: c["metadata"].append((k.strip(), v)) try: if replay.map_info.metadata["version"]: c["metadata"].append(("Version", replay.map_info.metadata["version"])) except KeyError: pass except Exception as exc: c["metadata"].append(("Error", "Problem with metadata. Please report to Dansan.")) logger.error("FIXME: to broad exception handling.") logger.error("Problem with metadata (replay.id '%d'), replay.map_info.metadata: %s", replay.id, replay.map_info.metadata) logger.exception("Exception: %s", exc) c["xtaward_heroes"] = XTAwards.objects.filter(replay=replay, isAlive=1) c["xtaward_los"] = XTAwards.objects.filter(replay=replay, isAlive=0) page_history = request.session.get("page_history") if page_history and isinstance(page_history, list): # check data (session data is user input) for page in list(page_history): if not gameid_re.findall(page): page_history.remove(page) if gameID not in page_history: if len(page_history) > 4: page_history.remove(page) page_history.insert(0, gameID) else: page_history = [gameID] request.session["page_history"] = page_history return render(request, 'replay.html', c)
def browse_archive(request, bfilter): c = all_page_infos(request) for browse_filter in ["t_today", "t_yesterday", "t_this_month", "t_last_month", "t_this_year", "t_last_year", "t_ancient"]: c[browse_filter] = replay_filter(Replay.objects.all(), "date " + browse_filter) try: sist = SiteStats.objects.get(id=1) except ObjectDoesNotExist: sist = update_stats() tags = map(lambda x: (Tag.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.tags.split('|')) c["top_tags"] = list() for t in range(0, 12, 3): if t + 3 > len(tags): break c["top_tags"].append((tags[t], tags[t + 1], tags[t + 2])) maps = map(lambda x: (Map.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.maps.split('|')) c["top_maps"] = list() for r in range(0, 8, 2): if r + 2 > len(maps): break c["top_maps"].append((maps[r], maps[r + 1])) c["top_players"] = map(lambda x: (PlayerAccount.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.active_players.split('|')) c["all_games"] = map(lambda x: (Game.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.games.split('|')) c["replays_num"] = Replay.objects.count() c["gr_all"] = GameRelease.objects.all() c["tags_all"] = Tag.objects.all() c["maps_all"] = Map.objects.all() c["pa_num"] = PlayerAccount.objects.exclude(accountid__lte=0).count() c["ah_num"] = Replay.objects.values("autohostname").distinct().count() c["user_num"] = len([user for user in User.objects.all() if user.replays_uploaded() > 0]) c["first_replay"] = Replay.objects.first() c["last_replay"] = Replay.objects.last() hosts = dict() for host in Replay.objects.values_list("autohostname", flat=True): if host: try: hosts[host] += 1 except KeyError: hosts[host] = 0 c["autohosts"] = [(name, count) for name, count in hosts.items() if count > 0] c["autohosts"].sort(key=operator.itemgetter(1), reverse=True) c["uploaders"] = [(user.username, user.replays_uploaded()) for user in User.objects.all() if user.replays_uploaded() > 0] c["uploaders"].sort(key=operator.itemgetter(1), reverse=True) if bfilter and bfilter.strip(): bfilter = str(MySQLdb.escape_string(bfilter)) args = bfilter.split("/") filters_ = map(lambda x: x.split("="), args) # only accept "date=1224/tag=8v8" etc c["filters"] = list() have = list() for filter_ in filters_: if len(filter_) == 2 and filter_[1].strip() and filter_[0] in ["date", "map", "tag", "game", "gameversion", "player", "autohost", "uploader"] and \ filter_[0] not in have: try: if filter_[0] == "date": pass elif filter_[0] == "map": c["map_name"] = Map.objects.get(id=filter_[1]).name elif filter_[0] == "tag": Tag.objects.get(id=filter_[1]) elif filter_[0] == "game": Game.objects.get(id=filter_[1]) elif filter_[0] == "gameversion": gr = GameRelease.objects.get(id=filter_[1]) c["game_abbreviation"] = gr.game.abbreviation + " " + gr.version elif filter_[0] == "player": PlayerAccount.objects.get(id=filter_[1]) elif filter_[0] == "autohost": Replay.objects.filter(autohostname=filter_[1])[0] elif filter_[0] == "uploader": Replay.objects.filter(uploader__username=filter_[1])[0] else: raise Exception("unknown filter type '%s'" % filter_[0]) # all fine, add filter c["filters"].append((filter_[0], filter_[1])) have.append(filter_[0]) except Exception as exc: # object doesnt exist logger.exception("FIXME: to broad exception handling.") logger.debug("invalid filter_: '%s' Exception: %s", filter_, exc) else: c["filters"] = "" return render(request, 'browse_archive.html', c)
def gamerelease_modal(request, gameid): c = all_page_infos(request) c["gameversions"] = GameRelease.objects.filter(game__id=gameid).order_by("-id") return render(request, 'modal_gameversions.html', c)
def all_comments(request): c = all_page_infos(request) return render(request, 'comments.html', c)
def upload(request): global timer c = all_page_infos(request) UploadFormSet = formset_factory(UploadFileForm, extra=5) if request.method == 'POST': formset = UploadFormSet(request.POST, request.FILES) replays = [] if formset.is_valid(): for form in formset: if form.cleaned_data: logger.info( "upload by request.user=%r form.cleaned_data=%r", request.user, form.cleaned_data) ufile = form.cleaned_data['file'] short = form.cleaned_data['short'] long_text = form.cleaned_data['long_text'] tags = form.cleaned_data['tags'] timer = SrsTiming() timer.start("upload()") (path, written_bytes) = save_uploaded_file( ufile.read(), ufile.name) logger.info( "User '%s' uploaded file '%s' with title '%s', parsing it now.", request.user, os.path.basename(path), short[:20]) if written_bytes != ufile.size: logger.warn("written_bytes=%r != ufile.size=%r", written_bytes, ufile.size) try: replay, msg = parse_uploaded_file( path, timer, tags, short, long_text, request.user) logger.debug("replay=%r msg=%r", replay, msg) replays.append((True, replay)) except parse_demo_file.BadFileType: form._errors = { 'file': [u'Not a spring demofile: %s.' % ufile.name] } replays.append( (False, 'Not a spring demofile: %s.' % ufile.name)) continue except AlreadyExistsError as exc: form._errors = { 'file': [ u'Uploaded replay already exists: "{}"'.format( exc.replay.title) ] } replays.append(( False, 'Uploaded replay already exists: <a href="{}">{}</a>.' .format(exc.replay.get_absolute_url(), exc.replay.title))) continue finally: timer.stop("upload()") logger.info("timings:\n%s", timer) if len(replays) == 0: logger.error("no replay created, this shouldn't happen") elif len(replays) == 1: if replays[0][0]: return HttpResponseRedirect(replays[0][1].get_absolute_url()) else: # fall through to get back on page with error msg pass else: c['replays'] = replays c["replay_details"] = True return render(request, 'multi_upload_success.html', c) else: # form = UploadFileForm() formset = UploadFormSet() c['formset'] = formset return render(request, 'upload.html', c)
def replay(request, gameID): c = all_page_infos(request) if not gameid_re.findall(gameID): raise Http404("Malformed gameID: %r." % gameID) try: replay = Replay.objects.prefetch_related().get(gameID=gameID) c["replay"] = replay c["comment_obj"] = replay except ObjectDoesNotExist: raise Http404("No replay with gameID '" + strip_tags(gameID) + "' found.") if not replay.published: return render(request, 'replay_unpublished.html', c) game = replay.game_release.game match_type = replay.match_type_short sldb_connection_error = False try: match_skills = get_sldb_match_skills([replay.gameID]) if match_skills: match_skills = match_skills[0] except SLDBConnectionError as exc: sldb_connection_error = True logger.error("get_sldb_match_skills(%s): %s", [replay.gameID], exc) match_skills = {"status": 3} # ignore, we'll just use the old values from the DB in the view else: if match_skills and match_skills["status"] == 0: # update skill data in DB logger.debug("got match data for %s from sldb", replay) for player in match_skills["players"]: pa = player["account"] pa_skill = pa.get_rating(game, match_type) mu, si = player["skills"][1] if pa_skill.trueskill_mu != mu or pa_skill.trueskill_sigma != si: try: playername = Player.objects.get(account=pa, replay=replay, spectator=False).name except ObjectDoesNotExist: playername = "??" pa_skill.trueskill_mu = mu pa_skill.trueskill_sigma = si if pa_skill.playername == "" or pa_skill.playername == "??": pa_skill.playername = playername pa_skill.save() defaults = dict(match_date=replay.unixTime, playername=playername, trueskill_mu=mu, trueskill_sigma=si) RatingHistory.objects.update_or_create( match=replay, game=game, match_type=match_type, playeraccount=pa, defaults=defaults) if pa.sldb_privacy_mode != player["privacyMode"]: pa.sldb_privacy_mode = player["privacyMode"] pa.save() if not replay.rated: replay.rated = True replay.save() else: # ignore, we'll just use the old values from the DB in the view logger.debug("no match data from SLDB") pass # fill cache prefetching all entries from DB in one call all_players = Player.objects.filter(replay=replay) allyteams = Allyteam.objects.filter(replay=replay) if not allyteams.filter(winner=True).exists() or ( replay.upload_date.year >= 2016 and allyteams.filter( winner=True, num=0).exists()) and not sldb_connection_error: # workaround for issue #89: guess winner from ratings fix_missing_winner(replay) c["allyteams"] = [] match_rating_history = RatingHistory.objects.filter(match=replay, match_type=match_type) [entry for entry in match_rating_history ] # prefetch all ratings of this match by hitting the DB only once for at in allyteams: playeraccounts = PlayerAccount.objects.filter( player__team__allyteam=at).order_by("player__team__num") teams = Team.objects.filter(allyteam=at) players = all_players.filter(account__in=playeraccounts) players_w_rating = list() old_rating = 0 new_rating = 0 lobby_rank_sum = 0 if replay.rated == False or replay.notcomplete or not players.exists( ) or not replay.game_release.game.sldb_name or all_players.filter( account__accountid=0).exists(): # notcomplete, no SLDB rating or bot present -> no rating players_w_rating = [(player, None, None) for player in players] else: # TrueSkill ratings for pa in playeraccounts: if match_skills and match_skills["status"] == 0: # use SLDB-provided values def _get_players_skills(pa): for pl in match_skills["players"]: if pl["account"] == pa: return pl pl = _get_players_skills(pa) pl_new = pl["skills"][1][0] pl_old = pl["skills"][0][0] else: # use old method of DB lookups for current and previous matchs DB entries try: pl_new = match_rating_history.get( playeraccount=pa).trueskill_mu except ObjectDoesNotExist: # no rating on this replay pl_new = None try: # find previous TS value pl_old = RatingHistory.objects.filter( playeraccount=pa, game=game, match_type=match_type, match__unixTime__lt=replay.unixTime).order_by( "-match__unixTime")[0].trueskill_mu except IndexError: pl_old = 25 # default value for new players # privatize? if playeraccounts.count() > 2 or pa.sldb_privacy_mode == 0: new_rating += pl_new if pl_new else 0 old_rating += pl_old if pl_old else 0 else: new_rating += privatize_skill(pl_new) if pl_new else 0 old_rating += privatize_skill(pl_old) if pl_old else 0 if pa.sldb_privacy_mode != 0 and ( not request.user.is_authenticated() or pa.accountid != request.user.userprofile.accountid): if pl_new: pl_new = privatize_skill(pl_new) if pl_old: pl_old = privatize_skill(pl_old) players_w_rating.append( (all_players.get(account=pa, spectator=False), pl_old, pl_new)) if teams: lobby_rank_sum = reduce( lambda x, y: x + y, [pl.rank for pl in all_players.filter(team__allyteam=at)], 0) c["allyteams"].append( (at, players_w_rating, old_rating, new_rating, lobby_rank_sum)) c["has_bot"] = replay.tags.filter(name="Bot").exists() c["specs"] = all_players.filter(replay=replay, spectator=True).order_by("id") c["upload_broken"] = UploadTmp.objects.filter(replay=replay).exists() c["mapoptions"] = MapOption.objects.filter(replay=replay).order_by("name") c["modoptions"] = ModOption.objects.filter(replay=replay).order_by("name") c["replay_details"] = True c["was_stopped"] = not allyteams.filter(winner=True).exists() if c["was_stopped"]: logger.info("was_stopped=True: allyteams=%r replay=%r", allyteams, replay) c["is_draw"] = allyteams.filter(winner=True).count() > 1 c["pagedescription"] = "%s %s %s match on %s (%s)" % ( replay.num_players, replay.match_type, replay.game_release.game.name, replay.map_info.name, replay.unixTime) c["replay_owners"] = get_owner_list(replay.uploader) c["extra_media"] = ExtraReplayMedia.objects.filter(replay=replay) c["known_video_formats"] = [ "video/webm", "video/mp4", "video/ogg", "video/x-flv", "application/ogg" ] c["has_video"] = c["extra_media"].filter( media_magic_mime__in=c["known_video_formats"]).exists() c["metadata"] = list() if replay.map_info.width > 128: # api.springfiles.com returnd pixel size map_px_x = replay.map_info.width / 512 map_px_y = replay.map_info.height / 512 else: # api.springfiles.com returnd Spring Map Size map_px_x = replay.map_info.width map_px_y = replay.map_info.height try: c["metadata"].append(("Size", "{} x {}".format(map_px_x, map_px_y))) try: c["metadata"].append(("Wind", "{} - {}".format( replay.map_info.metadata["metadata"]["MinWind"], replay.map_info.metadata["metadata"]["MaxWind"]))) except KeyError: pass try: c["metadata"].append( ("Tidal", str(replay.map_info.metadata["metadata"]["TidalStrength"]))) except KeyError: pass for k, v in replay.map_info.metadata["metadata"].items(): if type(v) == str and not v.strip(): continue elif type(v) == list and not v: continue elif k.strip() in [ "", "Width", "TidalStrength", "MapFileName", "MapMinHeight", "Type", "MapMaxHeight", "Resources", "Height", "MinWind", "MaxWind", "StartPos" ]: # either already added above, or ignore uninteresting data continue else: c["metadata"].append((k.strip(), v)) try: if replay.map_info.metadata["version"]: c["metadata"].append( ("Version", replay.map_info.metadata["version"])) except KeyError: pass except Exception as exc: c["metadata"].append( ("Error", "Problem with metadata. Please report to Dansan.")) logger.error("FIXME: to broad exception handling.") logger.error( "Problem with metadata (replay.id '%d'), replay.map_info.metadata: %s", replay.id, replay.map_info.metadata) logger.exception("Exception: %s", exc) c["xtaward_heroes"] = XTAwards.objects.filter(replay=replay, isAlive=1) c["xtaward_los"] = XTAwards.objects.filter(replay=replay, isAlive=0) page_history = request.session.get("page_history") if page_history and isinstance(page_history, list): # check data (session data is user input) for page in list(page_history): if not gameid_re.findall(page): page_history.remove(page) if gameID not in page_history: if len(page_history) > 4: page_history.remove(page) page_history.insert(0, gameID) else: page_history = [gameID] request.session["page_history"] = page_history return render(request, 'replay.html', c)
def browse_archive(request, bfilter): c = all_page_infos(request) for browse_filter in [ "t_today", "t_yesterday", "t_this_month", "t_last_month", "t_this_year", "t_last_year", "t_ancient" ]: c[browse_filter] = replay_filter(Replay.objects.all(), "date " + browse_filter) try: sist = SiteStats.objects.get(id=1) except ObjectDoesNotExist: sist = update_stats() tags = map( lambda x: (Tag.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.tags.split('|')) c["top_tags"] = list() for t in range(0, 12, 3): if t + 3 > len(tags): break c["top_tags"].append((tags[t], tags[t + 1], tags[t + 2])) maps = map( lambda x: (Map.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.maps.split('|')) c["top_maps"] = list() for r in range(0, 8, 2): if r + 2 > len(maps): break c["top_maps"].append((maps[r], maps[r + 1])) c["top_players"] = map( lambda x: (PlayerAccount.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.active_players.split('|')) c["all_games"] = map( lambda x: (Game.objects.get(id=int(x.split(".")[0])), x.split(".")[1]), sist.games.split('|')) c["replays_num"] = Replay.objects.count() c["gr_all"] = GameRelease.objects.all() c["tags_all"] = Tag.objects.all() c["maps_all"] = Map.objects.all() c["pa_num"] = PlayerAccount.objects.exclude(accountid__lte=0).count() c["ah_num"] = Replay.objects.values("autohostname").distinct().count() c["user_num"] = len( [user for user in User.objects.all() if user.replays_uploaded() > 0]) c["first_replay"] = Replay.objects.first() c["last_replay"] = Replay.objects.last() hosts = dict() for host in Replay.objects.values_list("autohostname", flat=True): if host: try: hosts[host] += 1 except KeyError: hosts[host] = 0 c["autohosts"] = [(name, count) for name, count in hosts.items() if count > 0] c["autohosts"].sort(key=operator.itemgetter(1), reverse=True) c["uploaders"] = [(user.username, user.replays_uploaded()) for user in User.objects.all() if user.replays_uploaded() > 0] c["uploaders"].sort(key=operator.itemgetter(1), reverse=True) if bfilter and bfilter.strip(): bfilter = str(MySQLdb.escape_string(bfilter)) args = bfilter.split("/") filters_ = map(lambda x: x.split("="), args) # only accept "date=1224/tag=8v8" etc c["filters"] = list() have = list() for filter_ in filters_: if len(filter_) == 2 and filter_[1].strip() and filter_[0] in ["date", "map", "tag", "game", "gameversion", "player", "autohost", "uploader"] and \ filter_[0] not in have: try: if filter_[0] == "date": pass elif filter_[0] == "map": c["map_name"] = Map.objects.get(id=filter_[1]).name elif filter_[0] == "tag": Tag.objects.get(id=filter_[1]) elif filter_[0] == "game": Game.objects.get(id=filter_[1]) elif filter_[0] == "gameversion": gr = GameRelease.objects.get(id=filter_[1]) c["game_abbreviation"] = gr.game.abbreviation + " " + gr.version elif filter_[0] == "player": PlayerAccount.objects.get(id=filter_[1]) elif filter_[0] == "autohost": Replay.objects.filter(autohostname=filter_[1])[0] elif filter_[0] == "uploader": Replay.objects.filter(uploader__username=filter_[1])[0] else: raise Exception("unknown filter type '%s'" % filter_[0]) # all fine, add filter c["filters"].append((filter_[0], filter_[1])) have.append(filter_[0]) except Exception as exc: # object doesnt exist logger.exception("FIXME: to broad exception handling.") logger.debug("invalid filter_: '%s' Exception: %s", filter_, exc) else: c["filters"] = "" return render(request, 'browse_archive.html', c)