def edit_game(request, slug): """Lets the user suggest changes to a game for a moderator to verify""" # Load game object and get changeable fields and their defaults game = get_object_or_404(Game, slug=slug) change_model = game.get_change_model() # Workaround: Assigning change_model to initial in the form # directly will display the error immediately that changes must be made initial = change_model if request.method == "POST" else None # Sanity check: Disallow change-suggestions for changes if game.change_for: return HttpResponseBadRequest("You can only apply changes to a game") # Initialise form with rejected values or with the working copy form = GameEditForm(request.POST or change_model, request.FILES, initial=initial) # If form was submitted and is valid, persist suggestion for moderation if request.method == "POST" and form.is_valid(): # Save the game change_suggestion = form.save(commit=False) change_suggestion.change_for = game change_suggestion.save() form.save_m2m() # Save metadata (author + reason) change_suggestion_meta = GameSubmission( user=request.user, game=change_suggestion, reason=request.POST["reason"] ) change_suggestion_meta.save() return redirect(reverse("game-submitted-changes", kwargs={"slug": slug})) # Render template return render(request, "games/submit.html", {"form": form, "game": game})
def edit_game(request, slug): """Lets the user suggest changes to a game for a moderator to verify""" # Load game object and get changeable fields and their defaults game = get_object_or_404(Game, slug=slug) change_model = game.get_change_model() # Workaround: Assigning change_model to initial in the form # directly will display the error immediately that changes must be made initial = change_model if request.method == 'POST' else None # Sanity check: Disallow change-suggestions for changes if game.change_for: return HttpResponseBadRequest('You can only apply changes to a game') # Initialise form with rejected values or with the working copy form = GameEditForm(request.POST or change_model, request.FILES or None, initial=initial) # If form was submitted and is valid, persist suggestion for moderation if request.method == 'POST' and form.is_valid(): # Save the game change_suggestion = form.save(commit=False) change_suggestion.change_for = game change_suggestion.save() form.save_m2m() # Save metadata (author + reason) change_suggestion_meta = GameSubmission(user=request.user, game=change_suggestion, reason=request.POST['reason']) change_suggestion_meta.save() redirect_url = request.build_absolute_uri( reverse('game-submitted-changes')) # Enforce https if not settings.DEBUG: redirect_url = redirect_url.replace('http:', 'https:') LOGGER.info('Change-suggestions for game submitted, redirecting to %s', redirect_url) return redirect(redirect_url) # Render template return render(request, 'games/submit.html', {'form': form, 'game': game})