Ejemplo n.º 1
0
async def delete_job(job_id: int, token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = Job.select().where(Job.c.id == job_id)
    is_exist = await database.fetch_one(query)
    if not is_exist:
        response = {'detail': 'JOB NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    if is_exist.created_by != authenticated_user['user_id']:
        response = {
            'detail': 'UNAUTHORIZED ACCESS(YOU CAN DELETE YOUR JOB ONLY)',
            'status': 401
        }
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = Job.update().where(Job.c.id == job_id).values(status='d')
    await database.execute(query)
    response = {
        'detail': "JOB ID: {} DELETED SUCCESSFULLY".format(job_id),
        'status': 200
    }
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 2
0
async def refresh_token(token: str = Header(None)):
    user = verify_token(token)
    if not user:
        response = {'detail': 'INVALID OR EXPIRED TOKEN', 'status': 403}
        return JSONResponse(status_code=status.HTTP_403_FORBIDDEN,
                            content=response)

    token = genrate_token(user)
    response = {'detail': 'TOKEN REFRESHED', 'token': token, 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 3
0
async def remove_favourite(favourite_id: int, token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = FavouriteJob.delete().where(FavouriteJob.c.id == favourite_id)
    await database.execute(query)
    response = {'detail': "FAVOURITE JOB REMOVED", 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 4
0
async def get_my_job(apply_id: int, token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    my_job = session.query(AppliedJob).filter(
        AppliedJob.c.id == apply_id).first()
    if not my_job:
        response = {'detail': 'JOB NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    job = session.query(Job).filter(Job.c.id == my_job.job_id).first()
    if not job:
        response = {'detail': 'JOB NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    recruiter = session.query(User).filter(User.c.id == job.created_by).first()
    recruiter_details = 'NA'
    if recruiter:
        recruiter_details = {
            'id': recruiter.id,
            'first_name': recruiter.first_name,
            'last_name': recruiter.last_name,
            'email': recruiter.username,
            'phone': recruiter.phone,
            'gender': recruiter.gender,
        }
    data = {
        'my_status': my_job.status.value if my_job.status else 'NA',
        'job_details': {
            'id': job.id,
            'company_name': job.company_name,
            'job_title': job.job_title,
            'job_type': job.job_type,
            'experiance_min': job.experiance_min,
            'experiance_max': job.experiance_max,
            'job_count': job.job_count,
            'location': job.location,
            'job_status': job.status.value if job.status else 'NA',
        },
        'recruiter_details': recruiter_details
    }

    response = {'Job details': data, 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 5
0
async def get_my_all_jobs(token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    my_jobs = session.query(AppliedJob).filter(
        AppliedJob.c.user_id == authenticated_user['user_id'])
    if my_jobs.count() == 0:
        response = {'detail': 'JOBS NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    job_ids = pluck(my_jobs, 'job_id')
    job_ids = set(job_ids)
    total_jobs = len(job_ids)
    jobs = session.query(Job).filter(Job.c.id.in_(job_ids))
    if jobs.count() == 0:
        response = {'detail': 'JOBS NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    my_all_jobs = []
    for my_job in my_jobs:
        for job in jobs:
            if my_job.job_id == job.id:
                data = {
                    'my_status':
                    my_job.status.value if my_job.status else 'NA',
                    'job_details': {
                        'id': job.id,
                        'company_name': job.company_name,
                        'job_title': job.job_title,
                        'job_type': job.job_type.value,
                        'experiance_min': job.experiance_min,
                        'experiance_max': job.experiance_max,
                        'job_count': job.job_count,
                        'location': job.location,
                        'job_status': job.status.value if job.status else 'NA',
                    }
                }
                my_all_jobs.append(data)

    response = {'Total': total_jobs, 'Jobs': my_all_jobs, 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 6
0
async def get_favourite(token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    favourites = session.query(FavouriteJob).all()
    all_favourite = []
    for d in favourites:
        data = {
            'id': d.id,
            'user_id': d.user_id,
            'job_id': d.job_id,
        }
        all_favourite.append(data)
    response = {'Favourites': all_favourite, 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 7
0
async def update_user(user_id: int,
                      user: UserValidator,
                      token: str = Header(None)):
    query = User.select().where(User.c.id == user_id)
    is_exist = await database.fetch_one(query)
    if not is_exist:
        response = {'detail': 'USER NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    if user_id != authenticated_user['user_id']:
        response = {
            'detail': 'UNAUTHORIZED ACCESS(YOU CAN UPDATE YOUR ACCOUNT ONLY)',
            'status': 401
        }
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    if user.username:
        query = User.select().where(User.c.username == user.username)
        is_exist_username = await database.fetch_one(query)
        if is_exist_username and is_exist_username.id != user_id:
            response = {'detail': 'USERNAME NOT AVAILABLE', 'status': 406}
            return JSONResponse(status_code=status.HTTP_406_NOT_ACCEPTABLE,
                                content=response)

    query = User.update().where(User.c.id == user_id).values(
        username=user.username if user.username else is_exist.username,
        password=encode_password(user.password)
        if user.password else is_exist.password,
        first_name=user.first_name if user.first_name else is_exist.first_name,
        last_name=user.last_name if user.last_name else is_exist.last_name,
        phone=user.phone if user.phone else is_exist.phone,
        gender=user.gender if user.gender else is_exist.gender,
        role=user.role if user.role else is_exist.role,
    )
    await database.execute(query)
    return {**user.dict(), "ID": user_id}
Ejemplo n.º 8
0
async def add_favourite(job_id: int, token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = session.query(FavouriteJob).filter(
        FavouriteJob.c.user_id == authenticated_user['user_id'],
        FavouriteJob.c.job_id == job_id).first()
    if not query:
        query = FavouriteJob.insert().values(
            user_id=authenticated_user['user_id'],
            job_id=job_id,
            is_liked=True,
        )
        session.execute(query)
        session.commit()

    response = {'detail': 'FAVOURITE JOB ADDED', 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 9
0
async def delete_user(user_id: int, token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    if user_id != authenticated_user['user_id']:
        response = {
            'detail': 'UNAUTHORIZED ACCESS(YOU CAN DELETE YOUR ACCOUNT ONLY)',
            'status': 401
        }
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = User.delete().where(User.c.id == user_id)
    await database.execute(query)
    response = {
        'detail': "USER ID: {} DELETED SUCCESSFULLY".format(user_id),
        'status': 200
    }
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 10
0
async def apply_job(job_id: int,
                    job: AppliedJobValidator,
                    token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = Job.select().where(Job.c.id == job_id)
    is_exist = await database.fetch_one(query)
    if not is_exist:
        response = {'detail': 'REQUESTED JOB NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    query = session.query(AppliedJob).filter(
        AppliedJob.c.job_id == job_id,
        AppliedJob.c.user_id == authenticated_user['user_id']).first()
    if query:
        response = {'detail': 'ALREADY APPLIED', 'status': 406}
        return JSONResponse(status_code=status.HTTP_406_NOT_ACCEPTABLE,
                            content=response)

    query = AppliedJob.insert().values(
        user_id=authenticated_user['user_id'],
        job_id=job_id,
        status='applied',
    )
    apply = AppliedJob.insert().values(user_id=authenticated_user['user_id'],
                                       job_id=job_id,
                                       status='a')
    session.execute(apply)
    session.commit()
    response = {'detail': 'SUCCESSFULLY APPLIED', 'status': 200}
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Ejemplo n.º 11
0
 def decorated(*args, **kwargs):
     token = request.headers.get('auth-token')
     print("token: %s" % (token, ))  # DEBUG
     if not verify_token(token):
         return "Unauthorized"
     return f(*args, **kwargs)