Example #1
0
def login_link(request, token=None):
    login_link = get_or_4xx(LoginLink, token)

    if request.method == 'GET':
        return login_link.app_resource()

    password = request.json.get('password')
    if login_link.password != password:
        return {'error': 'invalid password'}, 401

    user = login_link.user

    # Update the user profile with any fields that may be provided
    for attr in User.REGISTRATION_FIELDS:
        if attr not in request.json:
            continue
        setattr(user, attr, request.json[attr])

    user.save()

    if not user.is_complete():
        return {'error': 'registration incomplete'}, 403

    # Increment login count + login timestamp
    login_link.num_logins += 1
    login_link.last_login = timezone.now()
    login_link.save()

    auth.login(request, user)

    login_link.user.refresh_from_db()
    return login_link.app_resource()
Example #2
0
def invite(request):
    emails = request.json['emails']

    _type = request.json['type']
    if _type not in (Survey.Type.EXEMPLAR, Survey.Type.APPLICANT):
        return {
            'error': 'type must be one of "exemplar" or "applicant"',
        }, 400

    job = get_or_4xx(Job, request.json['job'])

    surveys = sorted(Survey.objects.filter(job=job, type=_type),
                     key=lambda s: s.version,
                     reverse=True)

    if not surveys:
        return {
            'error': 'no surveys found for this job & type',
        }, 400

    # Pick the latest version
    survey = surveys[0]

    invitation = SurveyInvitation.objects.create(hiring_manager=request.user,
                                                 type=_type,
                                                 survey=survey,
                                                 emails=emails)

    return invitation.app_resource()
Example #3
0
    def __call__(self, request):
        if request.user.is_authenticated:
            request.user = request.user.hydrated_user()
        elif settings.ENV.is_dev():
            token = request.META.get('HTTP_X_USER')
            if token:
                request.user = get_or_4xx(User, token).hydrated_user()

        return self.get_response(request)
Example #4
0
def job(request, token):
    job = get_or_4xx(Job, token)

    if request.method == 'POST':
        f = JobForm(request.json, instance=job)
        if not f.is_valid():
            raise Http400(f.errors)
        job = f.save()

    return job.app_resource()
Example #5
0
def company(request, token):
    company = get_or_4xx(Company, token)

    if request.method == 'POST':
        f = CompanyForm(request.json, instance=company)
        if not f.is_valid():
            raise Http400(f.errors)
        company = f.save()

    return company.app_resource()
Example #6
0
        def __init__(self, *args, **kwargs):
            if args:
                data = args[0]
                for key, value in data.items():
                    if key not in token_fields:
                        continue
                    klass = token_fields[key]
                    if isinstance(value, list):
                        data[key] = [get_or_4xx(klass, t).pk for t in value]
                    else:
                        data[key] = get_or_4xx(klass, value).pk

            super(BaseModelFormImpl, self).__init__(*args, **kwargs)

            # If there is any initial data present, use that as the fallback
            # value.
            data = self.initial.copy()
            data.update(self.data)
            self.data = data
Example #7
0
def response(request, token):
    sr = get_or_4xx(SurveyResponse, token)
    return sr.app_resource()