Permalink
Browse files

Ch18: Refactor NewsLink views with new URLs.

  • Loading branch information...
jambonrose committed Mar 11, 2015
1 parent 438a2dd commit 7dcdf223188c8b574534002d829b7dc1134f2cf8
@@ -62,6 +62,11 @@ def get_delete_url(self):
return reverse('organizer_startup_delete',
kwargs={'slug': self.slug})
def get_newslink_create_url(self):
return reverse(
'organizer_newslink_create',
kwargs={'startup_slug': self.slug})
def get_update_url(self):
return reverse('organizer_startup_update',
kwargs={'slug': self.slug})
@@ -90,9 +95,13 @@ def get_absolute_url(self):
def get_delete_url(self):
return reverse(
'organizer_newslink_delete',
kwargs={'pk': self.pk})
kwargs={
'startup_slug': self.startup.slug,
'newslink_slug': self.slug})
def get_update_url(self):
return reverse(
'organizer_newslink_update',
kwargs={'pk': self.pk})
kwargs={
'startup_slug': self.startup.slug,
'newslink_slug': self.slug})
@@ -8,7 +8,7 @@
<div class="row">
<div class="offset-by-two eight columns">
<form
action="{% url 'organizer_newslink_create' %}"
action="{{ startup.get_newslink_create_url }}"
method="post">
{% csrf_token %}
{{ form.as_p }}
@@ -53,7 +53,7 @@ <h2>{{ startup.name }}</h2>
<h3>Recent News</h3>
<p>
<a
href="{% url 'organizer_newslink_create' %}"
href="{{ startup.get_newslink_create_url }}"
class="button">
Add Article</a></p>
<ul>

This file was deleted.

Oops, something went wrong.
@@ -1,8 +1,9 @@
from django.conf.urls import url
from ..views import (
StartupCreate, StartupDelete, StartupDetail,
StartupList, StartupUpdate)
NewsLinkCreate, NewsLinkDelete,
NewsLinkUpdate, StartupCreate, StartupDelete,
StartupDetail, StartupList, StartupUpdate)
urlpatterns = [
url(r'^$',
@@ -14,10 +15,24 @@
url(r'^(?P<slug>[\w\-]+)/$',
StartupDetail.as_view(),
name='organizer_startup_detail'),
url(r'^(?P<startup_slug>[\w\-]+)/'
r'add_article_link/$',
NewsLinkCreate.as_view(),
name='organizer_newslink_create'),
url(r'^(?P<slug>[\w\-]+)/delete/$',
StartupDelete.as_view(),
name='organizer_startup_delete'),
url(r'^(?P<slug>[\w\-]+)/update/$',
StartupUpdate.as_view(),
name='organizer_startup_update'),
url(r'^(?P<startup_slug>[\w\-]+)/'
r'(?P<newslink_slug>[\w\-]+)/'
r'delete/$',
NewsLinkDelete.as_view(),
name='organizer_newslink_delete'),
url(r'^(?P<startup_slug>[\w\-]+)/'
r'(?P<newslink_slug>[\w\-]+)/'
r'update/$',
NewsLinkUpdate.as_view(),
name='organizer_newslink_update'),
]
@@ -1,3 +1,8 @@
from django.shortcuts import get_object_or_404
from .models import Startup
class PageLinksMixin:
page_kwarg = 'page'
@@ -49,3 +54,20 @@ def get_context_data(self, **kwargs):
self.last_page(page),
})
return context
class StartupContextMixin():
startup_slug_url_kwarg = 'startup_slug'
startup_context_object_name = 'startup'
def get_context_data(self, **kwargs):
startup_slug = self.kwargs.get(
self.startup_slug_url_kwarg)
startup = get_object_or_404(
Startup, slug__iexact=startup_slug)
context = {
self.startup_context_object_name:
startup,
}
context.update(kwargs)
return super().get_context_data(**context)
@@ -7,25 +7,31 @@
from .forms import (
NewsLinkForm, StartupForm, TagForm)
from .models import NewsLink, Startup, Tag
from .utils import PageLinksMixin
from .utils import (
PageLinksMixin, StartupContextMixin)
class NewsLinkCreate(CreateView):
class NewsLinkCreate(
StartupContextMixin, CreateView):
form_class = NewsLinkForm
model = NewsLink
class NewsLinkDelete(DeleteView):
class NewsLinkDelete(
StartupContextMixin, DeleteView):
model = NewsLink
slug_url_kwarg = 'newslink_slug'
def get_success_url(self):
return (self.object.startup
.get_absolute_url())
class NewsLinkUpdate(UpdateView):
class NewsLinkUpdate(
StartupContextMixin, UpdateView):
form_class = NewsLinkForm
model = NewsLink
slug_url_kwarg = 'newslink_slug'
class StartupCreate(CreateView):
@@ -21,7 +21,6 @@
from blog import urls as blog_urls
from contact import urls as contact_urls
from organizer.urls import (
newslink as newslink_urls,
startup as startup_urls, tag as tag_urls)
urlpatterns = [
@@ -36,7 +35,6 @@
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include(blog_urls)),
url(r'^contact/', include(contact_urls)),
url(r'^newslink/', include(newslink_urls)),
url(r'^startup/', include(startup_urls)),
url(r'^tag/', include(tag_urls)),
]

0 comments on commit 7dcdf22

Please sign in to comment.