Hi men, now i have my model is thus:

from django.core.mail import send_mail, mail_admins


from django.db import models
from django.template.defaultfilters import slugify
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User


from ckeditor.fields import RichTextField


from subtopic.models import Subtopic
from topic.models import Topic
from keywords.models import KeyWord


#Global Vars




# Create your models here.


class New(models.Model):
    author = models.ForeignKey(User, verbose_name='Autor', related_name=
'news')
    content = RichTextField(verbose_name='Contenido')
    created_date = models.DateTimeField(auto_now_add=True, verbose_name='Fecha 
y Hora')
    is_published = models.BooleanField(verbose_name='Publicada', default=
False,) #Nueva, para verificar si se publica o no
    keywords = models.ManyToManyField(KeyWord, blank=True, 
verbose_name='Palabras 
Clave', related_name='news')
    place = models.CharField(max_length=255, verbose_name='Lugar')
    source = models.URLField(verbose_name='Fuente', blank=True)
    subtopic = models.ForeignKey(Subtopic, verbose_name='Subtema', 
related_name='news') #Filtramos por Opinion para sacar todas las columnas
    times_viewed = models.PositiveIntegerField(default=0, editable=False, 
verbose_name='Veces Vista' )
    title = models.CharField(verbose_name='Título', max_length=255, unique=
True)
    slug =  models.SlugField(verbose_name='Slug', max_length=100, unique=
True)
    topic = models.ForeignKey(Topic, verbose_name='Tema', related_name=
'news', )


    class Meta:
        ordering = ['-created_date']
        verbose_name_plural = 'Noticias'
        verbose_name = 'Noticia'


        permissions = (
            ("can_publish", "Puede publicar noticias"), #Can publish news
        )

    def first_image(self):
        return self.images.first() # Siendo images el related_name en Image


    def first_video(self):
        return self.videos #Devuelve el video de la imagen


    def get_absolute_url(self):
        return reverse ('NewsDefaultView', args = [str(self.created_date.
strftime("%Y")), str(self.created_date.strftime("%m")), str(self.
created_date.strftime("%d")), str(self.slug)])


    def get_all_keys(self):
        keys_list = self.keywords.values_list('name', flat=True)
        return str(keys_list).strip("'[]'").replace("'",'')


    #def is_published(self): devuelve si la noticia ha sido pulicada para 
que salga en la lista de noticias


    def __str__(self):
        return self.title


    def save(self):  #Definir metodo para guardar, validar y otros metodos 
del Slug
        super(New, self).save() #Solo con este me funciona correctamente
        if not self.id:
            self.slug = slugify(self.title)
        super(New, self).save()


    def get_queryset(self, request):
        queryset = super(New, self).get_queryset(request)
        if request.user.groups.filter(name="Author").exist():  # is the 
user an author?
             queryset = queryset.filter(author=self.user)  # only allow 
users to edit their own posts
        return queryset


    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super(New, self).get_readonly_field(request, obj)
        if request.user.groups.filter(name="Author").exist():  # is the 
user an author?
            readonly_fields += ['is_published']  # don't allow authors to 
change the publish status
        return readonly_fields


from django.db.models.signals import post_save
def send_notification(sender, **kwargs):
        return   mail_admins('Nueva noticia espera por edición','Una nueva 
noticia ha sido creada y espera ser editada y publicada muy pronto, anda, 
te esperamos.', )


post_save.connect(send_notification, sender=New)


from permission import add_permission_logic
from permission.logics import AuthorPermissionLogic, StaffPermissionLogic
add_permission_logic(New, AuthorPermissionLogic(
    field_name='author',
    any_permission=False,
    change_permission=True,
    delete_permission=True,
))
add_permission_logic(New, StaffPermissionLogic(
    any_permission=False,
    change_permission=True,
    delete_permission=True,
))


from django.core.cache import cache
from django.db.models.signals import post_save
from django.dispatch import receiver


from django.contrib.sessions.models import Session
@receiver(post_save)
def clear_cache(sender, **kwargs):
    if sender != Session:
        cache.clear()

But, don't works, the author user can change other news and can publish 
:'(, what i'm doing bad?

I can created a group in python code? in models.py or other file or only 
can created a group in django admin?

El martes, 2 de septiembre de 2014 19:54:40 UTC-5, YusufSalahAdDin escribió:
>
> Wel wel wel, my code is it :
>
> news.models.py
>
> class New(models.Model):
>     author = models.ForeignKey(User, verbose_name='Autor', related_name=
> 'news')
>     content = RichTextField(verbose_name='Contenido')
>     created_date = models.DateTimeField(auto_now_add=True, 
> verbose_name='Fecha 
> y Hora')
>     is_published = models.BooleanField(verbose_name='Publicada', default=
> False,) #Nueva, para verificar si se publica o no
>     keywords = models.ManyToManyField(KeyWord, blank=True, 
> verbose_name='Palabras 
> Clave', related_name='news')
>     place = models.CharField(max_length=255, verbose_name='Lugar')
>     source = models.URLField(verbose_name='Fuente', blank=True)
>     subtopic = models.ForeignKey(Subtopic, verbose_name='Subtema', 
> related_name='news') #Filtramos por Opinion para sacar todas las columnas
>     times_viewed = models.PositiveIntegerField(default=0, editable=False, 
> verbose_name='Veces Vista' )
>     title = models.CharField(verbose_name='Título', max_length=255, unique
> =True)
>     slug =  models.SlugField(verbose_name='Slug', max_length=100, unique=
> True)
>     topic = models.ForeignKey(Topic, verbose_name='Tema', related_name=
> 'news', )
>
>
>     class Meta:
>         ordering = ['-created_date']
>         verbose_name_plural = 'Noticias'
>         verbose_name = 'Noticia'
>
>
>         permissions = (
>             ("can_publish", "Puede publicar noticias"), #Can publish news
>         )
>
> from permission import add_permission_logic
> from permission.logics import AuthorPermissionLogic, StaffPermissionLogic
> add_permission_logic(New, AuthorPermissionLogic(
>     field_name='author',
>     any_permission=False,
>     change_permission=True,
>     delete_permission=True,
> ))
> add_permission_logic(New, StaffPermissionLogic(
>     any_permission=False,
>     change_permission=True,
>     delete_permission=True,
> ))
>
>
> As you can see, i'm using django-permission.
>
> What have i do ?
>
> El lunes, 1 de septiembre de 2014 12:07:19 UTC-5, Collin Anderson escribió:
>>
>> > Can't create other users
>> > Can't create other topics or subtopics
>> These can be done using built in permissions and groups
>>
>> Something like this might work for the rest:
>>
>> class NewsAdmin(models.Model):
>>
>>     def get_queryset(self, request):
>>         queryset = super(NewsAdmin, self).get_queryset(request):
>>         if request.user.groups.filter(name="Author").exist():  # is the 
>> user an author?
>>              queryset = queryset.filter(author=self.user)  # only allow 
>> users to edit their own posts
>>         return queryset
>>
>>     def get_readonly_fields(self, request, obj=None):
>>         readonly_fields = super(NewsAdmin, self).get_readonly_field(
>> request, obj)
>>         if request.user.groups.filter(name="Author").exist():  # is the 
>> user an author?
>>             readonly_fields += ['published']  # don't allow authors to 
>> change the publish status
>>         return readonly_fields
>>
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c9625f3f-a332-405b-8831-e6ac1374c073%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to