Пример #1
0
def validate_access_processor(request, e_name):
    # Use the following to see if the pages produce what's expected
    # return "invalid code"
    # return "incorrect code"
    # return "no email"
    # return "participate"

    try:
        validate_access_code(request.POST.get("access_code"))
    except ValidationError:
        return "invalid code"

    # Get the validated code
    a_code=request.POST.get("access_code")

    # See if there's a participant entry with that code
    subject = None
    try:
        subject = Participant.objects.get(access_code=a_code)
    except:
        return "incorrect code"

    # check if code belongs to participant
    # get session email and then pid and verify the accesscode
    # goes with that pid
    user_email = request.session['email']
    # Create variable we'll contain the user information in
    user = None
    # Make sure there's not already a user before creating one
    try:
        user = User.objects.get(email=user_email)
        # There is a user. Check to see if the permission
        # needs changing.
    except: # The session expired or didn't exist, or there's no
        # user. Either way, redirect to the consent page to start
        # over
        return "no email"

    pid = create_participant_id(user, e_name, True)
    if(subject.participant!=pid):
        return "wrong code"

    # The user is logged in and has the correct code so let them
    # participate
    return "participate"
Пример #2
0
def validate_view_processor(request, e_name):
    # For easy testing try the following
    # return {"which_version": "no email"}
    # return {"which_version": "no experiment"}
    # return {"which_version": "already participated"}
    # return {"which_version": "couldn't send email"}
    # return {"which_version": "validate"}

    # Get the experiment data
    exp = None
    try:
        exp = Experiment.objects.get(url_name=e_name)
    except:
        return {"which_version": "no experiment", "e_name": e_name, "pretty_name": exp.pretty_name.lower()}

    try:
        validate_email(request.POST.get("email"))
    except ValidationError:
        return {"which_version": "no email", "e_name": e_name, "pretty_name": exp.pretty_name.lower()}

    user_email = request.POST.get("email")
    permission = request.POST.get("permission_to_contact")
    permission = permission == "on"

    # Set session
    request.session['email'] = user_email
    request.session['submitted'] = False

    # Create variable we'll contain the user information in
    user = None
    # Make sure there's not already a user before creating one
    try:
        user = User.objects.get(email=user_email)
        # There is a user. Check to see if the permission
        # needs changing.
    except: # There was no user, so create one
        user = User(email=user_email)
        user.save()

    if(permission != user.permission_to_contact):
        user.permission_to_contact = permission
        user.save()

    # Create the participant id number
    pid = create_participant_id(user, e_name, permission)

    # Get the institution for the participant
    institute = user_email.split("@")[1]

    # Check to see if they're already in the database and whether
    # or not they can participate again
    subject=None
    try:
        subject = Participant.objects.get(participant=pid, experiment=exp.id)
    except:
        # Create an access code for the participant
        a_code = create_participant_access_code(pid, e_name, permission)
        subject = Participant(participant=pid,
                            experiment=exp,
                            institution=institute,
                            access_code=a_code,
                            participation_time=0,
                            time_last_participated=timezone.now(),)
        subject.save()

    if((subject.participation_time == 0) or (exp.repeat == "t")):
        if(subject.participation_time > 0): #They've already participated
            #Check that they've waited enough time
            time_to_participate = None
            if exp.repeat_delay_units=="hours":
                time_to_participate = subject.time_last_participated + datetime.timedelta(hours=exp.repeat_delay)
            elif exp.repeat_delay_units=="days":
                time_to_participate = subject.time_last_participated + datetime.timedelta(days=exp.repeat_delay)
            elif exp.repeat_delay_units=="months":
                time_to_participate = subject.time_last_participated + datetime.timedelta(days=exp.repeat_delay*30)
            else: #Years
                time_to_participate = subject.time_last_participated + datetime.timedelta(days=exp.repeat_delay*365)
            if time_to_participate > timezone.now():
                time_string = time_to_participate.strftime("%I:%M %p, %B %d, %Y")
                return {"which_version": "have to wait", "e_name": e_name, "pretty_name": exp.pretty_name.lower(),
                        "time_to_participate": time_string}
            elif subject.access_code == 'ZZZZZZZZZZ': #They can participate, but they'll need a new access code
                a_code = create_participant_access_code(pid, e_name, permission)
                subject.access_code = a_code
                subject.save()
    else:
        return {"which_version": "already participated", "e_name": e_name, "pretty_name": exp.pretty_name.lower()}

    # send an email informing participants containing the access_code
    send_access_code_email(user, subject, exp)

    return {"which_version": "validate", "e_name": e_name, "pretty_name": exp.pretty_name.lower()}