def index(request, tag=None): if tag: tag_model = get_object_or_404(Tag, name=tag) urls = tag_model.url_set.all() else: urls = Url.objects.all() if request.method == "POST": form = UrlForm(request.POST) if form.is_valid(): url_data = form.cleaned_data['url'] title = form.cleaned_data['title'] tags = form.cleaned_data['tags'] try: url = Url(url=url_data, title=title) url.fill_title() url.save() url.add_tags_from_string(tags) if tag: url.add_tag(tag) except IntegrityError: pass return HttpResponseRedirect(request.path) else: form = UrlForm() return render_to_response("index.html", { 'urls': urls, 'form': form, })
def test_get_url_with_no_title(self): response = self.mocker.mock() response.read() self.mocker.result("foo.zip") urlopen = self.mocker.replace("urllib.urlopen") urlopen("http://example.com/foo.zip") self.mocker.result(response) self.mocker.replay() url = Url(url="http://example.com/foo.zip") url.fill_title() self.assertEquals(url.title, "")
def test_fill_title_does_not_override_title(self): response = self.mocker.mock() response.read() self.mocker.result(""" <html> <head> <title>Example Web Page</title> </head> </html> """) urlopen = self.mocker.replace("urllib.urlopen") urlopen("http://example.com") self.mocker.result(response) self.mocker.replay() url = Url(url="http://example.com", title="My Title") url.fill_title() self.assertEquals(url.title, "My Title")