def get(self, app_id): try: application_ref = db.collection( collection_names['JOB_APPLICATIONS']).document(app_id) application = application_ref.get().to_dict() score = 0 for username in application['yes']: user_ref = db.collection( collection_names["USERS"]).document(username) user = user_ref.get().to_dict() score += 1 * (user['correct'] / user['reviewed']) for username in application['no']: user_ref = db.collection( collection_names["USERS"]).document(username) user = user_ref.get().to_dict() score -= 1 * (user['correct'] / user['reviewed']) ret_application = application ret_application['score'] = score return ret_application, 200 except: traceback.print_exc() return {"message": "Error fetching application"}
def post(self): data = UserRegister._user_parser.parse_args() if not len(data['username']): return {"message": "The username has an invalid length"}, 400 try: user = db.collection(collection_names['USERS']).document( data['username']).get() except: traceback.print_exc() return {"message": "An error occurred looking up the user"}, 500 if user.exists: return {"message": "A user with that username already exists"}, 400 if not len(data['password']): return {"message": "The password has an invalid length"}, 400 try: db.collection(collection_names['USERS']).document( data['username']).set({ "username": data['username'], "password": data['password'], "first_name": data['first_name'], "last_name": data['last_name'], "user_type": data['user_type'], "correct": 0, "reviewed": 0 }) return {"message": "User created successfully."}, 201 except: traceback.print_exc() return {"message": "An error occurred creating the user"}, 500
def load_udata(uid): doc = db.collection('userdata').document(str(uid)).get() if not doc.exists: try: doc = db.collection('userdata').document(str(uid)).create( init_udata()) except: print('Database creation error') doc = db.collection('userdata').document(str(uid)).get() userdata[str(uid)] = doc.to_dict()
def post(self): data = UserLogin._user_parser.parse_args() try: user_ref = db.collection(collection_names['USERS']).document( data['username']) user = user_ref.get() except: traceback.print_exc() return { "message": "An error occurred trying to look up this user" }, 500 print(user) if user.exists: print("2") user_dict = user.to_dict() if safe_str_cmp(user_dict["password"], data["password"]): access_token = create_access_token( identity=user_dict['username'], fresh=True) refresh_token = create_refresh_token(user_dict['username']) return { "access_token": access_token, "refresh_token": refresh_token }, 200 return {"message": "Invalid Credentials!"}, 401
def save_udata(uid): ref = db.collection('userdata').document(str(uid)) try: ref.set(userdata[str(uid)]) except: print("Database error") return
def init_load(): ref = db.collection('userdata') for doc_ref in ref.get(): try: userdata[str(doc_ref.id)] = doc_ref.to_dict() update_dialog(str(doc_ref.id), 0) except: continue print(userdata)
def get(cls, username): try: user = db.collection( collection_names['USERS']).document(username).get() except: return {"message": "An error occurred looking up the user"}, 500 if user.exists: return user.to_dict(), 200 return {"message": "user not found"}, 404
def post(self): data = PostJob._post_parser.parse_args() data['poster_username'] = get_jwt_identity() try: job_post = db.collection(collection_names["JOB_POSTS"]).document() job_post.set(data) return {"message": "Job posted successfully!"}, 201 except: traceback.print_exc() return {"message": "Error submitting application"}, 500
def get(self): try: job_posts = db.collection(collection_names['JOB_POSTS']).where( "poster_username", "==", get_jwt_identity()).stream() return { "posts": {job_post.id: job_post.to_dict() for job_post in job_posts} }, 200 except: traceback.print_exc() return {"message": "Error fetching postings"}
def post(self): data = SubmitApplication._app_parser.parse_args() data['applicant_username'] = get_jwt_identity() data['yes'] = [] data['no'] = [] try: job_app = db.collection( collection_names["JOB_APPLICATIONS"]).document() job_app.set(data) return {"message": "Application submitted successfully!"}, 201 except: return {"message": "Error submitting application"}, 500
def get(self, job_post_id): try: applicants = db.collection( collection_names['JOB_APPLICATIONS']).where( "job_post_id", "==", job_post_id).stream() # TODO: recalculate the score of the applicant based on users applicants_score = [] for applicant in applicants: application = applicant.to_dict() score = 0 for username in application['yes']: user_ref = db.collection( collection_names["USERS"]).document(username) user = user_ref.get().to_dict() score += 1 * (user['correct'] / user['score']) for username in application['no']: user_ref = db.collection( collection_names["USERS"]).document(username) user = user_ref.get().to_dict() score -= 1 * (user['correct'] / user['score']) application['score'] = score application['id'] = application[score] applicants_score.append(application) return {"applicants": sort(applicants, key=Ket)}, 200 except: traceback.print_exc() return {"message": "Error fetching applicants"}
def post(self, app_id): data = ReviewByRecruiter._review_parser.parse_args() try: application_ref = db.collection( collection_names['JOB_APPLICATIONS']).document(app_id) application_ref.update({"decision": data['decision']}) # TODO: need to update all of the users in that rated that application application = application_ref.get().to_dict() for username in application['yes']: user_ref = db.collection( collection_names["USERS"]).document(username) user = user_ref.get().to_dict() if data['decision'] == 'yes': user['correct'] += 1 user['reviewed'] += 1 user_ref.set(user) for username in application['no']: user_ref = db.collection( collection_names["USERS"]).document(username) user = user_ref.get().to_dict() if data['decision'] == 'no': user['correct'] += 1 user['review'] += 1 user_ref.set(user) return {"message": "Your selection was successful"} except: traceback.print_exc() return {"message": "Error making a decision on application"}
def get(self, job_type): try: job_posts_query = db.collection( collection_names["JOB_POSTS"]).where("job_type", "==", job_type) job_posts = [ dict(post_id=post.id, **post.to_dict()) for post in job_posts_query.stream() ] return {"posts": job_posts}, 200 except: return {"message": "There was an error looking up the job list"}
def delete(cls, username): try: user_ref = db.collection( collection_names['USERS']).document(username) user = user_ref.get() except: return { "message": "An error occurred trying to look up this user" }, 500 if user.exists: try: user_ref.delete() except: return { "message": "An error occurred trying to delete this user" }, 500 return {"message": "User was deleted"}, 200 return {"message": "User not found"}, 404
def get(self): data = ApplicantApplications._req_parser.parse_args() try: user_applications = db.collection( collection_names["JOB_APPLICATIONS"]).where( "applicant_username", "==", get_jwt_identity()) cursor = user_applications.order_by("job_post_id") if data["previous_doc"]: cursor = cursor.start_after(data["previous_doc"]) cursor = cursor.limit(5).stream() return { "applications": [dict(app_id=app.id, **app.to_dict()) for app in cursor] }, 200 except: traceback.print_exc() return {"message": "Error fetching applications"}, 500
def post(self): data = ReviewByApplicant._req_parser.parse_args() try: doc_ref = db.collection( collection_names["JOB_APPLICATIONS"]).document(data['app_id']) new_value = doc_ref.get().to_dict() print(new_value) new_value["yes"].append(get_jwt_identity( )) if data['decision'] else new_value["no"].append( get_jwt_identity()) doc_ref.set(new_value) print(doc_ref.get().to_dict()) return {"message": "Successfully reviewed application"}, 200 except: traceback.print_exc() return {"message": "Failed to review application"}, 500
def get(self): data = ReviewByApplicant._req_parser.parse_args() try: job_app_collection = db.collection( collection_names["JOB_APPLICATIONS"]).order_by("job_post_id") if data["previous_doc"]: job_app_collection = job_app_collection.start_after( data["previous_doc"]) job_app_stream = job_app_collection.limit(1).stream() # I assumed this takes care of null case job_app = [ dict(id=app.id, **app.to_dict()) for app in job_app_stream ] return {"application": job_app}, 200 except: return {"message": "Failed to get applications"}, 500
from db import db from schema import slideshows, analytics, orgs campaigns_coll = db.collection("campaigns") def create_campaign( tags, name, date_from, date_to, location, lat, long, goals, slideshow_data, organisation_id, ): slideshow_id, slideshow = slideshows.create_slideshow( slideshow_data["title"], slideshow_data["description"], slideshow_data["image_urls"], ) analytics_id, analytic = analytics.create_empty_analytics() campaign = { "tags": tags, "name": name, "date_from": date_from, "date_to": date_to, "location": location, "lat": lat,
from google.cloud.firestore import ArrayUnion from time import time from helpers.auth_helper import hash_password from db import db from schema import slideshows org_acc_coll = db.collection("organisation_accounts") org_coll = db.collection("organisation") def create_organisation(password, email, organisation_name, slideshow_data): user_document = org_acc_coll.document(email).get() user_exists = user_document.exists if user_exists: return None slideshow_id, slideshow = slideshows.create_slideshow(**slideshow_data) organisation = { "name": organisation_name, "campaigns": {}, "slideshow_id": slideshow_id, } organisation_doc = org_coll.document() organisation_doc.set(organisation) org_acc = { "password": hash_password(password),
from db import db analytics_coll = db.collection("analytics") def create_empty_analytics(): analytics = { "locations": [], "conversions": [], } analytics_doc = analytics_coll.document() analytics_doc.set(analytics) return analytics_doc.id, analytics
from db import db slideshows_coll = db.collection("slideshow") def create_slideshow(title, description, image_urls): slideshow = { "title": title, "description": description, "image_urls": image_urls, } slideshow_doc = slideshows_coll.document() slideshow_doc.set(slideshow) return slideshow_doc.id, slideshow def get_slideshow(slideshow_id): slideshow_doc = slideshows_coll.document(slideshow_id).get() return slideshow_doc.to_dict()