Skip to content

Commit

Permalink
Merge pull request #1062 from dotKom/dashboard
Browse files Browse the repository at this point in the history
Dashboard inventory module
  • Loading branch information
hernil committed Feb 11, 2015
2 parents c645bca + 807e8bd commit 9198352
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 4 deletions.
8 changes: 8 additions & 0 deletions apps/dashboard/tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- encoding: utf-8 -*-

from datetime import date

from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import Group

from apps.approval.models import MembershipApproval
from apps.inventory.models import Batch


def has_access(request):
Expand Down Expand Up @@ -48,4 +51,9 @@ def get_base_context(request):
context['approval_pending'] = MembershipApproval.objects.filter(
processed=False).count()

# Check if there exists a batch in inventory that has expired
if request.user.has_perm('inventory.view_item'):
if Batch.objects.filter(expiration_date__lt=date.today()):
context['inventory_expired'] = True

return context
Empty file.
8 changes: 8 additions & 0 deletions apps/inventory/dashboard/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- encoding: utf-8 -*-

from django.conf.urls import patterns, url

urlpatterns = patterns('apps.inventory.dashboard.views',
url(r'^$', 'index', name='inventory'),
url(r'^item/(?P<pk>\d+)/$', 'details', name='details'),
)
42 changes: 42 additions & 0 deletions apps/inventory/dashboard/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- encoding: utf-8 -*-

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.shortcuts import render, get_object_or_404

from guardian.decorators import permission_required

from apps.dashboard.tools import has_access, get_base_context
from apps.inventory.models import Item, Batch


@login_required
@permission_required('inventory.view_item', return_403=True)
def index(request):

# Generic check to see if user has access to dashboard. (In Komiteer or superuser)
if not has_access(request):
raise PermissionDenied

# Create the base context needed for the sidebar
context = get_base_context(request)

context['items'] = Item.objects.all().order_by('name')

return render(request, 'inventory/dashboard/index.html', context)


@login_required
@permission_required('inventory.change_item', return_403=True)
def details(request, pk):
# Generic check to see if user has access to dashboard. (In Komiteer or superuser)
if not has_access(request):
raise PermissionDenied

# Create the base context needed for the sidebar
context = get_base_context(request)

context['item'] = get_object_or_404(Item, pk=pk)

return render(request, 'inventory/dashboard/details.html', context)
26 changes: 23 additions & 3 deletions apps/inventory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@

from django.db import models
from django.utils.translation import ugettext as _
from django.utils import timezone

class Item(models.Model):

name = models.CharField(_(u"Varetype"), max_length=50)

@property
def oldest_expiration_date(self):
return self.batches.all().order_by("expiration_date")[0].expiration_date
batches = self.batches.all().order_by("expiration_date")
if batches:
return batches[0].expiration_date
else:
return None

@property
def last_added(self):
return self.batches.all().order_by("-date_added")[0].date_added
batches = self.batches.all().order_by("-date_added")
if batches:
return batches[0].date_added
else:
return None

@property
def total_amount(self):
return sum([batch.amount for batch in self.batches.all()])

@property
def has_expired_batch(self):
if timezone.now().date() >= self.oldest_expiration_date:
return True
return False

def __unicode__(self):
return self.name

class Meta:
verbose_name = _(u"Vare")
verbose_name_plural = _(u"Varer")
permissions = (
("view_item", u"View Inventory Item"),
)

class Batch(models.Model):

Expand All @@ -33,6 +51,8 @@ class Batch(models.Model):
date_added = models.DateField(_(u"Dato lagt til"), editable = False, auto_now_add = True)
expiration_date = models.DateField(_(u"Utløpsdato"), null=True, blank=True, editable = True)



class Meta:
verbose_name = _(u"Batch")
verbose_name_plural = _(u"Batches")
Expand Down
40 changes: 40 additions & 0 deletions files/static/js/dashboard/inventory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
The Company module exposes functionality needed in the company section
of the dashboard.
*/

var Inventory = (function ($, tools) {

// Perform self check, display error if missing deps
var performSelfCheck = function () {
var errors = false
if ($ == undefined) {
console.error('jQuery missing!')
errors = !errors
}
if (tools == undefined) {
console.error('Dashboard tools missing!')
errors = !errors
}
if (errors) return false
return true
}

return {

// Bind them buttons and other initial functionality here
init: function () {

if (!performSelfCheck()) return

$('#inventory_item_list').tablesorter()

}

}

})(jQuery, Dashboard.tools)

