如何限制用户对自己的模型进行投票

发布于 2024-07-24 22:09:12 字数 208 浏览 4 评论 0原文

我正在使用 django-voting 作为我的两个模型的投票应用程序。 这两个模型都有“作者”字段。

如何在不修改 django-voting 应用程序的情况下限制用户对将此特定用户设置为作者的模型进行投票?

我首先想到的是 Django 中间件,但我不明白它的“proces_view”函数。 如果您认为中间件是正确的方法,请举例说明如何做到这一点。

I am using django-voting as a voting application for two of my models. Those both models have fields "author".

How can I restrict a user from voting on a model that has this particular user set as it's author without modifying django-voting app?

Django middleware is the first thing that comes to my mind, but I don't understand it's "proces_view" function. If you think middleware is the right way could you please give an example of how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行雁书 2024-07-31 22:09:12

在 settings.py 中的任何位置添加此代码:

from voting.managers import VoteManager

def check_user(func):
    def wrapper(self, obj, user, vote):
        if obj.user != user:
            return func(self, obj, user, vote)
        else:
            return None
            # or raise some exception
    return wrapper

VoteManager.record_vote = check_user(VoteManager.record_vote)

我没有运行此代码,也许它不正确,但我希望想法很清楚

Add this code anywhere in your settings.py:

from voting.managers import VoteManager

def check_user(func):
    def wrapper(self, obj, user, vote):
        if obj.user != user:
            return func(self, obj, user, vote)
        else:
            return None
            # or raise some exception
    return wrapper

VoteManager.record_vote = check_user(VoteManager.record_vote)

I didn't run this code, maybe it's incorrect, but I hope idea is clear

纸短情长 2024-07-31 22:09:12

为什么不通过另一个视图将请求重新路由到该特定 URI,而不是中间件黑客呢? 然后您可以执行您喜欢的任何逻辑,并随后在适当的情况下调用原始视图。

Rather than a middleware hack, why not reroute requests to that particular URI through another view? Then you can performs whatever logic you like, and subsequently call the original view if appropriate.

风筝在阴天搁浅。 2024-07-31 22:09:12

另一个想法是使用 post_save 信号

如下所示:这样

from django.db.models.signals import post_save
from voting.models import Vote

def check_user(sender, instance, **kwargs):
    if instance.user == instance.object.user:
        instance.delete()
        # do some other stuff to tell the user it didn't work

post_save.connect(check_user, sender=Vote)

做的好处与重写 VoteManager.record_vote 相比,它与投票模块的耦合不太紧密,如果他们进行更改,则不太可能破坏您的代码

编辑:如 Glader 的回答所示,您需要确保您投票的所有对象都具有“用户”属性。

Another idea is to use the post_save signal

like so:

from django.db.models.signals import post_save
from voting.models import Vote

def check_user(sender, instance, **kwargs):
    if instance.user == instance.object.user:
        instance.delete()
        # do some other stuff to tell the user it didn't work

post_save.connect(check_user, sender=Vote)

The benefit of doing this vs overriding VoteManager.record_vote is that it's less tightly coupled to the voting module, and if they make changes it's less likely to break your code

edit: as in Glader's answer, you need to make sure that all the objects you're voting on have a 'user' attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文