Skip to content

Commit

Permalink
Updated serializers.py to add dimensionality and stoichiometry to sys…
Browse files Browse the repository at this point in the history
…tems API results
  • Loading branch information
uthpalaherath committed Nov 5, 2024
1 parent fe14836 commit 5ae3d1a
Showing 1 changed file with 128 additions and 112 deletions.
240 changes: 128 additions & 112 deletions materials/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class BaseSerializer(serializers.ModelSerializer):

class Meta:
fields = (
'created',
'created_by',
'updated',
'updated_by',
"created",
"created_by",
"updated",
"updated_by",
)


class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = models.Author
fields = ('first_name', 'last_name', 'institution')
fields = ("first_name", "last_name", "institution")


class ReferenceSerializer(serializers.ModelSerializer):
Expand All @@ -29,28 +29,28 @@ class ReferenceSerializer(serializers.ModelSerializer):
class Meta:
model = models.Reference
fields = (
'pk',
'authors',
'title',
'journal',
'vol',
'pages_start',
'pages_end',
'year',
'doi_isbn',
"pk",
"authors",
"title",
"journal",
"vol",
"pages_start",
"pages_end",
"year",
"doi_isbn",
)


class PropertySerializer(serializers.ModelSerializer):
class Meta:
model = models.Property
fields = ('pk', 'name', 'method')
fields = ("pk", "name", "method")


class UnitSerializer(serializers.ModelSerializer):
class Meta:
model = models.Unit
fields = ('pk', 'label')
fields = ("pk", "label")


class ComputationalSerializer(BaseSerializer):
Expand All @@ -59,15 +59,15 @@ class ComputationalSerializer(BaseSerializer):
class Meta:
model = models.ComputationalDetails
fields = BaseSerializer.Meta.fields + (
'pk',
'code',
'level_of_theory',
'xc_functional',
'k_point_grid',
'level_of_relativity',
'basis_set_definition',
'numerical_accuracy',
'comment',
"pk",
"code",
"level_of_theory",
"xc_functional",
"k_point_grid",
"level_of_relativity",
"basis_set_definition",
"numerical_accuracy",
"comment",
)


Expand All @@ -77,11 +77,11 @@ class SynthesisSerializer(BaseSerializer):
class Meta:
model = models.SynthesisMethod
fields = BaseSerializer.Meta.fields + (
'pk',
'starting_materials',
'product',
'description',
'comment',
"pk",
"starting_materials",
"product",
"description",
"comment",
)


Expand All @@ -91,21 +91,21 @@ class ExperimentalSerializer(BaseSerializer):
class Meta:
model = models.ExperimentalDetails
fields = BaseSerializer.Meta.fields + (
'pk',
'method',
'description',
'comment',
"pk",
"method",
"description",
"comment",
)


class NumericalValueSerializer(BaseSerializer):
qualifier = serializers.CharField(source='get_qualifier_display')
qualifier = serializers.CharField(source="get_qualifier_display")

class Meta:
model = models.NumericalValue
fields = (
'qualifier',
'formatted',
"qualifier",
"formatted",
)


Expand All @@ -114,131 +114,147 @@ class DatapointSerializer(BaseSerializer):

class Meta:
model = models.Datapoint
fields = (
'values',
)
fields = ("values",)


class FixedValueSerializer(BaseSerializer):
class Meta:
model = models.NumericalValueFixed
depth = 1
fields = (
'physical_property',
'unit',
'subset',
'error',
'upper_bound',
'formatted',
)
"physical_property",
"unit",
"subset",
"error",
"upper_bound",
"formatted",
)


class SubsetSerializer(BaseSerializer):
crystal_system = serializers.CharField(source='get_crystal_system_display')
crystal_system = serializers.CharField(source="get_crystal_system_display")
datapoints = DatapointSerializer(many=True)
fixed_values = FixedValueSerializer(many=True)

class Meta:
model = models.Subset
depth = 1
fields = (
'pk',
'label',
'crystal_system',
'fixed_values',
'datapoints',
"pk",
"label",
"crystal_system",
"fixed_values",
"datapoints",
)


class DatasetSerializer(BaseSerializer):
sample_type = serializers.CharField(source='get_sample_type_display')
sample_type = serializers.CharField(source="get_sample_type_display")
subsets = SubsetSerializer(many=True)

class Meta:
model = models.Dataset
depth = 1
fields = BaseSerializer.Meta.fields + (
'pk',
'caption',
'system',
'primary_property',
'primary_unit',
'secondary_property',
'secondary_unit',
'reference',
'visible',
'is_experimental',
'dimensionality',
'sample_type',
'extraction_method',
'representative',
'linked_to',
'verified_by',
'computational',
'synthesis',
'experimental',
'subsets',
'space_group',
"pk",
"caption",
"system",
"primary_property",
"primary_unit",
"secondary_property",
"secondary_unit",
"reference",
"visible",
"is_experimental",
"dimensionality",
"sample_type",
"extraction_method",
"representative",
"linked_to",
"verified_by",
"computational",
"synthesis",
"experimental",
"subsets",
"space_group",
)


class DatasetSerializerInfo(serializers.ModelSerializer):
sample_type = serializers.CharField(source='get_sample_type_display')
sample_type = serializers.CharField(source="get_sample_type_display")

class Meta:
model = models.Dataset
depth = 1
fields = (
'pk',
'caption',
'system',
'primary_property',
'primary_unit',
'secondary_property',
'secondary_unit',
'reference',
'is_experimental',
'dimensionality',
'sample_type',
'extraction_method',
"pk",
"caption",
"system",
"primary_property",
"primary_unit",
"secondary_property",
"secondary_unit",
"reference",
"is_experimental",
"dimensionality",
"sample_type",
"extraction_method",
)


class DatasetSerializerSummary(serializers.ModelSerializer):
sample_type = serializers.CharField(source='get_sample_type_display')
sample_type = serializers.CharField(source="get_sample_type_display")

class Meta:
model = models.Dataset
fields = (
'pk',
'caption',
'system',
'primary_property',
'primary_unit',
'secondary_property',
'secondary_unit',
'reference',
'is_experimental',
'dimensionality',
'sample_type',
"pk",
"caption",
"system",
"primary_property",
"primary_unit",
"secondary_property",
"secondary_unit",
"reference",
"is_experimental",
"dimensionality",
"sample_type",
)


# Serializer for System_Stoichiometry (single field for stoichiometry)
class SystemStoichiometrySerializer(serializers.ModelSerializer):
class Meta:
model = models.System_Stoichiometry
fields = ("stoichiometry",)


# SystemSerializer with a custom field for stoichiometry
class SystemSerializer(serializers.ModelSerializer):
#datasets = DatasetSerializer(required = False, many=True)
stoichiometry = serializers.SerializerMethodField()

class Meta:
model = models.System
fields = (
'pk',
'compound_name',
'formula',
'group',
'organic',
'inorganic',
'iupac',
'last_update',
'derived_to_from',
'description',
#'datasets',
"pk",
"compound_name",
"formula",
"group",
"organic",
"inorganic",
"iupac",
"last_update",
"derived_to_from",
"description",
"dimensionality",
"stoichiometry",
)

def get_stoichiometry(self, obj):
# Retrieve the first related System_Stoichiometry instance
stoichiometry_set = obj.system_stoichiometry_set.first()
if stoichiometry_set:
# Return only the stoichiometry value as a plain string
return stoichiometry_set.stoichiometry
else:
return None # or a default message if no stoichiometry is found

0 comments on commit 5ae3d1a

Please sign in to comment.