Skip to content

Instantly share code, notes, and snippets.

@Porter97
Created February 12, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Porter97/198794a1f98e2456739ef6a614a2696f to your computer and use it in GitHub Desktop.
Save Porter97/198794a1f98e2456739ef6a614a2696f to your computer and use it in GitHub Desktop.
#...
import stream
#...
class User(db.Model, UserMixin):
#...
def confirm(self, token):
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token.encode('utf-8'))
except:
return False
if data.get('confirm') != self.id:
return False
try:
# Attempt to add user to Stream
client = stream.connect(current_app.config["STREAM_API_KEY"], current_app.config['STREAM_SECRET'])
client.users.add(str(self.id), {"username": self.username, "gravatar": self.gravatar(size=40)})
except:
# If attempt is unsuccessful, return False
return False
self.confirmed = True
db.session.add(self)
return True
#...
def change_email(self, token):
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token.encode('utf-8'))
except:
return False
if data.get('change_email') != self.id:
return False
new_email = data.get('new_email')
if new_email is None:
return False
if self.query.filter_by(email=new_email).first() is not None:
return False
self.email = new_email
self.avatar_hash = self.gravatar_hash()
db.session.add(self)
try:
# Attempt to add user to Stream
client = stream.connect(current_app.config["STREAM_API_KEY"], current_app.config['STREAM_SECRET'])
client.users.update(str(self.id), {"username": self.username, "gravatar": self.gravatar(size=40)})
except:
# If attempt is unsuccessful, return False
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment