Skip to content

Commit

Permalink
migrate to Flask-CLI from fask_scriptp
Browse files Browse the repository at this point in the history
  • Loading branch information
publicarray committed Oct 22, 2022
1 parent 645918e commit 4640a22
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 97 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Synology Package Repository
### Installation
1. Install dependencies with `poetry install`
2. Run the next commands in the virtual environment `poetry shell`
3. Create the tables with `python manage.py db create`
4. Populate the database with some fake packages with `python manage.py db populate`
3. Create the tables with `python manage.py create`
4. Populate the database with some fake packages with `python manage.py populate`
5. Add an user with `python manage.py user create -u Admin -e [email protected] -p adminadmin`
6. Grant the created user with Administrator permissions `python manage.py user add_role -u [email protected] -r admin`
7. Grant the created user with Package Administrator permissions `python manage.py user add_role -u [email protected] -r package_admin`
Expand All @@ -19,7 +19,7 @@ Synology Package Repository
To reset the environment, clean up with `python manage.py clean`.

### Run
1. Start the development server with `python manage.py runserver`
1. Start the development server with `python manage.py run`
2. Website is available at http://localhost:5000
3. Admin interface is available at http://localhost:5000/admin
4. NAS interface is available at http://localhost:5000/nas
Expand Down
153 changes: 75 additions & 78 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,28 @@
import os
import re
import shutil

from flask import current_app
from flask_migrate import MigrateCommand
from flask_script import Manager, Option
from flask_security.script import (
ActivateUserCommand,
AddRoleCommand,
CreateRoleCommand,
CreateUserCommand,
DeactivateUserCommand,
RemoveRoleCommand,
commit,
)
from flask_security.utils import encrypt_password
import click

from flask import Flask
from flask.cli import FlaskGroup
# from flask_security.script import (
# ActivateUserCommand,
# AddRoleCommand,
# CreateRoleCommand,
# CreateUserCommand,
# DeactivateUserCommand,
# RemoveRoleCommand,
# commit,
# )
# from flask_security.utils import encrypt_password

from spkrepo import create_app
from spkrepo.ext import db
from spkrepo.models import Architecture, Package, user_datastore
from spkrepo.utils import populate_db

manager = Manager(create_app)
manager.add_option("-c", "--config", dest="config", required=False)

# Migrate commands
manager.add_command("db", MigrateCommand)
cli = FlaskGroup(create_app)
# click.option("-c", "--config", dest="config", required=False)


def date_handler(obj):
Expand All @@ -40,74 +37,74 @@ def pprint(obj):


# User commands
class CreateSpkrepoUserCommand(CreateUserCommand):
"""Create a user"""

option_list = (
Option("-u", "--username", dest="username", default=None),
Option("-e", "--email", dest="email", default=None),
Option("-p", "--password", dest="password", default=None),
Option("-a", "--active", dest="active", default=""),
Option("-c", "--confirmed", dest="confirmed", default=""),
)

@commit
def run(self, **kwargs):
# handle confirmed
if re.sub(r"\s", "", str(kwargs.pop("confirmed"))).lower() in [
"",
"y",
"yes",
"1",
"active",
]:
kwargs["confirmed_at"] = datetime.datetime.now()

# sanitize active input
ai = re.sub(r"\s", "", str(kwargs["active"]))
kwargs["active"] = ai.lower() in ["", "y", "yes", "1", "active"]

from flask_security.forms import ConfirmRegisterForm
from werkzeug.datastructures import MultiDict

form = ConfirmRegisterForm(MultiDict(kwargs), csrf_enabled=False)

if form.validate():
kwargs["password"] = encrypt_password(kwargs["password"])
user_datastore.create_user(**kwargs)
print("User created successfully.")
kwargs["password"] = "****"
pprint(kwargs)
else:
print("Error creating user")
pprint(form.errors)


UserCommand = Manager(usage="Perform user actions")
UserCommand.add_command("create", CreateSpkrepoUserCommand)
UserCommand.add_command("activate", ActivateUserCommand)
UserCommand.add_command("deactivate", DeactivateUserCommand)
UserCommand.add_command("create_role", CreateRoleCommand)
UserCommand.add_command("remove_role", RemoveRoleCommand)
UserCommand.add_command("add_role", AddRoleCommand)
manager.add_command("user", UserCommand)


@MigrateCommand.command
# class CreateSpkrepoUserCommand(CreateUserCommand):
# """Create a user"""

# option_list = (
# Option("-u", "--username", dest="username", default=None),
# Option("-e", "--email", dest="email", default=None),
# Option("-p", "--password", dest="password", default=None),
# Option("-a", "--active", dest="active", default=""),
# Option("-c", "--confirmed", dest="confirmed", default=""),
# )

# @commit
# def run(self, **kwargs):
# # handle confirmed
# if re.sub(r"\s", "", str(kwargs.pop("confirmed"))).lower() in [
# "",
# "y",
# "yes",
# "1",
# "active",
# ]:
# kwargs["confirmed_at"] = datetime.datetime.now()

# # sanitize active input
# ai = re.sub(r"\s", "", str(kwargs["active"]))
# kwargs["active"] = ai.lower() in ["", "y", "yes", "1", "active"]

# from flask_security.forms import ConfirmRegisterForm
# from werkzeug.datastructures import MultiDict

# form = ConfirmRegisterForm(MultiDict(kwargs), csrf_enabled=False)

# if form.validate():
# kwargs["password"] = encrypt_password(kwargs["password"])
# user_datastore.create_user(**kwargs)
# print("User created successfully.")
# kwargs["password"] = "****"
# pprint(kwargs)
# else:
# print("Error creating user")
# pprint(form.errors)


# UserCommand = Manager(usage="Perform user actions")
# UserCommand.add_command("create", CreateSpkrepoUserCommand)
# UserCommand.add_command("activate", ActivateUserCommand)
# UserCommand.add_command("deactivate", DeactivateUserCommand)
# UserCommand.add_command("create_role", CreateRoleCommand)
# UserCommand.add_command("remove_role", RemoveRoleCommand)
# UserCommand.add_command("add_role", AddRoleCommand)
# click.add_command("user", UserCommand)


@cli.command()
def drop():
"""Drop database"""
db.drop_all()


@MigrateCommand.command
@cli.command()
def create():
"""Create Database"""
db.create_all()
populate_db()
db.session.commit()


@MigrateCommand.command
@cli.command()
def populate():
"""Populate the database with some packages"""
from spkrepo.tests.common import BuildFactory, PackageFactory, VersionFactory
Expand Down Expand Up @@ -250,7 +247,7 @@ def populate():
db.session.commit()


@MigrateCommand.command
@cli.command()
def depopulate():
"""Depopulate database"""
for package in Package.query.all():
Expand All @@ -259,7 +256,7 @@ def depopulate():
db.session.commit()


@manager.command
@cli.command()
def clean():
"""Clean data path"""
# do not remove and recreate the path since it may be a docker volume
Expand All @@ -273,4 +270,4 @@ def clean():


if __name__ == "__main__":
manager.run()
cli()
37 changes: 22 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ requests = "^2.23.0"
click = "^8.0.0"
flask-migrate = "^3.0.0"
alembic = "^1.4.2"
flask-script = "^2.0.6"
text-unidecode = "^1.3"
ipaddress = "^1.0.23"
flask-debugtoolbar = "^0.11.0"
bcrypt = "^3.1.7"
Flask-CLI = "^0.4.0"

[tool.poetry.dev-dependencies]
sphinx = "^3.0.3"
Expand Down

1 comment on commit 4640a22

@publicarray
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #88

Please sign in to comment.