def create_or_get_tag(tagname): tag = db.query(Tag).filter(Tag.tag == tagname).first() if not tag: tag = Tag() tag.tag = tagname db.add(tag) db.commit() return tag
def reset_password(self): c.page = 'pw reset' c.key = request.params.get('key') c.redirect_url = url(controller='account', action='login') if request.environ['REQUEST_METHOD'] == 'GET': if not c.key: return render('/pw_reset/request.mako') else: reset_token = self._check_valid_pw_reset_key(c.key) if not reset_token: h.add_message('Invalid reset token', 'error') return render('/blank.mako') return render('/pw_reset/set.mako') elif request.environ['REQUEST_METHOD'] == 'POST': if not c.key: # create a password request key email = request.params['email'] user = db.query(User).filter(User.email == email).first() if not user: h.add_message('Invalid email address provided.', 'error') return render('/pw_reset/request.mako') already_requested = db.query(PasswordResets).filter(PasswordResets.user_id == user.id).first() if already_requested: if already_requested.created < now() - datetime.timedelta(hours=2): db.delete(already_requested) else: h.add_message('A password reset has already been requested for this user.', 'error') return render('/blank.mako') pw_reset_key = PasswordResets() pw_reset_key.user_id = user.id pw_reset_key.key = self._generate_pw_reset_key() db.add(pw_reset_key) db.commit() send_reset_password_email(user.email, pw_reset_key.key) h.add_message('Password reset email sent!', 'success') return render('/blank.mako') else: # reset the user's password to what they've submitted reset_token = self._check_valid_pw_reset_key(c.key) if not reset_token: h.add_message('Invalid reset token', 'error') return render('/blank.mako') password = request.params['password'] password_confirm = request.params['password_confirm'] valid_password = validate_password(password, password_confirm) if not valid_password['status']: h.add_message(valid_password['msg'], 'error') return render('/pw_reset/set.mako') user = db.query(User).filter(User.id == reset_token.user_id).first() hashed_pass = h.hash_password(password) user.password = hashed_pass db.delete(reset_token) db.commit() h.add_message('Password successfully set. You should now be able to login.', 'success') return render('/blank.mako')
def create_quote(quote_body, notes, tags): newquote = Quote() newquote.body = quote_body newquote.notes = notes newquote.tags = [h.create_or_get_tag(tag) for tag in tags] newquote.submitted_by = c.user db.add(newquote) db.commit() return True
def create_user(username, password, email): conflicts = db.query(User).filter(or_(User.email == email, User.username == username)).first() if conflicts: if conflicts.email == email: raise NameError('Sorry! That email already exists in the system.') elif conflicts.username == username: raise NameError('Sorry! That username is already taken.') hashed_pass = h.hash_password(password) new_user = User() new_user.username = username new_user.password = hashed_pass new_user.email = email db.add(new_user) db.commit() return True
def create_user(username, password, email): conflicts = db.query(User).filter( or_(User.email == email, User.username == username)).first() if conflicts: if conflicts.email == email: raise NameError('Sorry! That email already exists in the system.') elif conflicts.username == username: raise NameError('Sorry! That username is already taken.') hashed_pass = h.hash_password(password) new_user = User() new_user.username = username new_user.password = hashed_pass new_user.email = email db.add(new_user) db.commit() return True
def reset_password(self): c.page = 'pw reset' c.key = request.params.get('key') c.redirect_url = url(controller='account', action='login') if request.environ['REQUEST_METHOD'] == 'GET': if not c.key: return render('/pw_reset/request.mako') else: reset_token = self._check_valid_pw_reset_key(c.key) if not reset_token: h.add_message('Invalid reset token', 'error') return render('/blank.mako') return render('/pw_reset/set.mako') elif request.environ['REQUEST_METHOD'] == 'POST': if not c.key: # create a password request key email = request.params['email'] user = db.query(User).filter(User.email == email).first() if not user: h.add_message('Invalid email address provided.', 'error') return render('/pw_reset/request.mako') already_requested = db.query(PasswordResets).filter( PasswordResets.user_id == user.id).first() if already_requested: if already_requested.created < now() - datetime.timedelta( hours=2): db.delete(already_requested) else: h.add_message( 'A password reset has already been requested for this user.', 'error') return render('/blank.mako') pw_reset_key = PasswordResets() pw_reset_key.user_id = user.id pw_reset_key.key = self._generate_pw_reset_key() db.add(pw_reset_key) db.commit() send_reset_password_email(user.email, pw_reset_key.key) h.add_message('Password reset email sent!', 'success') return render('/blank.mako') else: # reset the user's password to what they've submitted reset_token = self._check_valid_pw_reset_key(c.key) if not reset_token: h.add_message('Invalid reset token', 'error') return render('/blank.mako') password = request.params['password'] password_confirm = request.params['password_confirm'] valid_password = validate_password(password, password_confirm) if not valid_password['status']: h.add_message(valid_password['msg'], 'error') return render('/pw_reset/set.mako') user = db.query(User).filter( User.id == reset_token.user_id).first() hashed_pass = h.hash_password(password) user.password = hashed_pass db.delete(reset_token) db.commit() h.add_message( 'Password successfully set. You should now be able to login.', 'success') return render('/blank.mako')