Merge remote-tracking branch 'inventree/master'

This commit is contained in:
Oliver Walters 2019-04-27 01:08:14 +10:00
commit 922feec7f4
3 changed files with 151 additions and 7 deletions

View File

@ -3,8 +3,11 @@
import random import random
import string import string
import os import os
import sys
import argparse
fn = 'secret_key.txt' KEY_FN = 'secret_key.txt'
KEY_DIR = os.path.dirname(os.path.realpath(__file__))
def generate_key(): def generate_key():
@ -15,10 +18,28 @@ def generate_key():
if __name__ == '__main__': if __name__ == '__main__':
# Ensure key file is placed in same directory as this script parser = argparse.ArgumentParser(description='Generate Django SECRET_KEY file')
path = os.path.dirname(os.path.realpath(__file__)) parser.add_argument('--output', help='Specify key file path', default=None)
key_file = os.path.join(path, fn) parser.add_argument('--force', '-f', help='Override key file (if it exists)', action='store_true')
parser.add_argument('--dummy', '-d', help='Dummy run (display key only', action='store_true')
with open(key_file, 'w') as kf: args = parser.parse_args()
kf.write(generate_key())
print('Generated SECRET_KEY to {f}'.format(f=key_file)) if args.output:
key_filename = args.output
else:
key_filename = os.path.join(KEY_DIR, KEY_FN)
key_data = generate_key()
if args.dummy:
print('SECRET_KEY: {k}'.format(k=key_data))
sys.exit(0)
if not args.force and os.path.exists(key_filename):
print("Key file already exists - '{f}'".format(f=key_filename))
sys.exit(0)
with open(key_filename, 'w') as key_file:
print("Generating SECRET_KEY file - '{f}'".format(f=key_filename))
key_file.write(key_data)

View File

@ -55,6 +55,11 @@ class CategoryList(generics.ListCreateAPIView):
] ]
class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = CategorySerializer
queryset = PartCategory.objects.all()
class PartDetail(generics.RetrieveUpdateDestroyAPIView): class PartDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Part.objects.all() queryset = Part.objects.all()
serializer_class = PartSerializer serializer_class = PartSerializer
@ -205,6 +210,9 @@ class SupplierPriceBreakList(generics.ListCreateAPIView):
cat_api_urls = [ cat_api_urls = [
url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'),
url(r'^$', CategoryList.as_view(), name='api-part-category-list'), url(r'^$', CategoryList.as_view(), name='api-part-category-list'),
] ]

115
InvenTree/part/test_api.py Normal file
View File

@ -0,0 +1,115 @@
from rest_framework.test import APITestCase
from rest_framework import status
from django.urls import reverse
from django.contrib.auth import get_user_model
from .models import Part, PartCategory
class BomAPITest(APITestCase):
def setUp(self):
# Create a user for auth
User = get_user_model()
User.objects.create_user('testuser', 'test@testing.com', 'password')
self.client.login(username='testuser', password='password')
def test_category_list_empty(self):
# Check that we can retrieve an (empty) category list
url = reverse('api-part-category-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 0)
def test_add_categories(self):
# Check that we can add categories
data = {
'name': 'Animals',
'description': 'All animals go here'
}
url = reverse('api-part-category-list')
response = self.client.post(url, data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['pk'], 1)
# Add some sub-categories to the top-level 'Animals' category
for animal in ['cat', 'dog', 'zebra']:
data = {
'name': animal,
'description': 'A sort of animal',
'parent': 1,
}
response = self.client.post(url, data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['parent'], 1)
self.assertEqual(response.data['name'], animal)
self.assertEqual(response.data['pathstring'], 'Animals/' + animal)
# There should be now 4 categories
response = self.client.get(url, format='json')
self.assertEqual(len(response.data), 4)
class PartAPITest(APITestCase):
def setUp(self):
# Create a user for auth
User = get_user_model()
User.objects.create_user('testuser', 'test@testing.com', 'password')
self.client.login(username='testuser', password='password')
# Create some test data
TOP = PartCategory.objects.create(name='Top', description='Top level category')
A = PartCategory.objects.create(name='A', description='Cat A', parent=TOP)
B = PartCategory.objects.create(name='B', description='Cat B', parent=TOP)
C = PartCategory.objects.create(name='C', description='Cat C', parent=TOP)
Part.objects.create(name='Top.t', description='t in TOP', category=TOP)
Part.objects.create(name='A.a', description='a in A', category=A)
Part.objects.create(name='B.b', description='b in B', category=B)
Part.objects.create(name='C.c1', description='c1 in C', category=C)
Part.objects.create(name='C.c2', description='c2 in C', category=C)
def test_get_all_parts(self):
url = reverse('api-part-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 5)
def test_get_parts_by_cat(self):
url = reverse('api-part-list')
data = {'category': 4}
response = self.client.get(url, data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
# There should only be 2 objects in category C
self.assertEqual(len(response.data), 2)
for part in response.data:
self.assertEqual(part['category'], 4)
def test_include_children(self):
""" Test the special 'include_child_categories' flag
If provided, parts are provided for ANY child category (recursive)
"""
url = reverse('api-part-list')
data = {'category': 1}
response = self.client.get(url, data=data, format='json')
# There should be 1 part in this category
self.assertEqual(len(response.data), 1)
data['include_child_categories'] = 1
# Now request to include child categories
response = self.client.get(url, data=data, format='json')
# Now there should be 5 total parts
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 5)