Skip to content

Commit

Permalink
Final Stoichiometry Updates with 2 new SQL tables
Browse files Browse the repository at this point in the history
  • Loading branch information
reynavrb committed Apr 28, 2024
1 parent 12f2577 commit 3d8ee0b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 145 deletions.
31 changes: 18 additions & 13 deletions materials/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ class AddSystemForm(forms.Form):
max_length=100,
help_text='Enter n value for the dimensionality of this system if it applies')

from .models import System_Stoichiometry, Stoichiometry_Elements
class SystemStoichiometryForm(forms.ModelForm):
class Meta:
model = System_Stoichiometry
fields = ['system', 'stored_formula']

class StoichiometryElementsForm(forms.ModelForm):
class Meta:
model = Stoichiometry_Elements
fields = ['system_stoichiometry', 'element', 'string_value', 'float_value']

class AddPropertyForm(forms.Form):
name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
Expand All @@ -148,8 +137,19 @@ class AddUnitForm(forms.Form):
widget=forms.TextInput(attrs={'class': 'form-control'}),
max_length=100,
help_text='Label of the unit')


'''
class SystemStoichiometryForm(forms.ModelForm):
class Meta:
model = models.System_Stoichiometry
fields = ['system', 'stoichiometry']
widgets = {
'system': forms.Select(attrs={'class': 'form-control'}),
'stoichiometry': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'C:6,H:12,O:1'})
}
help_texts = {
'stoichiometry': 'Please provide the stoichiometry value in the format: C:6,H:12,O:1',
}
'''
class AddDataForm(forms.Form):
"""Main form for submitting data."""

Expand Down Expand Up @@ -190,6 +190,11 @@ class AddDataForm(forms.Form):
help_text=''
'Main description of the data. This can include an explanation of the '
'significance of the results.')
# stoichiometry FORM FILED WILL BE HERE
stoichiometry = forms.CharField(
label='Stoichiometry',
widget=forms.TextInput(attrs={'class': 'form-control'}),
max_length=2000, help_text="Please provide the stoichiometry value in the format: C:6,H:12,O:1")
extraction_method = AutoCharField(
label='Data extraction protocol',
model=models.Dataset, field='extraction_method',
Expand Down
15 changes: 3 additions & 12 deletions materials/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class System(models.Model):
dimensionality = models.PositiveSmallIntegerField(choices=DIMENSIONALITIES, default=0 )
n = models.CharField(max_length=50, blank=True)
tags = models.ManyToManyField(Tag, blank=True)
#primary_stoichiometry = models.CharField(max_length=255, blank=True)

def __str__(self):
return self.compound_name
Expand All @@ -144,20 +145,10 @@ def listAlternateNames(self):
#stoichiometry tables
class System_Stoichiometry(models.Model):
system = models.ForeignKey(System, on_delete=models.CASCADE)
stoichiometry = models.CharField(max_length=50, help_text="Please provide the stoichiometry value in the format: C:6,H:12,O:1", default="N/A")

@property
def formula(self):
return self.system.formula

stored_formula = models.CharField(max_length=200, blank=True, null=True)
#system_id = models.AutoField(primary_key=True)
def __str__(self):
return self.system.compound_name


class Meta:
verbose_name = "System Stoichiometry"
verbose_name_plural = "System Stoichiometries"
return f"Stoichiometry for {self.system.compound_name}"


class Stoichiometry_Elements(models.Model):
Expand Down
61 changes: 19 additions & 42 deletions materials/templates/materials/add_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load materials_tags %}

