2007-02-17 00:23:09 +00:00
|
|
|
from xml.dom.minidom import parseString
|
2007-09-04 00:50:06 +00:00
|
|
|
|
2007-05-08 11:19:34 +00:00
|
|
|
from django.core.mail import EmailMessage, SMTPConnection
|
2006-09-02 09:34:40 +00:00
|
|
|
from django.template import Context, Template
|
2007-05-10 11:27:59 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
|
2006-09-02 09:34:40 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2007-03-22 10:23:52 +00:00
|
|
|
from django.newforms.forms import Form
|
2007-09-04 00:53:59 +00:00
|
|
|
from django.newforms import fields
|
2007-05-07 12:34:18 +00:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
def get_view(request):
|
|
|
|
"A simple view that expects a GET request, and returns a rendered template"
|
|
|
|
t = Template('This is a test. {{ var }} is the value.', name='GET Template')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
c = Context({'var': request.GET.get('var', 42)})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
|
|
|
def post_view(request):
|
|
|
|
"""A view that expects a POST, and returns a different template depending
|
|
|
|
on whether any POST data is available
|
|
|
|
"""
|
2007-02-17 00:23:09 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if request.POST:
|
|
|
|
t = Template('Data received: {{ data }} is the value.', name='POST Template')
|
|
|
|
c = Context({'data': request.POST['value']})
|
|
|
|
else:
|
|
|
|
t = Template('Viewing POST page.', name='Empty POST Template')
|
|
|
|
c = Context()
|
2006-09-02 09:34:40 +00:00
|
|
|
else:
|
2007-02-17 00:23:09 +00:00
|
|
|
t = Template('Viewing GET page.', name='Empty GET Template')
|
2006-09-02 09:34:40 +00:00
|
|
|
c = Context()
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
return HttpResponse(t.render(c))
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-02-17 00:23:09 +00:00
|
|
|
def raw_post_view(request):
|
|
|
|
"""A view which expects raw XML to be posted and returns content extracted
|
|
|
|
from the XML"""
|
|
|
|
if request.method == 'POST':
|
|
|
|
root = parseString(request.raw_post_data)
|
|
|
|
first_book = root.firstChild.firstChild
|
|
|
|
title, author = [n.firstChild.nodeValue for n in first_book.childNodes]
|
|
|
|
t = Template("{{ title }} - {{ author }}", name="Book template")
|
|
|
|
c = Context({"title": title, "author": author})
|
|
|
|
else:
|
|
|
|
t = Template("GET request.", name="Book GET template")
|
|
|
|
c = Context()
|
|
|
|
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
def redirect_view(request):
|
|
|
|
"A view that redirects all requests to the GET view"
|
2007-08-31 11:37:28 +00:00
|
|
|
if request.GET:
|
|
|
|
from urllib import urlencode
|
|
|
|
query = '?' + urlencode(request.GET, True)
|
|
|
|
else:
|
|
|
|
query = ''
|
|
|
|
return HttpResponseRedirect('/test_client/get_view/' + query)
|
2007-03-22 10:23:52 +00:00
|
|
|
|
2007-05-10 11:27:59 +00:00
|
|
|
def double_redirect_view(request):
|
|
|
|
"A view that redirects all requests to a redirection view"
|
|
|
|
return HttpResponseRedirect('/test_client/permanent_redirect_view/')
|
|
|
|
|
|
|
|
def bad_view(request):
|
|
|
|
"A view that returns a 404 with some error content"
|
|
|
|
return HttpResponseNotFound('Not found!. This page contains some MAGIC content')
|
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
TestChoices = (
|
|
|
|
('a', 'First Choice'),
|
|
|
|
('b', 'Second Choice'),
|
|
|
|
('c', 'Third Choice'),
|
|
|
|
('d', 'Fourth Choice'),
|
|
|
|
('e', 'Fifth Choice')
|
|
|
|
)
|
|
|
|
|
|
|
|
class TestForm(Form):
|
|
|
|
text = fields.CharField()
|
|
|
|
email = fields.EmailField()
|
|
|
|
value = fields.IntegerField()
|
|
|
|
single = fields.ChoiceField(choices=TestChoices)
|
|
|
|
multi = fields.MultipleChoiceField(choices=TestChoices)
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
def form_view(request):
|
|
|
|
"A view that tests a simple form"
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = TestForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
t = Template('Valid POST data.', name='Valid POST Template')
|
|
|
|
c = Context()
|
|
|
|
else:
|
|
|
|
t = Template('Invalid POST data. {{ form.errors }}', name='Invalid POST Template')
|
|
|
|
c = Context({'form': form})
|
|
|
|
else:
|
2007-07-21 05:17:20 +00:00
|
|
|
form = TestForm(request.GET)
|
2007-03-22 10:23:52 +00:00
|
|
|
t = Template('Viewing base form. {{ form }}.', name='Form GET Template')
|
|
|
|
c = Context({'form': form})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-03-22 10:23:52 +00:00
|
|
|
return HttpResponse(t.render(c))
|
2007-05-07 12:34:18 +00:00
|
|
|
|
|
|
|
def form_view_with_template(request):
|
|
|
|
"A view that tests a simple form"
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = TestForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
message = 'POST data OK'
|
|
|
|
else:
|
|
|
|
message = 'POST data has errors'
|
|
|
|
else:
|
|
|
|
form = TestForm()
|
|
|
|
message = 'GET form page'
|
2007-09-04 00:54:40 +00:00
|
|
|
return render_to_response('form_view.html',
|
|
|
|
{
|
2007-05-07 12:34:18 +00:00
|
|
|
'form': form,
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
)
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
def login_protected_view(request):
|
|
|
|
"A simple view that is login protected."
|
|
|
|
t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template')
|
|
|
|
c = Context({'user': request.user})
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2006-09-02 09:34:40 +00:00
|
|
|
return HttpResponse(t.render(c))
|
2007-02-09 13:47:36 +00:00
|
|
|
login_protected_view = login_required(login_protected_view)
|
|
|
|
|
|
|
|
def session_view(request):
|
|
|
|
"A view that modifies the session"
|
|
|
|
request.session['tobacconist'] = 'hovercraft'
|
2007-09-04 00:54:40 +00:00
|
|
|
|
|
|
|
t = Template('This is a view that modifies the session.',
|
2007-02-09 13:47:36 +00:00
|
|
|
name='Session Modifying View Template')
|
|
|
|
c = Context()
|
|
|
|
return HttpResponse(t.render(c))
|
2007-02-11 00:23:31 +00:00
|
|
|
|
|
|
|
def broken_view(request):
|
|
|
|
"""A view which just raises an exception, simulating a broken view."""
|
|
|
|
raise KeyError("Oops! Looks like you wrote some bad code.")
|
2007-05-08 11:19:34 +00:00
|
|
|
|
|
|
|
def mail_sending_view(request):
|
|
|
|
EmailMessage(
|
2007-09-04 00:54:40 +00:00
|
|
|
"Test message",
|
|
|
|
"This is a test email",
|
|
|
|
"from@example.com",
|
2007-05-08 11:19:34 +00:00
|
|
|
['first@example.com', 'second@example.com']).send()
|
|
|
|
return HttpResponse("Mail sent")
|
|
|
|
|
|
|
|
def mass_mail_sending_view(request):
|
|
|
|
m1 = EmailMessage(
|
2007-09-04 00:54:40 +00:00
|
|
|
'First Test message',
|
|
|
|
'This is the first test email',
|
|
|
|
'from@example.com',
|
2007-05-08 11:19:34 +00:00
|
|
|
['first@example.com', 'second@example.com'])
|
|
|
|
m2 = EmailMessage(
|
2007-09-04 00:54:40 +00:00
|
|
|
'Second Test message',
|
|
|
|
'This is the second test email',
|
|
|
|
'from@example.com',
|
2007-05-08 11:19:34 +00:00
|
|
|
['second@example.com', 'third@example.com'])
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-05-08 11:19:34 +00:00
|
|
|
c = SMTPConnection()
|
|
|
|
c.send_messages([m1,m2])
|
2007-09-04 00:54:40 +00:00
|
|
|
|
2007-05-08 11:19:34 +00:00
|
|
|
return HttpResponse("Mail sent")
|