2015-01-28 12:35:27 +00:00
|
|
|
from django import forms
|
2014-04-02 00:46:34 +00:00
|
|
|
from django.conf.urls import url
|
2009-12-09 16:57:23 +00:00
|
|
|
from django.contrib import messages
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2015-01-09 21:59:00 +00:00
|
|
|
from django.template import engines
|
2010-12-07 13:57:01 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import reverse
|
2011-03-17 08:07:40 +00:00
|
|
|
from django.views.decorators.cache import never_cache
|
2013-03-18 22:48:47 +00:00
|
|
|
from django.views.generic.edit import FormView
|
2009-12-09 16:57:23 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
|
2010-12-07 13:57:01 +00:00
|
|
|
TEMPLATE = """{% if messages %}
|
|
|
|
<ul class="messages">
|
|
|
|
{% for message in messages %}
|
|
|
|
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
|
|
|
|
{{ message }}
|
|
|
|
</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
{% endif %}
|
|
|
|
"""
|
2009-12-09 16:57:23 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2011-03-17 08:07:40 +00:00
|
|
|
@never_cache
|
2009-12-09 16:57:23 +00:00
|
|
|
def add(request, message_type):
|
|
|
|
# don't default to False here, because we want to test that it defaults
|
|
|
|
# to False if unspecified
|
|
|
|
fail_silently = request.POST.get('fail_silently', None)
|
|
|
|
for msg in request.POST.getlist('messages'):
|
|
|
|
if fail_silently is not None:
|
|
|
|
getattr(messages, message_type)(request, msg,
|
|
|
|
fail_silently=fail_silently)
|
|
|
|
else:
|
|
|
|
getattr(messages, message_type)(request, msg)
|
2010-12-07 13:57:01 +00:00
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
show_url = reverse('show_message')
|
2009-12-09 16:57:23 +00:00
|
|
|
return HttpResponseRedirect(show_url)
|
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2011-03-17 08:07:40 +00:00
|
|
|
@never_cache
|
2010-12-07 13:57:01 +00:00
|
|
|
def add_template_response(request, message_type):
|
|
|
|
for msg in request.POST.getlist('messages'):
|
|
|
|
getattr(messages, message_type)(request, msg)
|
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
show_url = reverse('show_template_response')
|
2010-12-07 13:57:01 +00:00
|
|
|
return HttpResponseRedirect(show_url)
|
2009-12-09 16:57:23 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2011-03-17 08:07:40 +00:00
|
|
|
@never_cache
|
2009-12-09 16:57:23 +00:00
|
|
|
def show(request):
|
2015-01-09 21:59:00 +00:00
|
|
|
template = engines['django'].from_string(TEMPLATE)
|
|
|
|
return HttpResponse(template.render(request=request))
|
2009-12-09 16:57:23 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2011-03-17 08:07:40 +00:00
|
|
|
@never_cache
|
2010-12-07 13:57:01 +00:00
|
|
|
def show_template_response(request):
|
2015-01-09 21:59:00 +00:00
|
|
|
template = engines['django'].from_string(TEMPLATE)
|
|
|
|
return TemplateResponse(request, template)
|
2009-12-09 16:57:23 +00:00
|
|
|
|
2013-03-18 22:48:47 +00:00
|
|
|
|
|
|
|
class ContactForm(forms.Form):
|
|
|
|
name = forms.CharField(required=True)
|
|
|
|
slug = forms.SlugField(required=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ContactFormViewWithMsg(SuccessMessageMixin, FormView):
|
|
|
|
form_class = ContactForm
|
|
|
|
success_url = show
|
|
|
|
success_message = "%(name)s was created successfully"
|
|
|
|
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2014-06-03 11:30:14 +00:00
|
|
|
url('^add/(debug|info|success|warning|error)/$', add, name='add_message'),
|
2013-03-18 22:48:47 +00:00
|
|
|
url('^add/msg/$', ContactFormViewWithMsg.as_view(), name='add_success_msg'),
|
2014-06-03 11:30:14 +00:00
|
|
|
url('^show/$', show, name='show_message'),
|
2014-09-04 12:15:09 +00:00
|
|
|
url('^template_response/add/(debug|info|success|warning|error)/$',
|
|
|
|
add_template_response, name='add_template_response'),
|
2014-06-03 11:30:14 +00:00
|
|
|
url('^template_response/show/$', show_template_response, name='show_template_response'),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|