Tuesday, September 2, 2014

Re: How create a custom permission for a model?

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+unsubscribe@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/f8831c56-a326-447a-b296-214dc8f3cd3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment