Skip to content

Commit

Permalink
backend/tools: alternative CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
slaporte committed Oct 23, 2017
1 parent aff3be8 commit 6326a07
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions montage/rdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,9 @@ def create_round(self, name, description, directions, quorum,
jurors = [self.get_or_create_user(j, 'juror', campaign=self.campaign)
for j in jurors]

if type(deadline_date) is not DateTime:
deadline_date = js_isoparse(deadline_date)

for (k, v) in DEFAULT_ROUND_CONFIG.items():
config[k] = config.get(k, v)

Expand Down Expand Up @@ -2174,6 +2177,13 @@ def edit_series(self, series_id, series_dict):

def create_campaign(self, name, url, open_date, close_date, series_id, coords=None):
other_campaigns = self._get_campaigns_named(name)

if type(open_date) is not DateTime:
open_date = js_isoparse(open_date)

if type(close_date) is not DateTime:
close_date = js_isoparse(close_date)

if other_campaigns:
raise InvalidAction('A campaign named %s already exists' % name)
if not coords:
Expand Down
91 changes: 91 additions & 0 deletions tools/_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
import sys

import fire

CUR_PATH = os.path.dirname(os.path.abspath(__file__))
PROJ_PATH = os.path.dirname(CUR_PATH)

sys.path.append(PROJ_PATH)

from montage.rdb import (make_rdb_session,
UserDAO,
MaintainerDAO,
OrganizerDAO,
CoordinatorDAO,
lookup_user)


"""Generating a command line interface with Python Fire.
It's (mostly) functional, but may be daunting if you're not familiar
with the DAOs in ../rdb.py. I haven't tested the full campaign flow,
so you may encounter a few operations that still can't be done via
CLI.
Usage:
_admin.py user ...
_admin.py maintainer ...
_admin.py organizer ...
_admin.py coordinator --round-id=<round_id:int> ...
OR
_admin.py coordinator --campaign_id=<campaign_id:int> ...
(ignore the other options)
Tip: try _admin.py <command> -- --interactive if you want to explore
or use the results in a REPL.
It would be nice to decorate or list the functions that I want Fire
to expose. There are a lot of internal functions that we can ignore.
"""


class AdminTool(object):

def __init__(self, user='Slaporte', echo=False):
rdb_session = make_rdb_session(echo=echo)
self.rdb_session = rdb_session
user = lookup_user(rdb_session, user)
self.user_dao = UserDAO(rdb_session, user)
self.maint_dao = MaintainerDAO(self.user_dao)
self.org_dao = OrganizerDAO(self.user_dao)

def commit(self, func):
def wrapper_func(*args, **kwargs):
retval = func(*args, **kwargs)
self.rdb_session.commit()
return retval
return wrapper_func

def add_commit(self, cls):
for attr in cls.__dict__:
if callable(getattr(cls, attr)):
setattr(cls, attr, self.commit(getattr(cls, attr)))
return cls

def user(self):
return self.add_commit(self.user_dao)

def maintainer(self):
return self.add_commit(self.maint_dao)

def organizer(self):
return self.add_commit(self.org_dao)

def coordinator(self, round_id=None, campaign_id=None):
if round_id:
print round_id
coord = CoordinatorDAO.from_round(self.user_dao, round_id)
elif campaign_id:
coord = CoordinatorDAO.from_campaign(self.user_dao,
campaign_id)
else:
raise Exception('need round_id or campaign_id')
return self.add_commit(coord)


if __name__ == '__main__':
fire.Fire(AdminTool)

0 comments on commit 6326a07

Please sign in to comment.