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 31, 2020
1 parent 0e326d7 commit b668445
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 74 deletions.
61 changes: 42 additions & 19 deletions database/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
"Efficiency",
"RevisionMixin",
"User",
"Species",
"SpeciesRevision",
"ReactionRevision",
"RevisionManagerMixin",
]
and isinstance(obj, type)
and issubclass(obj, Model)
Expand All @@ -33,47 +34,69 @@


class RevisionAdmin(admin.ModelAdmin):

def get_readonly_fields(self, request, obj=None):
return [f.name for f in obj._meta.fields]


class IsomerInline(TabularInline):
model = models.Species.isomers.through
readonly_fields = ("isomer",)
can_delete = False

def has_add_permission(self, request, obj=None):
return False

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

def get_urls(self):
urls = super().get_urls()

return [
path(
r"species/<int:pk>/approve",
self.admin_site.admin_view(views.RevisionApprovalView.as_view()),
name="species-approval",
f"{self.url_name}/<int:pk>",
self.admin_site.admin_view(self.approval_view.as_view()),
name=self.url_name,
),
*urls,
]

def render_change_form(self, request, context, *args, **kwargs):
context["url_name"] = f"admin:{self.url_name}"

return super().render_change_form(request, context, *args, **kwargs)


class ImmutablePermissionMixin:
can_delete = False

def has_add_permission(self, request, obj=None):
return False


class IsomerInline(ImmutablePermissionMixin, TabularInline):
model = models.Species.isomers.through
readonly_fields = ("isomer",)


@admin.register(models.SpeciesRevision)
class SpeciesRevisionAdmin(admin.ModelAdmin):
exclude = ("hash", "isomers")
inlines = [IsomerInline]
url_name = "species-revision"
approval_view = views.SpeciesRevisionApprovalView


class StoichiometryInline(admin.TabularInline):
model = models.Stoichiometry
fields = ("species", "coeff")


class StoichiometryRevisionInline(ImmutablePermissionMixin, admin.TabularInline):
model = models.StoichiometryRevision
fields = ("species", "coeff")


@admin.register(models.Reaction)
class ReactionAdmin(admin.ModelAdmin):
inlines = [StoichiometryInline]


@admin.register(models.ReactionRevision)
class ReactionRevisionAdmin(RevisionAdmin):
inlines = [StoichiometryRevisionInline]
url_name = "reaction-approval"
approval_view = views.ReactionRevisionApprovalView


class AuthorshipInline(admin.TabularInline):
model = models.Authorship
fields = ("author", "order")
Expand Down
9 changes: 8 additions & 1 deletion database/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class Meta:
model = Stoichiometry
exclude = []

def has_changed(self):
return True


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

import database.models.kinetic_model
from django.conf import settings
Expand Down Expand Up @@ -99,9 +99,13 @@ class Migration(migrations.Migration):
name='Reaction',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('hash', models.CharField(max_length=32, unique=True)),
('created_on', models.DateTimeField(blank=True, 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)),
('prime_id', models.CharField(blank=True, max_length=10, verbose_name='PrIMe ID')),
('reversible', models.BooleanField()),
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('prime_id',),
Expand All @@ -128,15 +132,15 @@ class Migration(migrations.Migration):
name='Species',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(default=None, null=True)),
('created_on', models.DateTimeField(blank=True, 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)),
('prime_id', models.CharField(blank=True, max_length=9, verbose_name='PrIMe ID')),
('cas_number', models.CharField(blank=True, max_length=400, verbose_name='CAS Registry Number')),
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
('isomers', models.ManyToManyField(to='database.Isomer')),
('target', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Species')),
('target', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Species')),
],
options={
'verbose_name_plural': 'Species',
Expand Down Expand Up @@ -275,13 +279,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(blank=True, 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(blank=True, 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(blank=True, 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 +307,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(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Reaction'),
),
migrations.CreateModel(
name='KineticsComment',
fields=[
Expand Down Expand Up @@ -364,6 +378,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', models.Model),
),
migrations.CreateModel(
name='SpeciesRevision',
fields=[
Expand All @@ -373,7 +398,18 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('database.species',),
bases=('database.species', models.Model),
),
migrations.CreateModel(
name='StoichiometryRevision',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('database.stoichiometry', models.Model),
),
migrations.CreateModel(
name='Troe',
Expand Down Expand Up @@ -402,6 +438,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
11 changes: 7 additions & 4 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 @@ -25,10 +28,10 @@ class RevisionMixin(models.Model):
(PENDING, "Pending"),
(DENIED, "Denied"),
)
created_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
created_on = models.DateTimeField(default=None, null=True)
created_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
created_on = models.DateTimeField(default=None, null=True, blank=True)
revision = models.BooleanField(default=False)
target = models.ForeignKey("self", null=True, on_delete=models.SET_NULL)
target = models.ForeignKey("self", null=True, blank=True, on_delete=models.SET_NULL)
status = models.CharField(choices=STATUS_CHOICES, max_length=1, blank=True)

objects = NoRevisionManager()
Expand Down
2 changes: 1 addition & 1 deletion database/models/reaction_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def formula(self):


class Reaction(RevisionMixin):
hash = models.CharField(max_length=32, unique=True)
hash = models.CharField(max_length=32)
species = models.ManyToManyField("Species", through="Stoichiometry")
prime_id = models.CharField("PrIMe ID", blank=True, max_length=10)
reversible = models.BooleanField()
Expand Down
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
12 changes: 0 additions & 12 deletions database/templates/admin/database/change_form.html

This file was deleted.

14 changes: 14 additions & 0 deletions database/templates/admin/database/change_form_object_tools.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "admin/change_form_object_tools.html" %}
{% load i18n admin_urls %}

{% block object-tools-items %}
{{ block.super }}
{% if original and original.revision and not approved %}
<li>
<form action="{% url url_name original.id %}" method="POST">
{% csrf_token %}
<input class="historylink" type="submit" value="Approve">
</form>
</li>
{% endif %}
{% endblock %}
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
4 changes: 4 additions & 0 deletions database/templates/database/revision.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<h2>{{ name }} {{ object.id }} Revision</h2>
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form|crispy }}
{% endfor %}
{{ form|crispy }}
<input type="submit" class="btn btn-primary" value="Propose Changes">
</form>
Expand Down
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
Loading

0 comments on commit b668445

Please sign in to comment.