简体   繁体   中英

How can I revoke a custom model permission for a user in django

I am writing an app that has a large form with a lot of fields that have to be submitted in order. Each section of the form is available only when a user has the permission to access it, these permissions are defined within one of the models and the admin user gives them to the users when the conditions are met.

What i'm trying to do now is for the user permission to be deleted for that user when they submit that section of the form. Is there any way to accomplish this?

Here is the model:

class PartidosUsuarios(models.Model):
    idUsuario = models.ForeignKey(User)
    idFifa = models.ForeignKey(PartidosFifa, null=True)
    idPartido = models.CharField(max_length=20)
    PaisL = models.CharField(max_length=250)
    Local = models.IntegerField(max_length=11, default=0)
    Visita = models.IntegerField(max_length=11, default=0)
    PaisV = models.CharField(max_length=250)
    Resultado = models.CharField(max_length=250)
    Puntos = models.IntegerField(max_length=11, default=0)
    Capturado = models.CharField(max_length=10, default="No")
    class Meta:
        permissions = (
            ("grupos", "Puede ver partidos de grupos"),
            ("octavos", "Puede ver partidos de octavos"),
            ("cuartos", "Puede ver partidos de cuartos"),
            ("semis", "Puede ver partidos de semis"),
            ("final", "Puede ver partidos de final"),
        )
    def __unicode__(self):
        return unicode(self.idPartido)

And here is the view:

@login_required(login_url="/")
def inicio(request):
    if request.method == "POST":
        form = Pronosticos(request.POST)
        for i in range(47):
            pronostico, _ = PartidosUsuarios.objects.get_or_create(idUsuario=request.user, idPartido=request.POST.get("idPartido"+str(i), ""), PaisL=request.POST.get("PaisL"+str(i), ""), Local=request.POST.get("Local"+str(i), ""), Visita=request.POST.get("Visita"+str(i), ""), PaisV=request.POST.get("PaisV"+str(i), ""), Capturado="Si")
            if pronostico.Local > pronostico.Visita:
                pronostico.Resultado = "Local"
                pronostico.save()
            elif pronostico.Visita > pronostico.Local:
                pronostico.Resultado = "Visita"
                pronostico.save()
            elif pronostico.Local == pronostico.Visita:
                pronostico.Resultado = "Empate"
                pronostico.save()
    partidos_usuarios = PartidosUsuarios.objects.order_by("idPartido")
    partidos_fifa = PartidosFifa.objects.order_by("Partido")[:64]
    context = ({"partidos_fifa": partidos_fifa, "partidos_usuarios": partidos_usuarios})
    return render(request, "brasil/inicio.html", context)

In this case the permission that should be deleted upon submitting the form is the one called "grupos"

Any help will be greatly appreciated, thanks!

Well, it isn't clear for me if you have a role called grupos and you want to remove permissions for that role submitting forms or if you have a custom permission called grupos . But in both cases you can use: permissions.utils.remove_permission(obj, role, permission)

Where:

obj

The content object for which a permission should be removed.

role

The role for which a permission should be removed.

permission

The permission which should be removed. Either a permission object or the codename of a permission.

So, in your view, just call this function at your convenience.

For more info for working on permissions see: Django API for manage permissions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM