Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user cmd #178

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__pycache__
.DS_Store
env
venv
db
adele.sqlite
Expand Down
103 changes: 77 additions & 26 deletions app/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import pprint
from urllib.request import urlopen

Expand All @@ -6,25 +7,27 @@

from app import create_app
from app.api.routes import json_loads
from app.models import Image, ImageUrl
from app.models import Image, ImageUrl, Role, User

app = None
env = None


def add_default_users(db):
# TODO
db.session.flush()


def make_cli():
""" Creates a Command Line Interface for everydays tasks
"""Creates a Command Line Interface for everydays tasks

:return: Click groum
"""

@click.group()
@click.option('--config', default="dev")
@click.option("--config", default="dev")
def cli(config):
""" Generates the client"""
"""Generates the client"""
click.echo("Loading the application")
global app
global env
Expand All @@ -33,10 +36,10 @@ def cli(config):

@click.command("db-create")
def db_create():
""" Creates a local database
"""
"""Creates a local database"""
with app.app_context():
from app import db

db.create_all()

add_default_users(db)
Expand All @@ -46,11 +49,12 @@ def db_create():

@click.command("db-recreate")
def db_recreate():
""" Recreates a local database. You probably should not use this on
"""Recreates a local database. You probably should not use this on
production.
"""
with app.app_context():
from app import db

db.drop_all()
db.create_all()

Expand All @@ -61,8 +65,7 @@ def db_recreate():

@click.command("load-fixtures")
def db_load_fixtures():
""" Reload fixtures
"""
"""Reload fixtures"""
with app.app_context():
from app import db
from tests.data.entities import load_fixtures
Expand All @@ -73,17 +76,17 @@ def db_load_fixtures():
click.echo("Fixtures (re)loaded")

@click.command("add-manifest")
@click.option('--manifest-url', required=True)
@click.option('--doc-id', required=True)
@click.option("--manifest-url", required=True)
@click.option("--doc-id", required=True)
def db_add_manifest(manifest_url, doc_id):
"""
Fill the image & image_url tables with every image in the given manifest
"""
manifest_data = urlopen(manifest_url).read()
data = json_loads(manifest_data)

if data['@context'] == "http://iiif.io/api/presentation/3/context.json":
print('IIIF Presentation v3 detected')
if data["@context"] == "http://iiif.io/api/presentation/3/context.json":
print("IIIF Presentation v3 detected")

with app.app_context():
from app import db
Expand All @@ -96,25 +99,25 @@ def db_add_manifest(manifest_url, doc_id):
manifest_url=manifest_url,
canvas_idx=canvas_idx,
img_idx=img_idx,
doc_id=doc_id
doc_id=doc_id,
)
new_image_url = ImageUrl(
manifest_url=manifest_url,
canvas_idx=canvas_idx,
img_idx=img_idx,
img_url=image_url
img_url=image_url,
)

db.session.add(new_image)
db.session.add(new_image_url)
db.session.flush()
print('Adding new image:', new_image.serialize())
print('Adding new image_url:', new_image_url.serialize())
print("Adding new image:", new_image.serialize())
print("Adding new image_url:", new_image_url.serialize())

db.session.commit()

elif data['@context'] == "http://iiif.io/api/presentation/2/context.json":
print('IIIF Presentation v2 detected')
elif data["@context"] == "http://iiif.io/api/presentation/2/context.json":
print("IIIF Presentation v2 detected")
with app.app_context():
from app import db

Expand All @@ -126,38 +129,86 @@ def db_add_manifest(manifest_url, doc_id):
manifest_url=manifest_url,
canvas_idx=canvas_idx,
img_idx=img_idx,
doc_id=doc_id
doc_id=doc_id,
)
new_image_url = ImageUrl(
manifest_url=manifest_url,
canvas_idx=canvas_idx,
img_idx=img_idx,
img_url=image_url
img_url=image_url,
)

db.session.add(new_image)
db.session.add(new_image_url)
db.session.flush()
print('Adding new image:')
print("Adding new image:")
pprint.pprint(new_image.serialize())
print('Adding new image_url:')
print("Adding new image_url:")
pprint.pprint(new_image_url.serialize())

db.session.commit()
else:
print('@context not supported:', data['@context'])
print("@context not supported:", data["@context"])
return

@click.command("add-user")
@click.option("--email", required=True)
@click.option("--username", required=True)
@click.option("--password", required=True)
@click.option(
"--role", required=True, type=click.Choice(["admin", "teacher", "student"])
)
@click.option("--firstname", required=False)
@click.option("--lastname", required=False)
def db_add_user(email, username, password, role, firstname, lastname):
with app.app_context():
from app import db
from werkzeug.security import generate_password_hash

pwd_hash = generate_password_hash(password)

roles = []

if role == "admin":
roles.append(Role.query.filter(Role.name == "admin").first())
elif role == "teacher":
roles.append(Role.query.filter(Role.name == "teacher").first())
elif role == "student":
roles.append(Role.query.filter(Role.name == "student").first())

default_name = email.split("@")[0]

if firstname is None:
firstname = default_name

if lastname is None:
lastname = default_name

new_user = User(
username=username,
password=pwd_hash,
email=email,
email_confirmed_at=datetime.datetime.now(),
active=True,
first_name=firstname,
last_name=lastname,
roles=roles,
)

db.session.add(new_user)
db.session.commit()
print('User "%s" added' % username)

@click.command("run")
def run():
""" Run the application in Debug Mode [Not Recommended on production]
"""
"""Run the application in Debug Mode [Not Recommended on production]"""
app.run()

cli.add_command(db_create)
cli.add_command(db_recreate)
cli.add_command(db_add_manifest)
cli.add_command(db_load_fixtures)
cli.add_command(db_add_user)

cli.add_command(run)

Expand Down