Beispiel #1
0
def update_profile(request):
    body = request.data
    if 'recaptcha_token' in body and utils.verify_recaptcha(
            request.user.email, body['recaptcha_token'],
            'update_profile') == ResponseCodes.verify_recaptcha_failed:
        return JsonResponse(create_response(
            data=None,
            success=False,
            error_code=ResponseCodes.verify_recaptcha_failed),
                            safe=False)

    user = request.user
    if 'password' in body:
        user.set_password(body['password'])
    if 'username' in body:
        if User.objects.filter(username=body['username']).exists():
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.username_exists),
                                safe=False)
        user.username = body['username']
    if 'first_name' in body:
        user.first_name = body['first_name']
    if 'last_name' in body:
        user.last_name = body['last_name']
    if 'gender' in body:
        user.gender = body['gender']
    if 'dob' in body:
        user.dob = datetime.strptime(body['dob'], "%Y-%m-%d").date()
    if 'student_email' in body:
        user.student_email = body['student_email']
    if 'is_email_public' in body:
        user.is_email_public = body['is_email_public']
    if 'phone_number' in body:
        user.phone_number = body['phone_number']
    if 'emp_status_id' in body:
        if EmploymentStatus.objects.filter(
                pk=body['emp_status_id']).count() > 0:
            user.emp_status = EmploymentStatus.objects.get(
                pk=body['emp_status_id'])
    user_type, new = UserType.objects.get_or_create(name__iexact='Employer')

    if 'college_id' in body:
        if College.objects.filter(pk=body['college_id']).count() > 0:
            user.college = College.objects.get(pk=body['college_id'])
    if 'major' in body:
        user.major = insert_or_update_major(body['major'])
    if 'grad_year' in body:
        user.grad_year = body['grad_year']
    if 'job_title' in body:
        job_title = body['job_title']
        user.job_position = get_or_insert_position(job_title)
    if 'company' in body:
        company = body['company']
        user.company = get_or_create_company(company)

    if 'country_id' in body and 'state_id' in body:
        state = State.objects.get(pk=body['state_id'])
        if state.country.id != body['country_id']:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        country = Country.objects.get(pk=body['country_id'])
        user.country = country
        user.state = state

    user.signup_flow_completed = True
    user.save()
    return JsonResponse(create_response(
        data=ProfileSerializer(instance=user, many=False).data),
                        safe=False)
