Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form refactor #170

Merged
merged 19 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 47 additions & 79 deletions escalate/core/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.hashers import make_password
from django.contrib.admin import widgets
from core.models import (
CustomUser,
OrganizationPassword,
)
from core.models.view_tables import (
Person,
Material,
Inventory,
Actor,
Note,
Organization,
Systemtool,
SystemtoolType,
UdfDef,
Status,
Tag,
TagAssign,
TagType,
MaterialType,
Edocument,
InventoryMaterial,
Vessel,
)
from core.models import (CustomUser, OrganizationPassword, )
from core.models.view_tables import (Person, Material, Inventory, Actor, Note,
Organization, Systemtool, SystemtoolType,
UdfDef, Status, Tag, TagAssign, TagType, MaterialType,
Edocument, InventoryMaterial, Vessel, MaterialIdentifier)
from core.models.core_tables import TypeDef

from packaging import version
Expand All @@ -51,7 +33,7 @@ class LoginForm(forms.Form):


class CustomUserCreationForm(UserCreationForm):
class Meta:
class Meta:
model = CustomUser
fields = ["username", "password1", "password2"]

Expand Down Expand Up @@ -154,48 +136,40 @@ class Meta(PersonFormData.Meta):


class MaterialForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# pk of model that is passed in to filter for tags belonging to the model
material_instance = kwargs.get("instance", None)

current_material_identifiers = material_instance.identifier.all() if material_instance else MaterialIdentifier.objects.none()
current_material_types = material_instance.material_type.all() if material_instance else MaterialType.objects.none()

super(MaterialForm, self).__init__(*args, **kwargs)

self.fields['identifier'] = forms.ModelMultipleChoiceField(
initial=current_material_identifiers,
required=False,
queryset=MaterialIdentifier.objects.all())
self.fields['identifier'].widget.attrs.update(dropdown_attrs)

self.fields['material_type'] = forms.ModelMultipleChoiceField(
initial=current_material_types,
required=False,
queryset=MaterialType.objects.all())
self.fields['material_type'].widget.attrs.update(dropdown_attrs)
class Meta:
model = Material
fields = ["status"]
"""
'abbreviation': forms.CharField,
'chemical_name': forms.CharField,
'inchi': forms.CharField,
'inchikey': forms.CharField,
'molecular_formula': forms.CharField,
'smiles': forms.CharField
"""
fields = ['consumable', 'material_class', 'identifier', 'material_type', 'status']
field_classes = {
"create_date": forms.SplitDateTimeField,
}
"""
'abbreviation': 'Abbreviation',
'chemical_name': 'Chemical name',
'inchi': 'International Chemical Identifier (InChI)',
'inchikey': 'International Chemical Identifier key (InChI key)',
'molecular_formula': 'Molecular formula',
'smiles': 'Smiles',
"""
labels = {"create_date": "Create date", "material_status": "Status"}
"""
'abbreviation': forms.TextInput(attrs={'placeholder': 'Ex: Water'}),
'chemical_name': forms.TextInput(attrs={
'placeholder': 'Ex: Dihydrogen Monoxide'}),
'inchi': forms.TextInput(attrs={'placeholder': 'Ex: 1S/H2O/h1H2'}),
'inchikey': forms.TextInput(attrs={
'placeholder': 'Ex: XLYOFNOQVPJJNP-UHFFFAOYSA-N'}),
'molecular_formula': forms.TextInput(attrs={
'placeholder': 'Ex: H2O'}),
'smiles': forms.TextInput(attrs={'placeholder': 'Ex: O'}),
"""
# 'create_date': forms.SplitDateTimeField,
}
labels = {
'create_date': 'Create date',
'material_status': 'Status'
}
widgets = {
"create_date": forms.SplitDateTimeWidget(
date_format="%d-%m-%Y",
date_attrs={"placeholder": "DD-MM-YYYY"},
time_format="%H:%M",
time_attrs={"placeholder": "HH-MM"},
),
"material_status": forms.Select(attrs=dropdown_attrs),
'consumable': forms.CheckboxInput(),
'material_class': forms.RadioSelect(choices=model._meta.get_field('material_class').choices),
'status': forms.Select(attrs=dropdown_attrs),
}


Expand Down Expand Up @@ -631,9 +605,9 @@ class Meta:
"description": forms.CharField,
"part_no": forms.CharField,
#'onhand_amt': ValFormField,
#'onhand_amt': forms.CharField,
"expiration_date": forms.SplitDateTimeField,
"location": forms.CharField,
'onhand_amt': forms.CharField,
'expiration_date': forms.SplitDateTimeField,
'location': forms.CharField,
}
labels = {
"description": "Description",
Expand Down Expand Up @@ -663,25 +637,19 @@ class Meta:
class VesselForm(forms.ModelForm):
class Meta:
model = Vessel
# fields = ['plate_name', 'well_number']
fields = ["description", "parent"]
fields = ['description', 'parent', 'total_volume']
field_classes = {
#'plate_name': forms.CharField,
#'well_number': forms.CharField,
"description": forms.CharField,
'description': forms.CharField,
'total_volumne': forms.CharField,
}
labels = {
#'plate_name': 'Plate Name',
#'well_number': 'Well Number',
"description": "Description"
'description': 'Description',
'parent': 'Parent',
'total_volume': 'Total Volume'
}
widgets = {
"plate_name": forms.Textarea(
attrs={"cols": "10", "rows": "3", "placeholder": "Plate name"}
),
"well_number": forms.Textarea(
attrs={"cols": "10", "rows": "1", "placeholder": "Well number"}
),
'description': forms.TextInput(attrs={'placeholder': 'Vessel Information...'}),
'total_volumne': forms.TextInput(attrs={'placeholder': 'total Volume...'}),
}


Expand Down
4 changes: 2 additions & 2 deletions escalate/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.0.8 on 2021-11-17 16:33
# Generated by Django 3.0.14 on 2021-12-03 18:01

import core.models.core_tables
import core.models.custom_types
Expand Down Expand Up @@ -494,7 +494,7 @@ class Migration(migrations.Migration):
('description', models.CharField(blank=True, max_length=255, null=True)),
('uuid', core.models.core_tables.RetUUIDField(db_column='vessel_uuid', default=uuid.uuid4, primary_key=True, serialize=False)),
('total_volume', models.CharField(blank=True, max_length=255, null=True)),
('internal_slug', core.models.core_tables.SlugField(blank=True, editable=False, max_length=255, overwrite=True, populate_from=['description'])),
('internal_slug', core.models.core_tables.SlugField(blank=True, editable=False, max_length=255, overwrite=True, populate_from=['description', 'total_volume', 'parent__description'])),
('actor', models.ForeignKey(blank=True, db_column='actor_uuid', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='vessel_actor', to='core.Actor')),
('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='parent_vessel', to='core.Vessel')),
('status', models.ForeignKey(blank=True, db_column='status_uuid', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='vessel_status', to='core.Status')),
Expand Down
28 changes: 12 additions & 16 deletions escalate/core/models/view_tables/chemistry_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,18 @@ class Vessel(DateColumns, StatusColumn, ActorColumn, DescriptionColumn):
# dead_volume = models.CharField(max_length=255,blank=True, null=True)
# whole plate can leave well_number blank
# well_number = models.CharField(max_length = 16, blank=True, null=True)
parent = models.ForeignKey(
"Vessel",
on_delete=models.DO_NOTHING,
blank=True,
null=True,
related_name="parent_vessel",
)
internal_slug = SlugField(
populate_from=[
#'plate_name',
#'well_number'
"description"
],
overwrite=True,
max_length=255,
)
parent = models.ForeignKey('Vessel', on_delete=models.DO_NOTHING,
blank=True, null=True,
related_name='parent_vessel')
internal_slug = SlugField(populate_from=[
#'plate_name',
#'well_number'
'description',
"total_volume",
"parent__description"
],
overwrite=True,
max_length=255)

def __str__(self):
# return "{} {}".format(self.plate_name, self.well_number)
Expand Down
25 changes: 10 additions & 15 deletions escalate/core/views/crud_view_methods/detail_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,16 @@
# 'Status': ['status.description']
# },
# },
"Vessel": {
"model": core.models.view_tables.Vessel,
"detail_fields": [
"Plate Name",
"Well Number",
"Status",
"Date Added",
"Last Modified",
],
"detail_fields_need_fields": {
"Plate Name": ["plate_name"],
"Well Number": ["well_number"],
"Status": ["status.description"],
"Date Added": ["add_date"],
"Last Modified": ["mod_date"],
'Vessel': {
'model': core.models.view_tables.Vessel,
'detail_fields': ['Description', 'Parent', "Total Volume", 'Status', 'Date Added','Last Modified'],
'detail_fields_need_fields': {
'Description': ['description'],
'Parent': ['parent.description'],
"Total Volume": ["total_volume"],
'Status': ['status.description'],
'Date Added': ['add_date'],
'Last Modified': ['mod_date'],
},
},
}
62 changes: 30 additions & 32 deletions escalate/core/views/crud_view_methods/list_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,36 @@
"ordering": ["display_text"],
"field_contains": "",
},
"InventoryMaterial": {
"model": core.models.view_tables.InventoryMaterial,
"table_columns": [
"Description",
"Inventory",
"Material",
"Amount On Hand",
"Expiration Date",
"Location",
],
"column_necessary_fields": {
"Description": ["description"],
"Inventory": ["inventory.description"],
"Material": ["material.description"],
"Amount On Hand": ["onhand_amt"],
"Expiration Date": ["expiration_date"],
"Location": ["location"],
},
"ordering": ["description"],
"field_contains": "",
"org_related_path": "inventory__lab__organization",
},
"TagType": {
"model": core.models.view_tables.TagType,
"table_columns": ["Type", "Description",],
"column_necessary_fields": {"Type": ["type"], "Description": ["description"]},
"ordering": ["type"],
"field_contains": "",
},
"UdfDef": {
"UdfDef": {
"model": core.models.view_tables.UdfDef,
"table_columns": ["Description", "Value Type",],
"column_necessary_fields": {
Expand All @@ -162,37 +184,13 @@
"ordering": ["description"],
"field_contains": "",
},
"InventoryMaterial": {
"model": core.models.view_tables.InventoryMaterial,
"table_columns": [
"Description",
"Inventory",
"Material",
"Amount On Hand",
"Expiration Date",
"Location",
],
"column_necessary_fields": {
"Description": ["description"],
"Inventory": ["inventory.description"],
"Material": ["material.description"],
"Amount On Hand": ["onhand_amt"],
"Expiration Date": ["expiration_date"],
"Location": ["location"],
},
"ordering": ["description"],
"field_contains": "",
"org_related_path": "inventory__lab__organization",
},
"Vessel": {
"model": core.models.view_tables.Vessel,
#'table_columns': ['Plate Name', 'Well Number', ],
"table_columns": ["Description", "Parent"],
"column_necessary_fields": {
#'Plate Name': ['plate_name'],
#'Well Number': ['well_number'],
"Description": ["description"],
"Parent": ["parent"],
'Vessel': {
'model': core.models.view_tables.Vessel,
'table_columns': ['Description', 'Parent', "Total Volume"],
'column_necessary_fields': {
'Description': ['description'],
'Parent': ['parent.description'],
"Total Volume": ["total_volume"]
},
"ordering": ["description"],
"field_contains": "",
Expand Down
14 changes: 3 additions & 11 deletions escalate/core/views/crud_view_methods/model_view_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,9 @@ def get_queryset(self):
).__class__.__name__
if final_field_class_name == "ManyToManyField":
filter_kwargs.pop(related_field_query)
filter_kwargs[
f"{related_field}__internal_slug__icontains"
] = custom_slugify(filter_val)
filter_query = (
functools.reduce(
lambda q1, q2: q1 | q2,
[Q(**{k: v}) for k, v in filter_kwargs.items()],
)
if len(filter_kwargs) > 0
else Q()
)
filter_kwargs[f'{related_field}__internal_slug__icontains'] = custom_slugify(filter_val)
filter_query = functools.reduce(lambda q1, q2: q1 | q2, [
Q(**{k: v}) for k, v in filter_kwargs.items()]) if len(filter_kwargs) > 0 else Q()
new_queryset = new_queryset.filter(filter_query).distinct()
else:
new_queryset = base_query
Expand Down