def create(request): form = SnakeForm(request.user.profile, request.POST) if form.is_valid(): snake = form.save() messages.add_message(request, messages.SUCCESS, f"{snake.name} created successfully") return redirect(f"/u/{request.user.username}") return render(request, "core/snake/edit.html", {"form": form})
def test_new_snake_with_name_collision(): user = user_factory.basic(commit=True) Snake.objects.create(profile=user.profile, name="DSnek") form = SnakeForm( user.profile, {"name": "DSnek", "url": "https://dsnek.herokuapp.com", "is_public": True}, ) assert form.is_valid() is False assert len(form.errors) == 1
def update(request, snake_id): snake = request.user.profile.snakes.get(id=snake_id) form = SnakeForm(request.user.profile, request.POST, instance=snake) if form.is_valid(): snake = form.save() messages.add_message(request, messages.SUCCESS, f"{snake.name} updated successfully") return redirect(f"/u/{request.user.username}") return render(request, "core/snake/edit.html", {"form": form})
def test_new_snake(): user = user_factory.basic(commit=True) form = SnakeForm( user.profile, {"name": "DSnek", "url": "https://dsnek.herokuapp.com", "is_public": True}, ) form.save() user.refresh_from_db() snake = user.profile.snakes.get(name="DSnek") assert snake.url == "https://dsnek.herokuapp.com" assert snake.is_public is True
def edit(request, snake_id): try: snake = request.user.profile.snakes.get(id=snake_id) form = SnakeForm(request.user.profile, instance=snake) return render(request, "core/snake/edit.html", {"form": form}) except Snake.DoesNotExist: return redirect(f"/s/{snake_id}")
def test_update_existing_snake(): user = user_factory.basic(commit=True) snake = Snake.objects.create(profile=user.profile, name="DSnek") form = SnakeForm( user.profile, {"name": "DSnek", "url": "https://dsnek.herokuapp.com", "is_public": True}, instance=snake, ) assert snake.url == "" assert snake.is_public is False form.save() user.refresh_from_db() snake = user.profile.snakes.get(name="DSnek") assert snake.url == "https://dsnek.herokuapp.com" assert snake.is_public is True
def new(request): form = SnakeForm(request.user.profile) return render(request, "core/snake/new.html", {"form": form})