Compare commits
No commits in common. "f2647f0dbd4fc3c95aeee1c8dba9238b6dd87e47" and "2fce260ba861a5b85dd553c8edf9444d09ad3153" have entirely different histories.
f2647f0dbd
...
2fce260ba8
30 changed files with 741 additions and 512 deletions
|
@ -12,7 +12,25 @@ from knox.models import AuthToken
|
|||
from knox.views import LoginView as KnoxLoginView
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from authentication.serializers import UserSerializer, GroupSerializer
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
groups = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name')
|
||||
|
||||
class Meta:
|
||||
model = ExtendedUser
|
||||
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
read_only_fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return list(set(obj.get_permissions()))
|
||||
|
||||
|
||||
@receiver(post_save, sender=ExtendedUser)
|
||||
def create_auth_token(sender, instance=None, created=False, **kwargs):
|
||||
if created:
|
||||
AuthToken.objects.create(user=instance)
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
|
@ -20,17 +38,26 @@ class UserViewSet(viewsets.ModelViewSet):
|
|||
serializer_class = UserSerializer
|
||||
|
||||
|
||||
class GroupSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
members = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ('id', 'name', 'permissions', 'members')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return ["*:" + p.codename for p in obj.permissions.all()]
|
||||
|
||||
def get_members(self, obj):
|
||||
return [u.username for u in obj.user_set.all()]
|
||||
|
||||
|
||||
class GroupViewSet(viewsets.ModelViewSet):
|
||||
queryset = Group.objects.all()
|
||||
serializer_class = GroupSerializer
|
||||
|
||||
|
||||
@receiver(post_save, sender=ExtendedUser)
|
||||
def create_auth_token(sender, instance=None, created=False, **kwargs):
|
||||
if created:
|
||||
AuthToken.objects.create(user=instance)
|
||||
|
||||
|
||||
@api_view(['GET'])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def selfUser(request):
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
groups = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name')
|
||||
|
||||
class Meta:
|
||||
model = ExtendedUser
|
||||
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
read_only_fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return list(set(obj.get_permissions()))
|
||||
|
||||
|
||||
class GroupSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
members = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ('id', 'name', 'permissions', 'members')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return ["*:" + p.codename for p in obj.permissions.all()]
|
||||
|
||||
def get_members(self, obj):
|
||||
return [u.username for u in obj.user_set.all()]
|
|
@ -34,9 +34,7 @@ SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure-tm*$w_14iqbiy-!7(8#
|
|||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = truthy_str(os.getenv('DEBUG_MODE_ACTIVE', 'False'))
|
||||
|
||||
PRIMARY_HOST = os.getenv('HTTP_HOST', 'localhost')
|
||||
|
||||
ALLOWED_HOSTS = [PRIMARY_HOST]
|
||||
ALLOWED_HOSTS = [os.getenv('HTTP_HOST', 'localhost')]
|
||||
|
||||
MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost')
|
||||
|
||||
|
@ -146,8 +144,7 @@ else:
|
|||
'USER': os.getenv('DB_USER', 'system3'),
|
||||
'PASSWORD': os.getenv('DB_PASSWORD', 'system3'),
|
||||
'OPTIONS': {
|
||||
'charset': 'utf8mb4',
|
||||
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
|
||||
'charset': 'utf8mb4'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,9 @@ from .version import get_info
|
|||
|
||||
urlpatterns = [
|
||||
path('djangoadmin/', admin.site.urls),
|
||||
path('api/1/', include('inventory.api_v1')),
|
||||
path('api/1/', include('files.api_v1')),
|
||||
path('api/1/', include('files.media_v1')),
|
||||
path('api/2/', include('inventory.api_v2')),
|
||||
path('api/2/', include('files.api_v2')),
|
||||
path('media/2/', include('files.media_v2')),
|
||||
|
|
27
core/files/api_v1.py
Normal file
27
core/files/api_v1.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from rest_framework import serializers, viewsets, routers
|
||||
|
||||
from files.models import File
|
||||
|
||||
|
||||
class FileSerializer(serializers.ModelSerializer):
|
||||
data = serializers.CharField(max_length=1000000, write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = File
|
||||
fields = ['hash', 'data']
|
||||
read_only_fields = ['hash']
|
||||
|
||||
|
||||
class FileViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = FileSerializer
|
||||
queryset = File.objects.all()
|
||||
lookup_field = 'hash'
|
||||
permission_classes = []
|
||||
authentication_classes = []
|
||||
|
||||
|
||||
router = routers.SimpleRouter(trailing_slash=False)
|
||||
router.register(r'files', FileViewSet, basename='files')
|
||||
router.register(r'file', FileViewSet, basename='files')
|
||||
|
||||
urlpatterns = router.urls
|
65
core/files/media_v1.py
Normal file
65
core/files/media_v1.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
import os
|
||||
from django.http import HttpResponse
|
||||
from django.urls import path
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view, permission_classes, authentication_classes
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.settings import MEDIA_ROOT
|
||||
from files.models import File
|
||||
|
||||
|
||||
@swagger_auto_schema(method='GET', auto_schema=None)
|
||||
@api_view(['GET'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def media_urls(request, hash):
|
||||
try:
|
||||
file = File.objects.get(hash=hash)
|
||||
hash_path = file.file
|
||||
return HttpResponse(status=status.HTTP_200_OK,
|
||||
content_type=file.mime_type,
|
||||
headers={
|
||||
'X-Accel-Redirect': f'/redirect_media/{hash_path}',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}) # TODO Expires and Cache-Control
|
||||
|
||||
except File.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
@swagger_auto_schema(method='GET', auto_schema=None)
|
||||
@api_view(['GET'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def thumbnail_urls(request, hash):
|
||||
size = 200
|
||||
try:
|
||||
file = File.objects.get(hash=hash)
|
||||
hash_path = file.file
|
||||
if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'):
|
||||
from PIL import Image
|
||||
image = Image.open(file.file)
|
||||
image.thumbnail((size, size))
|
||||
rgb_image = image.convert('RGB')
|
||||
thumb_dir = os.path.dirname(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}')
|
||||
if not os.path.exists(thumb_dir):
|
||||
os.makedirs(thumb_dir)
|
||||
rgb_image.save(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}', 'jpeg', quality=90)
|
||||
|
||||
return HttpResponse(status=status.HTTP_200_OK,
|
||||
content_type="image/jpeg",
|
||||
headers={
|
||||
'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}) # TODO Expires and Cache-Control
|
||||
|
||||
except File.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('thumbs/<path:hash>', thumbnail_urls),
|
||||
path('images/<path:hash>', media_urls),
|
||||
]
|
0
core/files/tests/v1/__init__.py
Normal file
0
core/files/tests/v1/__init__.py
Normal file
68
core/files/tests/v1/test_files.py
Normal file
68
core/files/tests/v1/test_files.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
from django.test import TestCase, Client
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class FileTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.box = Container.objects.create(name='BOX')
|
||||
|
||||
def test_create_file_raw(self):
|
||||
from hashlib import sha256
|
||||
content = b"foo"
|
||||
chash = sha256(content).hexdigest()
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
file = File.objects.create(file=ContentFile(b"foo"), mime_type='text/plain', hash=chash, item=item)
|
||||
file.save()
|
||||
self.assertEqual(1, len(File.objects.all()))
|
||||
self.assertEqual(content, File.objects.all()[0].file.read())
|
||||
self.assertEqual(chash, File.objects.all()[0].hash)
|
||||
|
||||
def test_list_files(self):
|
||||
import base64
|
||||
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get('/api/1/files')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()[0]['hash'], item.hash)
|
||||
self.assertEqual(len(response.json()[0]['hash']), 64)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
self.assertEqual(File.objects.all()[0].file.read(), b"foo")
|
||||
|
||||
def test_one_file(self):
|
||||
import base64
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get(f'/api/1/file/{item.hash}')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['hash'], item.hash)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
self.assertEqual(File.objects.all()[0].file.read(), b"foo")
|
||||
|
||||
def test_create_file(self):
|
||||
import base64
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
response = client.post('/api/1/file',
|
||||
{'data': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
self.assertEqual(File.objects.all()[0].file.read(), b"foo")
|
||||
|
||||
def test_delete_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8'))
|
||||
self.assertEqual(len(File.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/file/{file.hash}')
|
||||
self.assertEqual(response.status_code, 204)
|
150
core/inventory/api_v1.py
Normal file
150
core/inventory/api_v1.py
Normal file
|
@ -0,0 +1,150 @@
|
|||
from django.utils import timezone
|
||||
from django.urls import re_path
|
||||
from rest_framework import routers, viewsets, serializers
|
||||
from rest_framework.decorators import api_view, permission_classes, authentication_classes
|
||||
from rest_framework.response import Response
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
from inventory.serializers import EventSerializer, ContainerSerializer
|
||||
|
||||
|
||||
class EventViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = EventSerializer
|
||||
queryset = Event.objects.all()
|
||||
permission_classes = []
|
||||
authentication_classes = []
|
||||
|
||||
|
||||
class ContainerViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = ContainerSerializer
|
||||
queryset = Container.objects.all()
|
||||
permission_classes = []
|
||||
authentication_classes = []
|
||||
|
||||
|
||||
class ItemSerializer(serializers.ModelSerializer):
|
||||
dataImage = serializers.CharField(write_only=True, required=False)
|
||||
cid = serializers.SerializerMethodField()
|
||||
box = serializers.SerializerMethodField()
|
||||
file = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Item
|
||||
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage']
|
||||
read_only_fields = ['uid']
|
||||
|
||||
def get_cid(self, instance):
|
||||
return instance.container.cid
|
||||
|
||||
def get_box(self, instance):
|
||||
return instance.container.name
|
||||
|
||||
def get_file(self, instance):
|
||||
if len(instance.files.all()) > 0:
|
||||
return instance.files.all().order_by('-created_at')[0].hash
|
||||
return None
|
||||
|
||||
def to_internal_value(self, data):
|
||||
if 'cid' in data:
|
||||
container = Container.objects.get(cid=data['cid'])
|
||||
internal = super().to_internal_value(data)
|
||||
internal['container'] = container
|
||||
return internal
|
||||
return super().to_internal_value(data)
|
||||
|
||||
def validate(self, attrs):
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
if 'dataImage' in validated_data:
|
||||
file = File.objects.create(data=validated_data['dataImage'])
|
||||
validated_data.pop('dataImage')
|
||||
item = Item.objects.create(**validated_data)
|
||||
item.files.set([file])
|
||||
return item
|
||||
return Item.objects.create(**validated_data)
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
if 'returned' in validated_data:
|
||||
if validated_data['returned']:
|
||||
validated_data['returned_at'] = timezone.now()
|
||||
validated_data.pop('returned')
|
||||
if 'dataImage' in validated_data:
|
||||
file = File.objects.create(data=validated_data['dataImage'])
|
||||
validated_data.pop('dataImage')
|
||||
instance.files.add(file)
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
|
||||
@api_view(['GET'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def search_items(request, event_slug, query):
|
||||
try:
|
||||
event = Event.objects.get(slug=event_slug)
|
||||
query_tokens = query.split(' ')
|
||||
q = Item.objects.filter(event=event)
|
||||
for token in query_tokens:
|
||||
if token:
|
||||
q = q.filter(description__icontains=token)
|
||||
return Response(ItemSerializer(q, many=True).data)
|
||||
except Event.DoesNotExist:
|
||||
return Response(status=404)
|
||||
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def item(request, event_slug):
|
||||
try:
|
||||
event = Event.objects.get(slug=event_slug)
|
||||
if request.method == 'GET':
|
||||
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
||||
elif request.method == 'POST':
|
||||
validated_data = ItemSerializer(data=request.data)
|
||||
if validated_data.is_valid():
|
||||
validated_data.save(event=event)
|
||||
return Response(validated_data.data, status=201)
|
||||
except Event.DoesNotExist:
|
||||
return Response(status=404)
|
||||
|
||||
|
||||
@api_view(['GET', 'PUT', 'DELETE'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def item_by_id(request, event_slug, id):
|
||||
try:
|
||||
event = Event.objects.get(slug=event_slug)
|
||||
item = Item.objects.get(event=event, uid=id)
|
||||
if request.method == 'GET':
|
||||
return Response(ItemSerializer(item).data)
|
||||
elif request.method == 'PUT':
|
||||
validated_data = ItemSerializer(item, data=request.data)
|
||||
if validated_data.is_valid():
|
||||
validated_data.save()
|
||||
return Response(validated_data.data)
|
||||
elif request.method == 'DELETE':
|
||||
item.delete()
|
||||
return Response(status=204)
|
||||
except Item.DoesNotExist:
|
||||
return Response(status=404)
|
||||
except Event.DoesNotExist:
|
||||
return Response(status=404)
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
re_path('events/?$', EventViewSet.as_view({'get': 'list', 'post': 'create'})),
|
||||
re_path('events/(?P<pk>[0-9]+)/?$',
|
||||
EventViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
|
||||
re_path('boxes/?$', ContainerViewSet.as_view({'get': 'list', 'post': 'create'})),
|
||||
re_path('boxes/(?P<pk>[0-9]+)/?$',
|
||||
ContainerViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
|
||||
re_path('box/?$', ContainerViewSet.as_view({'get': 'list', 'post': 'create'})),
|
||||
re_path('box/(?P<pk>[0-9]+)/?$',
|
||||
ContainerViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
|
||||
re_path('(?P<event_slug>[a-zA-Z0-9]+)/items/?$', item),
|
||||
re_path('(?P<event_slug>[a-zA-Z0-9]+)/items/(?P<query>[^/]+)/?$', search_items),
|
||||
re_path('(?P<event_slug>[a-zA-Z0-9]+)/item/?$', item),
|
||||
re_path('(?P<event_slug>[a-zA-Z0-9]+)/item/(?P<id>[0-9]+)/?$', item_by_id),
|
||||
]
|
|
@ -54,7 +54,6 @@ def item(request, event_slug):
|
|||
if validated_data.is_valid():
|
||||
validated_data.save(event=event)
|
||||
return Response(validated_data.data, status=201)
|
||||
return Response(status=400)
|
||||
except Event.DoesNotExist:
|
||||
return Response(status=404)
|
||||
|
||||
|
@ -64,7 +63,7 @@ def item(request, event_slug):
|
|||
def item_by_id(request, event_slug, id):
|
||||
try:
|
||||
event = Event.objects.get(slug=event_slug)
|
||||
item = Item.objects.get(event=event, id=id)
|
||||
item = Item.objects.get(event=event, uid=id)
|
||||
if request.method == 'GET':
|
||||
if not request.user.has_event_perm(event, 'view_item'):
|
||||
return Response(status=403)
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
# Generated by Django 4.2.7 on 2024-11-19 22:56
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('inventory', '0004_alter_event_created_at_alter_item_created_at'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='container',
|
||||
old_name='cid',
|
||||
new_name='id',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='event',
|
||||
old_name='eid',
|
||||
new_name='id',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='item',
|
||||
old_name='iid',
|
||||
new_name='id',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='item',
|
||||
old_name='uid',
|
||||
new_name='uid_deprecated',
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='item',
|
||||
unique_together=set(),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='item',
|
||||
name='container',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='inventory.container'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='item',
|
||||
name='event',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='inventory.event'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='item',
|
||||
unique_together={('uid_deprecated', 'event')},
|
||||
),
|
||||
]
|
|
@ -1,17 +0,0 @@
|
|||
# Generated by Django 4.2.7 on 2024-11-20 01:39
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('inventory', '0005_rename_cid_container_id_rename_eid_event_id_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelTable(
|
||||
name='event',
|
||||
table='common_event',
|
||||
),
|
||||
]
|
|
@ -1,16 +1,15 @@
|
|||
from itertools import groupby
|
||||
|
||||
from django.db import models
|
||||
from django.core.files.base import ContentFile
|
||||
from django.db import models, IntegrityError
|
||||
from django_softdelete.models import SoftDeleteModel, SoftDeleteManager
|
||||
|
||||
|
||||
class ItemManager(SoftDeleteManager):
|
||||
|
||||
def create(self, **kwargs):
|
||||
if 'uid_deprecated' in kwargs:
|
||||
raise ValueError('uid_deprecated must not be set manually')
|
||||
uid_deprecated = Item.all_objects.filter(event=kwargs['event']).count() + 1
|
||||
kwargs['uid_deprecated'] = uid_deprecated
|
||||
if 'uid' in kwargs:
|
||||
raise ValueError('uid must not be set manually')
|
||||
uid = Item.all_objects.filter(event=kwargs['event']).count() + 1
|
||||
kwargs['uid'] = uid
|
||||
return super().create(**kwargs)
|
||||
|
||||
def get_queryset(self):
|
||||
|
@ -18,47 +17,40 @@ class ItemManager(SoftDeleteManager):
|
|||
|
||||
|
||||
class Item(SoftDeleteModel):
|
||||
id = models.AutoField(primary_key=True)
|
||||
uid_deprecated = models.IntegerField()
|
||||
iid = models.AutoField(primary_key=True)
|
||||
uid = models.IntegerField()
|
||||
description = models.TextField()
|
||||
event = models.ForeignKey('Event', models.CASCADE)
|
||||
container = models.ForeignKey('Container', models.CASCADE)
|
||||
event = models.ForeignKey('Event', models.CASCADE, db_column='eid')
|
||||
container = models.ForeignKey('Container', models.CASCADE, db_column='cid')
|
||||
returned_at = models.DateTimeField(blank=True, null=True)
|
||||
created_at = models.DateTimeField(null=True, auto_now_add=True)
|
||||
updated_at = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
@property
|
||||
def related_issues(self):
|
||||
groups = groupby(self.issue_relation_changes.all(), lambda rel: rel.issue_thread.id)
|
||||
return [sorted(v, key=lambda r: r.timestamp)[0].issue_thread for k, v in groups]
|
||||
|
||||
|
||||
|
||||
objects = ItemManager()
|
||||
all_objects = models.Manager()
|
||||
|
||||
class Meta:
|
||||
unique_together = (('uid_deprecated', 'event'),)
|
||||
unique_together = (('uid', 'event'),)
|
||||
permissions = [
|
||||
('match_item', 'Can match item')
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return '[' + str(self.id) + ']' + self.description
|
||||
return '[' + str(self.uid) + ']' + self.description
|
||||
|
||||
|
||||
class Container(SoftDeleteModel):
|
||||
id = models.AutoField(primary_key=True)
|
||||
cid = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=255)
|
||||
created_at = models.DateTimeField(blank=True, null=True)
|
||||
updated_at = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return '[' + str(self.id) + ']' + self.name
|
||||
return '[' + str(self.cid) + ']' + self.name
|
||||
|
||||
|
||||
class Event(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
eid = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=255)
|
||||
slug = models.CharField(max_length=255, unique=True)
|
||||
start = models.DateTimeField(blank=True, null=True)
|
||||
|
@ -70,6 +62,3 @@ class Event(models.Model):
|
|||
|
||||
def __str__(self):
|
||||
return '[' + str(self.slug) + ']' + self.name
|
||||
|
||||
class Meta:
|
||||
db_table = 'common_event'
|
|
@ -1,12 +1,9 @@
|
|||
from django.utils import timezone
|
||||
from rest_framework import serializers
|
||||
from rest_framework.relations import SlugRelatedField
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
from inventory.shared_serializers import BasicItemSerializer
|
||||
from mail.models import EventAddress
|
||||
from tickets.shared_serializers import BasicIssueSerializer
|
||||
|
||||
|
||||
class EventAdressSerializer(serializers.ModelSerializer):
|
||||
|
@ -20,8 +17,8 @@ class EventSerializer(serializers.ModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = Event
|
||||
fields = ['id', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
||||
read_only_fields = ['id']
|
||||
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
||||
read_only_fields = ['eid']
|
||||
|
||||
|
||||
class ContainerSerializer(serializers.ModelSerializer):
|
||||
|
@ -29,27 +26,46 @@ class ContainerSerializer(serializers.ModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = Container
|
||||
fields = ['id', 'name', 'itemCount']
|
||||
read_only_fields = ['id', 'itemCount']
|
||||
fields = ['cid', 'name', 'itemCount']
|
||||
read_only_fields = ['cid', 'itemCount']
|
||||
|
||||
def get_itemCount(self, instance):
|
||||
return Item.objects.filter(container=instance.id).count()
|
||||
return Item.objects.filter(container=instance.cid).count()
|
||||
|
||||
|
||||
class ItemSerializer(BasicItemSerializer):
|
||||
class ItemSerializer(serializers.ModelSerializer):
|
||||
dataImage = serializers.CharField(write_only=True, required=False)
|
||||
related_issues = BasicIssueSerializer(many=True, read_only=True)
|
||||
cid = serializers.SerializerMethodField()
|
||||
box = serializers.SerializerMethodField()
|
||||
file = serializers.SerializerMethodField()
|
||||
returned = serializers.SerializerMethodField(required=False)
|
||||
event = serializers.SlugRelatedField(slug_field='slug', queryset=Event.objects.all(),
|
||||
allow_null=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = Item
|
||||
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues']
|
||||
read_only_fields = ['id']
|
||||
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage', 'returned', 'event']
|
||||
read_only_fields = ['uid']
|
||||
|
||||
def get_cid(self, instance):
|
||||
return instance.container.cid
|
||||
|
||||
def get_box(self, instance):
|
||||
return instance.container.name
|
||||
|
||||
def get_file(self, instance):
|
||||
if len(instance.files.all()) > 0:
|
||||
return instance.files.all().order_by('-created_at')[0].hash
|
||||
return None
|
||||
|
||||
def get_returned(self, instance):
|
||||
return instance.returned_at is not None
|
||||
|
||||
def to_internal_value(self, data):
|
||||
container = None
|
||||
returned = False
|
||||
if 'cid' in data:
|
||||
container = Container.objects.get(id=data['cid'])
|
||||
container = Container.objects.get(cid=data['cid'])
|
||||
if 'returned' in data:
|
||||
returned = data['returned']
|
||||
internal = super().to_internal_value(data)
|
||||
|
@ -60,8 +76,6 @@ class ItemSerializer(BasicItemSerializer):
|
|||
return internal
|
||||
|
||||
def validate(self, attrs):
|
||||
if not 'container' in attrs and not self.partial:
|
||||
raise serializers.ValidationError("This field cannot be empty.")
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
from rest_framework import serializers
|
||||
|
||||
from inventory.models import Event, Item
|
||||
|
||||
|
||||
class BasicItemSerializer(serializers.ModelSerializer):
|
||||
cid = serializers.SerializerMethodField()
|
||||
box = serializers.SerializerMethodField()
|
||||
file = serializers.SerializerMethodField()
|
||||
returned = serializers.SerializerMethodField(required=False)
|
||||
event = serializers.SlugRelatedField(slug_field='slug', queryset=Event.objects.all(),
|
||||
allow_null=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = Item
|
||||
fields = ['cid', 'box', 'id', 'description', 'file', 'returned', 'event']
|
||||
read_only_fields = ['id']
|
||||
|
||||
def get_cid(self, instance):
|
||||
return instance.container.id if instance.container else None
|
||||
|
||||
def get_box(self, instance):
|
||||
return instance.container.name if instance.container else None
|
||||
|
||||
def get_file(self, instance):
|
||||
if len(instance.files.all()) > 0:
|
||||
return instance.files.all().order_by('-created_at')[0].hash
|
||||
return None
|
||||
|
||||
def get_returned(self, instance):
|
||||
return instance.returned_at is not None
|
0
core/inventory/tests/v1/__init__.py
Normal file
0
core/inventory/tests/v1/__init__.py
Normal file
34
core/inventory/tests/v1/test_api.py
Normal file
34
core/inventory/tests/v1/test_api.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
from django.test import TestCase, Client
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class ApiTest(TestCase):
|
||||
|
||||
def test_root(self):
|
||||
from core.settings import SYSTEM3_VERSION
|
||||
response = client.get('/api/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()["framework_version"], SYSTEM3_VERSION)
|
||||
|
||||
def test_events(self):
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_containers(self):
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_files(self):
|
||||
response = client.get('/api/1/files')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_items(self):
|
||||
from inventory.models import Event
|
||||
Event.objects.create(slug='TEST1', name='Event')
|
||||
response = client.get('/api/1/TEST1/items')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
59
core/inventory/tests/v1/test_containers.py
Normal file
59
core/inventory/tests/v1/test_containers.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
from django.test import TestCase, Client
|
||||
from inventory.models import Container
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class ContainerTestCase(TestCase):
|
||||
|
||||
def test_empty(self):
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_members(self):
|
||||
Container.objects.create(name='BOX')
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['cid'], 1)
|
||||
self.assertEqual(response.json()[0]['name'], 'BOX')
|
||||
self.assertEqual(response.json()[0]['itemCount'], 0)
|
||||
|
||||
def test_multi_members(self):
|
||||
Container.objects.create(name='BOX 1')
|
||||
Container.objects.create(name='BOX 2')
|
||||
Container.objects.create(name='BOX 3')
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 3)
|
||||
|
||||
def test_create_container(self):
|
||||
response = client.post('/api/1/box', {'name': 'BOX'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['cid'], 1)
|
||||
self.assertEqual(response.json()['name'], 'BOX')
|
||||
self.assertEqual(response.json()['itemCount'], 0)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
self.assertEqual(Container.objects.all()[0].cid, 1)
|
||||
self.assertEqual(Container.objects.all()[0].name, 'BOX')
|
||||
|
||||
def test_update_container(self):
|
||||
from rest_framework.test import APIClient
|
||||
box = Container.objects.create(name='BOX 1')
|
||||
response = APIClient().put(f'/api/1/box/{box.cid}', {'name': 'BOX 2'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['cid'], 1)
|
||||
self.assertEqual(response.json()['name'], 'BOX 2')
|
||||
self.assertEqual(response.json()['itemCount'], 0)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
self.assertEqual(Container.objects.all()[0].cid, 1)
|
||||
self.assertEqual(Container.objects.all()[0].name, 'BOX 2')
|
||||
|
||||
def test_delete_container(self):
|
||||
box = Container.objects.create(name='BOX 1')
|
||||
Container.objects.create(name='BOX 2')
|
||||
self.assertEqual(len(Container.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/box/{box.cid}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
56
core/inventory/tests/v1/test_events.py
Normal file
56
core/inventory/tests/v1/test_events.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
from django.test import TestCase, Client
|
||||
from inventory.models import Event
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class EventTestCase(TestCase):
|
||||
|
||||
def test_empty(self):
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), [])
|
||||
|
||||
def test_members(self):
|
||||
Event.objects.create(slug='EVENT', name='Event')
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['slug'], 'EVENT')
|
||||
self.assertEqual(response.json()[0]['name'], 'Event')
|
||||
|
||||
def test_multi_members(self):
|
||||
Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
Event.objects.create(slug='EVENT2', name='Event 2')
|
||||
Event.objects.create(slug='EVENT3', name='Event 3')
|
||||
response = client.get('/api/1/events')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 3)
|
||||
|
||||
def test_create_event(self):
|
||||
response = client.post('/api/1/events', {'slug': 'EVENT', 'name': 'Event'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['slug'], 'EVENT')
|
||||
self.assertEqual(response.json()['name'], 'Event')
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
||||
self.assertEqual(Event.objects.all()[0].slug, 'EVENT')
|
||||
self.assertEqual(Event.objects.all()[0].name, 'Event')
|
||||
|
||||
def test_update_event(self):
|
||||
from rest_framework.test import APIClient
|
||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
response = APIClient().put(f'/api/1/events/{event.eid}', {'slug': 'EVENT2', 'name': 'Event 2 new'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['slug'], 'EVENT2')
|
||||
self.assertEqual(response.json()['name'], 'Event 2 new')
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
||||
self.assertEqual(Event.objects.all()[0].slug, 'EVENT2')
|
||||
self.assertEqual(Event.objects.all()[0].name, 'Event 2 new')
|
||||
|
||||
def test_remove_event(self):
|
||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
Event.objects.create(slug='EVENT2', name='Event 2')
|
||||
self.assertEqual(len(Event.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/events/{event.eid}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
133
core/inventory/tests/v1/test_items.py
Normal file
133
core/inventory/tests/v1/test_items.py
Normal file
|
@ -0,0 +1,133 @@
|
|||
from django.test import TestCase, Client
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class ItemTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.box = Container.objects.create(name='BOX')
|
||||
|
||||
def test_empty(self):
|
||||
response = client.get(f'/api/1/{self.event.slug}/item')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, b'[]')
|
||||
|
||||
def test_members(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = client.get(f'/api/1/{self.event.slug}/item')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(),
|
||||
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None}])
|
||||
|
||||
def test_members_with_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get(f'/api/1/{self.event.slug}/item')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(),
|
||||
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash}])
|
||||
|
||||
def test_multi_members(self):
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
Item.objects.create(container=self.box, event=self.event, description='3')
|
||||
response = client.get(f'/api/1/{self.event.slug}/item')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 3)
|
||||
|
||||
def test_create_item(self):
|
||||
response = client.post(f'/api/1/{self.event.slug}/item', {'cid': self.box.cid, 'description': '1'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json(),
|
||||
{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None})
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '1')
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
|
||||
def test_create_item_with_file(self):
|
||||
import base64
|
||||
response = client.post(f'/api/1/{self.event.slug}/item',
|
||||
{'cid': self.box.cid, 'description': '1',
|
||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode(
|
||||
'utf-8')}, content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['uid'], 1)
|
||||
self.assertEqual(response.json()['description'], '1')
|
||||
self.assertEqual(response.json()['box'], 'BOX')
|
||||
self.assertEqual(response.json()['cid'], self.box.cid)
|
||||
self.assertEqual(len(response.json()['file']), 64)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '1')
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
|
||||
def test_update_item(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = client.put(f'/api/1/{self.event.slug}/item/{item.uid}', {'description': '2'},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(),
|
||||
{'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None})
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '2')
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
|
||||
def test_update_item_with_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = client.put(f'/api/1/{self.event.slug}/item/{item.uid}',
|
||||
{'description': '2',
|
||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['uid'], 1)
|
||||
self.assertEqual(response.json()['description'], '2')
|
||||
self.assertEqual(response.json()['box'], 'BOX')
|
||||
self.assertEqual(response.json()['cid'], self.box.cid)
|
||||
self.assertEqual(len(response.json()['file']), 64)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '2')
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
|
||||
def test_delete_item(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
self.assertEqual(len(Item.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/{self.event.slug}/item/{item.uid}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
|
||||
def test_delete_item2(self):
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item2 = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
self.assertEqual(len(Item.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/{self.event.slug}/item/{item2.uid}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
item3 = Item.objects.create(container=self.box, event=self.event, description='3')
|
||||
self.assertEqual(item3.uid, 3)
|
||||
self.assertEqual(len(Item.objects.all()), 2)
|
||||
|
||||
def test_item_count(self):
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
response = client.get('/api/1/boxes')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['itemCount'], 2)
|
||||
|
||||
def test_item_nonexistent(self):
|
||||
response = client.get(f'/api/1/NOEVENT/item')
|
||||
self.assertEqual(response.status_code, 404)
|
|
@ -24,7 +24,7 @@ class ContainerTestCase(TestCase):
|
|||
response = self.client.get('/api/2/boxes/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['id'], 1)
|
||||
self.assertEqual(response.json()[0]['cid'], 1)
|
||||
self.assertEqual(response.json()[0]['name'], 'BOX')
|
||||
self.assertEqual(response.json()[0]['itemCount'], 0)
|
||||
|
||||
|
@ -39,28 +39,28 @@ class ContainerTestCase(TestCase):
|
|||
def test_create_container(self):
|
||||
response = self.client.post('/api/2/box/', {'name': 'BOX'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['id'], 1)
|
||||
self.assertEqual(response.json()['cid'], 1)
|
||||
self.assertEqual(response.json()['name'], 'BOX')
|
||||
self.assertEqual(response.json()['itemCount'], 0)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
self.assertEqual(Container.objects.all()[0].id, 1)
|
||||
self.assertEqual(Container.objects.all()[0].cid, 1)
|
||||
self.assertEqual(Container.objects.all()[0].name, 'BOX')
|
||||
|
||||
def test_update_container(self):
|
||||
box = Container.objects.create(name='BOX 1')
|
||||
response = self.client.put(f'/api/2/box/{box.id}/', {'name': 'BOX 2'}, content_type='application/json')
|
||||
response = self.client.put(f'/api/2/box/{box.cid}/', {'name': 'BOX 2'}, content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['id'], 1)
|
||||
self.assertEqual(response.json()['cid'], 1)
|
||||
self.assertEqual(response.json()['name'], 'BOX 2')
|
||||
self.assertEqual(response.json()['itemCount'], 0)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
self.assertEqual(Container.objects.all()[0].id, 1)
|
||||
self.assertEqual(Container.objects.all()[0].cid, 1)
|
||||
self.assertEqual(Container.objects.all()[0].name, 'BOX 2')
|
||||
|
||||
def test_delete_container(self):
|
||||
box = Container.objects.create(name='BOX 1')
|
||||
Container.objects.create(name='BOX 2')
|
||||
self.assertEqual(len(Container.objects.all()), 2)
|
||||
response = self.client.delete(f'/api/2/box/{box.id}/')
|
||||
response = self.client.delete(f'/api/2/box/{box.cid}/')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Container.objects.all()), 1)
|
||||
|
|
|
@ -39,7 +39,7 @@ class EventTestCase(TestCase):
|
|||
def test_update_event(self):
|
||||
from rest_framework.test import APIClient
|
||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
response = APIClient().put(f'/api/2/events/{event.id}/', {'slug': 'EVENT2', 'name': 'Event 2 new'})
|
||||
response = APIClient().put(f'/api/2/events/{event.eid}/', {'slug': 'EVENT2', 'name': 'Event 2 new'})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['slug'], 'EVENT2')
|
||||
self.assertEqual(response.json()['name'], 'Event 2 new')
|
||||
|
@ -51,7 +51,7 @@ class EventTestCase(TestCase):
|
|||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||
Event.objects.create(slug='EVENT2', name='Event 2')
|
||||
self.assertEqual(len(Event.objects.all()), 2)
|
||||
response = client.delete(f'/api/2/events/{event.id}/')
|
||||
response = client.delete(f'/api/2/events/{event.eid}/')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
||||
|
||||
|
|
|
@ -28,15 +28,9 @@ class ItemTestCase(TestCase):
|
|||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['id'], item.id)
|
||||
self.assertEqual(response.json()[0]['description'], '1')
|
||||
self.assertEqual(response.json()[0]['box'], 'BOX')
|
||||
self.assertEqual(response.json()[0]['cid'], self.box.id)
|
||||
self.assertEqual(response.json()[0]['file'], None)
|
||||
self.assertEqual(response.json()[0]['returned'], False)
|
||||
self.assertEqual(response.json()[0]['event'], self.event.slug)
|
||||
self.assertEqual(len(response.json()[0]['related_issues']), 0)
|
||||
self.assertEqual(response.json(),
|
||||
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||
'returned': False, 'event': self.event.slug}])
|
||||
|
||||
def test_members_with_file(self):
|
||||
import base64
|
||||
|
@ -44,15 +38,9 @@ class ItemTestCase(TestCase):
|
|||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['id'], item.id)
|
||||
self.assertEqual(response.json()[0]['description'], '1')
|
||||
self.assertEqual(response.json()[0]['box'], 'BOX')
|
||||
self.assertEqual(response.json()[0]['cid'], self.box.id)
|
||||
self.assertEqual(response.json()[0]['file'], file.hash)
|
||||
self.assertEqual(response.json()[0]['returned'], False)
|
||||
self.assertEqual(response.json()[0]['event'], self.event.slug)
|
||||
self.assertEqual(len(response.json()[0]['related_issues']), 0)
|
||||
self.assertEqual(response.json(),
|
||||
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash,
|
||||
'returned': False, 'event': self.event.slug}])
|
||||
|
||||
def test_multi_members(self):
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
|
@ -63,89 +51,71 @@ class ItemTestCase(TestCase):
|
|||
self.assertEqual(len(response.json()), 3)
|
||||
|
||||
def test_create_item(self):
|
||||
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box.id, 'description': '1'})
|
||||
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box.cid, 'description': '1'})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['id'], 1)
|
||||
self.assertEqual(response.json()['description'], '1')
|
||||
self.assertEqual(response.json()['box'], 'BOX')
|
||||
self.assertEqual(response.json()['cid'], self.box.id)
|
||||
self.assertEqual(response.json()['file'], None)
|
||||
self.assertEqual(response.json()['returned'], False)
|
||||
self.assertEqual(response.json()['event'], self.event.slug)
|
||||
self.assertEqual(len(response.json()['related_issues']), 0)
|
||||
self.assertEqual(response.json(),
|
||||
{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||
'returned': False, 'event': self.event.slug})
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].id, 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '1')
|
||||
self.assertEqual(Item.objects.all()[0].container.id, self.box.id)
|
||||
|
||||
def test_create_item_without_container(self):
|
||||
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'description': '1'})
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_create_item_without_description(self):
|
||||
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box.id})
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
|
||||
def test_create_item_with_file(self):
|
||||
import base64
|
||||
response = self.client.post(f'/api/2/{self.event.slug}/item/',
|
||||
{'cid': self.box.id, 'description': '1',
|
||||
{'cid': self.box.cid, 'description': '1',
|
||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode(
|
||||
'utf-8')}, content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['id'], 1)
|
||||
self.assertEqual(response.json()['uid'], 1)
|
||||
self.assertEqual(response.json()['description'], '1')
|
||||
self.assertEqual(response.json()['box'], 'BOX')
|
||||
self.assertEqual(response.json()['id'], self.box.id)
|
||||
self.assertEqual(response.json()['cid'], self.box.cid)
|
||||
self.assertEqual(len(response.json()['file']), 64)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].id, 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '1')
|
||||
self.assertEqual(Item.objects.all()[0].container.id, self.box.id)
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
|
||||
def test_update_item(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/', {'description': '2'},
|
||||
response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/', {'description': '2'},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['id'], item.id)
|
||||
self.assertEqual(response.json()['description'], '2')
|
||||
self.assertEqual(response.json()['box'], 'BOX')
|
||||
self.assertEqual(response.json()['cid'], self.box.id)
|
||||
self.assertEqual(response.json()['file'], None)
|
||||
self.assertEqual(response.json()['returned'], False)
|
||||
self.assertEqual(response.json()['event'], self.event.slug)
|
||||
self.assertEqual(len(response.json()['related_issues']), 0)
|
||||
self.assertEqual(response.json(),
|
||||
{'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||
'returned': False, 'event': self.event.slug})
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].id, 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '2')
|
||||
self.assertEqual(Item.objects.all()[0].container.id, self.box.id)
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
|
||||
def test_update_item_with_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/',
|
||||
response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/',
|
||||
{'description': '2',
|
||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['id'], 1)
|
||||
self.assertEqual(response.json()['uid'], 1)
|
||||
self.assertEqual(response.json()['description'], '2')
|
||||
self.assertEqual(response.json()['box'], 'BOX')
|
||||
self.assertEqual(response.json()['id'], self.box.id)
|
||||
self.assertEqual(response.json()['cid'], self.box.cid)
|
||||
self.assertEqual(len(response.json()['file']), 64)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
self.assertEqual(Item.objects.all()[0].id, 1)
|
||||
self.assertEqual(Item.objects.all()[0].uid, 1)
|
||||
self.assertEqual(Item.objects.all()[0].description, '2')
|
||||
self.assertEqual(Item.objects.all()[0].container.id, self.box.id)
|
||||
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
|
||||
def test_delete_item(self):
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
self.assertEqual(len(Item.objects.all()), 2)
|
||||
response = self.client.delete(f'/api/2/{self.event.slug}/item/{item.id}/')
|
||||
response = self.client.delete(f'/api/2/{self.event.slug}/item/{item.uid}/')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
|
||||
|
@ -153,11 +123,11 @@ class ItemTestCase(TestCase):
|
|||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item2 = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
self.assertEqual(len(Item.objects.all()), 2)
|
||||
response = self.client.delete(f'/api/2/{self.event.slug}/item/{item2.id}/')
|
||||
response = self.client.delete(f'/api/2/{self.event.slug}/item/{item2.uid}/')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Item.objects.all()), 1)
|
||||
item3 = Item.objects.create(container=self.box, event=self.event, description='3')
|
||||
self.assertEqual(item3.id, 3)
|
||||
self.assertEqual(item3.uid, 3)
|
||||
self.assertEqual(len(Item.objects.all()), 2)
|
||||
|
||||
def test_item_count(self):
|
||||
|
@ -178,7 +148,7 @@ class ItemTestCase(TestCase):
|
|||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/', {'returned': True},
|
||||
response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.uid}/', {'returned': True},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
item.refresh_from_db()
|
||||
|
@ -198,4 +168,4 @@ class ItemTestCase(TestCase):
|
|||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['id'], item1.id)
|
||||
self.assertEqual(response.json()[0]['uid'], item1.uid)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from base64 import b64decode
|
||||
import logging
|
||||
|
||||
from django.urls import re_path
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
|
@ -14,9 +14,8 @@ from inventory.models import Event
|
|||
from mail.models import Email
|
||||
from mail.protocol import send_smtp, make_reply, collect_references
|
||||
from notify_sessions.models import SystemEvent
|
||||
from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher, ItemRelation
|
||||
from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher
|
||||
from tickets.serializers import IssueSerializer, CommentSerializer, ShippingVoucherSerializer
|
||||
from tickets.shared_serializers import RelationSerializer
|
||||
|
||||
|
||||
class IssueViewSet(viewsets.ModelViewSet):
|
||||
|
@ -24,16 +23,6 @@ class IssueViewSet(viewsets.ModelViewSet):
|
|||
queryset = IssueThread.objects.all()
|
||||
|
||||
|
||||
class RelationViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = RelationSerializer
|
||||
queryset = ItemRelation.objects.all()
|
||||
|
||||
|
||||
class CommentViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = CommentSerializer
|
||||
queryset = Comment.objects.all()
|
||||
|
||||
|
||||
class ShippingVoucherViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = ShippingVoucherSerializer
|
||||
queryset = ShippingVoucher.objects.all()
|
||||
|
@ -138,7 +127,6 @@ def add_comment(request, pk):
|
|||
|
||||
router = routers.SimpleRouter()
|
||||
router.register(r'tickets', IssueViewSet, basename='issues')
|
||||
router.register(r'matches', RelationViewSet, basename='matches')
|
||||
router.register(r'shipping_vouchers', ShippingVoucherViewSet, basename='shipping_vouchers')
|
||||
|
||||
urlpatterns = ([
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
# Generated by Django 4.2.7 on 2024-11-20 23:58
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('inventory', '0006_alter_event_table'),
|
||||
('tickets', '0011_train_old_spam'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='issuethread',
|
||||
name='related_items',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='itemrelation',
|
||||
name='issue_thread',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='item_relation_changes', to='tickets.issuethread'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='itemrelation',
|
||||
name='item',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='issue_relation_changes', to='inventory.item'),
|
||||
),
|
||||
]
|
|
@ -1,7 +1,5 @@
|
|||
from itertools import groupby
|
||||
|
||||
from django.utils import timezone
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django_softdelete.models import SoftDeleteModel
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
|
@ -45,6 +43,7 @@ class IssueThread(SoftDeleteModel):
|
|||
name = models.CharField(max_length=255)
|
||||
event = models.ForeignKey(Event, null=True, on_delete=models.SET_NULL, related_name='issue_threads')
|
||||
manually_created = models.BooleanField(default=False)
|
||||
related_items = models.ManyToManyField(Item, through='ItemRelation')
|
||||
|
||||
def short_uuid(self):
|
||||
return self.uuid[:8]
|
||||
|
@ -77,11 +76,6 @@ class IssueThread(SoftDeleteModel):
|
|||
return
|
||||
self.assignments.create(assigned_to=value)
|
||||
|
||||
@property
|
||||
def related_items(self):
|
||||
groups = groupby(self.item_relation_changes.all(), lambda rel: rel.item.id)
|
||||
return [sorted(v, key=lambda r: r.timestamp)[0].item for k, v in groups]
|
||||
|
||||
def __str__(self):
|
||||
return '[' + str(self.id) + '][' + self.short_uuid() + '] ' + self.name
|
||||
|
||||
|
@ -138,8 +132,8 @@ class Assignment(models.Model):
|
|||
|
||||
class ItemRelation(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='item_relation_changes')
|
||||
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='issue_relation_changes')
|
||||
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='item_relations')
|
||||
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='issues')
|
||||
timestamp = models.DateTimeField(auto_now_add=True)
|
||||
status = models.CharField(max_length=255, choices=RELATION_STATUS_CHOICES, default='possible')
|
||||
|
||||
|
|
|
@ -2,10 +2,9 @@ from rest_framework import serializers
|
|||
|
||||
from authentication.models import ExtendedUser
|
||||
from inventory.models import Event
|
||||
from inventory.shared_serializers import BasicItemSerializer
|
||||
from mail.api_v2 import AttachmentSerializer
|
||||
from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher
|
||||
from tickets.shared_serializers import BasicIssueSerializer
|
||||
from inventory.serializers import ItemSerializer
|
||||
|
||||
|
||||
class CommentSerializer(serializers.ModelSerializer):
|
||||
|
@ -38,10 +37,14 @@ class ShippingVoucherSerializer(serializers.ModelSerializer):
|
|||
read_only_fields = ('id', 'timestamp', 'used_at')
|
||||
|
||||
|
||||
class IssueSerializer(BasicIssueSerializer):
|
||||
class IssueSerializer(serializers.ModelSerializer):
|
||||
timeline = serializers.SerializerMethodField()
|
||||
last_activity = serializers.SerializerMethodField()
|
||||
related_items = BasicItemSerializer(many=True, read_only=True)
|
||||
assigned_to = serializers.SlugRelatedField(slug_field='username', queryset=ExtendedUser.objects.all(),
|
||||
allow_null=True, required=False)
|
||||
event = serializers.SlugRelatedField(slug_field='slug', queryset=Event.objects.all(),
|
||||
allow_null=True, required=False)
|
||||
related_items = ItemSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = IssueThread
|
||||
|
@ -69,8 +72,8 @@ class IssueSerializer(BasicIssueSerializer):
|
|||
last_mail = self.emails.order_by('-timestamp').first().timestamp if self.emails.count() > 0 else None
|
||||
last_assignment = self.assignments.order_by('-timestamp').first().timestamp if \
|
||||
self.assignments.count() > 0 else None
|
||||
last_relation = self.item_relation_changes.order_by('-timestamp').first().timestamp if \
|
||||
self.item_relation_changes.count() > 0 else None
|
||||
last_relation = self.item_relations.order_by('-timestamp').first().timestamp if \
|
||||
self.item_relations.count() > 0 else None
|
||||
args = [x for x in [last_state_change, last_comment, last_mail, last_assignment, last_relation] if
|
||||
x is not None]
|
||||
return max(args)
|
||||
|
@ -112,13 +115,13 @@ class IssueSerializer(BasicIssueSerializer):
|
|||
'timestamp': assignment.timestamp,
|
||||
'assigned_to': assignment.assigned_to.username,
|
||||
})
|
||||
for relation in (obj.item_relation_changes.all()):
|
||||
for relation in obj.item_relations.all():
|
||||
timeline.append({
|
||||
'type': 'item_relation',
|
||||
'id': relation.id,
|
||||
'status': relation.status,
|
||||
'timestamp': relation.timestamp,
|
||||
'item': BasicItemSerializer(relation.item).data,
|
||||
'item': ItemSerializer(relation.item).data,
|
||||
})
|
||||
for shipping_voucher in obj.shipping_vouchers.all():
|
||||
timeline.append({
|
||||
|
@ -130,3 +133,5 @@ class IssueSerializer(BasicIssueSerializer):
|
|||
})
|
||||
return sorted(timeline, key=lambda x: x['timestamp'])
|
||||
|
||||
def get_queryset(self):
|
||||
return IssueThread.objects.all().order_by('-last_activity')
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
from rest_framework import serializers
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from inventory.models import Event
|
||||
from tickets.models import IssueThread, ItemRelation
|
||||
|
||||
|
||||
class RelationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ItemRelation
|
||||
fields = ('id', 'status', 'timestamp', 'item', 'issue_thread')
|
||||
|
||||
|
||||
class BasicIssueSerializer(serializers.ModelSerializer):
|
||||
assigned_to = serializers.SlugRelatedField(slug_field='username', queryset=ExtendedUser.objects.all(),
|
||||
allow_null=True, required=False)
|
||||
event = serializers.SlugRelatedField(slug_field='slug', queryset=Event.objects.all(),
|
||||
allow_null=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = IssueThread
|
||||
fields = ('id', 'name', 'state', 'assigned_to', 'uuid', 'event')
|
||||
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
|
|
@ -1,149 +0,0 @@
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
from django.test import TestCase, Client
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from inventory.models import Event, Container, Item
|
||||
from mail.models import Email, EmailAttachment
|
||||
from tickets.models import IssueThread, StateChange, Comment, ItemRelation
|
||||
from django.contrib.auth.models import Permission
|
||||
from knox.models import AuthToken
|
||||
|
||||
from base64 import b64encode
|
||||
|
||||
|
||||
class IssueItemMatchApiTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||
self.user.user_permissions.add(*Permission.objects.all())
|
||||
self.user.save()
|
||||
self.event = Event.objects.create(slug='evt')
|
||||
self.box = Container.objects.create(name='box1')
|
||||
self.item = Item.objects.create(container=self.box, description="foo", event=self.event)
|
||||
self.token = AuthToken.objects.create(user=self.user)
|
||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
||||
now = datetime.now()
|
||||
self.issue = IssueThread.objects.create(
|
||||
name="test issue",
|
||||
event=self.event,
|
||||
)
|
||||
self.mail1 = Email.objects.create(
|
||||
subject='test',
|
||||
body='test',
|
||||
sender='test',
|
||||
recipient='test',
|
||||
issue_thread=self.issue,
|
||||
timestamp=now,
|
||||
)
|
||||
self.comment = Comment.objects.create(
|
||||
issue_thread=self.issue,
|
||||
comment="test",
|
||||
timestamp=now + timedelta(seconds=3),
|
||||
)
|
||||
self.match = ItemRelation.objects.create(
|
||||
issue_thread=self.issue,
|
||||
item=self.item,
|
||||
timestamp=now + timedelta(seconds=5),
|
||||
)
|
||||
|
||||
def test_issues(self):
|
||||
response = self.client.get('/api/2/tickets/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['id'], self.issue.id)
|
||||
self.assertEqual(response.json()[0]['name'], "test issue")
|
||||
self.assertEqual(response.json()[0]['state'], "pending_new")
|
||||
self.assertEqual(response.json()[0]['event'], "evt")
|
||||
self.assertEqual(response.json()[0]['assigned_to'], None)
|
||||
self.assertEqual(response.json()[0]['uuid'], self.issue.uuid)
|
||||
self.assertEqual(response.json()[0]['last_activity'], self.match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(len(response.json()[0]['timeline']), 4)
|
||||
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['type'], 'mail')
|
||||
self.assertEqual(response.json()[0]['timeline'][2]['type'], 'comment')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['id'], self.mail1.id)
|
||||
self.assertEqual(response.json()[0]['timeline'][2]['id'], self.comment.id)
|
||||
self.assertEqual(response.json()[0]['timeline'][0]['state'], 'pending_new')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['sender'], 'test')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['recipient'], 'test')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['subject'], 'test')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['body'], 'test')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['timestamp'],
|
||||
self.mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(response.json()[0]['timeline'][2]['comment'], 'test')
|
||||
self.assertEqual(response.json()[0]['timeline'][2]['timestamp'],
|
||||
self.comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(response.json()[0]['timeline'][3]['status'], 'possible')
|
||||
self.assertEqual(response.json()[0]['timeline'][3]['timestamp'],
|
||||
self.match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(response.json()[0]['timeline'][3]['item']['description'], "foo")
|
||||
self.assertEqual(response.json()[0]['timeline'][3]['item']['event'], "evt")
|
||||
self.assertEqual(response.json()[0]['timeline'][3]['item']['box'], "box1")
|
||||
self.assertEqual(response.json()[0]['related_items'][0]['description'], "foo")
|
||||
self.assertEqual(response.json()[0]['related_items'][0]['event'], "evt")
|
||||
self.assertEqual(response.json()[0]['related_items'][0]['box'], "box1")
|
||||
|
||||
def test_members(self):
|
||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['id'], self.item.id)
|
||||
self.assertEqual(response.json()[0]['description'], 'foo')
|
||||
self.assertEqual(response.json()[0]['box'], 'box1')
|
||||
self.assertEqual(response.json()[0]['cid'], self.box.id)
|
||||
self.assertEqual(response.json()[0]['file'], None)
|
||||
self.assertEqual(response.json()[0]['returned'], False)
|
||||
self.assertEqual(response.json()[0]['event'], self.event.slug)
|
||||
self.assertEqual(len(response.json()[0]['related_issues']), 1)
|
||||
self.assertEqual(response.json()[0]['related_issues'][0]['id'], self.issue.id)
|
||||
self.assertEqual(response.json()[0]['related_issues'][0]['name'], "test issue")
|
||||
|
||||
def test_add_match(self):
|
||||
response = self.client.get('/api/2/matches/')
|
||||
self.assertEqual(1, len(response.json()))
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
issue = IssueThread.objects.create(name="test issue", event=self.event)
|
||||
|
||||
response = self.client.post(f'/api/2/matches/',
|
||||
{'item': item.id, 'issue_thread': issue.id},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
|
||||
response = self.client.get('/api/2/matches/')
|
||||
self.assertEqual(2, len(response.json()))
|
||||
|
||||
response = self.client.get('/api/2/tickets/')
|
||||
self.assertEqual(4, len(response.json()[0]['timeline']))
|
||||
self.assertEqual('item_relation', response.json()[0]['timeline'][3]['type'])
|
||||
self.assertEqual('possible', response.json()[0]['timeline'][3]['status'])
|
||||
self.assertEqual(1, len(response.json()[0]['related_items']))
|
||||
|
||||
def test_change_match_state(self):
|
||||
response = self.client.get('/api/2/matches/')
|
||||
self.assertEqual(1, len(response.json()))
|
||||
|
||||
response = self.client.get('/api/2/tickets/')
|
||||
self.assertEqual(4, len(response.json()[0]['timeline']))
|
||||
self.assertEqual('item_relation', response.json()[0]['timeline'][3]['type'])
|
||||
self.assertEqual('possible', response.json()[0]['timeline'][3]['status'])
|
||||
self.assertEqual(1, len(response.json()[0]['related_items']))
|
||||
|
||||
response = self.client.post(f'/api/2/matches/',
|
||||
{'item': self.item.id, 'issue_thread': self.issue.id, 'status': 'confirmed'},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(response.json()['status'], 'confirmed')
|
||||
self.assertEqual(response.json()['id'], 2)
|
||||
|
||||
response = self.client.get('/api/2/matches/')
|
||||
self.assertEqual(2, len(response.json()))
|
||||
|
||||
response = self.client.get('/api/2/tickets/')
|
||||
self.assertEqual(5, len(response.json()[0]['timeline']))
|
||||
self.assertEqual('item_relation', response.json()[0]['timeline'][3]['type'])
|
||||
self.assertEqual('possible', response.json()[0]['timeline'][3]['status'])
|
||||
self.assertEqual('item_relation', response.json()[0]['timeline'][4]['type'])
|
||||
self.assertEqual('confirmed', response.json()[0]['timeline'][4]['status'])
|
||||
self.assertEqual(1, len(response.json()[0]['related_items']))
|
|
@ -3,9 +3,9 @@ from datetime import datetime, timedelta
|
|||
from django.test import TestCase, Client
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from inventory.models import Event, Container, Item
|
||||
from inventory.models import Event
|
||||
from mail.models import Email, EmailAttachment
|
||||
from tickets.models import IssueThread, StateChange, Comment, ItemRelation
|
||||
from tickets.models import IssueThread, StateChange, Comment
|
||||
from django.contrib.auth.models import Permission
|
||||
from knox.models import AuthToken
|
||||
|
||||
|
@ -18,8 +18,6 @@ class IssueApiTest(TestCase):
|
|||
self.user.user_permissions.add(*Permission.objects.all())
|
||||
self.user.save()
|
||||
self.event = Event.objects.create(slug='evt')
|
||||
self.box = Container.objects.create(name='box1')
|
||||
self.item = Item.objects.create(container=self.box, description="foo", event=self.event)
|
||||
self.token = AuthToken.objects.create(user=self.user)
|
||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
||||
|
||||
|
@ -56,11 +54,6 @@ class IssueApiTest(TestCase):
|
|||
comment="test",
|
||||
timestamp=now + timedelta(seconds=3),
|
||||
)
|
||||
match = ItemRelation.objects.create(
|
||||
issue_thread=issue,
|
||||
item = self.item,
|
||||
timestamp=now + timedelta(seconds=5),
|
||||
)
|
||||
self.assertEqual('pending_new', issue.state)
|
||||
self.assertEqual('test issue', issue.name)
|
||||
self.assertEqual(None, issue.assigned_to)
|
||||
|
@ -74,8 +67,8 @@ class IssueApiTest(TestCase):
|
|||
self.assertEqual(response.json()[0]['event'], "evt")
|
||||
self.assertEqual(response.json()[0]['assigned_to'], None)
|
||||
self.assertEqual(response.json()[0]['uuid'], issue.uuid)
|
||||
self.assertEqual(response.json()[0]['last_activity'], match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(len(response.json()[0]['timeline']), 5)
|
||||
self.assertEqual(response.json()[0]['last_activity'], comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(len(response.json()[0]['timeline']), 4)
|
||||
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state')
|
||||
self.assertEqual(response.json()[0]['timeline'][1]['type'], 'mail')
|
||||
self.assertEqual(response.json()[0]['timeline'][2]['type'], 'mail')
|
||||
|
@ -99,15 +92,6 @@ class IssueApiTest(TestCase):
|
|||
self.assertEqual(response.json()[0]['timeline'][3]['comment'], 'test')
|
||||
self.assertEqual(response.json()[0]['timeline'][3]['timestamp'],
|
||||
comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(response.json()[0]['timeline'][4]['status'], 'possible')
|
||||
self.assertEqual(response.json()[0]['timeline'][4]['timestamp'],
|
||||
match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
self.assertEqual(response.json()[0]['timeline'][4]['item']['description'], "foo")
|
||||
self.assertEqual(response.json()[0]['timeline'][4]['item']['event'], "evt")
|
||||
self.assertEqual(response.json()[0]['timeline'][4]['item']['box'], "box1")
|
||||
self.assertEqual(response.json()[0]['related_items'][0]['description'], "foo")
|
||||
self.assertEqual(response.json()[0]['related_items'][0]['event'], "evt")
|
||||
self.assertEqual(response.json()[0]['related_items'][0]['box'], "box1")
|
||||
|
||||
def test_issues_incomplete_timeline(self):
|
||||
now = datetime.now()
|
||||
|
|
Loading…
Reference in a new issue