mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor "ownership" concepts in templates
- Template code was very messy - Makes it a lot "simpler" - Adds convenience functions to the StockLocation model - Adds pre-calculated data to the template - Display ownership information on the stocklocation page
This commit is contained in:
parent
0da5957c50
commit
d6764573c3
@ -63,6 +63,43 @@ class StockLocation(InvenTreeTree):
|
||||
help_text=_('Select Owner'),
|
||||
related_name='stock_locations')
|
||||
|
||||
def get_location_owner(self):
|
||||
"""
|
||||
Get the closest "owner" for this location.
|
||||
|
||||
Start at this location, and traverse "up" the location tree until we find an owner
|
||||
"""
|
||||
|
||||
for loc in self.get_ancestors(include_self=True, ascending=True):
|
||||
if loc.owner is not None:
|
||||
return loc.owner
|
||||
|
||||
return None
|
||||
|
||||
def check_ownership(self, user):
|
||||
"""
|
||||
Check if the user "owns" (is one of the owners of) the location.
|
||||
"""
|
||||
|
||||
# Superuser accounts automatically "own" everything
|
||||
if user.is_superuser:
|
||||
return True
|
||||
|
||||
ownership_enabled = common.models.InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
|
||||
|
||||
if not ownership_enabled:
|
||||
# Location ownership function is not enabled, so return True
|
||||
return True
|
||||
|
||||
owner = self.get_location_owner()
|
||||
|
||||
if owner is None:
|
||||
# No owner set, for this location or any location above
|
||||
# So, no ownership checks to perform!
|
||||
return True
|
||||
|
||||
return user in owner.get_related_owners(include_group=True)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('stock-location-detail', kwargs={'pk': self.id})
|
||||
|
||||
|
@ -20,7 +20,6 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block actions %}
|
||||
{% setting_object 'STOCK_OWNERSHIP_CONTROL' as owner_control %}
|
||||
|
||||
<!-- Admin view -->
|
||||
{% if location and user.is_staff and roles.stock_location.change %}
|
||||
@ -40,7 +39,7 @@
|
||||
</ul>
|
||||
</div>
|
||||
<!-- Check permissions and owner -->
|
||||
{% if owner_control.value == "False" or owner_control.value == "True" and user in owners or user.is_superuser %}
|
||||
{% if user_owns_location %}
|
||||
{% if roles.stock.change %}
|
||||
<div class='btn-group' role='group'>
|
||||
<button id='stock-actions' title='{% trans "Stock actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
|
||||
@ -76,13 +75,11 @@
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if owner_control.value == "False" or owner_control.value == "True" and user in owners or user.is_superuser or not location %}
|
||||
{% if roles.stock_location.add %}
|
||||
{% if user_owns_location and roles.stock_location.add %}
|
||||
<button class='btn btn-success' id='location-create' type='button' title='{% trans "Create new stock location" %}'>
|
||||
<span class='fas fa-plus-circle'></span> {% trans "New Location" %}
|
||||
</button>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block details_left %}
|
||||
@ -108,23 +105,23 @@
|
||||
<td><em>{% trans "Top level stock location" %}</em></td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% if ownership_enabled and location_owner %}
|
||||
<tr>
|
||||
<td><span class='fas fa-users'></span></td>
|
||||
<td>{% trans "Location Owner" %}</td>
|
||||
<td>
|
||||
{{ location_owner }}
|
||||
{% if not user_owns_location %}
|
||||
<span class='badge rounded-pill bg-warning badge-right' title='{% trans "You are not in the list of owners of this location. This stock location cannot be edited." %}'>
|
||||
{% trans "Read Only" %}
|
||||
</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</table>
|
||||
{% endblock details_left %}
|
||||
|
||||
{% block details_below %}
|
||||
{% setting_object 'STOCK_OWNERSHIP_CONTROL' as owner_control %}
|
||||
{% if owner_control.value == "True" %}
|
||||
{% authorized_owners location.owner as owners %}
|
||||
|
||||
{% if location and not user in owners and not user.is_superuser %}
|
||||
<div class='alert alert-block alert-info'>
|
||||
{% trans "You are not in the list of owners of this location. This stock location cannot be edited." %}<br>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% endblock details_below %}
|
||||
|
||||
{% block details_right %}
|
||||
{% if location %}
|
||||
<table class='table table-striped table-condensed'>
|
||||
|
@ -63,6 +63,11 @@ class StockIndex(InvenTreeRoleMixin, ListView):
|
||||
context['loc_count'] = StockLocation.objects.count()
|
||||
context['stock_count'] = StockItem.objects.count()
|
||||
|
||||
# No 'ownership' checks are necessary for the top-level StockLocation view
|
||||
context['user_owns_location'] = True
|
||||
context['location_owner'] = None
|
||||
context['ownership_enabled'] = common.models.InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
|
||||
|
||||
return context
|
||||
|
||||
|
||||
@ -76,6 +81,16 @@ class StockLocationDetail(InvenTreeRoleMixin, DetailView):
|
||||
queryset = StockLocation.objects.all()
|
||||
model = StockLocation
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
context['ownership_enabled'] = common.models.InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
|
||||
context['location_owner'] = context['location'].get_location_owner()
|
||||
context['user_owns_location'] = context['location'].check_ownership(self.request.user)
|
||||
|
||||
return context
|
||||
|
||||
|
||||
class StockItemDetail(InvenTreeRoleMixin, DetailView):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user