uml py
class UMLClass(object):
def init(self, package: UMLPackage, name: str, id: Union[int, str]):
...
self.composed_of: List[UMLClass] = []
parse py
54:
package_sort_classes(model_package)
256:
def package_sort_classes(package):
unordered_list = package.classes
unordered_composed_list = []
for cls in unordered_list:
if cls.composed_of != []:
unordered_composed_list.append(cls)
package.classes.remove(cls)
# This is crap, do some propper sorting
unordered_list = unordered_composed_list
composed_list = []
for cls1 in unordered_list:
for cls2 in cls1.composed_of:
if cls2 in unordered_composed_list:
unordered_composed_list.remove(cls2)
composed_list.append(cls2)
composed_list += unordered_composed_list
package.classes += composed_list
for child in package.children:
package_sort_classes(child)
serializers py jinja
from rest_framework import serializers
from drf_nest.serializer_fields import TypeField
{% for cls in package.classes %}
from .models import {{ cls.name | case_class }}{% endfor %}
{% for cls in package.classes %}
class {{ cls.name | case_class }}Serializer(serializers.HyperlinkedModelSerializer):
type = TypeField()
{% for assoc in cls.associations_to %}{% if assoc.association_type.name == "COMPOSITION" %}
{{ assoc.source_name | snakecase }} = {{ assoc.source.name | case_class }}Serializer(required=False{% if assoc.cardinality.name == "MANY_TO_ONE" %}, many=True{% endif %}){% endif %}{% endfor %}
class Meta:
model = {{ cls.name | case_class }}
fields = ('type', 'url',
{% for attr in cls.attributes %}'{{ attr.name | snakecase }}',{% endfor %}{% if cls.generalization != None %}{% for attr in cls.generalization.attributes %}'{{ attr.name | snakecase }}',{% endfor %}{% endif %}
{% for assoc in cls.associations_from %}'{{ assoc.destination_name | snakecase }}',{% endfor %}
{% for assoc in cls.associations_to %}'{{ assoc.source_name | snakecase }}',{% endfor %}
)
{% endfor %}
models py jinja
from django.utils.translation import gettext_lazy as _
from django.db import models
from simple_history.models import HistoricalRecords
from {{ package.root_package.name | snakecase }}.validators import validate_even
{% for cls in package.classes %}{% if cls.generalization != None %}{% if cls.generalization.package != package %}
from {{ cls.generalization.package.name | case_package }}.models import {{ cls.generalization.name | case_class }}{% endif %}{% endif %}{% endfor %}
{% for enum in package.enumerations %}class ENUM_{{ enum.name | case_class }}(models.TextChoices):
{% for attr in enum.values %}
{{ attr.replace('-','').replace('/','') | snakecase | uppercase }} = '{{ attr }}', _('{{ attr }}'){% endfor %}
{% endfor %}
{% for cls in package.classes %}{% if cls.is_abstract %}class {{ cls.name | case_class }}( models.Model ):
{% for attr in cls.attributes %}
{% if attr.classification %} {{ attr.name | snakecase }} = models.CharField( max_length=100, choices=ENUM_{{ attr.classification.name | case_class }}.choices, blank=True, null=True )
{% else %} {{ attr.name | snakecase }} = models.{% if attr.stereotype == "auto" %}AutoField{% else %}{{ attr.dest_type }}{% endif %}( {% if attr.is_id %}primary_key=True, {% else %}blank=True, null=True, {% endif %}{% if attr.length %}max_length={{ attr.length }}{% endif %} )
{% endif %}{% endfor %}
{% if cls.is_abstract %} class Meta:
abstract = True
{% endif %}{% endif %}
{% endfor %}
{% for cls in package.classes %}{% if not cls.is_abstract %}class {{ cls.name | case_class }}( {% if cls.generalization %}{{ cls.generalization.name | case_class }}{% else %}models.Model{% endif %} ):
{% for attr in cls.attributes %}
{% if attr.classification %} {{ attr.name | snakecase }} = models.CharField( max_length=100, choices=ENUM_{{ attr.classification.name | case_class }}.choices, blank=True, null=True )
{% else %} {{ attr.name | snakecase }} = models.{% if attr.stereotype == "auto" %}AutoField{% else %}{{ attr.dest_type }}{% endif %}( {% if attr.is_id %}primary_key=True, {% else %}blank=True, null=True, {% endif %}{% if attr.dest_type == "DecimalField" %}max_digits=10, decimal_places=2, {% endif %}{% if attr.length %}max_length={{ attr.length }}{% endif %}{% if attr.validations != [] %}validators=[validate_even]{% endif %} )
{% endif %}{% endfor %}
{% for rel in cls.associations_from %}{% if rel.association_type.name == "ASSOCIATION" %}
{% if rel.cardinality.name == "ONE_TO_ONE" %}{{ rel.destination_name | snakecase }} = models.OneToOneField( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', on_delete=models.CASCADE, blank=True, null=True )
{% elif rel.cardinality.name == "ONE_TO_MANY" %}{{ rel.destination_name | snakecase }} = models.ForeignKey( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', related_name='{{ rel.destination_name | snakecase }}', on_delete=models.CASCADE, blank=True, null=True )
{% elif rel.cardinality.name == "MANY_TO_MANY" %}{{ rel.destination_name | snakecase }} = models.ManyToManyField( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', blank=True ){% endif %}
{% elif rel.association_type.name == "COMPOSITION" %}
{% if rel.cardinality.name == "ONE_TO_ONE" %}{{ rel.destination_name | snakecase }} = models.OneToOneField( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', related_name='{{ rel.source_name | snakecase }}', on_delete=models.CASCADE )
{% else %}{{ rel.destination_name | snakecase }} = models.ForeignKey( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', related_name='{{ rel.source_name | snakecase }}', on_delete=models.CASCADE ){% endif %}
{% endif %}{% endfor %}
{% for rel in cls.associations_to %}{% if rel.association_type.name != "COMPOSITION" %}
{% if rel.cardinality.name == "MANY_TO_ONE" %}{{ rel.source_name | snakecase }} = models.ForeignKey( '{{ rel.source.package.name | case_package }}.{{ rel.source.name | case_class }}', on_delete=models.CASCADE, related_name='{{ rel.destination_name | snakecase }}', blank=True, null=True ){% endif %}
{% endif %}{% endfor %}
{% if 'auditable' in cls.stereotypes %}
history = HistoricalRecords(){% endif%}
{% if cls.is_abstract %} class Meta:
abstract = True
{% endif %}{% endif %}
{% endfor %}