Skip to content

Commit

Permalink
Merge pull request #14 from SETeam59/Backend_bug_fix
Browse files Browse the repository at this point in the history
Fixed the backend connection and mapping issues
  • Loading branch information
NarasimhaNayan authored Oct 19, 2023
2 parents b447f4c + 11cbebb commit 28da679
Showing 1 changed file with 58 additions and 64 deletions.
122 changes: 58 additions & 64 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import hashlib
import uuid

existing_endpoints = ["/applications", "/resume"]
existing_endpoints = ["/applications", "/resume","/boards","/getBoards"]


def create_app():
Expand Down Expand Up @@ -222,66 +222,6 @@ def logout():
except:
return jsonify({"error": "Internal server error"}), 500

# search function
# params:
# -keywords: string
@app.route("/search")
def search():
"""
Searches the web and returns the job postings for the given search filters
:return: JSON object with job results
"""
keywords = (
request.args.get("keywords")
if request.args.get("keywords")
else "random_test_keyword"
)
salary = request.args.get("salary") if request.args.get("salary") else ""
keywords = keywords.replace(" ", "+")
if keywords == "random_test_keyword":
return json.dumps({"label": str("successful test search")})
# create a url for a crawler to fetch job information
if salary:
url = (
"https://www.google.com/search?q="
+ keywords
+ "%20salary%20"
+ salary
+ "&ibp=htl;jobs"
)
else:
url = "https://www.google.com/search?q=" + keywords + "&ibp=htl;jobs"

# webdriver can run the javascript and then render the page first.
# This prevent websites don't provide Server-side rendering
# leading to crawlers cannot fetch the page
chrome_options = Options()
# chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
user_agent = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/71.0.3578.98 Safari/537.36 "
)
chrome_options.add_argument(f"user-agent={user_agent}")
driver = webdriver.Chrome(
ChromeDriverManager().install(), chrome_options=chrome_options
)
driver.get(url)
content = driver.page_source
driver.close()
soup = BeautifulSoup(content)

# parsing searching results to DataFrame and return
df = pd.DataFrame(columns=["jobTitle", "companyName", "location"])
mydivs = soup.find_all("div", {"class": "PwjeAc"})
for i, div in enumerate(mydivs):
df.at[i, "jobTitle"] = div.find("div", {"class": "BjJfJf PUpOsf"}).text
df.at[i, "companyName"] = div.find("div", {"class": "vNEEBe"}).text
df.at[i, "location"] = div.find("div", {"class": "Qk80Jf"}).text
df.at[i, "date"] = div.find_all("span", class_="SuWscb", limit=1)[0].text
return jsonify(df.to_dict("records"))

# get data from the CSV file for rendering root page
@app.route("/applications", methods=["GET"])
def get_data():
Expand Down Expand Up @@ -319,6 +259,7 @@ def add_application():
"id": get_new_application_id(userid),
"jobTitle": request_data["jobTitle"],
"companyName": request_data["companyName"],
"boards.columns":request_data['exobj'],
"date": request_data.get("date"),
"jobLink": request_data.get("jobLink"),
"location": request_data.get("location"),
Expand All @@ -330,6 +271,58 @@ def add_application():
return jsonify(current_application), 200
except:
return jsonify({"error": "Internal server error"}), 500

@app.route("/getBoards", methods=["POST"])
def get_boards():
try:
userid = get_userid_from_header()
user = Users.objects(id=userid).first()

if user:
board = user.board # Access the 'board' field from the user object
if board:
return jsonify(board), 200
else:
return jsonify({"error": "User not found"}), 404

except Exception as e:
return jsonify({"error": "Internal server error"}), 500


@app.route("/boards", methods=["POST"], )
def add_boards():
"""
Add/Update board for the user
:return: JSON object with status and message
"""
try:
userid = get_userid_from_header()
data = request.form
data_dict = data.to_dict()
json_string = next(iter(data_dict.keys()))
board_data_dict = json.loads(json_string)
print("Srj1",json_string)
print("Srj2",type(board_data_dict["board"]))
print("Srj3",userid)
try:
print("Srj4",board_data_dict["board"])
request_data = board_data_dict["board"]

# _ = request_data["boardName"]
except:
return jsonify({"error": "Missing fields in input"}), 400

user = Users.objects(id=userid).first()
if user:
print("Srj5",user.fullName)
user.update(board=board_data_dict["board"])
user.save()
print("Errrrr")
return jsonify(board_data_dict), 200
except:
return jsonify({"error": "Internal server error"}), 500
# return '1',200

@app.route("/applications/<int:application_id>", methods=["PUT"])
def update_application(application_id):
Expand Down Expand Up @@ -368,7 +361,7 @@ def update_application(application_id):

return jsonify(app_to_update), 200
except:
return jsonify({"error": "Internal server error"}), 500
return jsonify({"error": "Internal server error"}), 500

@app.route("/applications/<int:application_id>", methods=["DELETE"])
def delete_application(application_id):
Expand Down Expand Up @@ -470,12 +463,12 @@ def get_resume():
password = info["password"]
app.config["MONGODB_SETTINGS"] = {
"db": "appTracker",
"host": f"mongodb+srv://{username}:{password}@applicationtracker.287am.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
"host": f"mongodb+srv://{username}:{password}@cluster0.en3fo.mongodb.net/todolistDB?retryWrites=true&w=majority",
}
db = MongoEngine()
db.init_app(app)


#TODO change the mappings
class Users(db.Document):
"""
Users class. Holds full name, username, password, as well as applications and resumes
Expand All @@ -488,6 +481,7 @@ class Users(db.Document):
authTokens = db.ListField()
applications = db.ListField()
resume = db.FileField()
board=db.ListField()

def to_json(self):
"""
Expand Down

0 comments on commit 28da679

Please sign in to comment.