$(document).ready(function () {
Inventory.init()
})
7 changes: 6 additions & 1 deletion onlineweb4/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# Online Notifier Owner Verification (checked yearly or so by Google)
url(r'^google79c0b331a83a53de\.html$', lambda r: HttpResponse(
"google-site-verification: google79c0b331a83a53de.html", content_type="text/html")),

# Wiki
(r'^notify/', get_notify_pattern()),
(r'^wiki/', get_wiki_pattern())
Expand Down Expand Up @@ -97,6 +97,11 @@
url(r'^feedback/', include('apps.feedback.urls')),
)

if 'apps.inventory' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
url(r'^dashboard/inventory/', include('apps.inventory.dashboard.urls')),
)

if 'apps.offline' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
url(r'^offline/', include('apps.offline.urls')),
Expand Down
1 change: 1 addition & 0 deletions templates/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h3>Velkommen til Online Dashboard</h3>
<li><a href="auth/groups/">Grupper</a></li>
<li><a href="approval/">Søknader</a></li>
<li><a href="company/">Bedrifter</a></li>
<li><a href="inventory/">Varelager</a></li>
</ul>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions templates/dashboard_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ <h4 class="center-text"><small>Velkommen</small><br />{{ request.user.first_nam
</ul>
</li>
{% endif %}
{% if 'inventory.view_item' in user_permissions %}
<li class="treeview">
<a href="/dashboard/inventory/">
<i class="fa fa-archive blue"></i> <span>Varelager</span>
{% if inventory_expired %}
<span class="pull-right"><i class="fa fa-warning orange"></i>&nbsp;&nbsp;</span>
{% endif %}
</a>
{% endif %}
<!--<li>
<a href="#">
<i class="fa fa-bell blue"></i> <span>Til oppfølging</span>
Expand Down
51 changes: 51 additions & 0 deletions templates/inventory/dashboard/details.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{% extends "dashboard_base.html" %}
{% load compress %}

{% block title %}
Varelager - Online Dashboard
{% endblock title %}

{% block styles %}
{{ block.super }}
{% compress css %}
{% endcompress %}
{% endblock %}

{% block js %}
{{ block.super }}
{% compress js %}
<script src="{{ STATIC_URL }}js/dashboard/inventory.js"></script>
{% endcompress %}
{% endblock js %}

{% block page-header %}
Varelager
{% endblock %}

{% block breadcrumbs %}
<li>Varelager</li>
{% endblock %}

{% block content %}
<div class="row">
<div class="col-md-12">
<h3>{{ item.name }}</h3>
<table class="table table-striped table-condensed tablesorter" id="inventory_item_list">
<thead>
<tr>
<th>Mengde</th>
<th>Utløpsdato</th>
</tr>
</thead>
<tbody>
{% for batch in item.batches.all %}
<tr>
<td>{{ batch.amount }}</td>
<td>{{ batch.expiration_date|date:'Y-m-d' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock content %}
70 changes: 70 additions & 0 deletions templates/inventory/dashboard/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{% extends "dashboard_base.html" %}
{% load compress %}

{% block title %}
Varelager - Online Dashboard
{% endblock title %}

{% block styles %}
{{ block.super }}
{% compress css %}
{% endcompress %}
{% endblock %}

{% block js %}
{{ block.super }}
{% compress js %}
<script src="{{ STATIC_URL }}js/dashboard/inventory.js"></script>
{% endcompress %}
{% endblock js %}

{% block page-header %}
Varelager
{% endblock %}

{% block breadcrumbs %}
<li>Varelager</li>
{% endblock %}

{% block content %}
<div class="row">
<div class="col-md-12">
<a href="#" class="btn btn-primary"><i class="fa fa-plus"></i> Legg til vare</a>
</div>
</div>
<br />
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Vareoversikt</h3>
</div>
<div class="panel-body">
<p>Her finner du en oversikt over varelageret. Alle oppføringer vil ha en eller flere mengder av varen tilknyttet seg, slik at man kan skille på utløpsdatoer i batch.</p>
<table class="table table-striped table-condensed tablesorter" id="inventory_item_list">
<thead>
<tr>
<th>Navn</th>
<th>Mengde</th>
<th>Eldste utløpsdato</th>
<th>Nyeste oppføring</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td><a href="/dashboard/inventory/item/{{ item.id }}/">{{ item.name }}</a></td>
<td>{{ item.total_amount }}</td>
<td {% if item.has_expired_batch %}class="red"{% endif %}>
{{ item.oldest_expiration_date|date:'Y-m-d' }}
</td>
<td>{{ item.last_added|date:'Y-m-d' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock content %}

0 comments on commit 9198352

Please sign in to comment.