Skip to content

Commit

Permalink
finish reaction proposal creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kiancm committed Dec 26, 2020
1 parent 0e326d7 commit 7466606
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 18 deletions.
7 changes: 4 additions & 3 deletions database/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"Efficiency",
"RevisionMixin",
"User",
"Species",
"SpeciesRevision",
"RevisionManagerMixin",
]
and isinstance(obj, type)
and issubclass(obj, Model)
Expand All @@ -46,8 +46,9 @@ class IsomerInline(TabularInline):
def has_add_permission(self, request, obj=None):
return False

@admin.register(models.Species)
class SpeciesRevisionAdmin(RevisionAdmin):

@admin.register(models.SpeciesRevision)
class SpeciesRevisionAdmin(admin.ModelAdmin):
exclude = ("hash", "isomers")
inlines = [IsomerInline]

Expand Down
6 changes: 5 additions & 1 deletion database/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ class Meta:


StoichiometryFormSet = inlineformset_factory(
Reaction, Stoichiometry, form=StoichiometryForm, can_delete=True
Reaction,
Stoichiometry,
form=StoichiometryForm,
can_delete=True,
extra=1,
)
49 changes: 45 additions & 4 deletions database/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 3.0 on 2020-12-24 08:03
# Generated by Django 3.0 on 2020-12-26 00:30

import database.models.kinetic_model
import database.models.mixins
from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
Expand Down Expand Up @@ -99,9 +100,13 @@ class Migration(migrations.Migration):
name='Reaction',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(default=None, null=True)),
('revision', models.BooleanField(default=False)),
('status', models.CharField(blank=True, choices=[('A', 'Approved'), ('P', 'Pending'), ('D', 'Denied')], max_length=1)),
('hash', models.CharField(max_length=32, unique=True)),
('prime_id', models.CharField(blank=True, max_length=10, verbose_name='PrIMe ID')),
('reversible', models.BooleanField()),
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('prime_id',),
Expand Down Expand Up @@ -275,13 +280,18 @@ class Migration(migrations.Migration):
name='Stoichiometry',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('stoichiometry', models.FloatField()),
('created_on', models.DateTimeField(default=None, null=True)),
('revision', models.BooleanField(default=False)),
('status', models.CharField(blank=True, choices=[('A', 'Approved'), ('P', 'Pending'), ('D', 'Denied')], max_length=1)),
('coeff', models.FloatField()),
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
('reaction', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='database.Reaction')),
('species', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='database.Species')),
('target', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Stoichiometry')),
],
options={
'verbose_name_plural': 'Stoichiometries',
'unique_together': {('species', 'reaction', 'stoichiometry')},
'unique_together': {('species', 'reaction', 'coeff', 'revision', 'created_on')},
},
),
migrations.CreateModel(
Expand All @@ -298,6 +308,11 @@ class Migration(migrations.Migration):
name='species',
field=models.ManyToManyField(through='database.Stoichiometry', to='database.Species'),
),
migrations.AddField(
model_name='reaction',
name='target',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Reaction'),
),
migrations.CreateModel(
name='KineticsComment',
fields=[
Expand Down Expand Up @@ -364,6 +379,17 @@ class Migration(migrations.Migration):
name='source',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='database.Source'),
),
migrations.CreateModel(
name='ReactionRevision',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('database.reaction', database.models.mixins.RevisionManagerMixin),
),
migrations.CreateModel(
name='SpeciesRevision',
fields=[
Expand All @@ -373,7 +399,18 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('database.species',),
bases=('database.species', database.models.mixins.RevisionManagerMixin),
),
migrations.CreateModel(
name='StoichiometryRevision',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('database.stoichiometry', database.models.mixins.RevisionManagerMixin),
),
migrations.CreateModel(
name='Troe',
Expand Down Expand Up @@ -402,6 +439,10 @@ class Migration(migrations.Migration):
},
bases=('database.basekineticsdata',),
),
migrations.AlterUniqueTogether(
name='reaction',
unique_together={('hash', 'revision', 'created_on')},
),
migrations.CreateModel(
name='Pressure',
fields=[
Expand Down
7 changes: 5 additions & 2 deletions database/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def get_queryset(self):
return super().get_queryset().filter(revision=True)


class RevisionManagerMixin:
class RevisionManagerMixin(models.Model):
objects = RevisionManager()

class Meta:
abstract = True


class RevisionMixin(models.Model):
APPROVED = "A"
Expand All @@ -31,7 +34,7 @@ class RevisionMixin(models.Model):
target = models.ForeignKey("self", null=True, on_delete=models.SET_NULL)
status = models.CharField(choices=STATUS_CHOICES, max_length=1, blank=True)

objects = NoRevisionManager()
# objects = NoRevisionManager()

class Meta:
abstract = True
16 changes: 10 additions & 6 deletions database/models/revisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
from .mixins import RevisionManagerMixin


class SpeciesRevision(Species, RevisionManagerMixin):
pass
class SpeciesRevision(RevisionManagerMixin, Species):

class Meta:
proxy = True

class ReactionRevision(Reaction, RevisionManagerMixin):
pass

class ReactionRevision(RevisionManagerMixin, Reaction):
class Meta:
proxy = True

class StoichiometryRevision(Stoichiometry, RevisionManagerMixin):
pass

class StoichiometryRevision(RevisionManagerMixin, Stoichiometry):
class Meta:
proxy = True
2 changes: 2 additions & 0 deletions database/templates/database/reaction_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{% block content %}
<h2>{{ reaction.equation }}</h2>
<h2>ID: {{ reaction.pk }}</h2>
<a class="btn btn-primary" href="{% url 'reaction-revision' reaction.id %}">Edit</a>

<br>
<h3>Reactants</h3>
<div class="list-group">
Expand Down
8 changes: 7 additions & 1 deletion database/templates/database/revision.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<h2>{{ name }} {{ object.id }} Revision</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
{% if formset %}
{% for form in formset %}
{{ form|crispy }}
{% endfor %}
{% else %}
{{ form|crispy }}
{% endif %}
<input type="submit" class="btn btn-primary" value="Propose Changes">
</form>
{% endblock %}
5 changes: 5 additions & 0 deletions database/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
name="reaction-search",
),
path(r"reaction/<int:pk>", views.ReactionDetail.as_view(), name="reaction-detail"),
path(
r"reaction/<int:pk>/revise",
views.ReactionRevisionView.as_view(),
name="reaction-revision",
),
path(r"kinetics/<int:pk>", views.KineticsDetail.as_view(), name="kinetics-detail"),
path(r"kineticmodel/<int:pk>", views.KineticModelDetail.as_view(), name="kinetic-model-detail"),
path(r"drawstructure/<int:pk>", views.DrawStructure.as_view(), name="draw-structure"),
Expand Down
7 changes: 6 additions & 1 deletion database/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ def save_form(self, form):

return self.object


def form_valid(self, form):
self.save_form(form)

Expand Down Expand Up @@ -396,6 +395,12 @@ def post(self, request):
else:
return self.form_invalid(form)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["formset"] = self.get_formset()

return context

def form_valid(self, form):
instance = self.save_form(form)
formset = self.get_formset()
Expand Down

0 comments on commit 7466606

Please sign in to comment.