def post(self, request): form = UrlForm(request.POST) if form.is_valid(): full_url = form.cleaned_data.get('full_url') url_code = generate_short_code() Url.objects.create(url_code=url_code, full_url=full_url) return HttpResponseRedirect( reverse('shortener:success', kwargs={'url_code': url_code})) return HttpResponseRedirect(reverse('shortener:error'))
def post(self, request): form = UrlForm(data=request.POST) if form.is_valid(): # check if url exists in DB try: url = UrlModel.objects.get(url=form.cleaned_data['url']) except UrlModel.DoesNotExist: url = form.save(commit=False) # generates random slug slug = get_random_string(length=6) while UrlModel.objects.filter(slug=slug).exists(): slug = get_random_string(length=6) url.slug = slug url.save() return render(request, 'base.html', context={'new_url': url}) return render(request, 'base.html', context={'form_errors': form.errors})
def shorten_url(request): if request.method == 'GET': form = UrlForm() return render(request, 'shorten_url.html', { 'form': form } ) else: url = UrlForm(request.POST) if url.is_valid(): url = Url.objects.create(original=url.cleaned_data['original'], short=generate_short_url()) request.session["short_url"]=url.short return HttpResponseRedirect('../show_url.html' ) else: form = UrlForm() return render(request, 'shorten_url.html', { 'form': form, 'error_message': "Invalid input. Try something like 'http://www.cnn.com'", } )
def url_form_handler(request): """ Направляет на домашнюю страницу и обрабатывает форму """ urls_from_db = ShortUrls.objects.all() if request.method == 'GET': context = { 'form': UrlForm(), 'urls_from_db': urls_from_db, 'HOST': HOST } return render(request, 'shortener/home.html', context) elif request.method == "POST": form = UrlForm(request.POST) if form.is_valid(): long_url = form.cleaned_data.get('long_url') if ShortUrls.objects.filter(long_url=long_url).exists(): current_url = ShortUrls.objects.get(long_url=long_url) context = { 'short_url': f'{HOST}/{current_url.short_url}', 'HOST': HOST } return render(request, 'shortener/edit_url.html', context) else: current_url = ShortUrls(long_url=long_url) current_url.save() current_url.short_url = base_62_encode(current_url.id) current_url.save() context = { 'short_url': f'{HOST}/{current_url.short_url}', 'HOST': HOST } return render(request, 'shortener/edit_url.html', context) else: errors = form.errors return HttpResponse(errors) else: return HttpResponse('INVALID REQUEST')
def url_form_handler(request): """Directs to home_page and handles form""" urls_from_db = Url.objects.all() if request.method == 'GET': context = { 'form': UrlForm(), 'urls_from_db': urls_from_db, 'HOST': HOST } return render(request, 'shortener/home_page.html', context) elif request.method == 'POST': form = UrlForm(request.POST) if form.is_valid(): long_url = form.cleaned_data.get('long_url') if Url.objects.filter(long_url=long_url).exists(): current_url = Url.objects.get(long_url=long_url) context = { 'short_url': f'{HOST}/{current_url.short_url}', 'HOST': HOST } return render(request, 'shortener/to_short_url.html', context) else: current_url = Url(long_url=long_url) current_url.save() current_url.short_url = base_62_encode(current_url.id) current_url.save() context = { 'short_url': f'{HOST}/{current_url.short_url}', 'HOST': HOST } return render(request, 'shortener/to_short_url.html', context) else: errors = form.errors return HttpResponse(errors) else: return HttpResponse('INVALID REQUEST')
def shorten(request): if request.method == "POST": form = UrlForm(request.POST) if form.is_valid(): url = form.save(commit=True) return redirect('index')
def get(self, request): ctx = {'form': UrlForm()} return render(request, template_name='shortener/shortener.html', context=ctx)
def test_form_invalid(self): form_data = {'full_url': 'invalid_url.'} form = UrlForm(data=form_data) self.assertEqual(form.is_valid(), False)
def test_form_valid(self): form_data = {'full_url': 'https://www.google.com/'} form = UrlForm(data=form_data) self.assertEqual(form.is_valid(), True)