{% block body %}
<!--change-->
<div class="card card-default">
<div class="card-header">
<h4>Add Data</h4>
Expand Down Expand Up @@ -98,11 +99,12 @@ <h4>Add Data</h4>
{{"Stoichiometry extraction is case sensitive. For example, CsCl will read as Cs:1, Cl:1 as it needs to be distinguishable from CSCl, which will extract as C:1, S:1, Cl:1"|tooltip}}
</div>
<div id="stoichiometry-display"></div>
</div>
</div>
<div class="form-group col-md-6">
<label for="stoichiometry">Stoichiometry</label>
<input type="text" class="form-control" id="stoichiometry" name="stoichiometry">
</div>
{% comment %} THE stoichiometry INPUT WILL BE HERE {% endcomment %}
{% input_field main_form.stoichiometry %}
{% comment %}<input type="text" class="form-control" id="id_stoichiometry" name="stoichiometry" placeholder="Please provide the stoichiometry value in the format: C:6,H:12,O:1">{% endcomment %}
</div>
<div class="form-group col-md-6">
{% input_field system_form.organic %}
</div>
Expand Down Expand Up @@ -150,9 +152,6 @@ <h5 class="modal-title" id="stoichiometryModalLabel">Is this the correct stoichi
<input type="text" id="newStoichiometryInput" style="display: none;" placeholder="Enter stoichiometry here">
<button type="button" class="btn btn-primary" onclick="handleNewStoichiometry()">Enter</button>
</div>
<div id="stoichiometryWarning" style="display: none; color: red;">
Warning: Stoichiometry should not contain the letter 'V'.
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="handleStoichiometryConfirmation('Yes')">Yes</button>
Expand Down Expand Up @@ -619,6 +618,7 @@ <h5 class="modal-title" id="stoichiometryModalLabel">Is this the correct stoichi
let initial_secondary_property = '{{ main_form.secondary_property.value }}';
let initial_secondary_unit = '{{ main_form.secondary_unit.value }}';
</script>

<script src="{% static 'materials/javascript/add_data.js' %}"></script>
<script>
// Prefill the data subsets from a previous instance of the form
Expand Down Expand Up @@ -714,63 +714,40 @@ <h5 class="modal-title" id="stoichiometryModalLabel">Is this the correct stoichi

}
function handleStoichiometryConfirmation(answer){
const stoichiometryTextBox = document.getElementById('stoichiometry');
const stoichiometryTextBox = document.getElementById('system-stoichiometry-input');
const newStoichiometrySection = document.getElementById('newStoichiometrySection');

if(answer == 'Yes'){
const stoichiometryOutput = document.getElementById('stoichiometryOutput').innerHTML;


//Validate
if (stoichiometryOutput.includes('V')) {
document.getElementById('stoichiometryWarning').style.display = 'block';
return;
}
if(stoichiometryTextBox){
stoichiometryTextBox.value = stoichiometryOutput;
}
console.log("Stoichiometry2:", stoichiometryOutput);
id_stoichiometry.value = stoichiometryOutput;
}
else if (answer == 'No') {
showNewStoichiometrySection();
}
document.getElementById('stoichiometryWarning').style.display = 'none';
$('#stoichiometryModal').modal('hide');
}
function showNewStoichiometrySection() {
document.getElementById('newStoichiometrySection').style.display = 'block';
document.getElementById('newStoichiometryInput').style.display = 'block';
document.getElementById('enterButton').style.display = 'block';
document.getElementById('stoichiometryWarning').style.display = 'none'; //Hidden Warning
}

function handleNewStoichiometry() {
const stoichiometryTextBox = document.getElementById('stoichiometry');
const newStoichiometryInput = document.getElementById('newStoichiometryInput');
const stoichiometryTextBox = document.getElementById('system-stoichiometry-input');
const newStoichiometrySection = document.getElementById('newStoichiometrySection');
const newStoichiometryInput = document.getElementById('newStoichiometryInput').value;
const stoichiometryOutput = document.getElementById('stoichiometryOutput').innerHTML;

// Validate
if (newStoichiometryInput.includes('V')) {
document.getElementById('stoichiometryWarning').style.display = 'block';
return;
}
stoichiometryTextBox.value = newStoichiometryInput.value;
stoichiometryOutput.value = newStoichiometryInput.value;

// Set the value in the main stoichiometry text box
if (stoichiometryTextBox && newStoichiometryInput.trim() !== '') {
stoichiometryTextBox.value = newStoichiometryInput.trim();
}
// Hide the new text box and Enter button
newStoichiometrySection.style.display = 'none';
// Clear the new stoichiometry input
newStoichiometryInput.value = '';
// Hide the stoichiometry confirmation modal
document.getElementById('stoichiometryWarning').style.display = 'none';
$('#stoichiometryModal').modal('hide');
}


</script>




</script>



{% endblock %}
Loading

0 comments on commit 3d8ee0b

Please sign in to comment.