Skip to content

Commit

Permalink
implement test mode for submission (reuse IDs)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Jul 18, 2015
1 parent 6e9e1e7 commit 143bb82
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
11 changes: 10 additions & 1 deletion mpcontribs/io/mpfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def insert_id(self, cid, mp_cat_id):
# only works for single section files like in `utils.submit_mpfile`
first_sub_key = self.document[mp_cat_id].keys()[0]
self.document[mp_cat_id].insert_before(first_sub_key, ('cid', str(cid)))
for key in ['test_index']:
self.document[mp_cat_id].pop(key)
for idx_str in self.comments.keys():
comment = self.comments.pop(idx_str)
idx_str_split = idx_str.split('*')
Expand All @@ -119,6 +121,13 @@ def insert_id(self, cid, mp_cat_id):
if len(idx_str_split) > 1: idx_str += '*'
self.comments[idx_str] = comment

def set_test_mode(self, mp_cat_id, idx=0):
"""insert a key-value entry indicating test submission"""
# only works for single section files like in `utils.submit_mpfile`
first_sub_key = self.document[mp_cat_id].keys()[0]
self.document[mp_cat_id].insert_before(
first_sub_key, ('test_index', idx+733773))

@force_encoded_string_output
def get_string(self, with_comments=False):
"""Returns a string to be written as a file"""
Expand All @@ -133,7 +142,7 @@ def get_string(self, with_comments=False):
sep = '' if min_indentor in key else ':'
if lines and key == min_indentor:
lines.append('')
if table_start in value:
if isinstance(value, string_types) and table_start in value:
value = value[len(table_start):]
lines.append(make_pair(key, value, sep=sep))
if with_comments:
Expand Down
4 changes: 4 additions & 0 deletions mpcontribs/rest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from config import mp_level01_titles
from bson.objectid import ObjectId
from utils import get_short_object_id
from datetime import datetime

class ContributionMongoAdapter(object):
"""adapter/interface for user contributions"""
Expand Down Expand Up @@ -48,6 +49,9 @@ def submit_contribution(self, mpfile, contributor_email, project=None):
mp_cat_id = mpfile.document.keys()[0]
data = mpfile.document[mp_cat_id]
update, cid = False, ObjectId() # TODO: new vs update
if 'test_index' in data:
test_index = int(data['test_index'])
cid = ObjectId.from_datetime(datetime.fromordinal(test_index))
cid_short = get_short_object_id(cid)
collaborators = [contributor_email]
if update: # check contributor permissions if update mode
Expand Down
14 changes: 10 additions & 4 deletions mpcontribs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
from io.utils import nest_dict, RecursiveDict
from mpcontribs.config import SITE

def get_short_object_id(cid): return str(cid)[-6:]

def submit_mpfile(path_or_mpfile, target=None):
def get_short_object_id(cid):
length = 7
cid_short = str(cid)[-length:]
if cid_short == '0'*length:
cid_short = str(cid)[:length]
return cid_short

def submit_mpfile(path_or_mpfile, target=None, test=False):
if isinstance(path_or_mpfile, string_types) and \
not os.path.isfile(path_or_mpfile):
print('{} not found'.format(path_or_mpfile))
Expand All @@ -24,9 +29,10 @@ def submit_mpfile(path_or_mpfile, target=None):
mpfile = MPFile.from_file(path_or_mpfile)
mpfile.apply_general_section()
# split into contributions: treat every mp_cat_id as separate DB insert
for key, value in mpfile.document.iteritems():
for idx, (key, value) in enumerate(mpfile.document.iteritems()):
mp_cat_id = key.split('--')[0]
mpfile_single = MPFile.from_dict(mp_cat_id, value)
if test: mpfile_single.set_test_mode(mp_cat_id, idx)
print('submit contribution for {} ...'.format(mp_cat_id))
if target is not None:
mpfile_single.write_file('tmp')
Expand Down
2 changes: 1 addition & 1 deletion mpcontribs/webui/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index(path):
if not path and request.method == 'GET':
return render_template('choose.html')
mpfile = path if path else request.files['file']
content = submit_mpfile(mpfile)
content = submit_mpfile(mpfile, test=True)
for value in content.itervalues():
for project_data in value.itervalues():
for cid in project_data:
Expand Down
4 changes: 3 additions & 1 deletion scripts/mgc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def submit(args):
viewer(args)
else:
check_pymatgen()
submit_mpfile(args.mpfile, target=mpr)
submit_mpfile(args.mpfile, target=mpr, test=args.test)

def delete(args):
check_pymatgen()
Expand Down Expand Up @@ -121,6 +121,8 @@ def main():
parser_submit.add_argument('mpfile', type=str, help='MPFile to submit')
parser_submit.add_argument('--dry', help='dry-run: use viewer instead of MP',
action='store_true')
parser_submit.add_argument('--test', help='reuse ID to avoid spam in Plotly',
action='store_true')
parser_submit.set_defaults(func=submit)

parser_delete = subparsers.add_parser(
Expand Down

0 comments on commit 143bb82

Please sign in to comment.