Exemple #1
0
    def post(self):
        data = request.get_json()
        if UserModel.get_by_username(data[USERNAME]):
            return {MESSAGE: "A user with that username already exists"}, 400

        if UserModel.get_by_email(data[EMAIL]):
            return {MESSAGE: "A user with that email already exists"}, 400

        if not WhitelistEmailsModel.check_email(data[EMAIL]):
            return {MESSAGE: "email is not valid"}, 400

        data["password"] = UserModel.encrypt_password(data["password"])
        user = UserModel(**data)
        user.save_to_mongo()

        oj_data = OjModel(data[USERNAME])
        oj_data.save_to_mongo()
        # app.logger.info("User created successfully " + user.username + " " + user.email)
        return {MESSAGE: "User created successfully."}, 201
Exemple #2
0
    def post(self):
        data = request.get_json()

        if USERNAME in data:
            user = UserModel.get_by_username(data[USERNAME])
        elif EMAIL in data:
            user = UserModel.get_by_email(data[EMAIL])
        else:
            return {MESSAGE: "username or email is required"}, 400
        if not user:
            return {MESSAGE: "invalid username or password"}, 400

        temp_pass = get_random_alphanumeric_string(8)
        user.reset_pass = {
            "expires_on": (datetime.today() + timedelta(hours=1)).timestamp(),
            "temp_pass": UserModel.encrypt_password(temp_pass)
        }
        user.update_to_mongo()

        mail_template = MailTemplate.get_by_template_name("password_reset")
        threading.Thread(target=mail_sender.send_mail,
                         args=[[user.email], mail_template.subject, mail_template.message % temp_pass]).start()

        return {MESSAGE: "Please check your mail inbox"}, 200