def submit(request): """ View for submitting an item. On GET it shows the page for submitting news items. Some of the data can be filled in from parameters passed in from the URL. On POST it tries to submit a news item. """ next = reverse('news.views.news_items.submit') if request.method != 'POST': title = '' url = '' text = '' submission_error = '' if 'submit_title' in request.session: title = get_from_session(request, 'submit_title') url = get_from_session(request, 'submit_url') text = get_from_session(request, 'submit_text') submission_error = get_from_session(request, 'submission_error') # this enables submission by bookmarklet elif 'title' in request.GET: title = request.GET.get('title', '') url = request.GET.get('url', '') url = urllib.unquote(url) return render_to_response('news/submit.html', {'title': title, 'url': url, 'text': text, 'submission_error': submission_error, 'next': next}, context_instance=RequestContext(request)) title = get_from_POST_or_404(request, 'title') url = get_from_POST_or_404(request, 'url') text = get_from_POST_or_404(request, 'text') url = improve_url(url) submission_error = check_submission(title, url, text) if submission_error: request.session['submit_title'] = title request.session['submit_url'] = url request.session['submit_text'] = text request.session['submission_error'] = submission_error return HttpResponseRedirect(reverse('news.views.news_items.submit')) if not request.user.is_authenticated(): request.session['submit_title'] = title request.session['submit_url'] = url request.session['submit_text'] = text return HttpResponseRedirect( reverse('news.views.login.login_view') + '?next=' + next) # if the url has already been posted, just vote up this story if url and NewsItem.objects.filter(url=url): newsitem = NewsItem.objects.filter(url=url)[0] news_item_next = reverse('news.views.news_items.news_item', args=(newsitem.id,)) vote_next = reverse('news.views.voting.vote') + '?' + \ urllib.urlencode({'id': newsitem.id, 'type': 'news_item', 'direction': 'up', 'next': news_item_next}) return HttpResponseRedirect(vote_next) # now we can create the news item newsitem = NewsItem.objects.create(poster=request.user.get_profile(), title=title, url=url, text=text) Rated.objects.create(rankable=newsitem, userprofile=request.user.get_profile(), direction='up') return HttpResponseRedirect( reverse('news.views.news_items.news_item', args=(newsitem.id,)))
def user(request, username): """ View for a user. On GET it shows the user profile (which is editable if the user is logged in and looking at their own profile.) On POST it tries to update the user's profile. """ userprofile = get_object_or_404(UserProfile, user__username=username) next = reverse('news.views.users.user', args=(userprofile.username,)) if request.method != 'POST': posting_error = get_from_session(request, 'userprofile_posting_error') email_address = get_from_session(request, 'userprofile_email') website = get_from_session(request, 'userprofile_website') about = get_from_session(request, 'userprofile_about') return render_to_response('news/user.html', {'viewing_userprofile': userprofile, 'next': next, 'posting_error': posting_error, 'error_email': email_address, 'error_website': website, 'error_about': about}, context_instance=RequestContext(request)) # don't allow users to POST without being logged in. # Also make sure the the user trying to post is the # one that is logged in. assert_or_404(request.user.is_authenticated()) assert_or_404(request.user == userprofile.user) email_address = get_from_POST_or_404(request, 'email_address') website = get_from_POST_or_404(request, 'website') about = get_from_POST_or_404(request, 'about') option_show_email = request.POST.get('option_show_email', False) option_use_javascript = request.POST.get('option_use_javascript', False) option_show_dead = request.POST.get('option_show_dead', False) # there shouldn't be a problem with option_show_email # or option_use_javascript, or option_show_dead, # so we can go ahead and set them if option_use_javascript == False: userprofile.option_use_javascript = False else: userprofile.option_use_javascript = True if option_show_email == False: userprofile.option_show_email = False else: userprofile.option_show_email = True if option_show_dead == False: userprofile.option_show_dead = False else: userprofile.option_show_dead = True userprofile.save() if email_address and not valid_email(email_address): request.session['userprofile_posting_error'] = "Email address too long" request.session['userprofile_email'] = email_address request.session['userprofile_website'] = website request.session['userprofile_about'] = about return HttpResponseRedirect(reverse('news.views.users.user', args=(userprofile.username,))) if website: website = improve_url(website) if not valid_url(website): request.session['userprofile_posting_error'] = "URL not valid" request.session['userprofile_email'] = email_address request.session['userprofile_website'] = website request.session['userprofile_about'] = about return HttpResponseRedirect(reverse('news.views.users.user', args=(userprofile.username,))) if about and not valid_text(about): request.session['userprofile_posting_error'] = "About not valid" request.session['userprofile_email'] = email_address request.session['userprofile_website'] = website request.session['userprofile_about'] = about return HttpResponseRedirect(reverse('news.views.users.user', args=(userprofile.username,))) userprofile.website = website userprofile.about = about userprofile.user.email = email_address userprofile.save() userprofile.user.save() return HttpResponseRedirect( reverse('news.views.users.user', args=(userprofile.username,)))
def testImproveURL(self): """ Test improve_url(). """ self.assertEquals(improve_url('http://google.com'), 'http://google.com') self.assertEquals(improve_url('google.com'), 'http://google.com')