Add tag views

This commit is contained in:
Johannes Schriewer 2025-01-06 01:00:56 +01:00
parent 03a10e87c3
commit 417606c214
4 changed files with 199 additions and 4 deletions

View file

@ -29,6 +29,9 @@
<li>
<a href="{% url 'distributor-list' %}"><img class="icon" title="Distributors" src="{% static "inventory/img/distributor.svg" %}"></a>
</li>
<li>
<a href="{% url 'tag-list' %}"><img class="icon" title="Tags" src="{% static "inventory/img/tags.svg" %}"></a>
</li>
<li>
<a href="{% url 'workshop-list' %}"><img class="icon" title="Workshops" src="{% static "inventory/img/workshop.svg" %}"></a>
</li>

View file

@ -0,0 +1,118 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Tag {{ tag.name }}{% endblock %}
{% block header_bar %}
<a href="{% url "tag-list" %}"><img class="icon" src="{% static "inventory/img/back.svg" %}"></a>
{{ tag.name }}
<span class="small">{{ tag.description}}</span>
{% endblock %}
{% block header_icons %}
{% if user.is_staff %}
<li>
<a href="{% url "admin:inventory_tag_change" object_id=tag.pk %}"><img class="icon" src="{% static "inventory/img/edit.svg" %}"></a>
</li>
{% endif %}
{% endblock %}
{% block content %}
{% if tag.workshop_set.count > 0 %}
<h3>Workshops</h3>
<table class="list">
<tbody>
{% for workshop in tag.workshop_set.all %}
<tr>
<td>
<a href="{% url 'workshop-detail' workshop.id %}" title="{{ workshop.description }}">{{ workshop.name }}</a>
</td>
<td>
{% if user.is_staff %}
<a class="edit" href="{% url "admin:inventory_workshop_change" object_id=workshop.pk %}"><img class="icon" src="{% static 'inventory/img/edit.svg' %}"></a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if tag.box_set.count > 0 %}
<h3>Boxes</h3>
<table class="list">
<tbody>
{% for box in tag.box_set.all %}
<tr>
<td>
<a href="{% url 'box-detail' box.id %}" title="{{ box.description }}">{{ box.name }}</a>
</td>
<td>
{% if user.is_staff %}
<a class="edit" href="{% url "admin:inventory_workshop_change" object_id=box.pk %}"><img class="icon" src="{% static 'inventory/img/edit.svg' %}"></a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if tag.distributor_set.count > 0 %}
<h3>Distributors</h3>
<table class="list">
<tbody>
{% for distributor in tag.distributor_set.all %}
<tr>
<td>
<a href="{% url 'distributor-detail' distributor.id %}" title="{{ distributor.description }}">{{ distributor.name }}</a>
</td>
<td>
{% if user.is_staff %}
<a class="edit" href="{% url "admin:inventory_workshop_change" object_id=distributor.pk %}"><img class="icon" src="{% static 'inventory/img/edit.svg' %}"></a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if tag.manufacturer_set.count > 0 %}
<h3>Manufacturers</h3>
<table class="list">
<tbody>
{% for manufacturer in tag.manufacturer_set.all %}
<tr>
<td>
<a href="{% url 'manufacturer-detail' manufacturer.id %}" title="{{ manufacturer.description }}">{{ manufacturer.name }}</a>
</td>
<td>
{% if user.is_staff %}
<a class="edit" href="{% url "admin:inventory_workshop_change" object_id=manufacturer.pk %}"><img class="icon" src="{% static 'inventory/img/edit.svg' %}"></a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% if items %}
<h3>Items</h3>
{% url 'tag-detail' tag.id as this_url %}
{% include "inventory/pagination.html" with url=this_url id="items_paginator_top" param="item" paginator=items %}
{% include "inventory/item_list.html" with items=items show_manufacturer=1 show_distributor=1 %}
{% include "inventory/pagination.html" with url=this_url id="items_paginator_bottom" param="item" paginator=items %}
{% endif %}
{% if tag.formfactor_set.count > 0 %}
<h3>Form factors</h3>
<ul class="compact-list">
{% for formfactor in tag.formfactor_set.all %}
<li>{{ formfactor.name }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

View file

@ -0,0 +1,58 @@
{% extends "base.html" %}
{% load static %}
{% load formatstring %}
{% block title %}Tags{% endblock %}
{% block header_bar %}
Tags
{% endblock %}
{% block content %}
<script>
function updateSearch(e) {
const input = document.getElementById('search');
const text = input.value.toLowerCase();
if (text.length >= 2) {
const tagcloud = document.getElementById('tagcloud');
const items = tagcloud.getElementsByTagName('li');
for(i = 0; i < items.length; i++) {
const element = items[i];
if (element.dataset.search.includes(text)) {
element.style.display = "inline-block";
} else {
element.style.display = "none";
}
}
} else {
const tagcloud = document.getElementById('tagcloud');
const items = tagcloud.getElementsByTagName('li');
for(i = 0; i < items.length; i++) {
const element = items[i];
element.style.display = "inline-block";
}
}
if (e) {
e.preventDefault();
}
}
</script>
<form>
<label for="search">Search:</label>
<input type="text" id="search" name="search" oninput="updateSearch(event)">
<button onclick="updateSearch(event)">Search</button>
</form>
<ul class="tag-list" id="tagcloud">
{% for tag in object_list %}
<li data-search="{{ tag.name | lower }} {{ tag.description | lower }}"><a href="{% url 'tag-detail' tag.id %}" title="{{ tag.name }}">{{ tag.name }}</a></li>
{% empty %}
No tags
{% endfor %}
</ul>
{% if user.is_staff %}
<p><a href="{% url "admin:inventory_tag_add" %}?layout=1">Create new tag...</a></p>
{% endif %}
{% endblock %}

View file

@ -1,13 +1,29 @@
from django.views import View
from django.core.paginator import Paginator
from django.db.models import Q
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import ListView, DetailView
from django.conf import settings
from inventory.models import Tag
from inventory.models import Tag, Item
class TagView(View):
pass
@method_decorator(login_required, name='dispatch')
class TagView(DetailView):
context_object_name = 'tag'
queryset = Tag.objects.all().prefetch_related('workshop_set', 'box_set', 'distributor_set', 'manufacturer_set', 'formfactor_set')
def get_context_data(self, *args, object_list=None, **kwargs):
result = super().get_context_data(*args, object_list=object_list, **kwargs)
p = self.request.GET.get("item_page", 1)
direct_tags = Q(tags__in=[self.get_object()])
formfactor_tags = Q(form_factor__tags__in=[self.get_object()])
items = Item.objects.filter(direct_tags | formfactor_tags).distinct().select_related('container', 'manufacturer')
paginator = Paginator(items, getattr(settings, "PAGE_SIZE", 10))
result.update({
"items": paginator.get_page(p)
})
return result
@method_decorator(login_required, name='dispatch')
class TagListView(ListView):