def like():
    try:
        user = g.user
        if user.worker:
            current_job = validate_request_attr(
                request, ["current_job"])["current_job"]
            for currentRec in user.worker.recommendation:
                if currentRec.job_id == current_job.id:
                    currentRec.worker_match = 1
                    if currentRec.job_match == 1:
                        create.new_chat(user.worker.id, current_job.id)
            return Success("UPDATE_CORRECT").asdict()
        else:
            val_attr = validate_request_attr(
                request, ["current_worker", "current_job"])
            current_job = val_attr["current_job"]
            current_worker = val_attr["current_worker"]
            for currentRec in current_job.recommendation:
                if currentRec.worker_id == current_worker.id:
                    currentRec.job_match = 1
                    if currentRec.worker_match == 1:
                        create.new_chat(user.worker.id, current_job.id)
            return Success("UPDATE_CORRECT").asdict()
    except Error as myError:
        return myError
Exemple #2
0
def update_profile():
    try:
        user = g.user
        if user is None:
            raise Error("MUST_LOGGED_IN")
        if user.employer:
            validated_attr = validate_request_attr(request, [
                "company_name", "description", "logo", "image",
                "master_certificate"
            ])
            employer = user.employer
            update.update_employer(employer, "company_name",
                                   validated_attr['company_name'])
            update.update_employer(employer, "description",
                                   validated_attr['description'])
            update.update_employer(employer, "logo", validated_attr['logo'])
            update.update_employer(employer, "image", validated_attr['image'])
            update.update_employer(employer, "master_certificate",
                                   validated_attr['master_certificate'])
        else:
            validated_attr = validate_request_attr(request, [
                "first_name", "last_name", "birth_day", "birth_month",
                "birth_year", "location", "image", "cv", "hashtags"
            ])
            worker = user.worker
            update.update_worker(worker, "first_name",
                                 validated_attr['first_name'])
            update.update_worker(worker, "last_name",
                                 validated_attr['last_name'])
            print(validated_attr['birth_day'])
            update.update_worker(worker, "birth_day",
                                 validated_attr['birth_day'])
            update.update_worker(worker, "birth_month",
                                 validated_attr['birth_month'])
            update.update_worker(worker, "birth_year",
                                 validated_attr['birth_year'])
            update.update_worker(worker, "location",
                                 validated_attr['location'])
            update.update_worker(worker, "image", validated_attr['image'])
            update.update_worker(worker, "cv", validated_attr['cv'])
            update.update_worker(worker, "hashtags",
                                 validated_attr['hashtags'])
        return Success("PROFILE_UPDATED").asdict()
    except Error as myError:
        return myError.asdict()
Exemple #3
0
def register():
    try:
        validated_attr = validate_request_attr(
            request, ["user_type", "email", "password"])
        if read.get_user_by('email', validated_attr["email"]) is not None:
            raise Error('EMAIL_EXIST_ALREADY')
        # Create user
        verification_token = generate_token()
        new_user = None
        # validate & create_employer
        if validated_attr["user_type"] == 'employer':
            employer_attr = validate_request_attr(request, ["company_name"])
            validate_user(validated_attr["email"], validated_attr["password"])
            #validate_employer(employer_attr["company_name"])
            new_user = create.create_user(
                validated_attr["email"],
                generate_password_hash(validated_attr["password"]),
                verification_token)
            create.create_employer(employer_attr['company_name'], new_user.id)
        # or validate & create worker
        elif validated_attr["user_type"] == 'worker':
            worker_attr = validate_request_attr(request, [
                "birth_day", "birth_month", "birth_year", "first_name",
                "last_name"
            ])
            validate_user(validated_attr["email"], validated_attr["password"])
            #validate_worker(worker_attr['first_name'], worker_attr['last_name'])
            new_user = create.create_user(
                validated_attr["email"],
                generate_password_hash(validated_attr["password"]),
                verification_token)
            create.create_worker(worker_attr['birth_day'],
                                 worker_attr['birth_month'],
                                 worker_attr['birth_year'], new_user.id,
                                 worker_attr['first_name'],
                                 worker_attr['last_name'])

        #send_verification_email(email, request.json['last_name'], verification_token)
        auth_token = encode_auth_token(new_user.id)

        return Success('REGISTERED').asdict() | {
            "auth_token": auth_token.decode()
        }
    except Error as myError:
        return myError.asdict()
Exemple #4
0
def delete_job():
    try:
        validated_attr = validate_request_attr(request, ["job_id"])
        current_job = read.get_job_by("id", validated_attr['job_id'])
        delete.delete_job(current_job)
        return Success("JOB_DELETED").asdict()

    except Error as myError:
        return myError.asdict()
Exemple #5
0
def toggle_activate_job():
    try:
        validated_attr = validate_request_attr(request, ["job_id"])
        job = read.get_job_by("id", validated_attr["job_id"])
        if job.is_active == False:
            update.update_job(job, "is_active", True)
        else:
            update.update_job(job, "is_active", False)
        return Success("PROFILE_UPDATED").asdict()
    except Error as myError:
        return myError.asdict()
