Skip to content

Commit cb24b57

Browse files
committed
- Added generate_local_key to init command in CLI
- Added publish_locally option to publish command in CLI - Added local_user option in create_admin_user CLI command - Updated the version number to 0.5.5 - Updated key features in README.md
1 parent 9fe5398 commit cb24b57

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Includes GraphQL and generic ABAC auth workflow integrations.
1111
- Create a GraphQL endpoint and a schema validating ORM with the same code.
1212
- Authentication collections, indexes, user-defined functions, and roles included.
1313
- Generic CRUD user-defined functions included
14+
- Create a REST API to integrate Fauna and Non-Fauna related tasks
1415
- Schema validation based on the [Valley](https://github.com/capless/valley) library.
1516

1617
## Full Documentation

pfunk/cli.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
import json
33
import os
44
import sys
5+
import datetime
56

67
from jinja2 import TemplateNotFound
78
from valley.utils import import_util
89
from werkzeug.serving import run_simple
10+
from pfunk.client import FaunaClient, q
911

1012
from pfunk.contrib.auth.collections import Group, PermissionGroup
13+
from pfunk.exceptions import DocNotFound
1114
from pfunk.template import wsgi_template, project_template, collections_templates, key_template
1215
from pfunk.utils.deploy import Deploy
1316

@@ -23,13 +26,15 @@ def load_config_file(filename):
2326
return config
2427

2528
@pfunk.command()
29+
@click.option('--generate_local_key', prompt=True, help='Specifies whether to generate a local database and key',
30+
default=False)
2631
@click.option('--stage_name', prompt=True, help='Application stage', default='dev')
2732
@click.option('--email', prompt=True, help='Default From Email')
2833
@click.option('--bucket', prompt=True, help='S3 Bucket')
2934
@click.option('--fauna_key', prompt=True, help='Fauna Key')
3035
@click.option('--api_type', type=click.Choice(['web', 'rest', 'none']), prompt=True, help='API Type (web, rest, none)')
3136
@click.argument('name')
32-
def init(name: str, api_type: str, fauna_key: str, bucket: str, email: str, stage_name: str):
37+
def init(name: str, api_type: str, fauna_key: str, bucket: str, email: str, stage_name: str, generate_local_key: bool):
3338
"""
3439
Creates a PFunk project
3540
Args:
@@ -64,6 +69,17 @@ def init(name: str, api_type: str, fauna_key: str, bucket: str, email: str, stag
6469
f.write(project_template.render())
6570
with open(f'{name}/collections.py', 'x') as f:
6671
f.write(collections_templates.render())
72+
if generate_local_key:
73+
client = FaunaClient(secret='secret')
74+
db_name = f'{name}-local'
75+
client.query(
76+
q.create_database({'name': db_name})
77+
)
78+
key = client.query(
79+
q.create_key({'database': q.database(db_name), 'role': 'admin'})
80+
)
81+
click.secho(f'Fauna Local Secret (copy into your .env or pipenv file): {key}', fg='green')
82+
6783
else:
6884
click.echo('There is already a project file in this directory.')
6985

@@ -121,10 +137,11 @@ def local(hostname: str, port: int, wsgi: str, config_file: str, use_debugger: b
121137

122138

123139
@pfunk.command()
140+
@click.option('--publish_locally', prompt=True, help='Specifies whether to publish the schema locally.', default=False)
124141
@click.option('--config_path', help='Configuration file path', default='pfunk.json')
125142
@click.option('--project_path', help='Project module path')
126143
@click.argument('stage_name')
127-
def publish(stage_name: str, project_path: str, config_path: str):
144+
def publish(stage_name: str, project_path: str, config_path: str, publish_locally: bool):
128145
"""
129146
Publish GraphQL schema to Fauna
130147
Args:
@@ -140,8 +157,10 @@ def publish(stage_name: str, project_path: str, config_path: str):
140157
if not project_path:
141158
project_path = f'{config.get("name")}.project.project'
142159
project = import_util(project_path)
143-
secret = config['stages'][stage_name]['fauna_secret']
144-
os.environ['FAUNA_SECRET'] = secret
160+
if not publish_locally:
161+
162+
secret = config['stages'][stage_name]['fauna_secret']
163+
os.environ['FAUNA_SECRET'] = secret
145164
project.publish()
146165

147166

@@ -168,6 +187,7 @@ def seed_keys(stage_name: str, config_path: str):
168187
return keys_path
169188

170189
@pfunk.command()
190+
@click.option('--local_user', help='Specifies whether the user is local.', prompt=True, default=False)
171191
@click.option('--config_path', help='Configuration file path', default='pfunk.json')
172192
@click.option('--project_path', help='Project module path')
173193
@click.option('--username', prompt=True, help='Username')
@@ -178,7 +198,7 @@ def seed_keys(stage_name: str, config_path: str):
178198
@click.option('--group_slug', prompt=True, help='User Group Slug', default=None)
179199
@click.argument('stage_name')
180200
def create_admin_user(stage_name: str, group_slug: str, last_name: str, first_name: str, email: str, password: str, username: str,
181-
project_path: str, config_path: str):
201+
project_path: str, config_path: str, local_user: bool):
182202
"""
183203
Create an admin user in the project's Fauna user collection.
184204
Args:
@@ -191,14 +211,16 @@ def create_admin_user(stage_name: str, group_slug: str, last_name: str, first_na
191211
username: Username for the user
192212
project_path: Project path
193213
config_path: Config path
214+
local_user: Specifies whether to create the user locally.
194215
195216
Returns:
196217
197218
"""
198219
config = load_config_file(config_path)
199220
secret = config['stages'][stage_name]['fauna_secret']
200221
User = import_util('pfunk.contrib.auth.collections.User')
201-
os.environ['FAUNA_SECRET'] = secret
222+
if not local_user:
223+
os.environ['FAUNA_SECRET'] = secret
202224

203225
user = User.create(
204226
username=username,
@@ -210,7 +232,10 @@ def create_admin_user(stage_name: str, group_slug: str, last_name: str, first_na
210232
)
211233

212234
if group_slug:
213-
group = Group.get_by('unique_Group_slug', group_slug)
235+
try:
236+
group = Group.get_by('unique_Group_slug', group_slug)
237+
except DocNotFound:
238+
group = Group.create(name=group_slug, slug=group_slug)
214239
if not project_path:
215240
project_path = f'{config.get("name")}.project.project'
216241
sys.path.insert(0, os.getcwd())

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pfunk"
3-
version = "0.5.0"
3+
version = "0.5.5"
44
description = "A Python library created make building FaunaDB GraphQL schemas and authentication code easier."
55
authors = ["Brian Jinwright"]
66
license = "Apache-2.0"

0 commit comments

Comments
 (0)