Beispiel #2
0
def get_email_detail(service, user_id, msg_id, user, source):
    """Get a Message with given ID.
    Args:
      service: Authorized Gmail API service instance.
      user_id: User's email address. The special value "me"
      can be used to indicate the authenticated user.
      msg_id: The ID of the Message required.
    Returns:
      A Message.
    """
    try:
        if JobApplication.objects.all().filter(msg_id=msg_id).count() > 0:
            log('this email parsed before', 'e')
            return
        message = service.users().messages().get(userId=user_id,
                                                 id=msg_id,
                                                 format='full').execute()
        mail_from = None
        mail_subject = None
        mail_body = None
        original_date = None
        for header in message['payload']['headers']:
            if header['name'] == 'Subject':
                mail_subject = str(header['value'])
                if mail_from is not None and original_date is not None:
                    break
            elif header['name'] == 'From':
                mail_from = str(header['value'])
                if mail_subject is not None and original_date is not None:
                    break
            elif header['name'] == 'Date':
                date = header['value']
                original_date = header['value']
                date = convert_time(str(date))
        if 'parts' not in message['payload']:
            if message['payload']['mimeType'] == 'text/html' and int(
                    message['payload']['body']['size']) > 0:
                mail_body = str(
                    base64.urlsafe_b64decode(
                        message['payload']['body']['data'].encode('ASCII')))
            else:
                mail_body = None
        else:
            for part in message['payload']['parts']:
                if (part['mimeType'] == 'text/html'):
                    # get mail's body as a string
                    mail_body = str(
                        base64.urlsafe_b64decode(
                            part['body']['data'].encode('ASCII')))
                    break
                else:
                    mail_body = None

        if original_date is None:
            log(mail_subject, 'e')
            log(str(message['payload']['headers']), 'e')

        if mail_subject is not None and mail_body is not None and original_date is not None:
            inserted_before = GoogleMail.objects.all().filter(msg_id=msg_id)
            if inserted_before.count() == 0:
                app_source = Source.objects.get(value__iexact=source)
                mail = GoogleMail(user=user,
                                  subject=mail_subject,
                                  body=mail_body,
                                  date=date,
                                  msg_id=msg_id,
                                  app_source=app_source)
                mail.save()
            else:
                mail = inserted_before[0]
        else:
            mail = None

        job_title = ''
        company = ''
        image_url = ''

        if source == 'LinkedIn':
            # job_title and company are in the subject in LinkedIn mails
            # we should find them in the subject
            if 'for ' in mail_subject and ' at ' in mail_subject:
                job_title = mail_subject[mail_subject.index('for ') +
                                         4:mail_subject.index(' at ')]
            if 'at ' in mail_subject:
                company = mail_subject[mail_subject.index('at ') + 3:]

            if mail_body is not None:
                # trying to find company logo in the body
                s = find_nth(mail_body, 'https://media.licdn.com', 2)
                if s != -1:
                    e = find_nth(mail_body, '" alt="' + company + '"', 1)
                    image_url = mail_body[s:e].replace('&', '&')
                    image_exists = requests.get(image_url)
                    if len(image_url) > 300:
                        image_url = None
                    if image_exists.status_code == 404:
                        image_url = None
                else:
                    image_url = None

        elif source == 'Vettery':
            # job_title and company are in the body in Vettery mails
            if mail_body is not None:
                if 'Role: ' in mail_body and 'Salary' in mail_body:
                    job_title = mail_body[mail_body.index('Role: ') +
                                          6:mail_body.index('Salary')]
                    job_title = remove_html_tags(job_title)
                if 'interview with ' in mail_body and '. Interested?' in mail_body:
                    company = mail_body[mail_body.index('interview with ') +
                                        15:mail_body.index('. Interested?')]
                elif 'Our take on ' in mail_body and ':</span>' in mail_body:
                    company = mail_body[mail_body.index('Our take on ') +
                                        12:mail_body.index(':</span></')]
                image_url = None
        elif source == 'Hired.com':
            # job_title and company are in the body in Hired.com mails
            if 'Request: ' in mail_subject and ' at ' in mail_subject:
                job_title = mail_subject[mail_subject.index('Request: ') +
                                         9:mail_subject.index(' at ')]
            if 'at ' in mail_subject and '($' in mail_subject:
                company = mail_subject[mail_subject.index('at ') +
                                       3:mail_subject.index('($')]
            image_url = None
        elif source == 'Indeed':
            # job_title is in the subject
            if 'Indeed Application: ' in mail_subject:
                job_title = mail_subject[mail_subject.
                                         index('Indeed Application: ') + 20:]

            if mail_body is not None:
                # company is in the body
                if 'updates from' in mail_body and '</b>' in mail_body:
                    c_start_index = mail_body.index('updates from') + 16
                    c_end_index = mail_body[c_start_index:(c_start_index +
                                                           100)].index('</b>')
                    company = mail_body[c_start_index:c_start_index +
                                        c_end_index]
                image_url = None
        elif source == 'glassdoor':
            # company is in the subject
            if 'on to ' in mail_subject and ' completed.' in mail_subject:
                company = mail_subject[mail_subject.index('on to ') +
                                       6:mail_subject.index(' completed.')]

            if mail_body is not None:
                # job_title is in the body
                soup = bs(mail_body, 'html.parser')
                images = soup.findAll('img')
                for image in images:
                    if image.has_attr('alt') and image['alt'] == company:
                        image_url = image['src']
                        image_exists = requests.get(image_url)
                        if image_exists.status_code == 404:
                            image_url = None
                        break
                job_title = soup.find(
                    'a',
                    attrs={
                        'style': 'text-decoration: none; color:#0066cc'
                    }).contents[0]
        elif source == 'jobvite.com':
            if 'Recruiting Team' in mail_from:
                company = mail_from[:mail_from.find(' Recruiting Team')]
                if ' for ' in mail_subject and ' at ' in mail_subject:
                    job_title = mail_subject[mail_subject.index(' for ') +
                                             5:mail_subject.index(' at ')]

                if mail_body is not None:
                    # check the body for more accurate companmy name
                    if ' at ' in mail_body and '. We' in mail_body:
                        company = mail_body[mail_body.index(' at ') +
                                            4:mail_body.index('. We')]

                    # check the body for the job title
                    if ' the ' in mail_body and ' role at ' + company in mail_body:
                        job_title = mail_body[mail_body.index(' the ') +
                                              5:mail_body.index(' role at ')]
                    elif ' the ' in mail_body and ' job at ' + company in mail_body:
                        job_title = mail_body[mail_body.index(' the ') +
                                              5:mail_body.index(' job at ')]
            else:
                # jobinvite sends the approval email with this keyword
                return
        elif source == 'smartrecruiters.com':
            company = mail_subject[mail_subject.rindex('applying to ') +
                                   12:].strip(string.punctuation)
            soup = bs(mail_body, 'html.parser')
            ps = soup.findAll('p')
            first_parag = ''
            if len(ps) == 0:
                first_parag = soup.text
            for p in ps:
                if company in p.text:
                    first_parag = p.text
                    break
            if first_parag != '':
                # needs more data to determine the pattern
                if ' position of ' in first_parag and '. We' in first_parag:
                    job_title = first_parag[first_parag.index(' position of '
                                                              ) +
                                            13:first_parag.index('. We')]
                elif 'application for the ' in first_parag and ', ' + company in first_parag:
                    job_title = first_parag[first_parag.
                                            index('application for the ') +
                                            20:first_parag.index(', ' +
                                                                 company)]
        elif source == 'greenhouse.io':
            pass
        elif source == 'lever.co':
            company = mail_from[:mail_from.index(' <*****@*****.**>')]
            company = company.replace('"', '')
            if 'application for ' in mail_body and '.&nbsp;</div' in mail_body:
                job_title = mail_body[mail_body.index("application for ") +
                                      16:mail_body.index(".&nbsp;</div")]
            elif 'application for ' in mail_body and '. Our' in mail_body:
                job_title = mail_body[mail_body.index("application for ") +
                                      16:mail_body.index(". Our")]
            elif 'application for ' in mail_body and ', and we are d' in mail_body:
                job_title = mail_body[mail_body.index('application for ') +
                                      16:mail_body.index(', and we are d')]
                job_title = str(unicode_to_ascii(job_title))
            elif 'application for our ' in mail_body and ' opening' in mail_body:
                job_title = mail_body[mail_body.index("application for our ") +
                                      20:mail_body.index(" opening")]
            elif 'application for the ' in mail_body and ' role.' in mail_body:
                job_title = mail_body[mail_body.index("application for the ") +
                                      20:mail_body.index(" role.")]

        elif source == 'ziprecruiter.com':
            soup = bs(mail_body, 'html.parser')
            ps = soup.findAll('b')
            if len(ps) == 2:
                job_title = ps[0].text
                company = ps[1].text

        inserted_before = JobApplication.objects.all().filter(msg_id=msg_id)
        if inserted_before.count() == 0 and job_title != '' and company != '':
            jt = get_or_insert_position(job_title)

            jc = get_or_create_company(company)

            if ApplicationStatus.objects.filter(default=True).count() == 0:
                status = ApplicationStatus(value='Applied', default=True)
                status.save()
            else:
                status = ApplicationStatus.objects.get(default=True)
            source = Source.objects.get(value__iexact=source)
            japp = JobApplication(position=jt,
                                  company_object=jc,
                                  apply_date=date,
                                  msg_id=msg_id,
                                  app_source=source,
                                  user=user,
                                  application_status=status)
            japp.save()
            log('Job Application inserted : ' + str(japp), 'i')
            status_history = StatusHistory(job_post=japp,
                                           application_status=status)
            status_history.save()
            if mail is not None:
                mail.job_post = japp
                mail.app_source = source
                mail.save()

    except errors.HttpError as error:
        if error.resp.status == 403 or error.resp.status == 401:
            return False
        log('An error occurred: %s' % error, 'e')
    except Exception as e:
        log(traceback.format_exception(None, e, e.__traceback__), 'e')
