def kineticsEntryEdit(request, section, subsection, index): """ A view for editing an entry in a kinetics database. """ # Load the kinetics database, if necessary loadDatabase("kinetics", section) # Determine the entry we wish to view try: database = getKineticsDatabase(section, subsection) except ValueError: raise Http404 index = int(index) for entry in database.entries.values(): if entry.index == index: break else: raise Http404 if request.method == "POST": form = KineticsEntryEditForm(request.POST, error_class=DivErrorList) if form.is_valid(): new_entry = form.cleaned_data["entry"] new_entry.index = index new_entry.history = copy.copy(entry.history) new_history = ( time.strftime("%Y-%m-%d"), "{0.first_name} {0.last_name} <{0.email}>".format(request.user), "action", form.cleaned_data["change"], ) new_entry.history.append(new_history) # Get the entry as a entry_string entry_buffer = StringIO.StringIO(u"") rmgpy.data.kinetics.saveEntry(entry_buffer, new_entry) entry_string = entry_buffer.getvalue() entry_buffer.close() if False: # Just return the text. return HttpResponse(entry_string, mimetype="text/plain") if False: # Render it as if it were saved. return render_to_response( "kineticsEntry.html", { "section": section, "subsection": subsection, "databaseName": database.name, "entry": new_entry, "reference": entry.reference, "kinetics": entry.data, }, context_instance=RequestContext(request), ) if True: # save it database.entries[index] = new_entry path = os.path.join(settings.DATABASE_PATH, "kinetics", section, subsection + ".py") database.save(path) commit_author = "{0.first_name} {0.last_name} <{0.email}>".format(request.user) commit_message = "{1}:{2} {3}\n\nChange to kinetics/{0}/{1} entry {2} submitted through RMG website:\n{3}\n{4}".format( section, subsection, index, form.cleaned_data["change"], commit_author ) commit_result = subprocess.check_output( ["git", "commit", "-m", commit_message, "--author", commit_author, path], cwd=settings.DATABASE_PATH, stderr=subprocess.STDOUT, ) # return HttpResponse(commit_result, mimetype="text/plain") kwargs = {"section": section, "subsection": subsection, "index": index} forward_url = reverse(kineticsEntry, kwargs=kwargs) message = """ Changes saved succesfully:<br> <pre>{0}</pre><br> See result at <a href="{1}">{1}</a>. """.format( commit_result, forward_url ) return render_to_response( "simple.html", {"title": "Change saved successfully.", "body": message}, context_instance=RequestContext(request), ) # redirect return HttpResponseRedirect(forward_url) else: # not POST # Get the entry as a entry_string entry_buffer = StringIO.StringIO(u"") rmgpy.data.kinetics.saveEntry(entry_buffer, entry) entry_string = entry_buffer.getvalue() entry_buffer.close() # entry_string = entry.item.reactants[0].toAdjacencyList() # remove leading 'entry(' entry_string = re.sub("^entry\(\n", "", entry_string) # remove the 'index = 23,' line entry_string = re.sub("\s*index = \d+,\n", "", entry_string) # remove the history and everything after it (including the final ')' ) entry_string = re.sub("\s+history = \[.*", "", entry_string, flags=re.DOTALL) form = KineticsEntryEditForm(initial={"entry": entry_string}) return render_to_response( "kineticsEntryEdit.html", {"section": section, "subsection": subsection, "databaseName": database.name, "entry": entry, "form": form}, context_instance=RequestContext(request), )
def kineticsEntryNewTraining(request, family): """ A view for creating a new entry in a kinetics family training depository. """ # Load the kinetics database, if necessary loadDatabase("kinetics", "families") try: database = getKineticsDatabase("families", family + "/training") except ValueError: raise Http404 entry = None if request.method == "POST": form = KineticsEntryEditForm(request.POST, error_class=DivErrorList) if form.is_valid(): new_entry = form.cleaned_data["entry"] # determine index for new entry (1 higher than highest) max_index = max(database.entries.keys() or [0]) index = max_index + 1 # check it's not already there for entry in database.entries.values(): if entry.item.isIsomorphic(new_entry.item): kwargs = {"section": "families", "subsection": family + "/training", "index": entry.index} forward_url = reverse(kineticsEntry, kwargs=kwargs) message = """ This reaction is already in the {0} training set. View or edit it at <a href="{1}">{1}</a>. """.format( family, forward_url ) return render_to_response( "simple.html", {"title": "Reaction already in training set.", "body": message}, context_instance=RequestContext(request), ) new_entry.index = index new_history = ( time.strftime("%Y-%m-%d"), "{0.first_name} {0.last_name} <{0.email}>".format(request.user), "action", "New entry. " + form.cleaned_data["change"], ) new_entry.history = [new_history] # Get the entry as a entry_string entry_buffer = StringIO.StringIO(u"") rmgpy.data.kinetics.saveEntry(entry_buffer, new_entry) entry_string = entry_buffer.getvalue() entry_buffer.close() # redirect when done kwargs = {"section": "families", "subsection": family + "/training", "index": index} forward_url = reverse(kineticsEntry, kwargs=kwargs) if False: # Just return the text. return HttpResponse(entry_string, mimetype="text/plain") if True: # save it database.entries[index] = new_entry path = os.path.join(settings.DATABASE_PATH, "kinetics", "families", family, "training.py") database.save(path) commit_author = "{0.first_name} {0.last_name} <{0.email}>".format(request.user) commit_message = "New {family}/training/{index} reaction: {msg}\n\nNew kinetics/families/{family}/training entry number {index} submitted through RMG website:\n{msg}\n{author}".format( family=family, index=index, msg=form.cleaned_data["change"], author=commit_author ) commit_result = subprocess.check_output( ["git", "commit", "-m", commit_message, "--author", commit_author, path], cwd=settings.DATABASE_PATH, stderr=subprocess.STDOUT, ) message = """ New training reaction saved succesfully:<br> <pre>{0}</pre><br> See result at <a href="{1}">{1}</a>. """.format( commit_result, forward_url ) return render_to_response( "simple.html", {"title": "New rate saved successfully.", "body": message}, context_instance=RequestContext(request), ) else: # not POST form = KineticsEntryEditForm() return render_to_response( "kineticsEntryEdit.html", { "section": "families", "subsection": family + "/training", "databaseName": family, "entry": entry, "form": form, }, context_instance=RequestContext(request), )