【问题标题】:Python create a unique hash/encrypted urlPython 创建一个唯一的哈希/加密 URL
【发布时间】:2018-07-31 21:00:35
【问题描述】:

我想创建一个应用程序,如果有人想申请为我的公司工作,我将在我的应用程序中创建一个条目,然后我会向他们发送一个唯一的 URL,该 URL 会指向我想要的表单页面要散列的 URL,但我不太确定如何执行此操作。以下是我的代码:

view.py:

def applicant_email_view(request):
    form = ApplicantEmailForm(request.POST or None)
    if form.is_valid():
        inst = form.save(commit=False)
        subject = 'Applicant Form'
        text_content = 'Hola'
        html_content = """
                    <h3 style="color: #0b9ac4><a>http://127.0.0.1:8080/hiring/application_form/</a></h3>
                    """
        to = [inst.applicant_email]
        send_email(subject, text_content, html_content, to)

        inst.save()
    return render(request, 'hiring/applicant_email.html', {'form': form})

def application_form_view(request):
    form = JobApplicationForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        inst = form.save(commit=False)
        inst.save()
    context = {'form': form}
    return render(request, 'hiring/application_form.html', context=context)


def send_email(subject, text_content, html_content, to):
    from_email = 'test@testing.studio'
    footer = """<h5>Do not reply to this email. This is automated email notification from LemonCORE.</h5>
                </br>
                """
    email_body = html_content + footer
    msg = EmailMultiAlternatives(subject, text_content, from_email, to)
    msg.attach_alternative(email_body, "text/html")
    msg.send()

url.py:

url(r'^hiring/application_email/$', applicant_email_view, name='application_email_view'),
    url(r'^hiring/application_form/$', application_form_view, name='application_form_view'),

【问题讨论】:

    标签: python django hash sha1


    【解决方案1】:

    我假设您的主要想法是使用唯一字符串作为标识符,并且您尝试使用哈希作为唯一字符串。

    如果是这样,我建议使用uuid 模块。

    做类似的事情

    import uuid
    unique_key = str(uuid.uuid4()).replace('-')
    url = 'http://127.0.0.1:8080/hiring/application_form/{0}/'.format(unique_key)
    # Store the unique_key in the DB with the user object,
    # and use it in the email..etc.,
    

    在 URL 中获取地图捕获 UUID

    url(r'^hiring/application_form/(?P<uuid>[a-f0-9]+)/$', applicant_form_view, name='application_form_view')
    

    然后根据需要在视图和进程中使用它。

    【讨论】:

    • 感谢您的建议,这是一个很棒的建议,但是我确实有一个后续问题,让来自申请人_email_view 的说我添加另一个名为 unique_id 的字段,一旦我提交条目,如何我可以从视图中保存unique_id 吗?因为显然它不会在我的 html 中保存输入字段
    • 在模型中定义一个新属性,直接设置到模型对象中。
    【解决方案2】:

    如果我站在你的立场上(取决于你在这里写的内容),我会考虑一些不同的策略。 我不只是对 URL 进行哈希处理,而是实现某种 Map(在服务器端),它将 accountId/guestId/hashed URL(或任何其他唯一标识符)的条目存储到匹配 URL 创建的时间 - 然后我将配置一个基本吞吐量,它将定义每个条目的可用时间(您可以根据需要扩展此机制,这非常简单)。

    ** 对于散列 URL,您可以使用 SHA256 或任何其他实现,然后使用 base62 对其进行编码(如果我没记错的话,base64 包含“/”和“?”)。

    让我知道你的想法:)

    【讨论】:

    • 根据你和Arunmozhi所说的,对我来说最好的方法是根据申请人电子邮件模型的id创建一个唯一的字符串,但问题是,我如何存储唯一ID并使用在网址中
    • 对不起,我没听懂你...你能解释一下“我如何存储唯一 ID 并在 url 中使用 -”是什么意思吗?
    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多