1. H3n0xek
  2. diario-extras

Commits

H3n0xek  committed 18b96b4

status signals fix

  • Participants
  • Parent commits 4bd8865
  • Branches master

Comments (0)

Files changed (3)

File diario_extviews/views.py

View file
 from django.conf import settings
 from diario_extviews import settings as local_settings
 from django.core.urlresolvers import reverse
+from django.db.models.signals import post_save
 from django.http import Http404, HttpResponse
 from django.shortcuts import render
 from django.views.generic.list import ListView
 from django import forms
 from django.contrib import messages
 
-from diario.models import Entry
-from diario_moderation.models import EntryStatus, STATUS_ACCEPTED, \
-                                     STATUS_DRAFT, STATUS_QUEUED, \
-                                     STATUS_REJECTED, STATUS_REWRITE
+from diario.models import DIARIO_MODEL_CLASS
+from diario_moderation.models import (EntryStatus, STATUS_ACCEPTED, 
+                                     STATUS_DRAFT, STATUS_QUEUED, 
+                                     STATUS_REJECTED, STATUS_REWRITE,
+                                     create_status_model)                                     
 from diario.views.entries import EntryList
 
 
     status = None
     
     def get_queryset(self):
-        return Entry.objects.filter(entrystatus__status=self.status,
+        return DIARIO_MODEL_CLASS.objects.filter(entrystatus__status=self.status,
                                     author=self.request.user)
 
 class ListDrafts(ListByStatus):
  
 class EntryCreationForm(forms.ModelForm):
     class Meta:
-        model = Entry
-        fields = ('title', 'body_source')
+        model = DIARIO_MODEL_CLASS
+        fields = ('title', 'body')
     
 
 class CreateDraft(CreateView):
-    model = Entry
+    model = DIARIO_MODEL_CLASS
     form_class = EntryCreationForm
     template_name = 'diario/create_draft.html'
     limit_exceed_template = 'limit_exceeded.html'
     context_object_name = 'form'
     
     def dispatch(self, request, *args, **kwargs):
-        if not local_settings.DIARIO_LIMIT_DRAFTS or request.user.is_superuser():
+        if not local_settings.DIARIO_LIMIT_DRAFTS or request.user.is_superuser:
             return super(CreateDraft, self).dispatch(request, *args, **kwargs)
             
-        num_drafts = Entry.objects.filter(entrystatus__status=STATUS_DRAFT, author=request.user).count()
+        num_drafts = DIARIO_MODEL_CLASS.objects.filter(entrystatus__status=STATUS_DRAFT, author=request.user).count()
         if num_drafts >= local_settings.DIARIO_MAX_DRAFTS:
             return render(request, self.limit_exceed_template, {'limit': local_settings.DIARIO_MAX_DRAFTS })
         return super(CreateDraft, self).dispatch(request, *args, **kwargs)
-        
     
+    def form_valid(self, form):                
+        instance = form.save(commit=False)
+        instance.author = self.request.user
+        instance.save()
+        return super(CreateDraft, self).form_valid(form)
+        
     def get_initial(self):
         initial = super(CreateDraft, self).get_initial()
-        initial['author'] = self.request.user
+        initial['author'] = self.request.user        
         return initial
         
     def get_success_url(self):
 
 
 class EditDraft(UpdateView):
-    model = Entry
+    model = DIARIO_MODEL_CLASS
     form_class = EntryCreationForm
     template_name = 'diario/edit_draft.html'
-    context_object_name = 'form'
-    queryset = None    
-        
-    def get_success_url(self):
-        return reverse('edit-draft', kwargs={'pk': self.object.pk})
+    context_object_name = 'entry'
+    queryset = None            
 
-    def get_queryset(self):
+    def get_success_url(self):
+        return reverse('edit-draft', kwargs={'pk': self.object.pk})   
+        
+    def get_queryset(self):       
         user = self.request.user
-        queryset = Entry.objects.filter(entrystatus__status=STATUS_DRAFT)
+        queryset = DIARIO_MODEL_CLASS.objects.filter(entrystatus__status=STATUS_DRAFT)        
         if user.has_perm('diario_moderation.moderate_entry'):
-            return queryset
+            return queryset                
         return queryset.filter(author=user)
   
 
             return super(QueueDraft, self).get(request, *args, **kwargs)
         return HttpResponse(status=403)
    
+post_save.connect(create_status_model, sender=DIARIO_MODEL_CLASS)

File diario_moderation/models.py

View file
 from django.db import models
-from diario.models import Entry
+from diario.models import DIARIO_MODEL_CLASS
 from django.dispatch import receiver
 from django.db.models.signals import post_save
 
 
 
 class EntryStatus(models.Model):
-    entry = models.ForeignKey(Entry, unique=True)
+    entry = models.ForeignKey(DIARIO_MODEL_CLASS, unique=True)
     status = models.IntegerField(choices=MODERATION_STATUS)
     comment = models.TextField(blank=True, null=True)
 
         )
   
 
-@receiver(post_save, sender=Entry)
-def create_status_model(sender, instance, created, **kwargs):
+def create_status_model(sender, instance, created, **kwargs):    
     if created:
         EntryStatus.objects.create(entry=instance, status=STATUS_DRAFT)
 
-@receiver(post_save, sender=EntryStatus)
 def publish_entry(sender, instance, created, **kwargs):
     if instance.status == STATUS_ACCEPTED:
         e = instance.entry
-        e.is_draft = False
+        e.visible = True
         e.save()
 

File diario_moderation/views.py

View file
 from django.conf import settings
 from django.http import HttpResponse, Http404
-from diario.models import Entry
+from diario.models import DIARIO_MODEL_CLASS
 from diario_moderation.models import EntryStatus, MODERATION_STATUS, \
                                      STATUS_QUEUED
 
     status = forms.ChoiceField(choices=MODERATION_STATUS)
 
     class Meta:
-        model = Entry
+        model = DIARIO_MODEL_CLASS
         fields = ('title', 'slug', 'body_source', 'tags')
 
     def save(self, **kwargs):
 
 class ModerateEntry(UpdateView):
     form_class = EntryModerateForm
-    model = Entry
+    model = DIARIO_MODEL_CLASS
     template_name = 'diario/moderate_entry.html'
     
     def dispatch(self, request, *args, **kwargs):
     def get_queryset(self):
         u = self.request.user
         if u.has_perm('diario_moderation.moderate_entries'):
-            return Entry.objects.filter(entrystatus__status=STATUS_QUEUED)
+            return DIARIO_MODEL_CLASS.objects.filter(entrystatus__status=STATUS_QUEUED)
         raise Http404