Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add crud views with pluggable view mixin

- idea for this is to quickly standup a staff
  admin for announcements and provide your own
  authentication rules via the mixin.
  • Loading branch information...
commit 4546b7e9d5d8ef571e1e93cae5d4f85274b7cb1c 1 parent a5a9200
@paltman paltman authored
View
18 announcements/forms.py
@@ -0,0 +1,18 @@
+from django import forms
+
+from announcements.models import Announcement
+
+
+class AnnouncementForm(forms.ModelForm):
+
+ class Meta:
+ model = Announcement
+ fields = [
+ "title",
+ "content",
+ "site_wide",
+ "members_only",
+ "dismissal_type",
+ "publish_start",
+ "publish_end"
+ ]
View
30 announcements/mixins.py
@@ -0,0 +1,30 @@
+from django.conf import settings
+from django.utils.importlib import import_module
+
+
+def _resolve(mixin_setting):
+ if isinstance(mixin_setting, basestring):
+ try:
+ mod_name, klass_name = mixin_setting.rsplit(".", 1)
+ except ValueError:
+ raise Exception("Improperly configured.")
+ try:
+ mod = import_module(mod_name)
+ except ImportError:
+ raise Exception("Could not import %s" % mod_name)
+ try:
+ klass = getattr(mod, klass_name)
+ except AttributeError:
+ raise Exception("The module '%s' does not contain '%s'." % (mod_name, klass_name))
+ mixin_setting = klass
+ return mixin_setting
+
+
+class DefaultProtectedMixin(object):
+
+ pass
+
+
+ProtectedMixin = _resolve(
+ getattr(settings, "ANNOUNCEMENTS_PROTECTED_MIXIN", DefaultProtectedMixin)
+)
View
6 announcements/signals.py
@@ -0,0 +1,6 @@
+import django.dispatch
+
+
+announcement_created = django.dispatch.Signal(providing_args=["announcement", "request"])
+announcement_updated = django.dispatch.Signal(providing_args=["announcement", "request"])
+announcement_deleted = django.dispatch.Signal(providing_args=["announcement", "request"])
View
12 announcements/urls.py
@@ -1,7 +1,15 @@
from django.conf.urls.defaults import *
+from announcements.views import detail, dismiss
+from announcements.views import CreateAnnouncementView, UpdateAnnouncementView
+from announcements.views import DeleteAnnouncementView, AnnouncementListView
urlpatterns = patterns("",
- url(r"^(?P<pk>\d+)/$", "announcements.views.detail", name="announcements_detail"),
- url(r"^(?P<pk>\d+)/hide/$", "announcements.views.dismiss", name="announcement_dismiss")
+ url(r"announcement/(?P<pk>\d+)/$", detail, name="announcements_detail"),
+ url(r"announcement/(?P<pk>\d+)/hide/$", dismiss, name="announcement_dismiss"),
+
+ url(r"announcement/create/$", CreateAnnouncementView.as_view(), name="announcements_create"),
+ url(r"announcement/(?P<pk>\d+)/update/$", UpdateAnnouncementView.as_view(), name="announcements_update"),
+ url(r"announcement/(?P<pk>\d+)/delete/$", DeleteAnnouncementView.as_view(), name="announcements_delete"),
+ url(r"", AnnouncementListView.as_view(), name="announcements_list"),
)
View
69 announcements/views.py
@@ -1,8 +1,14 @@
+from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseNotAllowed
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
+from django.views.generic.edit import CreateView, UpdateView, DeleteView
+from django.views.generic.list import ListView
+from announcements.forms import AnnouncementForm
+from announcements.mixins import ProtectedMixin
from announcements.models import Announcement
+from announcements.signals import announcement_created, announcement_updated, announcement_deleted
def dismiss(request, pk):
@@ -30,3 +36,66 @@ def detail(request, pk):
return TemplateResponse(request, "announcements/detail.html", {
"announcement": announcement
})
+
+
+class CreateAnnouncementView(CreateView, ProtectedMixin):
+
+ model = Announcement
+ form_class = AnnouncementForm
+
+ def form_valid(self, form):
+ self.object = form.save(commit=False)
+ self.object.creator = self.request.user
+ self.object.save()
+
+ announcement_created.send(
+ sender=self.object,
+ announcement=self.object,
+ request=self.request
+ )
+ return super(CreateAnnouncementView, self).form_valid(form)
+
+ def get_success_url(self):
+ return reverse("announcements_list")
+
+
+class UpdateAnnouncementView(UpdateView, ProtectedMixin):
+
+ model = Announcement
+ form_class = AnnouncementForm
+
+ def form_valid(self, form):
+ response = super(UpdateAnnouncementView, self).form_valid(form)
+ announcement_updated.send(
+ sender=self.object,
+ announcement=self.object,
+ request=self.request
+ )
+ return response
+
+ def get_success_url(self):
+ return reverse("announcements_list")
+
+
+class DeleteAnnouncementView(DeleteView, ProtectedMixin):
+
+ model = Announcement
+
+ def form_valid(self, form):
+ response = super(DeleteAnnouncementView, self).form_valid(form)
+ announcement_deleted.send(
+ sender=self.object,
+ announcement=self.object,
+ request=self.request
+ )
+ return response
+
+ def get_success_url(self):
+ return reverse("announcements_list")
+
+
+class AnnouncementListView(ListView, ProtectedMixin):
+
+ model = Announcement
+ queryset = Announcement.objects.all().order_by("-creation_date")
+ paginate_by = 50
Please sign in to comment.
Something went wrong with that request. Please try again.