如何在 Django 中处理具有相同访问权限但不同规则的组的用户管理?
背景信息:
我为一家公司创建了一个内部网站。大部分工作都用于制作计算工具,销售人员可以使用这些工具为客户提供报价。创建可下载的 pdf 报价和合同、比较价格等。所有这一切都运行良好。
现在他们的销售人员已经分为两类。
- 一组是公司雇用的销售人员。
- 另一类是个人或公司本身。
问题:
我现在的挑战是,在某些情况下,我需要根据销售人员的类型展示不同的东西。计算工具的一些规则对于允许哪些数字等会有不同的规则。但是对于两个组来说,网站的很大一部分仍然是相同的。
我想知道是否有一个好的方法来处理这个问题?
我自己的想法:
我考虑过使用 contrib.auth 中提供的组来管理这个问题。这样我就可以保留一个代码库,但必须在很多不同的地方制定规则。验证表单以检查输入的数字是否允许的规则将取决于用户所在的组。有些内容会有不同的名称,或者工作流程可能会有所不同。有些工具仅适用于其中一组。现在这似乎是一个快速的解决方案,但如果两个团队需要进行越来越多的改变,那么这似乎很快就会变得难以管理。
我还考虑过制作两个不同的网站。这里的想法是创建两个组都使用的应用程序,因此我只需要为该 1 个地方编写代码。然后我可以为每个站点制作自定义部分,并且不需要在大多数模板和视图中检查用户。但我不确定这是否是解决问题的好方法。它将产生大量额外的工作,如果两个组可以使用大量相同的代码,那么这可能并不真正需要。
最担心的是,我真的不知道这是如何演变的,因此最终可能会导致两个群体完全不同或只有很少的差异。我想做的是编写一些可以支持这两种情况的代码,这样我就不会在半年后后悔我的选择。
那么,如何处理用户管理的这种情况呢?我正在寻找解决此问题的想法技术或可重复使用的应用程序,而不是现成的解决方案。
澄清:
我的问题不是可以使用模板完成的纯粹演示,而是某些计算工具(填写的表格)将应用不同的规则/验证,并且在某些情况下完成的计算也会不同。因此,他们可能会看到相同的表格,但不允许输入相同的数字,并且相同的数字可能不会给出相同的结果。
Background information:
I have created an internal site for a company. Most of the work has gone into making calculation tools that their sale persons can use to make offers for clients. Create pdf offers and contracts that can be downloaded, compare prices etc. All of this is working fine.
Now their sale persons have been divided into two groups.
- One group is sale personal that is hired by the company.
- The other group is persons a company themselves.
The question:
My challenge now is, that I in some cases need to display different things depending on the type of sales person. Some of the rules for the calculation tools will have different rules as to which numbers will be allowed etc. But a big part of the site will still be the same for both groups.
What I would like to know, is if there is a good way of handling this problem?
My own thoughts:
I thought about managing this by using the groups that is available in contrib.auth. That way I could keep a single code base, but would have to make rules a lot of different places. Rules for validating forms to check if the numbers entered is allowed, will depend on the group the user is in. Some things will have different names, or the workflow might be a bit different. Some tools will only be available to one of the groups. This seems like a quick solution here and now, but if the two groups will need to change more and more, it seems like this would quickly become hard to manage.
I also thought about making two different sites. The idea here was to create apps that both groups use, so I only would need to make the code for that 1 place. Then I could make the custom parts for each site and wouldn't need to check for the user in most templates and views. But I'm not sure if this is a good way to go about things. It will create a lot of extra work, and if the two groups can use a lot of the same code, this might not really be needed.
The biggest concern is that I don't really know how this evolve, so it could end up with the two groups being entire different or with only very few differences. What I would like to do, is write some code that can support both scenarios so I wont end up regretting my choice a half year from now.
So, how do you handle this case of user management. I'm looking for ideas techniques or reusable apps that address this problem, not a ready made solution.
Clarifications:
My issue is not pure presentation that can be done with templates, but also that certain calculation tools (a form that is filled out) will have different rules/validation applied to them, and in some cases the calculations done will also be different. So they might see the same form, but wont be allowed to enter the same numbers, and the same numbers might not give the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
组上使用代理模型 django 附带的
和User
模型。然后在代理模型中编写您的授权和计算方法。如果稍后添加新组,您只需添加/更改这两个代理模型内部的方法。然后使 Group 和 User 的每个实例(显然仅在必要时,而不是实际上每个实例)找到代理模型而不是实际的 contrib 模型。
you could use proxy models on the
Group
andUser
models that come packed with django.then write your authorization and calculation methods inside the proxy model. if a new group is added later, you only need to add/change the methods inside of those two proxy models. then make every instance of Group and User (obviously only where necessary, not literally every one) find the proxy model instead of the actual contrib model.
如果我理解正确,您似乎希望两个不同的组可以访问所有相同的视图,但他们会看到不同的数字。您可以通过为不同的组制作单独的模板,然后根据当前用户的组为每个视图加载适当的模板来实现此效果。
同样,您可以使用上下文处理器将当前组放入每个视图的上下文中,然后将条件放入模板中以选择要显示的数字。
另一种选择是为两个不同的组提供两组独立的视图。然后在视图上使用装饰器以确保组只访问适合他们的视图。
If I'm understanding you correctly, it seems like you want to have two different groups have access to all the same views, but they will see different numbers. You can achieve this effect by making separate templates for the different groups, and then loading the appropriate template for each view depending on the group of the current user.
Similarly you can use a context processor to put the current group into the context for every view, and then put conditionals in the templates to select which numbers to show.
The other option is to have two separate sets of views for the two different groups. Then use decorators on the views to make sure the groups only go to the views that are for them.