让 Django 超级用户在游戏化组件中定义新徽章

发布于 2024-12-22 07:27:23 字数 324 浏览 6 评论 0原文

我们正在 Django 中实现一个项目的游戏化组件。我们希望超级用户能够在 Django 管理界面中定义新的徽章并重新定义现有的徽章。

我们看到的问题是,有时需要在代码中定义徽章的复杂条件。超级用户将具有技术知识,但不会是程序员。

您将如何让超级用户在 Django 管理界面中执行此操作,而不是更改应用程序?

以下是徽章的一些示例:

  • 创建了 10 个标签,这些标签已被 10 个不同的人用于 10 个问题 成员。
  • 回答了 10 个未回答的问题。
  • 针对一个问题写了 20 条评论(每条评论至少获得 5 分)。

We are implementing the gamification component of a project in Django. We would like the superuser to be able to define new badges and redefine existing ones inside the Django admin interface.

The problem as we see it is that defining the sometimes complicated conditions for badges needs to be done in code. The superuser will be technically knowledgeable, but will not be a programmer.

How would you go about letting the superuser do this in the Django admin interface, rather than alter the application?

Here are some examples of badges:

  • Created 10 tags that have been used on 10 questions by 10 different
    members.
  • Answered 10 unanswered questions.
  • Wrote 20 comments to a question (comments got at least 5 points each).

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

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

发布评论

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

评论(1

迷爱 2024-12-29 07:27:23

您没有说您是否希望超级用户是程序员。

如果您可以假设这一点,那么您可以在数据库字段中包含 Python 代码,并使用 evalexec 来运行它。

例如:

badge.function = """
def award_badge(user):
    return False
"""

vars = {}
exec(badge.function, vars)

if vars["award_badge"](user):
    ...

显然,这有很大的滥用或错误范围,如果您希望非技术用户创建徽章,那么当然不合适。

根据您需要创建的规则的复杂性,您也许能够创建规则引擎来执行它们。在这种情况下,您创建一种迷你语言,其中决定是否授予徽章的过程中的每个步骤都由数据库中的一行指定。规则引擎是一个复杂的话题,所以我不会在这里详细介绍。

在代码中对徽章规则进行硬编码可能比使用这些建议更好。

You don't say if you expect the superuser to be a programmer or not.

If you can assume this then you could include Python code in a database field and use eval or exec to run it.

For example:

badge.function = """
def award_badge(user):
    return False
"""

vars = {}
exec(badge.function, vars)

if vars["award_badge"](user):
    ...

Clearly this has a lot of scope for abuse or mistakes, and certainly wouldn't be suitable if you expect non-technical users to be creating badges.

Depending on the complexity of the rules you need to create you might be able to create rules engine for executing them. In this case you create a sort of mini-language where each step in the process of deciding whether to award a badge or not is specified by a row in the database. Rules engines are a complicated topic so I won't go into detail here.

You're probably better off hard coding the rules for badges in your code than using either of these suggestions.

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