Beispiel #1
0
def add_super_company_user():

    first_company = CompanyModel.find_by_name("OneSteward")

    if not first_company:
        first_company = CompanyModel("OneSteward", "*****@*****.**",
                                     "555-555-5555")
        first_company.save_to_db()

    first_staff = StaffModel.find_by_name("admin")
    if not first_staff:
        first_staff = StaffModel("admin", "admin",
                                 generate_password_hash("admin_password"),
                                 first_company.id)

        first_staff.save_to_db()

    first_user = UserModel.find_by_name("NA")
    if not first_user:
        first_user = UserModel(generate_password_hash("admin_password"),
                               name="NA",
                               email="NA",
                               phone="")

        first_user.save_to_db()
Beispiel #2
0
    def post(self):
        data = UserRegister.parser.parse_args()

        username = data['username']
        if UserModel.find_by_username(username):
            return {
                "message": f"A user with name '{username}' already exists"
            }, 400

        user = UserModel(**data)
        user.save_to_db()
        return {"message": f"User '{username}' created successfully"}, 201
Beispiel #3
0
def user_register():

    form = UserCreateForm()

    if form.validate_on_submit():
        user = UserModel(
            hashed_password = generate_password_hash(form.password.data),
            name = form.username.data,
            email = form.email.data,
            phone = form.phone.data
        )
        user.save_to_db()
        return redirect(url_for("web.index"))

    return render_template("user_register.html", form=form)
Beispiel #4
0
    def post(self):
        data = self.parser.parse_args()
        role = "USER"
        profile_img = "0"  # str type, 0~99 preset images
        password_hash = generate_password_hash(data["password"])

        user = UserModel.find_by_username(data["username"])
        if user:
            return {"message": "username already exists."}, 400

        user = UserModel.find_by_email(data["email"])
        if user:
            return {"message": "email already exists."}, 400

        user = UserModel(role=role,
                         username=data["username"],
                         password_hash=password_hash,
                         email=data["email"],
                         profile_img=profile_img)
        try:
            user.save_to_db()
            identity = {"role": user.role, "id": user.id}

            registration_confirmation(username=user.username,
                                      recipient=user.email)

            access_token = create_access_token(identity=identity,
                                               fresh=True,
                                               expires_delta=self.expires)
            refresh_token = create_refresh_token(identity=identity)
        except:
            return {
                "message": "something went wrong during user registration."
            }, 500

        return {
            "message": "user registered!",
            "role": user.role,
            "id": user.id,
            "username": user.username,
            "email": user.email,
            "profile_img": user.profile_img,
            "reg_date": str(user.date),
            "access_token": access_token,
            "refresh_token": refresh_token
        }, 201
Beispiel #5
0
    def post(self):
        data = self.user_parser.parse_args()
        user = UserModel.find_by_name(data["username"])
        if user:
            return {"message": "username:already exists"}, 400

        user = UserModel(generate_password_hash(data["password"]),
                         data["username"], data["email"])
        try:
            user.save_to_db()
            identity = {"auth_level": "user", "id": user.id}
            access_token = create_access_token(identity=identity)
            refresh_token = create_refresh_token(identity=identity)
            return {
                "message": "User created successfully.",
                "access_token": access_token,
                "refresh_token": refresh_token
            }, 201
        except:
            return {"message": "something went wrong."}, 500
Beispiel #6
0
db.drop_all()
from rest_api.models.video import VideoModel # noqa
from rest_api.models.user import UserModel # noqa
from rest_api.models.comment import CommentModel # noqa
from rest_api.models.jwt import RevokedTokenModel # noqa
from rest_api.models.rating import RatingModel # noqa
db.create_all()

#### init db with essential data ####

# create first user as admin
admin = UserModel(role='admin',
            username='******',
            password_hash= generate_password_hash('admin_password'),
            email='*****@*****.**',
            profile_img='0')
admin.save_to_db()
print('...admin created')

# create second user as guest
guest = UserModel(role='guest',
            username='******',
            password_hash= generate_password_hash('guest_password'),
            email='*****@*****.**',
            profile_img='0')
guest.save_to_db()
print ('...guest created')


print('...all done!')