Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue732 creaing item category #752

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion inventory/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from .models import Item, ItemLoan
from .models import Item, ItemCategory, ItemLoan


@admin.register(Item)
Expand All @@ -18,6 +18,7 @@ class ItemAdmin(admin.ModelAdmin):
"location",
"can_loan",
"max_loan_duration",
"category",
]
},
),
Expand All @@ -40,3 +41,4 @@ class ItemLoanAdmin(admin.ModelAdmin):


admin.site.register(ItemLoan, ItemLoanAdmin)
admin.site.register(ItemCategory)
27 changes: 27 additions & 0 deletions inventory/migrations/0029_item_category_name_and_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.13 on 2023-03-14 19:00

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('inventory', '0028_alter_itemloan_contact_phone'),
]

operations = [
migrations.CreateModel(
name='ItemCategory',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('category_name', models.CharField(max_length=50)),
('category_description', models.CharField(max_length=200)),
],
),
migrations.AddField(
model_name='item',
name='category',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='inventory.itemcategory', verbose_name='Category'),
),
]
19 changes: 19 additions & 0 deletions inventory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
from applications.validators import validate_phone_number
from files.models import Image

""" Model to store the item`s category"""


class ItemCategory(models.Model):
category_name = models.CharField(max_length=50)
"""Contains information about category of item"""
category_description = models.CharField(max_length=200)

def __str__(self):
return self.category_name + " (" + str(self.category_description) + ")"


class Item(models.Model):
"""Represents a single item in inventory"""
Expand All @@ -17,6 +28,13 @@ class Item(models.Model):
unknown_stock = models.BooleanField(
"Ukjent lagerbeholdning", null=False, blank=False, default=False
)
category = models.ForeignKey(
ItemCategory,
on_delete=models.SET_NULL,
verbose_name="Category",
null=True,
blank=True,
)
can_loan = models.BooleanField("Kan lånes", null=False, blank=False, default=True)
description = RichTextUploadingField("Beskrivelse", blank=True)
thumbnail = models.ForeignKey(
Expand Down Expand Up @@ -98,6 +116,7 @@ class ItemLoan(models.Model):
item = models.ForeignKey(
Item, on_delete=models.CASCADE, verbose_name="Lånegjenstand"
)

amount = models.IntegerField("Antall", validators=[MinValueValidator(1)])

# Automatically set once the application is accepted
Expand Down
36 changes: 34 additions & 2 deletions inventory/templates/inventory/inventory.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ <h4>Lager</h4>
<label for="filter">Navn</label>
</div>
<div class="input-field col s12 m6 l5">
<select name="sort_by" onchange="updateSortBy(this.value)">
<select id="selectSearchBar" name="sort_by" onchange="updateSortBy(this.value)">
<option value="" disabled {% if not sort_by %}selected{% endif %}>Velg sorteringskriterie</option>
<option value="popularity" {% if sort_by == "popularity" %}selected{% endif %}>Popularitet</option>
<option value="stock_dsc" {% if sort_by == "stock_dsc" %}selected{% endif %}>Lagerbeholdning (Synkende)</option>
<option value="stock_asc" {% if sort_by == "stock_asc" %}selected{% endif %}>Lagerbeholdning (Stigende)</option>
<option value="category" {% if sort_by == "category" %}selected{% endif %}>Category</option>
<option value="name" {% if sort_by == "name" %}selected{% endif %}>Navn</option>
</select>
<label>Sorter etter</label>
</div>
<br>
{% if sort_by == "category" %}
<div class="input-field col s12 m6 l6">
<select id="category_filter" onchange="updateCategory(this.value)">

</select>
<label>Category:</label>
</div>
{% endif %}
</div>


</form>

<div id="items_list"></div>
Expand All @@ -51,6 +61,8 @@ <h4>Lager</h4>
let page_number = {{ page }};
let sort_by = "{{ sort_by }}";



document.addEventListener('DOMContentLoaded', function () {
updateList();
});
Expand Down Expand Up @@ -97,6 +109,26 @@ <h4>Lager</h4>
$('#items_list').html(data);
}
});

$.ajax({
url: '/api/inventory/categories',
type: 'GET',
success: function(response) {
let categories = response.category;
let select = document.getElementById('category_filter');
let option
console.log(categories.length)
for (let i = 0; i<categories.length;i++){
if (categories[i]) {
option = document.createElement('option');
option.value = i;
option.text = categories[i].category_name;
select.appendChild(option);
console.log(categories[i].category_name)
}
}
}
});

// Update url to allow direct links to search
let state = window.history.state;
Expand Down
4 changes: 4 additions & 0 deletions inventory/templates/inventory/item_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ <h6>
Hylleplass <b>{{ item.location }}</b>.
{% endif %}

{% if item.category %}
Category: <b>{{ item.category.category_name }}</b>
{% endif %}

{% if not item.in_stock and item.next_loan_done is not None %}
<b>{{ item.next_loan_amount }} stk.</b> forventet på lager <b>{{ item.next_loan_done }}</b>
{% endif %}
Expand Down
4 changes: 3 additions & 1 deletion inventory/templates/inventory/items_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<img src="{% if item.thumbnail %}{{ item.thumbnail.thumb_url }}{% else %}{% static 'inventory/img/unknown.png'%}{% endif %}" class="circle">
<a href="{% url 'inventory:item' item.id %}">
{{ item.name }}
{% if item.category %}
<br><b style="color:silver; opacity: 0.5; font-size: smaller;">{{ item.category.category_name }}</b>
{% endif %}
<p style="color:#000 !important">
{% if item.unknown_stock %}
<i class="material-icons hs-yellow-text hide-on-small-only">question_mark</i>
Expand All @@ -24,7 +27,6 @@
<b>Ingen</b>
{% endif %}
på lager.

{% if item.has_location %}
Hylleplass <b>{{ item.location }}</b>.
{% endif %}
Expand Down
12 changes: 11 additions & 1 deletion inventory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from userprofile.models import Profile

from .models import Item, ItemLoan
from .models import Item, ItemCategory, ItemLoan


class InventoryListView(TemplateView):
Expand Down Expand Up @@ -64,6 +64,8 @@ def get(self, request):
items = items.order_by("-stock")
elif sort_by == "stock_asc":
items = items.order_by("stock")
elif sort_by == "category":
items = items.order_by("category")
elif sort_by == "popularity":
items = sorted(items, key=lambda item: -item.popularity())
else:
Expand All @@ -80,6 +82,14 @@ def get(self, request):
)


class InventoryCategory(APIView):
def get(self, request):

categories = ItemCategory.objects.values("category_name").distinct()

return Response({"category": categories})


class ItemDetailView(DetailView):
"""Detail view for individual inventory items"""

Expand Down
5 changes: 5 additions & 0 deletions website/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
inventory_views.InventoryListAPIView.as_view(),
name="inventory-api",
),
path(
"api/inventory/categories",
inventory_views.InventoryCategory.as_view(),
name="category-api",
),
path("inventory/", include("inventory.urls")),
path("vaktliste/", include("watchlist.urls")),
path("internalportal/", InternalPortalView.as_view(), name="internalportal"),
Expand Down