Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ch18: NewsLink views use startup_slug URL segment.
  • Loading branch information
jambonrose committed Jul 30, 2015
1 parent 7dcdf22 commit f4e337f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
13 changes: 7 additions & 6 deletions organizer/forms.py
Expand Up @@ -4,12 +4,6 @@
from .models import NewsLink, Startup, Tag


class NewsLinkForm(forms.ModelForm):
class Meta:
model = NewsLink
fields = '__all__'


class SlugCleanMixin:
"""Mixin class for slug cleaning method."""

Expand All @@ -22,6 +16,13 @@ def clean_slug(self):
return new_slug


class NewsLinkForm(
SlugCleanMixin, forms.ModelForm):
class Meta:
model = NewsLink
fields = '__all__'


class StartupForm(
SlugCleanMixin, forms.ModelForm):
class Meta:
Expand Down
15 changes: 14 additions & 1 deletion organizer/utils.py
@@ -1,6 +1,19 @@
from django.shortcuts import get_object_or_404

from .models import Startup
from .models import NewsLink, Startup


class NewsLinkGetObjectMixin():

def get_object(self, queryset=None):
startup_slug = self.kwargs.get(
self.startup_slug_url_kwarg)
newslink_slug = self.kwargs.get(
self.slug_url_kwarg)
return get_object_or_404(
NewsLink,
slug__iexact=newslink_slug,
startup__slug__iexact=startup_slug)


class PageLinksMixin:
Expand Down
15 changes: 11 additions & 4 deletions organizer/views.py
Expand Up @@ -8,17 +8,22 @@
NewsLinkForm, StartupForm, TagForm)
from .models import NewsLink, Startup, Tag
from .utils import (
PageLinksMixin, StartupContextMixin)
NewsLinkGetObjectMixin, PageLinksMixin,
StartupContextMixin)


class NewsLinkCreate(
StartupContextMixin, CreateView):
NewsLinkGetObjectMixin,
StartupContextMixin,
CreateView):
form_class = NewsLinkForm
model = NewsLink


class NewsLinkDelete(
StartupContextMixin, DeleteView):
NewsLinkGetObjectMixin,
StartupContextMixin,
DeleteView):
model = NewsLink
slug_url_kwarg = 'newslink_slug'

Expand All @@ -28,7 +33,9 @@ def get_success_url(self):


class NewsLinkUpdate(
StartupContextMixin, UpdateView):
NewsLinkGetObjectMixin,
StartupContextMixin,
UpdateView):
form_class = NewsLinkForm
model = NewsLink
slug_url_kwarg = 'newslink_slug'
Expand Down

0 comments on commit f4e337f

Please sign in to comment.