3

I want to create an app where if a person would like to apply working for my company, I'll create an entry in my app and I'll send them a unique URL where it lead to a form page, I wanted the URL to be hashed, but I'm not quite sure how to do this. Below is my code :

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 = '[email protected]'
    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'),

2 Answers 2

2

I am assuming your main idea is to use a unique string as an identifier and you are trying to use hashes as the unique strings.

If so, I would suggest using the uuid module.

Do something like

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.,

In the URL get map capture the UUID

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

Then use it in the view and process as you wish.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for suggestion, its a brilliant one, but a I do have a follow up question, let say from the applicant_email_view I add another field which called unique_id, once I submit the entry, how can I save the unique_id from the view? because obviously it wont have a input field in my html to save
Define a new attribute in the model and directly set it to the model object.
1

If I was in your shoes (depends on what you wrote here), I'd think about some different strategy. Instead of just hashing the URL, I'd implement some kind of Map (on the server side) which will store entries of accountId/guestId/hashed URL (or any other unique identifier) to the time when the matching URL has created - then I'd configure a basic throughput which will define for how long each entry is available (you can extend this mechanism as much as you want, it's very straightforward).

** For the hashed URL you can use some implementation of SHA256 or any other and then encode it using base62 (base64 contains "/" and "?" if I'm not wrong).

Let me know what do you think :)

2 Comments

Based on what you and Arunmozhi said, the best approach for me is to create a unique string based from the id of the ApplicantEmail model, but the problem is, how can I store the unique ID and used in the url
I'm sorry but I didn't get you... Can you please explain what do you mean by "how can I store the unique ID and used in the url –"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.