开发者

Generate Approval URLs using Django [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this po开发者_C百科st.

Closed 4 years ago.

Improve this question

I have a Django app where users should ask managers to join to some group.

I need to be able to send an approval mail to manager with URL that approves specific user to join a specific group.

How do I build such URL and how I identify the user and the group after clicking on that URL?


For an approval e-mail, you could send the user's id (either in clean state (id = 1 or hashed (if you hash take time to add some salt like id = 356a192b7913b04c54574d18c28d46e6395428ab) to the group manager.

Then when the group manager clicks on the link he is redirected to his "dashboard" with the user preselected for approval (you might want to list all the approval request of a user in that page (not yet approved))

And then the group manager can allow or deny acces for a user (for either a single group or many groups)


Here is how to create a unique key for the request

from random import random
from django.utils.hashcompat import sha_constructor

invite_salt = sha_constructor(str(random())).hexdigest()[:5]
invite_key = sha_constructor("|".join([admin_salt, email, url])).hexdigest()

Then store this in a field on a model, you might call it ApprovalRequest. Then you can build a view like

def approve(request, admin_key):
    approval_request = get_object_or_404(ApprovalRequest, invite_key=admin_key)

The ApprovalRequest should contain a ForeignKey to the user that initiated it and the group they wanted to join so you can handle it appropriately at this point, after which mark the approval request as complete.

To get a url for this approve view you can use reverse('approve', admin_key) assuming you mapped the view as follows in urls.py

url(r"^approve/([^/]+)/", 'example.views.approve', name='approve')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