def post(self): data = request.get_json() mail = data["mail"] username = data["username"] password = data["password"] user = UserModel.find_by_mail(mail) if user: if user.confermato == True: return "mail already taken", 400 epsw = password.encode('utf-8') hashed_password = hashlib.sha512(epsw).hexdigest() user.password = hashed_password user.username = username user.save_to_db() return "user modified" now = datetime.datetime.now() epsw = password.encode('utf-8') hashed_password = hashlib.sha512(epsw).hexdigest() user = UserModel(None, mail, username, None, None, None, 0, False, 0) user.password = hashed_password user.save_to_db() s = URLSafeTimedSerializer("password1") token = s.dumps(mail, salt="emailconfirm") #link="http://127.0.0.1:5000/confirm/"+token link = "https://seconda.herokuapp.com/confirm/" + token subject = "Conferma la tua mail su WaitingList" text = """ Ciao {}! Grazie per esserti registrato. Clicca il link qui sotto per completare la registrazione. {} Se non hai richiesto un account non preoccuparti, qualcuno si sara' confuso. Saluti, il Team WaitingList """.format(username, link) message = 'Subject: {}\n\n{}'.format(subject, text) server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login("*****@*****.**", "smartmates1") server.sendmail("smartmates2018gmail.com", mail, message) return {"message": "user created, to be confirmed"}, 200
def post(self) -> dict[str, Union[list[dict], dict[str, str]]]: data = UserSchema().load(request.json) # Check if user already exists if UserModel.query.filter_by(username=data['username']).first(): raise Conflict( f"User with username '{data['username']}' already exists.") elif UserModel.query.filter_by(email=data['email']).first(): raise Conflict(f"User with email {data['email']} already exists.") password = data.pop('password') user = UserModel(**data) user.password = password db.session.add(user) db.session.commit() return { 'data': [user.to_dict()], 'message': { 'text': f'Registered user "{user.username}" Successfully!!', 'priority': 'success' } }