Skip to content

Commit

Permalink
temp commit (AMEND ME)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiancm committed Dec 27, 2020
1 parent 7466606 commit f32a794
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 46 deletions.
File renamed without changes.
54 changes: 38 additions & 16 deletions database/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"RevisionMixin",
"User",
"SpeciesRevision",
"ReactionRevision",
"RevisionManagerMixin",
]
and isinstance(obj, type)
Expand All @@ -33,48 +34,69 @@


class RevisionAdmin(admin.ModelAdmin):

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

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

class IsomerInline(TabularInline):
model = models.Species.isomers.through
readonly_fields = ("isomer",)
return [
path(
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.update({"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]

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",
),
*urls,
]
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
1 change: 1 addition & 0 deletions database/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ class Meta:
form=StoichiometryForm,
can_delete=True,
extra=1,
fields=("coeff", "species")
)
29 changes: 14 additions & 15 deletions database/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by Django 3.0 on 2020-12-26 00:30
# Generated by Django 3.0 on 2020-12-26 22:04

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 @@ -100,13 +99,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)),
('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, unique=True)),
('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(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)),
],
options={
'ordering': ('prime_id',),
Expand All @@ -133,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 @@ -280,14 +279,14 @@ class Migration(migrations.Migration):
name='Stoichiometry',
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)),
('coeff', models.FloatField()),
('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)),
('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')),
('target', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Stoichiometry')),
],
options={
'verbose_name_plural': 'Stoichiometries',
Expand All @@ -311,7 +310,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='reaction',
name='target',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Reaction'),
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='database.Reaction'),
),
migrations.CreateModel(
name='KineticsComment',
Expand Down Expand Up @@ -388,7 +387,7 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('database.reaction', database.models.mixins.RevisionManagerMixin),
bases=('database.reaction', models.Model),
),
migrations.CreateModel(
name='SpeciesRevision',
Expand All @@ -399,7 +398,7 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('database.species', database.models.mixins.RevisionManagerMixin),
bases=('database.species', models.Model),
),
migrations.CreateModel(
name='StoichiometryRevision',
Expand All @@ -410,7 +409,7 @@ class Migration(migrations.Migration):
'indexes': [],
'constraints': [],
},
bases=('database.stoichiometry', database.models.mixins.RevisionManagerMixin),
bases=('database.stoichiometry', models.Model),
),
migrations.CreateModel(
name='Troe',
Expand Down
8 changes: 4 additions & 4 deletions database/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ 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()
objects = NoRevisionManager()

class Meta:
abstract = True
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
4 changes: 3 additions & 1 deletion database/templates/admin/database/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

{% block submit_buttons_bottom %}
{{ block.super }}
<form action="{% url 'admin:species-approval' original.id %}" method="POST">
{% if original and original.revision %}
<form action="{% url url_name original.id %}" method="POST">
{% csrf_token %}
<div class="submit-row">
<input type="submit" value="Approve">
</div>
</form>
{% endif %}
{% endblock %}
10 changes: 3 additions & 7 deletions database/templates/database/revision.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
<h2>{{ name }} {{ object.id }} Revision</h2>
<form method="post">
{% csrf_token %}
{% if formset %}
{% for form in formset %}
{{ form|crispy }}
{% endfor %}
{% else %}
{{ form|crispy }}
{% endif %}
{{ formset.management_form }}
{{ formset }}
{{ form|crispy }}
<input type="submit" class="btn btn-primary" value="Propose Changes">
</form>
{% endblock %}
39 changes: 37 additions & 2 deletions database/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from database.models.revisions import ReactionRevision
import functools
from itertools import zip_longest
from collections import defaultdict
Expand Down Expand Up @@ -26,7 +27,9 @@
Source,
Reaction,
Kinetics,
SpeciesRevision
SpeciesRevision,
Stoichiometry,
StoichiometryRevision,
)
from .filters import SpeciesFilter, ReactionFilter, SourceFilter
from .forms import RegistrationForm, StoichiometryFormSet
Expand Down Expand Up @@ -385,7 +388,7 @@ def get_formset(self):
else:
return self.formset_class(instance=instance)

def post(self, request):
def post(self, request, **kwargs):
form = self.get_form()
formset = self.get_formset()
self.object = self.get_object()
Expand Down Expand Up @@ -432,9 +435,16 @@ def post(self, request, pk):

return HttpResponseRedirect(self.get_url())

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

return context


class SpeciesRevisionApprovalView(RevisionApprovalView):
model = SpeciesRevision
url_name = "species-approval"

def get_url(self):
return reverse("admin:database_speciesrevision_changelist")
Expand All @@ -446,3 +456,28 @@ def update_model(self, revision):
species.isomers.clear()
species.isomers.add(*revision.isomers.all())
species.save()


class ReactionRevisionApprovalView(RevisionApprovalView):
model = ReactionRevision
url_name = "reaction-approval"

def get_url(self):
return reverse("admin:database_reactionrevision_changelist")

def update_model(self, revision):
reaction = revision.target
reaction.prime_id = revision.prime_id
reaction.reversible = revision.reversible
reaction.stoichiometry_set.delete()
reaction.save()
new_stoichs = StoichiometryRevision.objects.filter(reaction=revision)
print(new_stoichs)
for stoich in new_stoichs:
stoich.revision = False
stoich.created_on = None
stoich.created_by = None
stoich.status = ""
stoich.reaction = reaction
print(stoich)
stoich.save()

0 comments on commit f32a794

Please sign in to comment.