Skip to content

Commit

Permalink
Fixes #82
Browse files Browse the repository at this point in the history
  • Loading branch information
Semprini committed May 18, 2024
1 parent ee8ba5a commit aaaccd4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 145 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ This diagram shows all the features and how to model in UML

## Metamodel
This diagram shows the internal classes which are passed to the templates during generation.
![Metamodel](https://github.com/Semprini/pyMDG/raw/master/sample_recipes/images/EAID_B080F856_9EFB_46f2_8D69_1C79956D714A.png)
![Metamodel](https://raw.githubusercontent.com/Semprini/pyMDG/master/docs/_static/image/metamodel.png)

## Build the docs
Install sphinx
Expand Down
Binary file added docs/_static/image/metamodel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/docs.qea
Binary file not shown.
3 changes: 0 additions & 3 deletions mdg/parse/sparx_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,10 @@ def association_parse(session, tconnector: TConnector, package: UMLPackage, sour
# TODO: Extract list of stereotypes
if tconnector.stereotype is not None:
association.stereotypes = [tconnector.stereotype,]
print(association.stereotypes)
if tconnector.sourcestereotype is not None:
association.source_stereotypes = [tconnector.sourcestereotype,]
print(association.source_stereotypes)
if tconnector.deststereotype is not None:
association.destination_stereotypes = [tconnector.deststereotype,]
print(association.destination_stereotypes)

logging.debug(f"Created {association.cardinality.name} {association.association_type.name} from {association.source_name} to {association.destination_name}")
return association
Expand Down
26 changes: 19 additions & 7 deletions mdg/templates/Django/app/models.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ from {{ attr.classification.package.name | case_package }}.models import ENUM_{{

{% 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}}.choices, blank=True, null=True ){% else %}
{{ attr.name | snakecase }} = models.{% if "auto" in attr.stereotypes %}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 %} )
{{ attr.name | snakecase }} = models.{% if "Auto" in attr.stereotypes %}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 %}
class Meta:
abstract = True
{% endif %}{% endfor %}

{% for cls in package.classes %}{% if not cls.is_abstract and cls.specialized_by | length > 0 %}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 "auto" in attr.stereotypes %}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 %} )
{{ attr.name | snakecase }} = models.{% if "Auto" in attr.stereotypes %}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 == "MANY_TO_ONE" %}
{{ 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, blank=True, null=True ){% elif rel.cardinality.name == "MANY_TO_MANY" %}
Expand All @@ -27,12 +27,18 @@ from {{ attr.classification.package.name | case_package }}.models import ENUM_{{
{{ 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 == "ONE_TO_MANY" %}
{{ 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 %}{% endif %}
{% endif %}{% endfor %}{% set uniques = [] %}{% for attr in cls.attributes %}{% if "UniqueKey" in attr.stereotypes %}{{ uniques.append( attr.name ) | default("", True) }}{% endif %}{% endfor %}{% for rel in cls.associations_from %}{% if "UniqueKey" in rel.destination_stereotypes %}{{ uniques.append( rel.destination_name ) | default("", True) }}{% endif %}{% endfor %}{% if uniques != [] %}

{% endfor %}
class Meta():
constraints = [
models.UniqueConstraint(fields=[{% for unique in uniques%}'{{ unique | snakecase }}',{% endfor %}], name='unique {% for unique in uniques%}{{ unique | snakecase }}__{% endfor %}')
]
{% endif %}

{% endif %}{% endfor %}
{% for cls in package.classes %}{% if not cls.is_abstract and cls.specialized_by | length == 0 %}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 "auto" in attr.stereotypes %}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 %} )
{{ attr.name | snakecase }} = models.{% if "Auto" in attr.stereotypes %}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 == "MANY_TO_ONE" %}
{{ 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, blank=True, null=True ){% elif rel.cardinality.name == "MANY_TO_MANY" %}
Expand All @@ -41,6 +47,12 @@ from {{ attr.classification.package.name | case_package }}.models import ENUM_{{
{{ 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 == "ONE_TO_MANY" %}
{{ 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 %}{% endif %}
{% endif %}{% endfor %}{% set uniques = [] %}{% for attr in cls.attributes %}{% if "UniqueKey" in attr.stereotypes %}{{ uniques.append( attr.name ) | default("", True) }}{% endif %}{% endfor %}{% for rel in cls.associations_from %}{% if "UniqueKey" in rel.destination_stereotypes %}{{ uniques.append( rel.destination_name ) | default("", True) }}{% endif %}{% endfor %}{% if uniques != [] %}

{% endfor %}
class Meta():
constraints = [
models.UniqueConstraint(fields=[{% for unique in uniques%}'{{ unique | snakecase }}',{% endfor %}], name='unique {% for unique in uniques%}{{ unique | snakecase }}__{% endfor %}')
]
{% endif %}

{% endif %}{% endfor %}
Loading

0 comments on commit aaaccd4

Please sign in to comment.