Merge pull request #45 from SchrodingersGat/master

Added Users API
This commit is contained in:
Oliver 2017-04-20 22:55:01 +10:00 committed by GitHub
commit 2666d7961b
13 changed files with 72 additions and 25 deletions

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
from django.db import models
from django.contrib.contenttypes.models import ContentType
from rest_framework.exceptions import ValidationError
class Company(models.Model):
@ -150,7 +151,7 @@ class InvenTreeTree(models.Model):
pass
# Parent cannot be set to same ID (this would cause looping)
elif val == self.id:
return
raise ValidationError("Category cannot set itself as parent")
# Null parent is OK
elif val is None:
pass
@ -158,7 +159,7 @@ class InvenTreeTree(models.Model):
else:
kids = self.getUniqueChildren()
if val in kids:
return
raise ValidationError("Category cannot set a child as parent")
# Prohibit certain characters from tree node names
elif attrname == 'name':

View File

@ -8,6 +8,7 @@ from stock.urls import stock_urls, stock_loc_urls, stock_track_urls
from project.urls import prj_urls, prj_part_urls, prj_cat_urls, prj_run_urls
from supplier.urls import cust_urls, manu_urls, supplier_part_urls, price_break_urls, supplier_urls
from track.urls import unique_urls, part_track_urls
from users.urls import user_urls
admin.site.site_header = "InvenTree Admin"
@ -40,6 +41,9 @@ apipatterns = [
url(r'^project-category/', include(prj_cat_urls)),
url(r'^project-part/', include(prj_part_urls)),
url(r'^project-run/', include(prj_run_urls)),
# User URLs
url(r'^user/', include(user_urls)),
]
urlpatterns = [

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
from django.utils.translation import ugettext as _
from django.db import models
from django.core.validators import MinValueValidator
from django.contrib.auth.models import User
from supplier.models import SupplierPart
from part.models import Part
@ -29,6 +30,7 @@ class StockItem(models.Model):
# last time the stock was checked / counted
stocktake_date = models.DateField(blank=True, null=True)
stocktake_user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
review_needed = models.BooleanField(default=False)
@ -63,7 +65,7 @@ class StockItem(models.Model):
infinite = models.BooleanField(default=False)
def stocktake(self, count):
def stocktake(self, count, user):
""" Perform item stocktake.
When the quantity of an item is counted,
record the date of stocktake
@ -76,27 +78,7 @@ class StockItem(models.Model):
self.quantity = count
self.stocktake_date = datetime.now().date()
self.save()
def take_stock(self, amount):
""" Take items from stock
This function can be called by initiating a ProjectRun,
or by manually taking the items from the stock location
"""
if self.infinite:
return
amount = int(amount)
if amount < 0:
raise ValueError("Stock amount must be positive")
q = self.quantity - amount
if q < 0:
q = 0
self.quantity = q
self.stocktake_user = user
self.save()
def add_stock(self, amount):
@ -119,6 +101,9 @@ class StockItem(models.Model):
self.quantity = q
self.save()
def take_stock(self, amount):
self.add_stock(-amount)
def __str__(self):
return "{n} x {part} @ {loc}".format(
n=self.quantity,

View File

@ -18,6 +18,7 @@ class StockItemSerializer(serializers.HyperlinkedModelSerializer):
'notes',
'updated',
'stocktake_date',
'stocktake_user',
'review_needed',
'expected_arrival')

View File

@ -62,7 +62,7 @@ class StockStocktakeEndpoint(generics.UpdateAPIView):
def update(self, request, *args, **kwargs):
object = self.get_object()
object.stocktake(request.data['quantity'])
object.stocktake(request.data['quantity'], request.user)
serializer = self.get_serializer(object)

View File

3
InvenTree/users/admin.py Normal file
View File

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
# from __future__ import unicode_literals
# from django.contrib import admin

8
InvenTree/users/apps.py Normal file
View File

@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'

View File

@ -0,0 +1 @@
# -*- coding: utf-8 -*-

View File

@ -0,0 +1,14 @@
from rest_framework import serializers
from django.contrib.auth.models import User
class UserSerializer(serializers.HyperlinkedModelSerializer):
""" Serializer for a User
"""
class Meta:
model = User
fields = ('username',
'first_name',
'last_name',
'email',)

4
InvenTree/users/tests.py Normal file
View File

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# from __future__ import unicode_literals
# from django.test import TestCase

9
InvenTree/users/urls.py Normal file
View File

@ -0,0 +1,9 @@
from django.conf.urls import url
from . import views
user_urls = [
url(r'^(?P<pk>[0-9]+)/?$', views.UserDetail.as_view(), name='user-detail'),
url(r'^$', views.UserList.as_view()),
]

17
InvenTree/users/views.py Normal file
View File

@ -0,0 +1,17 @@
from rest_framework import generics, permissions
from django.contrib.auth.models import User
from .serializers import UserSerializer
class UserDetail(generics.RetrieveAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class UserList(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)