Commits

Anonymous committed 90642f0

Started using django-guardian and requiring permissions
Prepopulate slugs
Added forms.py
Added create_thread permission
Added get_post_url method
Added thread_create and post_create views
Pass new post form in context to thread_detail

  • Participants
  • Parent commits 95b09de

Comments (0)

Files changed (5)

File gnocchi_forum/admin.py

 from django.contrib import admin
 
+from guardian.admin import GuardedModelAdmin
+
 from gnocchi_forum import models
 
-class CategoryAdmin(admin.ModelAdmin):
+class CategoryAdmin(GuardedModelAdmin):
     list_display = ('name', 'order',)
     list_editable = ('order',)
+    prepopulated_fields = {
+        'slug': ('name',),
+    }
 admin.site.register(models.Category, CategoryAdmin)
 
-class ForumAdmin(admin.ModelAdmin):
+class ForumAdmin(GuardedModelAdmin):
     list_display = ('title', 'category',)
     list_filter = ('category',)
     prepopulated_fields = {
 class PostInline(admin.StackedInline):
     model = models.Post
 
-class ThreadAdmin(admin.ModelAdmin):
+class ThreadAdmin(GuardedModelAdmin):
     list_display = ('title', 'forum', 'pinned', 'state',)
     list_filter = ('forum', 'pinned', 'state',)
     prepopulated_fields = {
 
 admin.site.register(models.Post, PostAdmin)
 admin.site.register(models.Profile)
-

File gnocchi_forum/forms.py

+from django import forms
+
+from gnocchi_forum import models
+
+class NewThreadForm(forms.ModelForm):
+    class Meta:
+        model = models.Thread
+        exclude = ('slug',)
+
+class NewPostForm(forms.ModelForm):
+    class Meta:
+        model = models.Post
+        exclude = ('thread', 'posted_by', 'created', 'reply_to',)

File gnocchi_forum/models.py

         unique_together = (
             ('category', 'slug',),
         )
+        permissions = (
+            ('create_thread', 'Can start a thread'),
+        )
 
     def __unicode__(self):
         return u'[%s] %s' % (self.category, self.title,)
             'thread_slug': self.slug,
             'thread_id': self.id,
         }
+    @models.permalink
+    def get_post_url(self):
+        return 'thread-post', (), {
+            'thread_slug': self.slug,
+            'thread_id': self.id,
+        }
 
     def last_post(self):
         try:

File gnocchi_forum/urls.py

     url(r'^$', 'forum_index', name='forum-index' ),
     url(r'^forum/(?P<forum_slug>[-\w]+)/(?P<forum_id>\d+)/$',
         'forum_detail', name='forum-detail' ),
+    url(r'^thread/(?P<thread_slug>[-\w]+)/(?P<thread_id>\d+)/post/$',
+        'thread_detail', name='thread-post'),
     url(r'^thread/(?P<thread_slug>[-\w]+)/(?P<thread_id>\d+)/$',
         'thread_detail', name='thread-detail'),
 )

File gnocchi_forum/views.py

 from django.shortcuts import get_object_or_404
 from django.db.models import Max, Count
 
-from gnocchi_forum import models
+from django.contrib.auth.decorators import permission_required
+
+from gnocchi_forum import models, forms
 
 # Need:
 #   Front page [Categories + Forums]
         }
     )
 
+# Create new Thread
+@permission_required('thread.can_add')
+def thread_create(request, forum_id):
+    forum = get_object_or_404(models.Forum, pk=forum_id)
+    if not request.user.has_perm('create_thread', forum):
+        return HttpResponseForbidden()
+
+    if request.method == 'POST':
+        form = forms.NewThreadForm(request.POST)
+        if form.is_valid():
+            obj = form.save()
+            return HttpResponseRedirect(obj.get_absolute_url())
+    else:
+        form = forms.NewThreadForm()
+    return direct_to_template(request,
+        'forum/forum_form.html',
+        extra_context = {
+            'forum': forum,
+        }
+    )
+
 #   Thread detail [ post list ]
 def thread_detail(request, thread_slug, thread_id):
     thread = get_object_or_404(models.Thread, pk=thread_id)
         template_name='forum/thread_detail.html',
         extra_context = {
             'thread': thread,
+            'form': NewPostForm(),
         }
     )
 
 #   Post [ajax?]
+def post_create(request, thread_slug, thread_id):
+    thread = get_object_or_404(models.Thread, pk=thread_id)
+    if not thread_slug == thread.slug:
+        return HttpResponsePermanentRedirect(thread.get_absolute_url())
+    if request.method == 'POST':
+        form = forms.NewPostForm(request.POST)
+        if form.is_valid():
+            obj = form.save(commit=False)
+            obj.thread = thread
+            obj.save()
+            return HttpResponseRedirect(obj.get_absolute_url())
+    else:
+        form = forms.NewPostForm()
+    return direct_to_template(request,
+        'forum/post_form.html',
+        extra_context = {
+            'thread': thread,
+            'form': form,
+        }
+    )
 #   Edit Profile