def notification(): if "userId" in session: while True: #checking for post update_self status_self = profiledb.find_one({"_id": ObjectId(session["userId"])}) if status_self["post_update"] == True: profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": {"post_update": False}}) for i in postsdb.find({"userId": session["userId"]}).sort("_id", -1): user = profiledb.find_one({"_id": ObjectId(i["userId"])}) return jsonify({"type": "posts_self", "payload": [{ "_id": str(i["_id"]), "username": user["username"], "role": user["role"], "body": i["body"], "date": i["date"], "pfpURL": user["pfpURL"] }]}) #checking for post update_wall status_wall = updatedb.find_one() if status_wall["postId"] != session["wall_update"]: profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": {"post_update": False}}) session["wall_update"] = status_wall["postId"] for i in postsdb.find().sort("_id", -1): user = profiledb.find_one({"_id": ObjectId(i["userId"])}) return jsonify({"type": "posts_wall", "payload": [{ "_id": str(i["_id"]), "username": user["username"], "role": user["role"], "body": i["body"], "date": i["date"], "pfpURL": user["pfpURL"] }]}) #checking for task updates status_task = profiledb.find_one({"_id": ObjectId(session["userId"])}) if status_task["task_update"] == True: profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": {"task_update": False}}) user = profiledb.find_one({"_id": ObjectId(session["userId"])}) tasks = user["tasks"] task = tasks[-1] return jsonify({"type": "tasks", "payload": [{ "title": task["title"], "pfpURL": user["pfpURL"], "type": task["type"] }]}) #checking for classe updates #--------------------------- #checking for notification updates if profiledb.find_one({"_id": ObjectId(session["userId"])})["notification_update"] == True: profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": {"notification_update": False}}) user = profiledb.find_one({"_id": ObjectId(session["userId"])}) notifications = user["notifications"] notification = notifications[-1] return jsonify({"type": "notifications", "payload": [notification]})
def admin_class(): if profiledb.find_one({"_id": ObjectId(session["userId"])})["role"] == "admin": query = request.form api_key = '46838544' api_secret = 'c727ac0672a3ae6c1490b68e4032d8e982531450' opentok = OpenTok(api_key, api_secret) video_session = opentok.create_session() classdb.insert_one({ "title": query["title"], "body": query["body"], "session_id": video_session.session_id }) return redirect(url_for('profile', id=profiledb.find_one({"role": "admin"})["_id"])) """Only one endpoint for long polling"""
def admin_tasks(): if profiledb.find_one({"_id": ObjectId(session["userId"])})["role"] == "admin": query = request.form user = profiledb.update({"_id": ObjectId(query["_id"])}, {'$push': {"tasks": {"title": query["title"], "description": query[ "description"], "type": query["type"], "pfpURL": profiledb.find_one( {"_id": ObjectId(query["_id"])})[ "pfpURL"] }}}) profiledb.update_one({"_id": ObjectId(query["_id"])}, {"$push": {"notifications": { "sender": "Administrator", "action": "created a task for", "object": "You" }}}) profiledb.update_one({"_id": ObjectId(query["_id"])}, {"$set": {"task_update": True}}) profiledb.update_one({"_id": ObjectId(query["_id"])}, {"$set": {"notification_update": True}}) return redirect(url_for('profile', id=profiledb.find_one({"role": "admin"})["_id"]))
def admin_get(id): if profiledb.find_one({"_id": ObjectId(id)})["role"] == "admin": list = [] for i in profiledb.find(): list.append({ "_id": str(i["_id"]), "username": i["username"], "role": i["role"], "date": "", "body": str(i["_id"]), "pfpURL": i["pfpURL"] }) return jsonify(list)
def wall_get(): if "userId" in session: profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": {"post_update": False}}) posts = [] for i in postsdb.find().sort("_id", 1): user = profiledb.find_one({"_id": ObjectId(i["userId"])}) posts.append({ "_id": str(i["_id"]), "username": user["username"], "role": user["role"], "body": i["body"], "date": i["date"], "pfpURL": user["pfpURL"] }) return jsonify(posts)
def login(): if request.method == "POST": session.clear() form = request.form data = profiledb.find_one({"username": form["username"]}) if data and bcrypt.check_password_hash(data["password"], form["password"]): session.clear() update = updatedb.find_one() updatedb.update_one({"_id": update["_id"]}, {"$set": {"postId": None}}) session["userId"] = str(data["_id"]) session["wall_update"] = "" profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": {"post_update": False}}) return redirect(url_for('profile', id=session["userId"])) else: return render_template('login.html', msg="Username or Password incorrect") return render_template("login.html", msg="")
def login(): if request.method == "POST": session.clear() form = request.form data = profiledb.find_one({"_id": ObjectId(form["_id"])}) if data and flask_app.bcrypt.check_password_hash( data["password"], form["password"]): session.clear() session["userId"] = str(data["_id"]) session["wall_update"] = "" profiledb.update_one({"_id": ObjectId(session["userId"])}, {"$set": { "post_update": False }}) return redirect(url_for('profile')) else: return jsonify(401) return render_template("login.html")
def profile_get(id): if "userId" in session: user = profiledb.find_one({"_id": ObjectId(id)}) posts = [] for i in postsdb.find({"userId": id}).sort("_id", 1): posts.append({ "_id": str(i["_id"]), "username": user["username"], "role": user["role"], "body": i["body"], "date": i["date"], "pfpURL": user["pfpURL"] }) return jsonify({ "_id": id, "pfpURL": user["pfpURL"], "username": user["username"], "role": user["role"], "meals": user["meals"], "followers": user["followers"], "following": user["following"], "posts": posts, #"notifications": user["notifications"] })
def taskCreate(id): if "userId" in session: if request.method == "POST": profiledb.update_one({"_id": ObjectId(session["userId"])}, { "$set": {"task_update": True}}) query = request.form user = profiledb.update({"_id": ObjectId(session["userId"])}, {'$push': {"tasks": {"title": query["title"], "description": query["description"], "type": query["type"], "pfpURL": profiledb.find_one({"_id": ObjectId(id)})["pfpURL"] }}}) return redirect(url_for('profile', id=session["userId"])) else: return render_template('taskcreate.html')
def on_goUser(): username = request.get_json() if profiledb.find_one({"username": username["username"]}): return jsonify(True) else: return jsonify(False)
def profile(id): if "userId" in session: if profiledb.find_one({"_id": ObjectId(session["userId"])})["role"] == "admin": return render_template('adminProfile.html', id=id) else: return render_template('profileTemplate.html', id=id)
def __init__(self, _id): if (_id is not None) and (not plannerdb.find_one({"_id": ObjectId(_id)})) and (not profiledb.find_one({"_id": ObjectId(_id)})): raise SystemError(error) else: self._id = _id class Task: def __init__(self): class activityTask: def create(self, title, description, activity_id, blocks, append=False): planner_id = _id if planner_id is None: raise SystemError(error) if not set(blocks).isdisjoint(set(tasks_listdb.find_one({"_id": ObjectId(_id)})["blocks"])): if not append: return error task = tasksdb.insert_one({ "title": title, "description": description, "type": "activity", "activity_id": activity_id, "status": "pending", "score": 0, "timestamp": time.time() }) tasks_listdb.update_one({"point_id": ObjectId(_id)}, {"$push": {"tasks": str(task.inserted_id)}}) return return_code else: task = tasksdb.insert_one({ "title": title, "description": description, "type": "activity", "activity_id": activity_id, "status": "pending", "score": 0, "timestamp": time.time() }) tasks_listdb.insert_one({ "point_id": _id, "tasks": [str(task.inserted_id)], "blocks": blocks, "status": "pending", "score": 0, "timestamp": time.time() }) for i in blocks: plannerdb.update_one({"_id": ObjectId(_id)}, {"$set": {""}}) return return_code class classTask: def create(self, title, description, class_id, blocks, append=False): planner_id = _id if planner_id is None: raise SystemError(error) if not set(blocks).isdisjoint(set(tasks_listdb.find_one({"_id": ObjectId(_id)})["blocks"])): if not append: return error task = tasksdb.insert_one({ "title": title, "description": description, "type": "class", "class_id": class_id, "status": "pending", "score": 0, "timestamp": time.time() }) tasks_listdb.update_one({"point_id": ObjectId(_id)}, {"$push": {"tasks": str(task.inserted_id)}}) return return_code else: task = tasksdb.insert_one({ "title": title, "description": description, "type": "class", "activity_id": class_id, "status": "pending", "score": 0, "timestamp": time.time() }) tasks_listdb.insert_one({ "point_id": _id, "tasks": [str(task.inserted_id)], "blocks": blocks, "status": "pending", "score": 0, "timestamp": time.time() }) return return_code self.activityTask = activityTask self.classTask = classTask def create(self, title, description, blocks, append=False): planner_id = _id if planner_id is None: raise SystemError(error) if not set(blocks).isdisjoint(set(tasks_listdb.find_one({"_id": ObjectId(_id)})["blocks"])): if not append: return error task = tasksdb.insert_one({ "title": title, "description": description, "type": "miscellaneous", "status": "pending", "score": 0, "timestamp": time.time() }) tasks_listdb.update_one({"point_id": ObjectId(_id)}, {"$push": {"tasks": str(task.inserted_id)}}) return return_code else: task = tasksdb.insert_one({ "title": title, "description": description, "type": "miscellaneous", "status": "pending", "score": 0, "timestamp": time.time() }) tasks_listdb.insert_one({ "point_id": _id, "tasks": [str(task.inserted_id)], "blocks": blocks, "status": "pending", "score": 0, "timestamp": time.time() }) return return_code def update(self, _id): if tasksdb.find_one({"_id": ObjectId(_id)}): tasksdb.update_one({"_id": ObjectId(_id)}, {"$set": {"status": "completed"}}) else: return error def remove(self, task_id): if tasksdb.find_one({"_id": ObjectId(task_id)}): tasksdb.delete_one({"_id": ObjectId(task_id)}) tasks_listdb.update({"tasks": task_id}, {"$pull": {"tasks": task_id}}) tasks_listdb.delete_many({"tasks": []}) self.Task = Task class get: def __init__(self): planner_id = _id class week: def get(self, index): week = plannerdb.find_one({"_id": ObjectId(planner_id)})["planner"][index] return week self.week = week class day: def get(self, index): week_index = math.floor(index/7) relative_day_index = abs(7*math.floor(index/7) - index) planner = plannerdb.find_one({"_id": ObjectId(planner_id)})["planner"][week_index][relative_day_index] """code to display planner""" return planner self.day = day self.get = get