Refactor activity feed template logic

Yay for removing arbitrary if/else chains!
This commit is contained in:
2020-05-30 14:00:26 +01:00
parent 156e639bac
commit e45324f5b4
5 changed files with 48 additions and 59 deletions

View File

@@ -609,5 +609,9 @@ class RiskAssessment(models.Model):
# Blimey that was a lot of options # Blimey that was a lot of options
@property
def activity_feed_string(self):
return str(self.event)
def __str__(self): def __str__(self):
return "%i: %s" % (self.pk, self.event) return "%i - %s" % (self.pk, self.event)

View File

@@ -176,18 +176,14 @@ class Asset(models.Model, RevisionMixin):
errdict["csa"] = ["The CSA of a cable must be more than 0"] errdict["csa"] = ["The CSA of a cable must be more than 0"]
if not self.cable_type: if not self.cable_type:
errdict["cable_type"] = ["A cable must have a type"] errdict["cable_type"] = ["A cable must have a type"]
# if not self.circuits or self.circuits <= 0:
# errdict["circuits"] = ["There must be at least one circuit in a cable"]
# if not self.cores or self.cores <= 0:
# errdict["cores"] = ["There must be at least one core in a cable"]
# if self.socket is None:
# errdict["socket"] = ["A cable must have a socket"]
# if self.plug is None:
# errdict["plug"] = ["A cable must have a plug"]
if errdict != {}: # If there was an error when validation if errdict != {}: # If there was an error when validation
raise ValidationError(errdict) raise ValidationError(errdict)
@property
def activity_feed_string(self):
return str(self)
@receiver(pre_save, sender=Asset) @receiver(pre_save, sender=Asset)
def pre_save_asset(sender, instance, **kwargs): def pre_save_asset(sender, instance, **kwargs):

View File

@@ -38,10 +38,7 @@
Changed {% include 'version_changes.html' %} in Changed {% include 'version_changes.html' %} in
{% endif %} {% endif %}
{% include 'object_button.html' with object=version.changes.new %} {% include 'partials/object_button.html' with object=version.changes.new %}
{% if version.revision.comment %}
({{ version.revision.comment }})
{% endif %}
</small> </small>
</p> </p>
@@ -49,5 +46,4 @@
</div> </div>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@@ -1,10 +0,0 @@
{% load to_class_name from filters %}
{# pass in variable "object" to this template #}
{% if object.name %}
<a title="{% if object.is_rig == False %}Non-rig{% elif object.dry_hire %}Dry Hire{% elif object.is_rig %}Rig{%else%}{{object|to_class_name}}{% endif %} | '{{object.name}}'" href="{{ object.get_absolute_url }}">{% if object.is_rig == False %}Non-rig{% elif object.dry_hire %}Dry Hire{% elif object.is_rig %}Rig{%else%}{{object|to_class_name}}{% endif %} | '{{ object.activity_feed_string|default:object.name }}'</a>
{% elif object.asset_id %}
<a href="{{ object.get_absolute_url }}">Asset | {{object.asset_id }} {{object.description }}</a>
{% elif object.event %}
<a href="{{ object.get_absolute_url }}">{{object|to_class_name}} | {{object.event.name }}</a>
{% endif %}

View File

@@ -0,0 +1,3 @@
{% load to_class_name from filters %}
{# pass in variable "object" to this template #}
<a href="{{ object.get_absolute_url }}">{% if object.is_rig == False %}Non-rig{% elif object.dry_hire %}Dry Hire{% elif object.is_rig %}Rig{%else%}{{object|to_class_name}}{% endif %} | '{% if object.activity_feed_string %}{{object.activity_feed_string}}{% else %}{{object.name}}{%endif%}'</a>