def __init__(self, id_, username, hashed_password): self.username = username self.hashed_password = hashed_password self.id = id_ self.name = mongo.get_name(id_) self.email = mongo.get_email(id_) self.profile_pic = mongo.get_profile_pic(id_)
def generate_password(): id_ = user.get_id() email = mongoUsr.get_email(id_) username = email.split(".")[1].split("@")[0] password = username hashed_password = bcrypt.generate_password_hash(password).decode('utf-8') mongoUsr.add_login_info(id_, username, hashed_password) print(hashed_password)
def content_detail(bID): global file_id global up_count, down_count global upvote global downvote global book up_count = 0 down_count = 0 file_id = str(bID) book = mongoBook.get_book(file_id) # print(book) # print(type(book)) image_link = book["front"] # image_link = 'https://drive.google.com/thumbnail?authuser=0&sz=w320&id=1ArSB7DUAsUgxppF-Oc99n5BrztO7s-Ti' download_count = book["download"] file_link = 'https://drive.google.com/file/d/' + file_id + '/view?usp=sharing' page_num = book["page_number"] description = book["description"] Author = book["author"] upvote = len(book['upvote']) downvote = len(book["downvote"]) title = book["book_name"] # Display comments comment_content = [] comment_user_name = [] comment_user_profilepic = [] comment_time = [] data = mongoComment.get_all_comment(file_id) print(data) for cursor in data: comment_content.append(cursor['content']) comment_user_profilepic.append( mongoUsr.get_profile_pic(cursor['user_id'])) comment_user_name.append(mongoUsr.get_name(cursor['user_id'])) comment_time.append(cursor['time']) comment_content.reverse() comment_user_name.reverse() comment_time.reverse() comment_user_profilepic.reverse() up_icon = down_icon = '' if id_ in mongoBook.get_up(file_id): up_icon = '/static/images/up_active.png' down_icon = '/static/images/down_disabled.png' elif id_ in mongoBook.get_down(file_id): up_icon = '/static/images/up_disabled.png' down_icon = '/static/images/down_active.png' else: up_icon = '/static/images/up.png' down_icon = '/static/images/down.png' return render_template("content.html", comment_numb=len(comment_content), content=comment_content, time=comment_time, cusername=comment_user_name, cprofile_pic=comment_user_profilepic, display_navbar="inline", title=title, name=first_Name, picture=profile_pic, upvote_count=upvote, downvote_count=downvote, download_count=download_count, Author=Author, file_link=file_link, image_link=image_link, page_num=page_num, description=description, file_id=bID, up_icon = up_icon, down_icon = down_icon)
def index(): if current_user.is_authenticated: # form = Password() id_ = user.get_id() name = mongoUsr.get_name(id_) email = mongoUsr.get_email(id_) profile_pic = mongoUsr.get_profile_pic(id_) first_Name = name.split(' ', 1)[0] if mongoAdmin.is_admin(id_): role = 'admin' else: role = 'member' print("Logged in") return render_template('profile.html', name=first_Name, email=email, picture=profile_pic, role = role, display_navbar="inline") else: print("Not logged in") return render_template("login.html", text="Login", display_noti="none", display_navbar="none", name="SIGN UP NOW!")
def get(user_id): usr = mongo.get(user_id) if not usr: return None usr = user_info(id_=usr['_id'], name=usr['Fullname'], email=usr['Email'], profile_pic=usr['Profile_pic']) return usr
def login(): # Find out what URL to hit for Google login if request.method == "POST": username = request.form["username"] password = request.form["password"] # hashed_password = bcrypt.generate_password_hash(password).decode('utf-8') # print(hashed_password) usr_checked = mongoUsr.login(bcrypt, username, password) if usr_checked: # @login_manager.user_loader global user global first_Name global profile_pic global id_ id_ = mongoUsr.get_id(usr_checked.username) user = logUsr.user_info.get(id_) name = mongoUsr.get_name(id_) first_Name = name.split(' ', 1)[0] profile_pic = mongoUsr.get_profile_pic(id_) login_user(user) try: return redirect(last_url[-1]) except: return redirect(url_for("index")) else: print("login failed") return redirect(url_for("index")) elif request.method == 'GET': google_provider_cfg = get_google_provider_cfg() authorization_endpoint = google_provider_cfg["authorization_endpoint"] # Use library to construct the request for login and provide # scopes that let you retrieve user's profile from Google request_uri = client.prepare_request_uri( authorization_endpoint, redirect_uri=request.base_url + "/callback", scope=["openid", "email", "profile"], ) print(request_uri) return redirect(request_uri)
def admin(): materials = mongoAdmin.total_materials() users = mongoAdmin.total_users() online = mongoAdmin.total_online() online_list = [] offline_list = [] offline_last = [] users_id = mongoAdmin.get_all_id() for ele in users_id: name = mongoUsr.get_name(ele) if mongoAdmin.is_online(ele) == 'Active': online_list.append(name) else: offline_list.append(name) offline_last.append(mongoAdmin.is_online(ele)) online_list.sort() return render_template("admin.html", display_navbar="none", name=first_Name, users = users, materials = materials, online = online, online_list = online_list, offline_list = offline_list, len_online = len(online_list), len_offline = len(offline_list), offline_last = offline_last)
def load_user(user_id): print('loaded') # Maintain 'active' status mongoUsr.set_last_active(user_id) return logUsr.user_info.get(user_id)
def callback(): # Get authorization code Google sent back to you code = request.args.get("code") # Find out what URL to hit to get tokens that allow you to ask for # things on behalf of a user google_provider_cfg = get_google_provider_cfg() token_endpoint = google_provider_cfg["token_endpoint"] # Prepare and send request to get tokens! Yay tokens! token_url, headers, body = client.prepare_token_request( token_endpoint, authorization_response=request.url, redirect_url=request.base_url, code=code, ) token_response = requests.post( token_url, headers=headers, data=body, auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET), ) # Parse the tokens! client.parse_request_body_response(json.dumps(token_response.json())) userinfo_endpoint = google_provider_cfg["userinfo_endpoint"] uri, headers, body = client.add_token(userinfo_endpoint) userinfo_response = requests.get(uri, headers=headers, data=body) if userinfo_response.json().get("email_verified"): unique_id = userinfo_response.json()["sub"] users_email = userinfo_response.json()["email"] picture = userinfo_response.json()["picture"] users_name = userinfo_response.json()["name"] if mongoUsr.is_USTHer(users_email): # Add user information to Online database global user user = logUsr.user_info( id_=unique_id, name=users_name, email=users_email, profile_pic=picture ) global profile_pic global first_Name id_ = user.getid() name = user.getName() email = user.getEmail() profile_pic = user.getprofile_pic() student_id = get_studentid(email) first_Name = name.split(' ', 1)[0] if not mongoUsr.account_existed(id_): mongoUsr.register(id_, name, email, student_id, profile_pic) generate_password() print('Generated login info!') gmail.send(email, get_studentid(email), first_Name) login_user(user) # Create session timeout time = timedelta(minutes=60) # User will automagically kicked from session after 'time' app.permanent_session_lifetime = time return redirect(url_for('index')) else: return redirect(url_for('loginfail'))
def verify(self): mdict = mongo.login(self.username, self.hashed_password) if mdict: return True return False
def create(id_, name, email, profile_pic): mongo.register(id_, name, email, profile_pic)
def get_online(): User.get_online()