Exemple #6
0
def get_messages():
    try:
        validated_attr = validate_request_attr(request, ["chat_id"])
        messages = []
        current_chat = read.get_chat_by("id", validated_attr["chat_id"])
        for current_message in current_chat.message:
            messages.append({
                "message": current_message.content,
                "from_worker": current_message.from_worker
            })
        return Success("READ_CORRECT").asdict() | {"messages": messages}
    except Error as myError:
        return myError.asdict()
Exemple #7
0
def send_message():
    try:
        validated_attr = validate_request_attr(request, ["content", "chat_id"])
        current_user = g.user
        content = validated_attr['content']
        chat_id = validated_attr['chat_id']
        current_chat = read.get_chat_by("id", chat_id)
        if current_user.worker:
            create.create_message(content, True, current_chat.id)
        else:
            create.create_message(content, False, current_chat.id)
        return Success("MESSAGE_SENT").asdict()
    except Error as myError:
        return myError.asdict()
Exemple #8
0
def set_new_password():
    try:
        validated_attr = validate_request_attr(request, ["password"])
        password_reset_token = request.args.get('tk')
        password = validated_attr['password']
        user = read.get_user_by('password_reset_token', password_reset_token)
        if user is None:
            raise Error('CANT_RESET_PASSWORD')
        validate_password(password)
        update.set_user_attribute(user, 'password',
                                  generate_password_hash(password))
        update.set_user_attribute(user, 'password_reset_token', None)
        return Success('ENTER_PASSWORD').asdict()
    except Error as myError:
        return myError.asdict()
Exemple #9
0
def reset_password():
    try:
        validated_attr = validate_request_attr(request, ["email"])
        email = validated_attr['email']
        user = read.get_user_by('email', email)
        if user is None:
            raise Error('EMAIL_INCORRECT')
        else:
            password_reset_token = generate_token()
            send_pw_reset_email(user.email, user.last_name,
                                password_reset_token)
            update.set_user_attribute(user, 'password_reset_token',
                                      password_reset_token)
            return Success('PW_RESET_SEND').asdict()
    except Error as myError:
        return myError.asdict()
Exemple #10
0
def create_job():
    try:
        validated_attr = validate_request_attr(request, [
            "title", "type", "location", "description", "must_have",
            "hashtags", "nice_have"
        ])
        title = validated_attr["title"]
        type = validated_attr["type"]
        description = validated_attr["description"]
        location = validated_attr["location"]
        must_have = validated_attr["must_have"]
        nice_have = validated_attr["nice_have"]
        hashtags = validated_attr["hashtags"]
        employer = g.user.employer
        new_job = create.create_job(title, type, description,
                                    location, must_have, nice_have, hashtags,
                                    time.ctime(), employer.id)

        return Success("JOB_CREATED").asdict() | {"job_id": new_job.id}
    except Error as myError:
        return myError.asdict()
Exemple #11
0
def login():
    try:
        validated_attr = validate_request_attr(request, ["email", "password"])
        email = validated_attr['email']
        password = validated_attr['password']
        # Validation
        user = read.get_user_by('email', email)
        if user is None:
            raise Error('EMAIL_INCORRECT')

        if not check_password_hash(user.password, password):
            raise Error('PASSWORD_INCORRECT')

        # Login and create session
        auth_token = encode_auth_token(user.id)
        isWorker = True if user.worker else False
        return Success('LOGGED_IN').asdict() | {
            "auth_token": auth_token.decode(),
            "isWorker": isWorker
        }
    except Error as myError:
        return myError.asdict()
Exemple #12
0
def get_chats():
    try:
        chats = []
        # for worker get chats
        if g.user.worker:
            current_user = g.user
            for current_chat in current_user.worker.chat:
                chats.append({
                    "chat_id": current_chat.id,
                    "title": current_chat.job.title
                })
        else:
            # for jobs get chats
            validated_attr = validate_request_attr(request, ["job_id"])
            current_job = read.get_job_by("id", validated_attr['job_id'])
            for current_chat in current_job.chat:
                chats.append({
                    "chat_id": current_chat.id,
                    "first_name": current_chat.worker.first_name
                })
        return Success("READ_CORRECT").asdict() | {"chats": chats}
    except Error as myError:
        return myError.asdict()
Exemple #13
0
def update_job():
    try:
        validated_attr = validate_request_attr(request, [
            "title", "type", "description", "hashtags", "location",
            "must_have", "nice_have", "job_id"
        ])
        current_job = read.get_job_by("id", validated_attr['job_id'])
        user = g.user
        if not user.employer:
            raise Error("NO_JOBS")
        update.update_job(current_job, "title", validated_attr['title'])
        update.update_job(current_job, "type", validated_attr['type'])
        update.update_job(current_job, "description",
                          validated_attr['description'])
        update.update_job(current_job, "hashtags", validated_attr['hashtags'])
        update.update_job(current_job, "location", validated_attr['location'])
        update.update_job(current_job, "must_have",
                          validated_attr['must_have'])
        update.update_job(current_job, "nice_have",
                          validated_attr['nice_have'])
        return Success("PROFILE_UPDATED").asdict()
    except Error as myError:
        return myError.asdict()