Created
July 1, 2012 01:40
-
-
Save httpdss/3026437 to your computer and use it in GitHub Desktop.
NotifyMixin and NotifyCreateView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import stuff like NotifyCreateView and ProjectForm | |
class ProjectCreate(NotifyCreateView): | |
login_required = True | |
form_class = ProjectForm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.generic.edit import CreateView | |
from django.contrib import messages | |
from django.utils.translation import ugettext as _ | |
if "notification" in getattr(settings, "INSTALLED_APPS"): | |
from notification import models as notification | |
else: | |
notification = None | |
class NotifyMixin(object): | |
valid_type = messages.SUCCESS | |
valid_message = None | |
valid_flash = True | |
invalid_type = messages.ERROR | |
invalid_message = _("Some validation errors where found on the submitted form.") | |
invalid_flash = True | |
notify_list = None | |
notify_template = None | |
def show_invalid_flash(self): | |
if self.invalid_flash: | |
messages.add_message(self.request, self.invalid_type, self.invalid_message) | |
def show_valid_flash(self): | |
self.valid_message = _("The %s has been added successfully" % self.object._meta.verbose_name) | |
if self.valid_flash: | |
messages.add_message(self.request, self.valid_type, self.valid_message) | |
def send_notification(self): | |
if notification and self.notify_list and self.notify_template: | |
notification.send(self.notify_list, | |
self.notify_template, | |
self.get_context_data()) | |
class NotifyCreateView(CreateView, NotifyMixin): | |
def form_valid(self, form): | |
ret = super(NotifyCreateView, self).form_valid(form) | |
self.show_valid_flash() | |
self.send_notification() | |
return ret | |
def form_invalid(self, form, **kwargs): | |
show_invalid_flash() | |
return self.render_to_response(self.get_context_data(form=form)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment