def test_login(self): user = DatabaseHandler.get_instance().get_user(self.test_uid) user.confirmed = True DatabaseHandler.get_instance().store_user(user) response = json.loads(login(self.app, self.test_uid, self.test_password).get_data(as_text=True)) assert response["data"]
def delete_all_announcements(): i = input("Are you sure you want to DELETE ALL ANNOUNCEMENTS?(y/n)") i2 = input("Are you really sure? This shouldn't be done unless you are ABSOLUTELY SURE!(y/n)") if i == "y" and i2 == "y": for user in DatabaseHandler.get_instance().get_users(): user.announcements = [] DatabaseHandler.get_instance().store_user(user) else: print("Cancelled Deletion Of All Announcements.")
def new_announcement_review(self, teacher_email, announcement): teacher = DatabaseHandler.get_instance().get_user(teacher_email) student = DatabaseHandler.get_instance().get_user( announcement.user_name) review = Review(teacher, student, announcement) self.unconfirmed_reviews.append(review) print(self.unconfirmed_reviews) return review
def add_student_to_teacher(self): student_name = self.request_data["student_name"] teacher = DatabaseHandler.get_instance().get_user(session.get("uid")) # email or name?@!?!?!??!?!?!?!? if Helper.is_user_admin(teacher) and student_name not in teacher.students and DatabaseHandler.get_instance().user_exists(student_name): teacher.students.append(student_name) DatabaseHandler.get_instance().store_user(teacher) return jsonify(data=True) return jsonify(data=False)
def change_user_permission_level(self): user = DatabaseHandler.get_instance().get_user(self.request_data["email"]) permission_level = self.request_data["permission_level"] if Helper.is_user_admin(DatabaseHandler.get_instance().get_user(session.get("uid"))): user.permission_level = int(permission_level) DatabaseHandler.get_instance().store_user(user) return jsonify(data=True, status="Success") return jsonify(data=False, status="Failed to change user permission level. User attempting to change " "permission may not have sufficient privileges.")
def students(): students = [] for student in DatabaseHandler.get_instance().get_user( session.get("uid")).students: student = DatabaseHandler.get_instance().get_user(student) students.append( StudentTemplate(student.uid, student.permission_level, student.firstname + " " + student.lastname).get_markup()) return render_template("students.html", students=students)
def validate_review(self, review_id): copy = [] for review in self.unconfirmed_reviews: copy.append(review) for review in copy: if review.id == review_id: review.student._announcements.append(review.announcement) DatabaseHandler.get_instance().store_user(review.student) return True return False
def announcement(): teacher_uids = [] for user in DatabaseHandler.get_instance().get_users(): if Helper.is_user_admin(user) or type(user) is Teacher: teacher_uids.append(user.uid) trusted_post = DatabaseHandler.get_instance().get_user( session.get("uid") ).permission_level >= User.get_trusted_student_permission_level() announcement_form = AnnouncementForm( trusted_post, len(DatabaseHandler.get_instance().get_user( session.get("uid"))._announcements) + 1, teacher_uids) return render_template("post_announcement.html", announcement_form=announcement_form.get_markup())
def add_student(): student_uids = [] for user in DatabaseHandler.get_instance().get_users(): if user.permission_level < user.get_teacher_permisison_level(): student_uids.append(user.uid) return render_template("add_student.html", student_uids=student_uids)
def wrapper(*args, **kwargs): database = DatabaseHandler.get_instance() # Continue running the function, otherwise return an error return request_function(*args, **kwargs) if session.get("uid") is not None and database.get_user( session.get("uid")).confirmed \ else jsonify(data=False, status="Failed to log in.")
def wrapper(*args, **kwargs): if session.get("uid") is None: return user = DatabaseHandler.get_instance().get_user(session.get("uid")) if user.permission_level >= user.get_teacher_permisison_level(): return nav_function(*args, **kwargs) return None
def get_students(self): users = DatabaseHandler.get_instance().get_users() students = [] for user in users: if not Helper.is_user_admin(user): students.append(user.uid) return jsonify(data=students)
def dashboard(): announcements = [] db_announcements = DatabaseHandler.get_instance().get_dashboard() for announcement in db_announcements: announcements.append(DashboardAnnouncement(announcement).get_markup()) return render_template("dashboard.html", announcements=announcements)
def test_database(self): database = DatabaseHandler.get_instance() database.store_user(User(self.test_uid, request_handler.Helper.hash_password(self.test_password))) assert database.get_user(self.test_uid) is not None, b"Couldn't retrieve user from database!" user_names = [] for user in database.get_users(): user_names.append(user.uid) assert self.test_uid in user_names, b"Retrieving all users does not work!"
def validate_confirmation(self, confirmation_id): copy = {} for key in self.unconfirmed_users.keys(): copy[key] = self.unconfirmed_users[key] for user in copy.keys(): confirmation = copy[user] if confirmation.id == confirmation_id and confirmation.is_valid(): user.confirmed = True DatabaseHandler.get_instance().store_user(user) del self.unconfirmed_users[user] return True elif not confirmation.is_valid(): DatabaseHandler.get_instance().remove_user(user.uid) del self.unconfirmed_users[user] return False return False
def is_user_data_valid(email, password): database = DatabaseHandler.get_instance() if not database.user_exists(email) and len(password) >= 8 and "@pdsb.net" in email: return "Successfully signed up %s" % email elif len(password) < 8: return "Failed to sign up! Password has to be >= 8 characters in length" elif not "@pdsb.net" in email: return "Failed to signup! You must use your @pdsb.net email!" else: return "Failed to sign up! User %s already exists!" % email
def __init__(self): # This is why this class needs an instance, # The app crashes if this dict is static '''self.functions = {"user_exists": self.database.user_exists, "get_dashboard_data": self.dashboard, "get_new_announcement_id": self.get_new_announcement_id, "get_announcements_for_user": self.get_announcements_for_user, "delete_announcement": self.delete_announcement}''' self.request = {} self.request_data = {} self.request_function = "" self.database = DatabaseHandler.get_instance()
def user_announcements(): user_announcements = DatabaseHandler.get_instance().get_user( session.get("uid"))._announcements # Sort in chronological order(newest on top, oldest on bottom) user_announcements.sort(key=lambda x: x.time_stamp) # Have to reverse it for it to work user_announcements.reverse() editable_announcements = [] for announcement in user_announcements: editable_announcements.append( EditableAnnouncement(announcement).get_markup()) return render_template("user_announcements.html", user_announcements=editable_announcements)
def remove_student_from_teacher(self): student_name = self.request_data["student_name"] teacher = DatabaseHandler.get_instance().get_user(session.get("uid")) # email or name?@!?!?!??!?!?!?!?# email or name?@!?!?!??!?!?!?!?# email or name?@!?!?!??!?!?!?!?# email or name?@!?!?!??!?!?!?!?# email or name?@!?!?!??!?!?!?!? if Helper.is_user_admin(teacher) and student_name in teacher.students: student = DatabaseHandler.get_instance().get_user(student_name) student.permission_level = 0 DatabaseHandler.get_instance().store_user(student) teacher.students.remove(student_name) DatabaseHandler.get_instance().store_user(teacher) return jsonify(data=True) return jsonify(data=False)
def profile(): user = DatabaseHandler.get_instance().get_user(session.get("uid")) image_url = "" if os.path.isfile( os.path.join(os.path.dirname(__file__), "static", "data", "profile_images", user.uid + ".jpg")): image_url = url_for("static", filename="data/profile_images/" + user.uid + ".jpg") print(image_url) grade = user.grade if isinstance(user, Student) else "" return render_template("profile.html", uid=session.get("uid"), firstname=user.firstname, lastname=user.lastname, grade=grade, image_url=image_url)
def get_teacher_students(self): teacher = DatabaseHandler.get_instance().get_user(self.request_data["email"]) print(teacher.__dict__["students"]) return jsonify(data=teacher.__dict__["students"])
def is_user_admin(self): return jsonify(data=Helper.is_user_admin(DatabaseHandler.get_instance().get_user(self.request_data["email"])))
def is_user_auth_for_post_review(self): return jsonify(data=Helper.is_user_auth_for_post_review( DatabaseHandler.get_instance().get_user(self.request_data["email"])))
def get_user_permission_level(self): return jsonify(data=DatabaseHandler.get_instance().get_user(self.request_data["email"]).permission_level)
def test_signup(self): # b in front of string to turn it into a bytes object response = json.loads(signup(self.app, self.test_uid, self.test_password).get_data(as_text=True)) assert (response["data"] and not DatabaseHandler.get_instance().user_exists(self.test_uid)) \ or (not response["data"] and DatabaseHandler.get_instance().user_exists(self.test_uid))
def clear_database(): DatabaseHandler.get_instance().raw_data = {} DatabaseHandler.get_instance().write()