Beispiel #3
0
def position_applications(request):
    body = request.data
    if 'recaptcha_token' in body and utils.verify_recaptcha(
            None, body['recaptcha_token'],
            'add_job') == ResponseCodes.verify_recaptcha_failed:
        return JsonResponse(create_response(
            data=None,
            success=False,
            error_code=ResponseCodes.verify_recaptcha_failed),
                            safe=False)
    if request.method == "GET":
        status_id = request.GET.get('status_id')
        if status_id is not None:
            user_job_apps = PositionApplication.objects.filter(
                Q(user__id=request.user.id)
                | Q(position__company=request.user.company),
                application_status__id=status_id,
                is_deleted=False).order_by('-apply_date')
        else:
            user_job_apps = PositionApplication.objects.filter(
                Q(user__id=request.user.id)
                | Q(position__company=request.user.company),
                is_deleted=False).order_by('-apply_date')
        job_applications_list = PositionApplicationSerializer(
            instance=user_job_apps, many=True, context={
                'user': request.user
            }).data
        return JsonResponse(create_response(data=job_applications_list),
                            safe=False)
    elif request.method == "POST":
        position_id = body['position_id']
        company = body['company']
        application_date = body['application_date']
        status = int(body['status_id'])
        first_name = body['first_name']
        last_name = body['last_name']
        jt = PositionDetail.objects.get(pk=position_id)
        jc = get_or_create_company(company)
        job_application = PositionApplication(position=jt,
                                              company_object=jc,
                                              first_name=first_name,
                                              last_name=last_name,
                                              apply_date=application_date,
                                              user=request.user)
        job_application.application_status = ApplicationStatus.objects.get(
            pk=status)
        job_application.save()
        return JsonResponse(create_response(
            data=PositionApplicationSerializer(instance=job_application,
                                               many=False,
                                               context={
                                                   'user': request.user
                                               }).data),
                            safe=False)
    elif request.method == "PUT":
        status_id = body.get('status_id')
        rejected = body.get('rejected')
        job_application_ids = []
        if 'jobapp_ids' in body:
            job_application_ids = body['jobapp_ids']
        if 'jobapp_id' in body:
            job_application_ids.append(body['jobapp_id'])
        if len(job_application_ids) == 0:
            return JsonResponse(create_response(
                success=False, error_code=ResponseCodes.record_not_found),
                                safe=False)
        elif rejected is None and status_id is None:
            return JsonResponse(create_response(
                success=False, error_code=ResponseCodes.record_not_found),
                                safe=False)
        else:
            user_job_apps = PositionApplication.objects.filter(
                pk__in=job_application_ids)
            if user_job_apps.count() == 0:
                return JsonResponse(create_response(
                    success=False, error_code=ResponseCodes.record_not_found),
                                    safe=False)
            else:
                for user_job_app in user_job_apps:
                    if user_job_app.user == request.user:
                        if status_id is None:
                            user_job_app.is_rejected = rejected
                        else:
                            new_status = ApplicationStatus.objects.filter(
                                pk=status_id)
                            if new_status.count() == 0:
                                return JsonResponse(
                                    create_response(data=None,
                                                    success=False,
                                                    error_code=ResponseCodes.
                                                    invalid_parameters),
                                    safe=False)
                            else:
                                if rejected is None:
                                    user_job_app.application_status = new_status[
                                        0]
                                else:
                                    user_job_app.application_status = new_status[
                                        0]
                                    user_job_app.is_rejected = rejected
                                status_history = StatusHistory(
                                    pos_app=user_job_app,
                                    application_status=new_status[0])
                                status_history.save()
                        if rejected is not None:
                            user_job_app.rejected_date = timezone.now()
                        user_job_app.updated_date = timezone.now()
                        user_job_app.save()
                return JsonResponse(create_response(data=None), safe=False)
    elif request.method == "PATCH":
        job_app_id = body.get('jobapp_id')
        if job_app_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
        user_job_app = PositionApplication.objects.get(pk=job_app_id)

        if user_job_app.user != request.user:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)

        job_title = body.get('job_title')
        company = body.get('company')
        application_date = body.get('application_date')
        source = body.get('source')

        if application_date is not None:
            user_job_app.apply_date = application_date
        if job_title is not None:
            user_job_app.position = get_or_insert_position(job_title)
        if company is not None:
            user_job_app.company_object = get_or_create_company(company)
        user_job_app.updated_date = timezone.now()
        user_job_app.save()
        return JsonResponse(create_response(data=PositionApplicationSerializer(
            instance=user_job_app, many=False, context={
                'user': request.user
            }).data),
                            safe=False)
    elif request.method == "DELETE":
        job_application_ids = []
        if 'jobapp_ids' in body:
            job_application_ids = body['jobapp_ids']
        if 'jobapp_id' in body:
            job_application_ids.append(body['jobapp_id'])
        if len(job_application_ids) == 0 or PositionApplication.objects.filter(
                pk__in=job_application_ids).count() == 0:
            return JsonResponse(create_response(
                success=False, error_code=ResponseCodes.record_not_found),
                                safe=False)
        else:
            user_job_apps = PositionApplication.objects.filter(
                pk__in=job_application_ids)
            for user_job_app in user_job_apps:
                if user_job_app.user == request.user:
                    user_job_app.deleted_date = timezone.now()
                    user_job_app.is_deleted = True
                    user_job_app.save()
            return JsonResponse(create_response(data=None), safe=False)
Beispiel #4
0
def contacts(request, pos_app_pk):
    body = request.data
    if request.method == "GET":
        data = {}
        if pos_app_pk is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        else:
            contacts_list = Contact.objects.filter(pos_app_pk=pos_app_pk)
            contacts_list = ContactSerializer(instance=contacts_list,
                                              many=True).data

            data['contacts'] = contacts_list

            user_profile = request.user
            jobapp = PositionApplication.objects.get(pk=pos_app_pk)

        return JsonResponse(create_response(data=data,
                                            success=True,
                                            error_code=ResponseCodes.success),
                            safe=False)
    elif request.method == "POST":
        first_name = body.get('first_name')
        last_name = body.get('last_name')
        if pos_app_pk is None or first_name is None or last_name is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        user_job_app = PositionApplication.objects.get(pk=pos_app_pk)
        if user_job_app.user == request.user:
            phone_number = body.get('phone_number')
            linkedin_url = body.get('linkedin_url')
            description = body.get('description')
            email = body.get('email')
            job_title = body.get('job_title')
            jt = None
            jc = None
            if job_title is not None:
                jt = get_or_insert_position(job_title)

            company = body.get('company')
            if company is not None:
                jc = get_or_create_company(company)

            contact = Contact(pos_app=user_job_app,
                              first_name=first_name,
                              last_name=last_name,
                              phone_number=phone_number,
                              linkedin_url=linkedin_url,
                              description=description,
                              email=email,
                              position=jt,
                              company=jc)
            contact.save()
            data = ContactSerializer(instance=contact, many=False).data
            return JsonResponse(create_response(data=data), safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
    elif request.method == "PUT":
        contact_id = body.get('contact_id')
        if contact_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        contact = Contact.objects.get(pk=contact_id)
        if contact.pos_app.user == request.user:
            email = body.get('email')
            if email is not None:
                contact.email = email
            phone_number = body.get('phone_number')
            if phone_number is not None:
                contact.phone_number = phone_number
            linkedin_url = body.get('linkedin_url')
            if linkedin_url is not None:
                contact.linkedin_url = linkedin_url
            description = body.get('description')
            if description is not None:
                contact.description = description
            job_title = body.get('job_title')
            if job_title is not None:
                contact.position = get_or_insert_position(job_title)
            company = body.get('company')
            if company is not None:
                contact.company = get_or_create_company(company)

            contact.updated_date = timezone.now()
            contact.save()
            data = ContactSerializer(instance=contact, many=False).data
            return JsonResponse(create_response(
                data=data, success=True, error_code=ResponseCodes.success),
                                safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
    elif request.method == "DELETE":
        contact_id = body.get('contact_id')
        if contact_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        user_job_app_contact = Contact.objects.filter(pk=contact_id)
        if user_job_app_contact.count() == 0:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
        user_job_app_contact = user_job_app_contact[0]
        if user_job_app_contact.pos_app.user == request.user:
            user_job_app_contact.delete()
            return JsonResponse(create_response(
                data=None, success=True, error_code=ResponseCodes.success),
                                safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
Beispiel #5
0
def job_applications(request):
    body = request.data
    if 'recaptcha_token' in body and utils.verify_recaptcha(
            None, body['recaptcha_token'],
            'add_job') == ResponseCodes.verify_recaptcha_failed:
        return JsonResponse(create_response(
            data=None,
            success=False,
            error_code=ResponseCodes.verify_recaptcha_failed),
                            safe=False)
    if request.method == "GET":
        timestamp = request.GET.get('timestamp')
        if timestamp is not None:
            timestamp = int(timestamp) / 1000
            if timestamp is None:
                return JsonResponse(
                    create_response(
                        data=None,
                        success=False,
                        error_code=ResponseCodes.invalid_parameters))
            profile = request.user
            time = datetime.fromtimestamp(int(timestamp))
            user_job_apps = JobApplication.objects.filter(created__gte=time)
            job_application_list = JobApplicationSerializer(
                instance=user_job_apps,
                many=True,
                context={
                    'user': request.user
                }).data
            response = {
                'data': job_application_list,
                'synching': profile.synching
            }
            return JsonResponse(create_response(data=response), safe=False)
        status_id = request.GET.get('status_id')
        if status_id is not None:
            user_job_apps = JobApplication.objects.filter(
                application_status__id=status_id,
                user__id=request.user.id,
                is_deleted=False).order_by('-apply_date')
        else:
            user_job_apps = JobApplication.objects.filter(
                user_id=request.user.id,
                is_deleted=False).order_by('-apply_date')
        job_applications_list = JobApplicationSerializer(
            instance=user_job_apps, many=True, context={
                'user': request.user
            }).data
        return JsonResponse(create_response(data=job_applications_list),
                            safe=False)
    elif request.method == "POST":
        job_title = body['job_title']
        company = body['company']
        application_date = body['application_date']
        status = int(body['status_id'])
        source = body['source']

        jt = get_or_insert_position(job_title)
        jc = get_or_create_company(company)

        if Source.objects.filter(value__iexact=source).count() == 0:
            source = Source.objects.create(value=source)
        else:
            source = Source.objects.get(value__iexact=source)

        job_application = JobApplication(position=jt,
                                         company_object=jc,
                                         apply_date=application_date,
                                         msg_id='',
                                         app_source=source,
                                         user=request.user)
        job_application.application_status = ApplicationStatus.objects.get(
            pk=status)
        job_application.save()
        return JsonResponse(create_response(
            data=JobApplicationSerializer(instance=job_application,
                                          many=False,
                                          context={
                                              'user': request.user
                                          }).data),
                            safe=False)
    elif request.method == "PUT":
        status_id = body.get('status_id')
        rejected = body.get('rejected')
        job_application_ids = []
        if 'jobapp_ids' in body:
            job_application_ids = body['jobapp_ids']
        if 'jobapp_id' in body:
            job_application_ids.append(body['jobapp_id'])
        if len(job_application_ids) == 0:
            return JsonResponse(create_response(
                success=False, error_code=ResponseCodes.record_not_found),
                                safe=False)
        elif rejected is None and status_id is None:
            return JsonResponse(create_response(
                success=False, error_code=ResponseCodes.record_not_found),
                                safe=False)
        else:
            user_job_apps = JobApplication.objects.filter(
                pk__in=job_application_ids)
            if user_job_apps.count() == 0:
                return JsonResponse(create_response(
                    success=False, error_code=ResponseCodes.record_not_found),
                                    safe=False)
            else:
                for user_job_app in user_job_apps:
                    if user_job_app.user == request.user:
                        if status_id is None:
                            user_job_app.is_rejected = rejected
                        else:
                            new_status = ApplicationStatus.objects.filter(
                                pk=status_id)
                            if new_status.count() == 0:
                                return JsonResponse(
                                    create_response(data=None,
                                                    success=False,
                                                    error_code=ResponseCodes.
                                                    invalid_parameters),
                                    safe=False)
                            else:
                                if rejected is None:
                                    user_job_app.application_status = new_status[
                                        0]
                                else:
                                    user_job_app.application_status = new_status[
                                        0]
                                    user_job_app.is_rejected = rejected
                                status_history = StatusHistory(
                                    job_post=user_job_app,
                                    application_status=new_status[0])
                                status_history.save()
                        if rejected is not None:
                            user_job_app.rejected_date = datetime.now()
                        user_job_app.updated_date = datetime.now()
                        user_job_app.save()
                return JsonResponse(create_response(data=None), safe=False)
    elif request.method == "PATCH":
        job_app_id = body.get('jobapp_id')
        if job_app_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
        user_job_app = JobApplication.objects.get(pk=job_app_id)

        if user_job_app.user != request.user:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
        if user_job_app.msg_id is not None and user_job_app.msg_id != '':
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)

        job_title = body.get('job_title')
        company = body.get('company')
        application_date = body.get('application_date')
        source = body.get('source')

        if application_date is not None:
            user_job_app.apply_date = application_date
        if job_title is not None:
            user_job_app.position = get_or_insert_position(job_title)
        if company is not None:
            user_job_app.company_object = get_or_create_company(company)
        if source is not None:
            if Source.objects.filter(value__iexact=source).count() == 0:
                source = Source.objects.create(value=source)
            else:
                source = Source.objects.get(value__iexact=source)
            user_job_app.app_source = source
        user_job_app.updated_date = datetime.now()
        user_job_app.save()
        return JsonResponse(create_response(data=JobApplicationSerializer(
            instance=user_job_app, many=False, context={
                'user': request.user
            }).data),
                            safe=False)
    elif request.method == "DELETE":
        job_application_ids = []
        if 'jobapp_ids' in body:
            job_application_ids = body['jobapp_ids']
        if 'jobapp_id' in body:
            job_application_ids.append(body['jobapp_id'])
        if len(job_application_ids) == 0 or JobApplication.objects.filter(
                pk__in=job_application_ids).count() == 0:
            return JsonResponse(create_response(
                success=False, error_code=ResponseCodes.record_not_found),
                                safe=False)
        else:
            user_job_apps = JobApplication.objects.filter(
                pk__in=job_application_ids)
            for user_job_app in user_job_apps:
                if user_job_app.user == request.user:
                    user_job_app.deleted_date = datetime.now()
                    user_job_app.is_deleted = True
                    user_job_app.save()
            return JsonResponse(create_response(data=None), safe=False)
Beispiel #6
0
def contacts(request, job_app_pk):
    body = request.data
    if request.method == "GET":
        data = {}
        if job_app_pk is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        else:
            contacts_list = Contact.objects.filter(job_post__pk=job_app_pk)
            contacts_list = ContactSerializer(instance=contacts_list,
                                              many=True).data

            data['contacts'] = contacts_list

            user_profile = request.user
            if user_profile.user_type < int(User.UserTypes.student):
                alumni = []
            else:
                jobapp = JobApplication.objects.get(pk=job_app_pk)
                alumni_list = User.objects.filter(
                    college=user_profile.college,
                    company=jobapp.company_object,
                    user_type=int(User.UserTypes.alumni))
                alumni = AlumniSerializer(instance=alumni_list,
                                          many=True,
                                          context={
                                              'user': request.user
                                          }).data
            data['alumni'] = alumni
        return JsonResponse(create_response(data=data,
                                            success=True,
                                            error_code=ResponseCodes.success),
                            safe=False)
    elif request.method == "POST":
        first_name = body.get('first_name')
        last_name = body.get('last_name')
        if job_app_pk is None or first_name is None or last_name is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        user_job_app = JobApplication.objects.get(pk=job_app_pk)
        if user_job_app.user == request.user:
            phone_number = body.get('phone_number')
            linkedin_url = body.get('linkedin_url')
            description = body.get('description')
            email = body.get('email')
            job_title = body.get('job_title')
            jt = None
            jc = None
            if job_title is not None:
                jt = get_or_insert_position(job_title)

            company = body.get('company')
            if company is not None:
                jc = get_or_create_company(company)

            contact = Contact(job_post=user_job_app,
                              first_name=first_name,
                              last_name=last_name,
                              phone_number=phone_number,
                              linkedin_url=linkedin_url,
                              description=description,
                              email=email,
                              position=jt,
                              company=jc)
            contact.save()
            data = ContactSerializer(instance=contact, many=False).data
            return JsonResponse(create_response(data=data), safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
    elif request.method == "PUT":
        contact_id = body.get('contact_id')
        if contact_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        contact = Contact.objects.get(pk=contact_id)
        if contact.job_post.user == request.user:
            first_name = body.get('first_name')
            if first_name is not None:
                contact.first_name = first_name
            last_name = body.get('last_name')
            if last_name is not None:
                contact.last_name = last_name
            email = body.get('email')
            if email is not None:
                contact.email = email
            phone_number = body.get('phone_number')
            if phone_number is not None:
                contact.phone_number = phone_number
            linkedin_url = body.get('linkedin_url')
            if linkedin_url is not None:
                contact.linkedin_url = linkedin_url
            description = body.get('description')
            if description is not None:
                contact.description = description
            job_title = body.get('job_title')
            if job_title is not None:
                contact.position = get_or_insert_position(job_title)
            company = body.get('company')
            if company is not None:
                contact.company = get_or_create_company(company)

            contact.update_date = datetime.now()
            contact.save()
            data = ContactSerializer(instance=contact, many=False).data
            return JsonResponse(create_response(
                data=data, success=True, error_code=ResponseCodes.success),
                                safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
    elif request.method == "DELETE":
        contact_id = body.get('contact_id')
        if contact_id is None:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.invalid_parameters),
                                safe=False)
        user_job_app_contact = Contact.objects.filter(pk=contact_id)
        if user_job_app_contact.count() == 0:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)
        user_job_app_contact = user_job_app_contact[0]
        if user_job_app_contact.job_post.user == request.user:
            user_job_app_contact.delete()
            return JsonResponse(create_response(
                data=None, success=True, error_code=ResponseCodes.success),
                                safe=False)
        else:
            return JsonResponse(create_response(
                data=None,
                success=False,
                error_code=ResponseCodes.record_not_found),
                                safe=False)