def submit_game(request): form = GameForm(request.POST or None, request.FILES or None) if request.method == "POST" and form.is_valid(): game = form.save() submission = GameSubmission(user=request.user, game=game) submission.save() # Notify managers a game has been submitted subject = u"New game submitted: {0}".format(game.name) admin_url = reverse("admin:games_game_change", args=(game.id, )) body = u""" The game {0} has been added by {1}. It can be modified and published at https://lutris.net{2} """.format(game.name, request.user, admin_url) mail_managers(subject, body) redirect_url = request.build_absolute_uri(reverse("game-submitted")) # Enforce https if not settings.DEBUG: redirect_url = redirect_url.replace('http:', 'https:') LOGGER.info('Game submitted, redirecting to %s', redirect_url) return redirect(redirect_url) return render(request, 'games/submit.html', {'form': form})
def submit_game(request): form = GameForm(request.POST or None, request.FILES or None) if request.method == "POST" and form.is_valid(): game = form.save() submission = GameSubmission(user=request.user, game=game) submission.save() # Notify managers a game has been submitted admin_url = "https://lutris.net" + reverse( "admin:games_game_change", args=(game.id,) ) context = { "game_name": game.name, "username": request.user.username, "admin_link": admin_url, } subject = "New game submitted: {}".format(game.name) send_email("new_game", context, subject, settings.MANAGERS[0][1]) redirect_url = request.build_absolute_uri(reverse("game-submitted")) # Enforce https if not settings.DEBUG: redirect_url = redirect_url.replace("http:", "https:") LOGGER.info("Game submitted, redirecting to %s", redirect_url) return redirect(redirect_url) return render(request, "games/submit.html", {"form": form})
def submit_game(request): form = GameForm(request.POST or None, request.FILES or None) if request.method == "POST" and form.is_valid(): game = form.save() submission = GameSubmission(user=request.user, game=game) submission.save() # Notify managers a game has been submitted admin_url = "https://lutris.net" + reverse("admin:games_game_change", args=(game.id, )) context = { "game_name": game.name, "username": request.user.username, "admin_link": admin_url, } subject = "New game submitted: {}".format(game.name) send_email("new_game", context, subject, settings.MANAGERS[0][1]) redirect_url = request.build_absolute_uri(reverse("game-submitted")) # Enforce https if not settings.DEBUG: redirect_url = redirect_url.replace("http:", "https:") LOGGER.info("Game submitted, redirecting to %s", redirect_url) return redirect(redirect_url) return render(request, "games/submit.html", {"form": form})
def test_form_save_handles_saving_to_player(self): a_player = Player.objects.create() a_form = GameForm(data={'text': 'A new game'}) new_game = a_form.save(for_player=a_player) self.assertEqual(new_game, Game.objects.first()) self.assertEqual(new_game.text, "A new game") self.assertEqual(new_game.player, a_player)
def new_player(request): a_form = GameForm(data=request.POST) if a_form.is_valid(): player = Player.objects.create() a_form.save(for_player=player) response = redirect(player) else: response = render(request, "home.html", {"form": a_form}) return response
def new(request): if request.method == "POST": form = GameForm(request.POST) if form.is_valid(): game = Game.objects.create(max_participants=form.cleaned_data["num_participants"]) print "#part." + form.cleaned_data["num_participants"] print "#part." + game.max_participants return render(request, "created.html", {"game": game}) else: return render(request, "new.html", {})
def test_blank_data(self): form = GameForm({}) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, { 'location': ['This field is required.'], 'datetime': ['This field is required.'], 'max_players': ['This field is required.'], 'min_players': ['This field is required.'], })
def test_valid_data(self): form = GameForm({ 'location': 'Roble Field', 'datetime': datetime.now(tz=timezone.utc) + timedelta(days=1), 'max_players': 12, 'min_players': 10, }) self.assertTrue(form.is_valid())
def create(request): """Create a new game.""" form = GameForm() if request.POST: form = GameForm(request.POST) if form.is_valid(): new_game = form.save(commit=False) new_game.creator = request.user new_game.save() messages.success(request, "Your game was successfully saved!") return HttpResponseRedirect(new_game.get_absolute_url()) return render(request, 'games/create.html', {'form': form})
def screenshots(request, game_id): """View/edit screenshots for a game.""" game = get_object_or_404(Game, id=game_id, creator=request.user) room_for_more = game.screenshot_set.count() < settings.SCREENSHOTS_MAX form = ScreenshotForm() game = get_object_or_404(Game, creator=request.user, pk=game_id) form = GameForm(instance=game) room_for_more = game.screenshot_set.count() < settings.SCREENSHOTS_MAX screenshot_form = ScreenshotForm() if request.POST and room_for_more: screenshot_form = ScreenshotForm(request.POST, request.FILES) if screenshot_form.is_valid(): new_screenshot = screenshot_form.save(commit=False) new_screenshot.game = game new_screenshot.save() messages.success(request, "Your screenshot has been uploaded!") return HttpResponseRedirect(reverse('games.edit', args=[game.id])) c = { 'game': game, 'form': form, 'room_for_more': room_for_more, 'screenshot_form': screenshot_form, } return render(request, 'games/edit.html', c)
def edit(request, game_id): """Edit an existing game.""" game = get_object_or_404(Game, creator=request.user, pk=game_id) form = GameForm(instance=game) room_for_more = game.screenshot_set.count() < settings.SCREENSHOTS_MAX screenshot_form = ScreenshotForm() c = {"game": game, "form": form, "room_for_more": room_for_more, "screenshot_form": screenshot_form} if request.POST: form = GameForm(request.POST, instance=game) c.update({"form": form}) if form.is_valid(): form.save() messages.success(request, "Your changes were saved!") return HttpResponseRedirect(game.get_absolute_url()) return render(request, "games/edit.html", c)
def create(request): num_heroes = int(request.GET.get('num_heroes', 3)) is_random = 'random' in request.GET if request.method == 'POST': form = GameForm(num_heroes, False, request.POST) if form.is_valid() and not request.user.is_anonymous(): game = form.save(commit=False) game.profile = request.user game.villain_hp = game.villain.starting_hp game.save() heroes = Hero.objects.filter(pk__in=form.cleaned_data['heroes']) count = heroes.count() for hero in heroes: ActiveHero.objects.create( hero=hero, game=game, hp=hero.starting_hp, order=count, ) count -= 1 return redirect('games:show', game.pk) else: if is_random: initial = { 'environment': Environment.objects.order_by('?')[0], 'villain': Villain.objects.order_by('?')[0], } else: initial = {} form = GameForm(num_heroes, is_random, initial=initial) context = { 'form': form, } return render( request, 'games/create.html', context, )
def new(request): """ Renders the signup form, allowing a user to create a new user. Processes results from the form, attempting to create a new user. If new user is created, redirects to the users profile @renders: users/new.html to show the signup form @redirects: /game/<username> to show the newly created users profile. """ if request.method == "POST": form = GameForm(request.POST) if form.is_valid(): new_game = form.save() profile = request.user.get_profile() new_game.creator = profile new_game.save() profile.join_game(new_game.pk) return redirect("/games/" + new_game.game_name) else: form = GameForm() return render(request, "games/new.html", {"form": form})
def new_game(request): if request.method == 'POST': form = GameForm(request.POST) if form.is_valid(): game = form.save() logger.info( f"Created new game -- location: {game.location} -- datetime: {game.datetime} -- max_players: {game.max_players}" ) return redirect(reverse('games:detail', args=(game.id, ))) else: form = GameForm() context = {'form': form} return render(request, "games/new_game.html", context)
def edit(request, game_id): """Edit an existing game.""" game = get_object_or_404(Game, creator=request.user, pk=game_id) form = GameForm(instance=game) room_for_more = game.screenshot_set.count() < settings.SCREENSHOTS_MAX screenshot_form = ScreenshotForm() c = { 'game': game, 'form': form, 'room_for_more': room_for_more, 'screenshot_form': screenshot_form, } if request.POST: form = GameForm(request.POST, instance=game) c.update({'form': form}) if form.is_valid(): form.save() messages.success(request, "Your changes were saved!") return HttpResponseRedirect(game.get_absolute_url()) return render(request, 'games/edit.html', c)
def home_page(request): a_form = GameForm() response = render(request, "home.html", {"form": a_form}) return response
def new(request, parent_id = None): parent = None save_mode = None if parent_id: parent = get_object_or_404(Post, pk = parent_id) form = PostForm( request.POST or None, request.FILES or None, author = request.user, parent = parent, ) image_form = ImageForm(request.POST or None, request.FILES or None) game_form = GameForm(request.POST or None, request.FILES or None) tag_form = TagForm(request.POST or None) if parent is None else None valid = False #A reply if parent is not None: valid = (parent is not None and form.is_valid()) #a new post: if parent is None: valid = (parent is None and form.is_valid() and tag_form.is_valid()) #an image post: if request.GET.get('image',False): save_mode = 'image' valid = (parent is None and form.is_valid() and tag_form.is_valid() and image_form.is_valid()) #a game post: if request.GET.get('game',False): save_mode = 'game' valid = (parent is None and form.is_valid() and tag_form.is_valid() and game_form.is_valid()) if valid: post = form.save() if parent is None: tag_form.post = post tag_form.save() if parent is None: PostReader.clear(post = post) else: PostReader.clear(post = post.parent) if image_form.is_valid() and save_mode == 'image': image_form.save(post = post) if game_form.is_valid() and save_mode == 'game': game_form.save(post = post) game_form.save_m2m() r_post = post if not parent else parent return redirect(reverse("posts:view", args=[r_post.id, r_post.title_slug()])) context = { 'form' : form, 'tag_form' : tag_form, 'image_form' : image_form, 'game_form' : game_form, } return render_to_response( "posts/post_form.html", context, RequestContext(request) )
def test_form_validation_for_blank_games(self): a_form = GameForm(data={'text': ''}) self.assertFalse(a_form.is_valid()) self.assertEqual(a_form.errors['text'], [EMPTY_GAME_ERROR])
def test_form_game_input_has_placeholder_and_css_classes(self): # ESCAPE changes both " <, but not both are escaped in AS_P. a_form = GameForm() self.assertIn('placeholder="Enter game <SCORE : PLAYER_AGAINST>"', a_form.as_p()) self.assertIn('class="form-control input-lg"', a_form.as_p())