def loginnormal(): config = web.get_config() if request.method == 'POST': user_email = request.form["user[email]"] user_password = request.form["user[pwd]"] manager = UserManager(web.get_db()) if manager.checkconfirm(user_email) is True: login_user = manager.login_user(user_email, user_password, 'standard') if login_user is not None: id = login_user.get_id() name = img(web.get_db()).all_name() count = img(web.get_db()).all_count() web.login_user(load_user(id)) session['u_email'] = user_email return redirect('/user') else: return render_template("/inforPage.html", infor="Sorry, login failed, please check your Email or password and try again!", **dict(gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY'])) else: send = send_confirm_email token = generate_confirmation_token(user_email) confirm_url = url_for('confirm_email', token=token, _external=True) send(user_email, confirm_url) return render_template("/inforPage.html", infor="Sorry Please Confirm by email! Again. Email Send Again", **dict(gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']))
def send_forgot_email(email): sg = sendgrid.SendGridAPIClient( apikey=web.get_config()['SENDGRID_API_KEY']) if UserManager.getcodebyEmail(email) is not None: code = UserManager.getcodebyEmail(email) token = url_for('.forgotpassword', code=code) data = { "personalizations": [{ "to": [{ "email": email }], "subject": "welcome to sep,confirm your account" }], "from": { "email": "*****@*****.**" }, "content": [{ "type": "text/HTML", "value": token }] } response = sg.client.mail.send.post(request_body=data) print(response.status_code) print(response.body) print(response.headers) else: return None
def send_message(self, user_from, user_to, content): """ Send a message to another user. Creates a new message, and puts it in both users' inboxes :param user_from: the sender :param user_to: the recipent :param content: the content of the message :return: """ with self.db.session_scope() as session: # translate users if needed if isinstance(user_from, int): user_from = UserManager.user_by_id(user_from, session) if isinstance(user_to, int): user_to = UserManager.user_by_id(user_to, session) # create the message message = schema.PrivateMessage(owner=user_from, content=content) # add to the inboxes from_inbox = schema.MessageInbox(r_owner=user_from, r_partner=user_to, message=message, opened=datetime.datetime.now()) to_inbox = schema.MessageInbox(r_owner=user_to, r_partner=user_from, message=message) # commit session.add(message) session.add(from_inbox) session.add(to_inbox)
def forgotpwd(): config = web.get_config() if request.method == 'POST': useremail = request.form["user_email"] newpassword = request.form["user[password_forgot]"] manager = UserManager(web.get_db()) manager.changePasswordBynew(useremail, newpassword) return render_template("/inforPage.html", infor="Password change successfully, please login again.", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']), is_logged_in=web.is_logged_in(), user_email=session['u_email']) else: return render_template("/inforPage.html", infor="Password change failed.", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']), is_logged_in=web.is_logged_in(), user_email=session['u_email'])
def changpassword(): if request.method == 'POST': config = web.get_config() useremail = request.form["change[email]"] oldpassword = request.form["change[oldpassword]"] newpassword = request.form["change[newpassword]"] manager = UserManager(web.get_db()) if manager.changePassword(useremail, oldpassword, newpassword): # should use log out here? return render_template("/inforPage.html", infor="Change password successfully, please login again", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY'])) else: return render_template("/inforPage.html", infor="Oops! Failed to change password, please check!", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']))
def sendemailforgotpassword(): if request.method == 'POST': config = web.get_config() useremail = request.form["user[email_forgot]"] manager = UserManager(web.get_db()) if manager.isUserbyEmail(useremail): send = send_confirm_email token = generate_confirmation_token(useremail) confirm_url = url_for('forgot_confrimurl', token=token, _external=True) send(useremail, confirm_url) return render_template("/inforPage.html", infor="Email sent successfully.", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY'])) else: return render_template("/inforPage.html", infor="The email does not exist, please check your email" , **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']) )
def update_user_profile(): config = web.get_config() if request.method == 'POST': u_email = session['u_email'] new_firstname = request.form.get("new_fristname") new_lastname = request.form.get("new_lastname") new_genre = request.form.get("new_genre") password = request.form.get("password") new_password = request.form.get("new_password") print(new_lastname,new_firstname,new_genre) if UserManager(web.get_db()).update_profile(u_email, new_firstname, new_lastname, new_genre, password, new_password): return render_template("/inforPage.html", infor="Your profile was updated successfully!", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']), is_logged_in=web.is_logged_in(), user_email=session['u_email']) else: return render_template("/inforPage.html", infor="Failed to update your profile, please check and update again.", **dict( gauth_key=config['GAUTH_KEY'], fb_key=config['FB_KEY']), is_logged_in=web.is_logged_in(), user_email=session['u_email'])
def searchplaylist(): if request.method == 'POST': keywords = request.form["keywords"] allTrack = PlaylistManager( web.get_db()).api_getTrackByKeywords(keywords) if allTrack == False: return render_template( '/admin/searchplaylist.html', infor_message="There are no any results, Please input Again!") else: tracklist = {} track = {} user = {} playlist = {} i = 0 alltracklist = {} for track_data in allTrack: i = i + 1 track_author = track_data.t_author track_playlistid = track_data.l_id track_title = track_data.t_title track['TrackTitle'] = track_title track['TrackAuthor'] = track_author playlist_data = PlaylistManager( web.get_db()).api_getPlaylistByID(track_playlistid) userID = playlist_data.u_id playlist['PlaylistName'] = playlist_data.l_name user_detail = UserManager( web.get_db()).api_get_userDetail(userID) user['Gender'] = user_detail.u_gender user['Age'] = user_detail.u_age user['Genger'] = user_detail.u_genre tracklist["Track"] = track tracklist['Playlist'] = playlist tracklist['User'] = user alltracklist[i] = tracklist return render_template('/admin/searchplaylist.html', alltracklist=alltracklist, infor_message=" ") return render_template('/admin/searchplaylist.html')
def login(): should_redirect = False if web.get_current_user() is None: should_redirect = True manager = UserManager(web.get_db()) return dict(redirect='/user' if should_redirect else None, status=manager.api_login_user(request.json))