diff --git a/montage/rdb.py b/montage/rdb.py index c8bd6338..1aa01996 100644 --- a/montage/rdb.py +++ b/montage/rdb.py @@ -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) @@ -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: diff --git a/tools/_admin.py b/tools/_admin.py new file mode 100644 index 00000000..5cd5b628 --- /dev/null +++ b/tools/_admin.py @@ -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= ... + OR + _admin.py coordinator --campaign_id= ... + + (ignore the other options) + +Tip: try _admin.py -- --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)