Skip to content

Commit

Permalink
Merge branch 'dabih-release' into devfix-simple-postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
debboutr committed Mar 27, 2020
2 parents b71bd27 + efc8738 commit a7f2c19
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 273 deletions.
25 changes: 23 additions & 2 deletions chemreg/common/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
from django.conf import settings
from django.db import models

from chemreg.common.utils import get_current_user_pk


class CommonInfo(models.Model):
"""Common information to be applied to all models.
Attributes:
created_at (datetime.datetime): When the model was created.
created_by (chemreg.user.models.User): Who created this model.
updated_at (datetime.datetime): When the model was updated.
updated_by (chemreg.user.models.User): Who updated this model.
"""

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True, editable=False)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
default=get_current_user_pk,
editable=False,
related_name="%(class)s_created_by_set",
null=True,
on_delete=models.PROTECT,
)
updated_at = models.DateTimeField(auto_now=True, editable=False)
updated_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
editable=False,
related_name="%(class)s_updated_by_set",
null=True,
on_delete=models.PROTECT,
)

class Meta:
abstract = True
ordering = ["pk"]
17 changes: 17 additions & 0 deletions chemreg/common/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db.models.signals import pre_save
from django.dispatch import receiver

from chemreg.common.models import CommonInfo
from chemreg.common.utils import get_current_user_pk


@receiver(pre_save)
def set_updated_by(sender, instance, **kwargs):
"""Signal to set the `CommonInfo.updated_by`.
Arguments:
sender: the model class.
instance: the model instance.
"""
if isinstance(instance, CommonInfo):
instance.updated_by_id = get_current_user_pk()
19 changes: 19 additions & 0 deletions chemreg/common/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.db import models

import pytest
from crum import impersonate

from chemreg.common.models import CommonInfo


Expand All @@ -21,3 +24,19 @@ def test_commoninfo_attr():
def test_inherits_commoninfo(chemreg_model):
"""Test that all ChemReg models inherit CommonInfo."""
assert issubclass(chemreg_model, CommonInfo)


@pytest.mark.django_db
def test_user_link(user_factory):
"""Test that created_by/updated_by are saved."""
user_1 = user_factory()
assert user_1.created_by_id is None
assert user_1.updated_by_id is None
with impersonate(user_1):
user_2 = user_factory()
assert user_2.created_by_id == user_1.pk
assert user_2.updated_by_id == user_1.pk
with impersonate(user_2):
user_2.save()
assert user_2.created_by_id == user_1.pk
assert user_2.updated_by_id == user_2.pk
13 changes: 13 additions & 0 deletions chemreg/common/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from crum import get_current_user


def get_current_user_pk():
"""Retrieve the current user's primary key.
Returns:
The primary key of the current request's user.
"""
current_user = get_current_user()
if not current_user:
return None
return current_user.pk
32 changes: 0 additions & 32 deletions chemreg/compound/migrations/0001_basecompound.py

This file was deleted.

228 changes: 228 additions & 0 deletions chemreg/compound/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
# Generated by Django 3.0.3 on 2020-03-25 17:38

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

import computed_property.fields

import chemreg.common.utils
import chemreg.compound.models
import chemreg.compound.utils
import chemreg.compound.validators


def fwd_create_illdefined_querystructuretype(apps, schema_editor):
QueryStructureType = apps.get_model("compound", "QueryStructureType")
db_alias = schema_editor.connection.alias
QueryStructureType.objects.using(db_alias).create(
name="ill-defined",
label="Ill defined",
short_description="Ill defined",
long_description="Ill defined",
)


def rev_create_illdefined_querystructuretype(apps, schema_editor):
QueryStructureType = apps.get_model("compound", "QueryStructureType")
db_alias = schema_editor.connection.alias
QueryStructureType.objects.using(db_alias).filter(name="ill-defined").delete()


class Migration(migrations.Migration):

initial = True

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="BaseCompound",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"cid",
models.CharField(
default=chemreg.compound.utils.build_cid,
max_length=50,
unique=True,
validators=[
chemreg.compound.validators.validate_cid_regex,
chemreg.compound.validators.validate_cid_checksum,
],
),
),
("structure", models.TextField()),
(
"created_by",
models.ForeignKey(
default=chemreg.common.utils.get_current_user_pk,
editable=False,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="basecompound_created_by_set",
to=settings.AUTH_USER_MODEL,
),
),
(
"polymorphic_ctype",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="polymorphic_compound.basecompound_set+",
to="contenttypes.ContentType",
),
),
(
"updated_by",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="basecompound_updated_by_set",
to=settings.AUTH_USER_MODEL,
),
),
],
options={"ordering": ["pk"], "abstract": False,},
),
migrations.CreateModel(
name="DefinedCompound",
fields=[
(
"basecompound_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="compound.BaseCompound",
),
),
(
"inchikey",
computed_property.fields.ComputedCharField(
compute_from="_inchikey", editable=False, max_length=29
),
),
],
options={"ordering": ["pk"], "abstract": False,},
bases=("compound.basecompound",),
),
migrations.CreateModel(
name="QueryStructureType",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"name",
models.SlugField(
help_text="Query structure type name",
max_length=49,
unique=True,
verbose_name="name",
),
),
(
"label",
models.CharField(
help_text="Query structure type label",
max_length=99,
unique=True,
verbose_name="label",
),
),
(
"short_description",
models.CharField(
help_text="Query structure type short description",
max_length=499,
verbose_name="short description",
),
),
(
"long_description",
models.TextField(
help_text="Query structure type long description",
verbose_name="long description",
),
),
(
"created_by",
models.ForeignKey(
default=chemreg.common.utils.get_current_user_pk,
editable=False,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="querystructuretype_created_by_set",
to=settings.AUTH_USER_MODEL,
),
),
(
"updated_by",
models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="querystructuretype_updated_by_set",
to=settings.AUTH_USER_MODEL,
),
),
],
options={"ordering": ["pk"], "abstract": False,},
),
migrations.RunPython(
fwd_create_illdefined_querystructuretype,
rev_create_illdefined_querystructuretype,
),
migrations.CreateModel(
name="IllDefinedCompound",
fields=[
(
"basecompound_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="compound.BaseCompound",
),
),
(
"query_structure_type",
models.ForeignKey(
default=chemreg.compound.models.get_illdefined_qst,
on_delete=django.db.models.deletion.PROTECT,
to="compound.QueryStructureType",
),
),
],
options={"verbose_name": "ill-defined compound",},
bases=("compound.basecompound",),
),
]
26 changes: 0 additions & 26 deletions chemreg/compound/migrations/0002_definedcompound.py

This file was deleted.

Loading

0 comments on commit a7f2c19

Please sign in to comment.