2
2
import json
3
3
import os
4
4
import sys
5
+ import datetime
5
6
6
7
from jinja2 import TemplateNotFound
7
8
from valley .utils import import_util
8
9
from werkzeug .serving import run_simple
10
+ from pfunk .client import FaunaClient , q
9
11
10
12
from pfunk .contrib .auth .collections import Group , PermissionGroup
13
+ from pfunk .exceptions import DocNotFound
11
14
from pfunk .template import wsgi_template , project_template , collections_templates , key_template
12
15
from pfunk .utils .deploy import Deploy
13
16
@@ -23,13 +26,15 @@ def load_config_file(filename):
23
26
return config
24
27
25
28
@pfunk .command ()
29
+ @click .option ('--generate_local_key' , prompt = True , help = 'Specifies whether to generate a local database and key' ,
30
+ default = False )
26
31
@click .option ('--stage_name' , prompt = True , help = 'Application stage' , default = 'dev' )
27
32
@click .option ('--email' , prompt = True , help = 'Default From Email' )
28
33
@click .option ('--bucket' , prompt = True , help = 'S3 Bucket' )
29
34
@click .option ('--fauna_key' , prompt = True , help = 'Fauna Key' )
30
35
@click .option ('--api_type' , type = click .Choice (['web' , 'rest' , 'none' ]), prompt = True , help = 'API Type (web, rest, none)' )
31
36
@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 ):
33
38
"""
34
39
Creates a PFunk project
35
40
Args:
@@ -64,6 +69,17 @@ def init(name: str, api_type: str, fauna_key: str, bucket: str, email: str, stag
64
69
f .write (project_template .render ())
65
70
with open (f'{ name } /collections.py' , 'x' ) as f :
66
71
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
+
67
83
else :
68
84
click .echo ('There is already a project file in this directory.' )
69
85
@@ -121,10 +137,11 @@ def local(hostname: str, port: int, wsgi: str, config_file: str, use_debugger: b
121
137
122
138
123
139
@pfunk .command ()
140
+ @click .option ('--publish_locally' , prompt = True , help = 'Specifies whether to publish the schema locally.' , default = False )
124
141
@click .option ('--config_path' , help = 'Configuration file path' , default = 'pfunk.json' )
125
142
@click .option ('--project_path' , help = 'Project module path' )
126
143
@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 ):
128
145
"""
129
146
Publish GraphQL schema to Fauna
130
147
Args:
@@ -140,8 +157,10 @@ def publish(stage_name: str, project_path: str, config_path: str):
140
157
if not project_path :
141
158
project_path = f'{ config .get ("name" )} .project.project'
142
159
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
145
164
project .publish ()
146
165
147
166
@@ -168,6 +187,7 @@ def seed_keys(stage_name: str, config_path: str):
168
187
return keys_path
169
188
170
189
@pfunk .command ()
190
+ @click .option ('--local_user' , help = 'Specifies whether the user is local.' , prompt = True , default = False )
171
191
@click .option ('--config_path' , help = 'Configuration file path' , default = 'pfunk.json' )
172
192
@click .option ('--project_path' , help = 'Project module path' )
173
193
@click .option ('--username' , prompt = True , help = 'Username' )
@@ -178,7 +198,7 @@ def seed_keys(stage_name: str, config_path: str):
178
198
@click .option ('--group_slug' , prompt = True , help = 'User Group Slug' , default = None )
179
199
@click .argument ('stage_name' )
180
200
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 ):
182
202
"""
183
203
Create an admin user in the project's Fauna user collection.
184
204
Args:
@@ -191,14 +211,16 @@ def create_admin_user(stage_name: str, group_slug: str, last_name: str, first_na
191
211
username: Username for the user
192
212
project_path: Project path
193
213
config_path: Config path
214
+ local_user: Specifies whether to create the user locally.
194
215
195
216
Returns:
196
217
197
218
"""
198
219
config = load_config_file (config_path )
199
220
secret = config ['stages' ][stage_name ]['fauna_secret' ]
200
221
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
202
224
203
225
user = User .create (
204
226
username = username ,
@@ -210,7 +232,10 @@ def create_admin_user(stage_name: str, group_slug: str, last_name: str, first_na
210
232
)
211
233
212
234
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 )
214
239
if not project_path :
215
240
project_path = f'{ config .get ("name" )} .project.project'
216
241
sys .path .insert (0 , os .getcwd ())
0 commit comments