Compare commits
1 commit
testing
...
wip/integr
Author | SHA1 | Date | |
---|---|---|---|
85a0640fd5 |
56 changed files with 1066 additions and 1718 deletions
|
@ -1,35 +0,0 @@
|
||||||
name: Bug Report
|
|
||||||
about: File a bug report
|
|
||||||
labels:
|
|
||||||
- Kind/Bug
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: what-happened
|
|
||||||
attributes:
|
|
||||||
label: What happened?
|
|
||||||
description: Also tell us, what did you expect to happen?
|
|
||||||
placeholder: Tell us what you see!
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: dropdown
|
|
||||||
id: browsers
|
|
||||||
attributes:
|
|
||||||
label: What browsers are you seeing the problem on?
|
|
||||||
multiple: true
|
|
||||||
options:
|
|
||||||
- Firefox (Windows)
|
|
||||||
- Firefox (MacOS)
|
|
||||||
- Firefox (Linux)
|
|
||||||
- Firefox (Android)
|
|
||||||
- Firefox (iOS)
|
|
||||||
- Chrome (Windows)
|
|
||||||
- Chrome (MacOS)
|
|
||||||
- Chrome (Linux)
|
|
||||||
- Chrome (Android)
|
|
||||||
- Chrome (iOS)
|
|
||||||
- Safari
|
|
||||||
- Microsoft Edge
|
|
|
@ -1,27 +0,0 @@
|
||||||
name: 'New Feature'
|
|
||||||
about: 'This template is for new features'
|
|
||||||
labels:
|
|
||||||
- Kind/Feature
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Before creating a Feature Ticket, please check for duplicates.
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
### Implementation Checklist
|
|
||||||
- [ ] concept
|
|
||||||
- [ ] frontend
|
|
||||||
- [ ] backend
|
|
||||||
- [ ] unittests
|
|
||||||
- [ ] tested on staging
|
|
||||||
visible: [ content ]
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: 'Feature Description'
|
|
||||||
description: 'Explain the the feature.'
|
|
||||||
placeholder: Description
|
|
||||||
validations:
|
|
||||||
required: true
|
|
|
@ -1,60 +0,0 @@
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- testing
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: docker
|
|
||||||
container:
|
|
||||||
image: ghcr.io/catthehacker/ubuntu:act-22.04
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: '3.11'
|
|
||||||
cache-dependency-path: '**/requirements.dev.txt'
|
|
||||||
- name: Install dependencies
|
|
||||||
working-directory: core
|
|
||||||
run: pip3 install -r requirements.dev.txt
|
|
||||||
- name: Run django tests
|
|
||||||
working-directory: core
|
|
||||||
run: python3 manage.py test
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
needs: [test]
|
|
||||||
runs-on: docker
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Install ansible
|
|
||||||
run: |
|
|
||||||
apt update -y
|
|
||||||
apt install python3-pip -y
|
|
||||||
python3 -m pip install ansible
|
|
||||||
python3 -m pip install ansible-lint
|
|
||||||
|
|
||||||
- name: Populate relevant files
|
|
||||||
run: |
|
|
||||||
mkdir ~/.ssh
|
|
||||||
echo "${{ secrets.C3LF_SSH_TESTING }}" > ~/.ssh/id_ed25519
|
|
||||||
chmod 0600 ~/.ssh/id_ed25519
|
|
||||||
ls -lah ~/.ssh
|
|
||||||
command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )
|
|
||||||
eval $(ssh-agent -s)
|
|
||||||
ssh-add ~/.ssh/id_ed25519
|
|
||||||
echo "andromeda.lab.or.it ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDXPoO0PE+B9PYwbGaLo98zhbmjAkp6eBtVeZe43v/+T" >> ~/.ssh/known_hosts
|
|
||||||
mkdir /etc/ansible
|
|
||||||
echo "${{ secrets.C3LF_INVENTORY_TESTING }}" > /etc/ansible/hosts
|
|
||||||
|
|
||||||
- name: Check ansible version
|
|
||||||
run: |
|
|
||||||
ansible --version
|
|
||||||
|
|
||||||
- name: List ansible hosts
|
|
||||||
run: |
|
|
||||||
ansible -m ping Andromeda
|
|
||||||
|
|
||||||
- name: Deploy testing
|
|
||||||
run: |
|
|
||||||
cd deploy/ansible
|
|
||||||
ansible-playbook playbooks/deploy-c3lf-sys3.yml
|
|
|
@ -1,21 +0,0 @@
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
push:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: docker
|
|
||||||
container:
|
|
||||||
image: ghcr.io/catthehacker/ubuntu:act-22.04
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: '3.11'
|
|
||||||
cache-dependency-path: '**/requirements.dev.txt'
|
|
||||||
- name: Install dependencies
|
|
||||||
working-directory: core
|
|
||||||
run: pip3 install -r requirements.dev.txt
|
|
||||||
- name: Run django tests
|
|
||||||
working-directory: core
|
|
||||||
run: python3 manage.py test
|
|
|
@ -12,7 +12,25 @@ from knox.models import AuthToken
|
||||||
from knox.views import LoginView as KnoxLoginView
|
from knox.views import LoginView as KnoxLoginView
|
||||||
|
|
||||||
from authentication.models import ExtendedUser
|
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):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -20,17 +38,26 @@ class UserViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = UserSerializer
|
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):
|
class GroupViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Group.objects.all()
|
queryset = Group.objects.all()
|
||||||
serializer_class = GroupSerializer
|
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'])
|
@api_view(['GET'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def selfUser(request):
|
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()]
|
|
|
@ -15,11 +15,6 @@ import sys
|
||||||
import dotenv
|
import dotenv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def truthy_str(s):
|
|
||||||
return s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'sure', 'positive', 'uh-huh', '👍']
|
|
||||||
|
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
|
@ -29,14 +24,12 @@ dotenv.load_dotenv(BASE_DIR / '.env')
|
||||||
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
||||||
|
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure-tm*$w_14iqbiy-!7(8#ba7j+_@(7@rf2&a^!=shs&$03b%2*rv')
|
SECRET_KEY = 'django-insecure-tm*$w_14iqbiy-!7(8#ba7j+_@(7@rf2&a^!=shs&$03b%2*rv'
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = truthy_str(os.getenv('DEBUG_MODE_ACTIVE', 'False'))
|
DEBUG = True
|
||||||
|
|
||||||
PRIMARY_HOST = os.getenv('HTTP_HOST', 'localhost')
|
ALLOWED_HOSTS = [os.getenv('HTTP_HOST', 'localhost')]
|
||||||
|
|
||||||
ALLOWED_HOSTS = [PRIMARY_HOST]
|
|
||||||
|
|
||||||
MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost')
|
MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost')
|
||||||
|
|
||||||
|
@ -47,8 +40,6 @@ LEGACY_USER_PASSWORD = os.getenv('LEGACY_API_PASSWORD', 'legacy_password')
|
||||||
|
|
||||||
SYSTEM3_VERSION = "0.0.0-dev.0"
|
SYSTEM3_VERSION = "0.0.0-dev.0"
|
||||||
|
|
||||||
ACTIVE_SPAM_TRAINING = truthy_str(os.getenv('ACTIVE_SPAM_TRAINING', 'False'))
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
@ -146,8 +137,7 @@ else:
|
||||||
'USER': os.getenv('DB_USER', 'system3'),
|
'USER': os.getenv('DB_USER', 'system3'),
|
||||||
'PASSWORD': os.getenv('DB_PASSWORD', 'system3'),
|
'PASSWORD': os.getenv('DB_PASSWORD', 'system3'),
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'charset': 'utf8mb4',
|
'charset': 'utf8mb4'
|
||||||
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,9 +210,11 @@ CHANNEL_LAYERS = {
|
||||||
'hosts': [(os.getenv('REDIS_HOST', 'localhost'), 6379)],
|
'hosts': [(os.getenv('REDIS_HOST', 'localhost'), 6379)],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SMTP_GATEWAY_HOST = os.getenv('SMTP_GATEWAY_HOST', 'localhost')
|
||||||
|
SMTP_GATEWAY_PORT = int(os.getenv('SMTP_GATEWAY_PORT', '25'))
|
||||||
|
|
||||||
PROMETHEUS_METRIC_NAMESPACE = 'c3lf'
|
PROMETHEUS_METRIC_NAMESPACE = 'c3lf'
|
||||||
|
|
||||||
TEST_RUNNER = 'core.test_runner.FastTestRunner'
|
TEST_RUNNER = 'core.test_runner.FastTestRunner'
|
||||||
|
|
|
@ -21,6 +21,9 @@ from .version import get_info
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('djangoadmin/', admin.site.urls),
|
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('inventory.api_v2')),
|
||||||
path('api/2/', include('files.api_v2')),
|
path('api/2/', include('files.api_v2')),
|
||||||
path('media/2/', include('files.media_v2')),
|
path('media/2/', include('files.media_v2')),
|
||||||
|
|
16
core/core/wsgi.py
Normal file
16
core/core/wsgi.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
"""
|
||||||
|
WSGI config for core project.
|
||||||
|
|
||||||
|
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
||||||
|
|
||||||
|
application = get_wsgi_application()
|
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),
|
||||||
|
]
|
|
@ -1,24 +0,0 @@
|
||||||
# Generated by Django 4.2.7 on 2024-11-21 22:40
|
|
||||||
|
|
||||||
from django.db import migrations
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
dependencies = [
|
|
||||||
('files', '0002_alter_file_file'),
|
|
||||||
]
|
|
||||||
|
|
||||||
def set_creation_date(apps, schema_editor):
|
|
||||||
File = apps.get_model('files', 'File')
|
|
||||||
for file in File.objects.all():
|
|
||||||
if file.created_at is None:
|
|
||||||
if not file.item.created_at is None:
|
|
||||||
file.created_at = file.item.created_at
|
|
||||||
else:
|
|
||||||
file.created_at = max(File.objects.filter(
|
|
||||||
id__lt=file.id, created_at__isnull=False).values_list('created_at', flat=True))
|
|
||||||
file.save()
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RunPython(set_creation_date),
|
|
||||||
]
|
|
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)
|
|
@ -1,6 +1,6 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from inventory.models import Item, Container, ItemPlacement, Comment, Event
|
from inventory.models import Item, Container, Event
|
||||||
|
|
||||||
|
|
||||||
class ItemAdmin(admin.ModelAdmin):
|
class ItemAdmin(admin.ModelAdmin):
|
||||||
|
@ -22,17 +22,3 @@ class EventAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(Event, EventAdmin)
|
admin.site.register(Event, EventAdmin)
|
||||||
|
|
||||||
|
|
||||||
class ItemPlacementAdmin(admin.ModelAdmin):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(ItemPlacement, ItemPlacementAdmin)
|
|
||||||
|
|
||||||
|
|
||||||
class CommentAdmin(admin.ModelAdmin):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(Comment, CommentAdmin)
|
|
||||||
|
|
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),
|
||||||
|
]
|
|
@ -1,4 +1,4 @@
|
||||||
from django.urls import re_path
|
from django.urls import path
|
||||||
from django.contrib.auth.decorators import permission_required
|
from django.contrib.auth.decorators import permission_required
|
||||||
from rest_framework import routers, viewsets
|
from rest_framework import routers, viewsets
|
||||||
from rest_framework.decorators import api_view, permission_classes
|
from rest_framework.decorators import api_view, permission_classes
|
||||||
|
@ -6,9 +6,7 @@ from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
from inventory.models import Event, Container, Item
|
from inventory.models import Event, Container, Item
|
||||||
from inventory.serializers import EventSerializer, ContainerSerializer, ItemSerializer, SearchResultSerializer
|
from inventory.serializers import EventSerializer, ContainerSerializer, ItemSerializer
|
||||||
|
|
||||||
from base64 import b64decode
|
|
||||||
|
|
||||||
|
|
||||||
class EventViewSet(viewsets.ModelViewSet):
|
class EventViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -22,26 +20,18 @@ class ContainerViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Container.objects.all()
|
queryset = Container.objects.all()
|
||||||
|
|
||||||
|
|
||||||
def filter_items(items, query):
|
|
||||||
query_tokens = query.split(' ')
|
|
||||||
for item in items:
|
|
||||||
value = 0
|
|
||||||
for token in query_tokens:
|
|
||||||
if token in item.description:
|
|
||||||
value += 1
|
|
||||||
if value > 0:
|
|
||||||
yield {'search_score': value, 'item': item}
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
|
@permission_required('view_item', raise_exception=True)
|
||||||
def search_items(request, event_slug, query):
|
def search_items(request, event_slug, query):
|
||||||
try:
|
try:
|
||||||
event = Event.objects.get(slug=event_slug)
|
event = Event.objects.get(slug=event_slug)
|
||||||
if not request.user.has_event_perm(event, 'view_item'):
|
query_tokens = query.split(' ')
|
||||||
return Response(status=403)
|
q = Item.objects.filter(event=event)
|
||||||
items = filter_items(Item.objects.filter(event=event), b64decode(query).decode('utf-8'))
|
for token in query_tokens:
|
||||||
return Response(SearchResultSerializer(items, many=True).data)
|
if token:
|
||||||
|
q = q.filter(description__icontains=token)
|
||||||
|
return Response(ItemSerializer(q, many=True).data)
|
||||||
except Event.DoesNotExist:
|
except Event.DoesNotExist:
|
||||||
return Response(status=404)
|
return Response(status=404)
|
||||||
|
|
||||||
|
@ -50,8 +40,6 @@ def search_items(request, event_slug, query):
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def item(request, event_slug):
|
def item(request, event_slug):
|
||||||
try:
|
try:
|
||||||
event = None
|
|
||||||
if event_slug != 'none':
|
|
||||||
event = Event.objects.get(slug=event_slug)
|
event = Event.objects.get(slug=event_slug)
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
if not request.user.has_event_perm(event, 'view_item'):
|
if not request.user.has_event_perm(event, 'view_item'):
|
||||||
|
@ -64,11 +52,8 @@ def item(request, event_slug):
|
||||||
if validated_data.is_valid():
|
if validated_data.is_valid():
|
||||||
validated_data.save(event=event)
|
validated_data.save(event=event)
|
||||||
return Response(validated_data.data, status=201)
|
return Response(validated_data.data, status=201)
|
||||||
return Response(status=400)
|
|
||||||
except Event.DoesNotExist:
|
except Event.DoesNotExist:
|
||||||
return Response(status=404)
|
return Response(status=404)
|
||||||
except KeyError:
|
|
||||||
return Response(status=400)
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET', 'PUT', 'DELETE', 'PATCH'])
|
@api_view(['GET', 'PUT', 'DELETE', 'PATCH'])
|
||||||
|
@ -76,7 +61,7 @@ def item(request, event_slug):
|
||||||
def item_by_id(request, event_slug, id):
|
def item_by_id(request, event_slug, id):
|
||||||
try:
|
try:
|
||||||
event = Event.objects.get(slug=event_slug)
|
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 request.method == 'GET':
|
||||||
if not request.user.has_event_perm(event, 'view_item'):
|
if not request.user.has_event_perm(event, 'view_item'):
|
||||||
return Response(status=403)
|
return Response(status=403)
|
||||||
|
@ -114,8 +99,8 @@ router.register(r'boxes', ContainerViewSet, basename='boxes')
|
||||||
router.register(r'box', ContainerViewSet, basename='boxes')
|
router.register(r'box', ContainerViewSet, basename='boxes')
|
||||||
|
|
||||||
urlpatterns = router.urls + [
|
urlpatterns = router.urls + [
|
||||||
re_path(r'^(?P<event_slug>[\w-]+)/items/$', item, name='item'),
|
path('<event_slug>/items/', item),
|
||||||
re_path(r'^(?P<event_slug>[\w-]+)/items/(?P<query>[-A-Za-z0-9+/]*={0,3})/$', search_items, name='search_items'),
|
path('<event_slug>/items/<query>/', search_items),
|
||||||
re_path(r'^(?P<event_slug>[\w-]+)/item/$', item, name='item'),
|
path('<event_slug>/item/', item),
|
||||||
re_path(r'^(?P<event_slug>[\w-]+)/item/(?P<id>\d+)/$', item_by_id, name='item_by_id'),
|
path('<event_slug>/item/<id>/', item_by_id),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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,52 +0,0 @@
|
||||||
# Generated by Django 4.2.7 on 2024-11-23 15:27
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
import django.db.models.deletion
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
dependencies = [
|
|
||||||
('inventory', '0006_alter_event_table'),
|
|
||||||
]
|
|
||||||
|
|
||||||
def set_initial_container(apps, schema_editor):
|
|
||||||
Item = apps.get_model('inventory', 'Item')
|
|
||||||
for item in Item.objects.all():
|
|
||||||
item.container_history.get_or_create(container=item.container_old)
|
|
||||||
item.save()
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RenameField(
|
|
||||||
model_name='item',
|
|
||||||
old_name='container',
|
|
||||||
new_name='container_old',
|
|
||||||
),
|
|
||||||
migrations.CreateModel(
|
|
||||||
name='ItemPlacement',
|
|
||||||
fields=[
|
|
||||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
|
||||||
('timestamp', models.DateTimeField(auto_now_add=True)),
|
|
||||||
('container',
|
|
||||||
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='item_history',
|
|
||||||
to='inventory.container')),
|
|
||||||
('item',
|
|
||||||
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='container_history',
|
|
||||||
to='inventory.item')),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
migrations.CreateModel(
|
|
||||||
name='Comment',
|
|
||||||
fields=[
|
|
||||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
|
||||||
('comment', models.TextField()),
|
|
||||||
('timestamp', models.DateTimeField(auto_now_add=True)),
|
|
||||||
('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments',
|
|
||||||
to='inventory.item')),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
migrations.RunPython(set_initial_container),
|
|
||||||
migrations.RemoveField(
|
|
||||||
model_name='item',
|
|
||||||
name='container_old',
|
|
||||||
),
|
|
||||||
]
|
|
|
@ -1,102 +1,56 @@
|
||||||
from itertools import groupby
|
from django.core.files.base import ContentFile
|
||||||
|
from django.db import models, IntegrityError
|
||||||
from django.db import models
|
|
||||||
from django_softdelete.models import SoftDeleteModel, SoftDeleteManager
|
from django_softdelete.models import SoftDeleteModel, SoftDeleteManager
|
||||||
|
|
||||||
|
|
||||||
class ItemManager(SoftDeleteManager):
|
class ItemManager(SoftDeleteManager):
|
||||||
|
|
||||||
def create(self, **kwargs):
|
def create(self, **kwargs):
|
||||||
container = kwargs.pop('container')
|
if 'uid' in kwargs:
|
||||||
if 'uid_deprecated' in kwargs:
|
raise ValueError('uid must not be set manually')
|
||||||
raise ValueError('uid_deprecated must not be set manually')
|
uid = Item.all_objects.filter(event=kwargs['event']).count() + 1
|
||||||
uid_deprecated = Item.all_objects.filter(event=kwargs['event']).count() + 1
|
kwargs['uid'] = uid
|
||||||
kwargs['uid_deprecated'] = uid_deprecated
|
return super().create(**kwargs)
|
||||||
item = super().create(**kwargs)
|
|
||||||
item.container = container
|
|
||||||
return item
|
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return super().get_queryset().filter(returned_at__isnull=True)
|
return super().get_queryset().filter(returned_at__isnull=True)
|
||||||
|
|
||||||
|
|
||||||
class Item(SoftDeleteModel):
|
class Item(SoftDeleteModel):
|
||||||
id = models.AutoField(primary_key=True)
|
iid = models.AutoField(primary_key=True)
|
||||||
uid_deprecated = models.IntegerField()
|
uid = models.IntegerField()
|
||||||
description = models.TextField()
|
description = models.TextField()
|
||||||
event = models.ForeignKey('Event', 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)
|
returned_at = models.DateTimeField(blank=True, null=True)
|
||||||
created_at = models.DateTimeField(null=True, auto_now_add=True)
|
created_at = models.DateTimeField(null=True, auto_now_add=True)
|
||||||
updated_at = models.DateTimeField(blank=True, null=True)
|
updated_at = models.DateTimeField(blank=True, null=True)
|
||||||
|
|
||||||
@property
|
|
||||||
def container(self):
|
|
||||||
try:
|
|
||||||
return self.container_history.order_by('-timestamp').first().container
|
|
||||||
except AttributeError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
@container.setter
|
|
||||||
def container(self, value):
|
|
||||||
if self.container == value:
|
|
||||||
return
|
|
||||||
self.container_history.create(container=value)
|
|
||||||
|
|
||||||
@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()
|
objects = ItemManager()
|
||||||
all_objects = models.Manager()
|
all_objects = models.Manager()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = (('uid_deprecated', 'event'),)
|
unique_together = (('uid', 'event'),)
|
||||||
permissions = [
|
permissions = [
|
||||||
('match_item', 'Can match item')
|
('match_item', 'Can match item')
|
||||||
]
|
]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '[' + str(self.id) + ']' + self.description
|
return '[' + str(self.uid) + ']' + self.description
|
||||||
|
|
||||||
|
|
||||||
class Container(SoftDeleteModel):
|
class Container(SoftDeleteModel):
|
||||||
id = models.AutoField(primary_key=True)
|
cid = models.AutoField(primary_key=True)
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
created_at = models.DateTimeField(blank=True, null=True)
|
created_at = models.DateTimeField(blank=True, null=True)
|
||||||
updated_at = models.DateTimeField(blank=True, null=True)
|
updated_at = models.DateTimeField(blank=True, null=True)
|
||||||
|
|
||||||
@property
|
|
||||||
def items(self):
|
|
||||||
try:
|
|
||||||
history = self.item_history.order_by('-timestamp').all()
|
|
||||||
return [v for k, v in groupby(history, key=lambda item: item.item.id)]
|
|
||||||
except AttributeError:
|
|
||||||
return []
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '[' + str(self.id) + ']' + self.name
|
return '[' + str(self.cid) + ']' + self.name
|
||||||
|
|
||||||
|
|
||||||
class ItemPlacement(models.Model):
|
|
||||||
id = models.AutoField(primary_key=True)
|
|
||||||
item = models.ForeignKey('Item', models.CASCADE, related_name='container_history')
|
|
||||||
container = models.ForeignKey('Container', models.CASCADE, related_name='item_history')
|
|
||||||
timestamp = models.DateTimeField(auto_now_add=True)
|
|
||||||
|
|
||||||
|
|
||||||
class Comment(models.Model):
|
|
||||||
id = models.AutoField(primary_key=True)
|
|
||||||
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='comments')
|
|
||||||
comment = models.TextField()
|
|
||||||
timestamp = models.DateTimeField(auto_now_add=True)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return str(self.item) + ' comment #' + str(self.id)
|
|
||||||
|
|
||||||
|
|
||||||
class Event(models.Model):
|
class Event(models.Model):
|
||||||
id = models.AutoField(primary_key=True)
|
eid = models.AutoField(primary_key=True)
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
slug = models.CharField(max_length=255, unique=True)
|
slug = models.CharField(max_length=255, unique=True)
|
||||||
start = models.DateTimeField(blank=True, null=True)
|
start = models.DateTimeField(blank=True, null=True)
|
||||||
|
@ -108,6 +62,3 @@ class Event(models.Model):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '[' + str(self.slug) + ']' + self.name
|
return '[' + str(self.slug) + ']' + self.name
|
||||||
|
|
||||||
class Meta:
|
|
||||||
db_table = 'common_event'
|
|
||||||
|
|
|
@ -1,29 +1,24 @@
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.relations import SlugRelatedField
|
|
||||||
|
|
||||||
from files.models import File
|
from files.models import File
|
||||||
from inventory.models import Event, Container, Item
|
from inventory.models import Event, Container, Item
|
||||||
from inventory.shared_serializers import BasicItemSerializer
|
|
||||||
from mail.models import EventAddress
|
from mail.models import EventAddress
|
||||||
from tickets.shared_serializers import BasicIssueSerializer
|
|
||||||
|
|
||||||
|
class EventAdressSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = EventAddress
|
||||||
|
fields = ['address']
|
||||||
|
|
||||||
|
|
||||||
class EventSerializer(serializers.ModelSerializer):
|
class EventSerializer(serializers.ModelSerializer):
|
||||||
addresses = SlugRelatedField(many=True, slug_field='address', queryset=EventAddress.objects.all())
|
addresses = EventAdressSerializer(many=True, required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Event
|
model = Event
|
||||||
fields = ['id', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
||||||
read_only_fields = ['id']
|
read_only_fields = ['eid']
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
|
||||||
data = data.copy()
|
|
||||||
addresses = data.pop('addresses', None)
|
|
||||||
dict = super().to_internal_value(data)
|
|
||||||
if addresses:
|
|
||||||
dict['addresses'] = [EventAddress.objects.get_or_create(address=x)[0] for x in addresses]
|
|
||||||
return dict
|
|
||||||
|
|
||||||
|
|
||||||
class ContainerSerializer(serializers.ModelSerializer):
|
class ContainerSerializer(serializers.ModelSerializer):
|
||||||
|
@ -31,29 +26,44 @@ class ContainerSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Container
|
model = Container
|
||||||
fields = ['id', 'name', 'itemCount']
|
fields = ['cid', 'name', 'itemCount']
|
||||||
read_only_fields = ['id', 'itemCount']
|
read_only_fields = ['cid', 'itemCount']
|
||||||
|
|
||||||
def get_itemCount(self, instance):
|
def get_itemCount(self, instance):
|
||||||
return len(instance.items)
|
return Item.objects.filter(container=instance.cid).count()
|
||||||
|
|
||||||
|
|
||||||
class ItemSerializer(BasicItemSerializer):
|
class ItemSerializer(serializers.ModelSerializer):
|
||||||
timeline = serializers.SerializerMethodField()
|
|
||||||
dataImage = serializers.CharField(write_only=True, required=False)
|
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)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Item
|
model = Item
|
||||||
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues',
|
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage', 'returned']
|
||||||
'timeline']
|
read_only_fields = ['uid']
|
||||||
read_only_fields = ['id']
|
|
||||||
|
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):
|
def to_internal_value(self, data):
|
||||||
container = None
|
container = None
|
||||||
returned = False
|
returned = False
|
||||||
if 'cid' in data:
|
if 'cid' in data:
|
||||||
container = Container.objects.get(id=data['cid'])
|
container = Container.objects.get(cid=data['cid'])
|
||||||
if 'returned' in data:
|
if 'returned' in data:
|
||||||
returned = data['returned']
|
returned = data['returned']
|
||||||
internal = super().to_internal_value(data)
|
internal = super().to_internal_value(data)
|
||||||
|
@ -64,8 +74,6 @@ class ItemSerializer(BasicItemSerializer):
|
||||||
return internal
|
return internal
|
||||||
|
|
||||||
def validate(self, attrs):
|
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)
|
return super().validate(attrs)
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
|
@ -87,42 +95,3 @@ class ItemSerializer(BasicItemSerializer):
|
||||||
validated_data.pop('dataImage')
|
validated_data.pop('dataImage')
|
||||||
instance.files.add(file)
|
instance.files.add(file)
|
||||||
return super().update(instance, validated_data)
|
return super().update(instance, validated_data)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_timeline(obj):
|
|
||||||
timeline = []
|
|
||||||
for comment in obj.comments.all():
|
|
||||||
timeline.append({
|
|
||||||
'type': 'comment',
|
|
||||||
'id': comment.id,
|
|
||||||
'timestamp': comment.timestamp,
|
|
||||||
'comment': comment.comment,
|
|
||||||
})
|
|
||||||
for relation in (obj.issue_relation_changes.all()):
|
|
||||||
timeline.append({
|
|
||||||
'type': 'issue_relation',
|
|
||||||
'id': relation.id,
|
|
||||||
'status': relation.status,
|
|
||||||
'timestamp': relation.timestamp,
|
|
||||||
'issue_thread': BasicIssueSerializer(relation.issue_thread).data,
|
|
||||||
})
|
|
||||||
for placement in (obj.container_history.all()):
|
|
||||||
timeline.append({
|
|
||||||
'type': 'placement',
|
|
||||||
'id': placement.id,
|
|
||||||
'timestamp': placement.timestamp,
|
|
||||||
'cid': placement.container.id,
|
|
||||||
'box': placement.container.name
|
|
||||||
})
|
|
||||||
return sorted(timeline, key=lambda x: x['timestamp'])
|
|
||||||
|
|
||||||
|
|
||||||
class SearchResultSerializer(serializers.Serializer):
|
|
||||||
search_score = serializers.IntegerField()
|
|
||||||
item = ItemSerializer()
|
|
||||||
|
|
||||||
def to_representation(self, instance):
|
|
||||||
return {**ItemSerializer(instance['item']).data, 'search_score': instance['search_score']}
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Item
|
|
||||||
|
|
|
@ -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 sorted(instance.files.all(), key=lambda x: x.created_at, reverse=True)[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/')
|
response = self.client.get('/api/2/boxes/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 1)
|
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]['name'], 'BOX')
|
||||||
self.assertEqual(response.json()[0]['itemCount'], 0)
|
self.assertEqual(response.json()[0]['itemCount'], 0)
|
||||||
|
|
||||||
|
@ -39,28 +39,28 @@ class ContainerTestCase(TestCase):
|
||||||
def test_create_container(self):
|
def test_create_container(self):
|
||||||
response = self.client.post('/api/2/box/', {'name': 'BOX'})
|
response = self.client.post('/api/2/box/', {'name': 'BOX'})
|
||||||
self.assertEqual(response.status_code, 201)
|
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()['name'], 'BOX')
|
||||||
self.assertEqual(response.json()['itemCount'], 0)
|
self.assertEqual(response.json()['itemCount'], 0)
|
||||||
self.assertEqual(len(Container.objects.all()), 1)
|
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')
|
self.assertEqual(Container.objects.all()[0].name, 'BOX')
|
||||||
|
|
||||||
def test_update_container(self):
|
def test_update_container(self):
|
||||||
box = Container.objects.create(name='BOX 1')
|
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.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()['name'], 'BOX 2')
|
||||||
self.assertEqual(response.json()['itemCount'], 0)
|
self.assertEqual(response.json()['itemCount'], 0)
|
||||||
self.assertEqual(len(Container.objects.all()), 1)
|
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')
|
self.assertEqual(Container.objects.all()[0].name, 'BOX 2')
|
||||||
|
|
||||||
def test_delete_container(self):
|
def test_delete_container(self):
|
||||||
box = Container.objects.create(name='BOX 1')
|
box = Container.objects.create(name='BOX 1')
|
||||||
Container.objects.create(name='BOX 2')
|
Container.objects.create(name='BOX 2')
|
||||||
self.assertEqual(len(Container.objects.all()), 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(response.status_code, 204)
|
||||||
self.assertEqual(len(Container.objects.all()), 1)
|
self.assertEqual(len(Container.objects.all()), 1)
|
||||||
|
|
|
@ -39,7 +39,7 @@ class EventTestCase(TestCase):
|
||||||
def test_update_event(self):
|
def test_update_event(self):
|
||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
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.status_code, 200)
|
||||||
self.assertEqual(response.json()['slug'], 'EVENT2')
|
self.assertEqual(response.json()['slug'], 'EVENT2')
|
||||||
self.assertEqual(response.json()['name'], 'Event 2 new')
|
self.assertEqual(response.json()['name'], 'Event 2 new')
|
||||||
|
@ -47,32 +47,18 @@ class EventTestCase(TestCase):
|
||||||
self.assertEqual(Event.objects.all()[0].slug, 'EVENT2')
|
self.assertEqual(Event.objects.all()[0].slug, 'EVENT2')
|
||||||
self.assertEqual(Event.objects.all()[0].name, 'Event 2 new')
|
self.assertEqual(Event.objects.all()[0].name, 'Event 2 new')
|
||||||
|
|
||||||
def test_update_event(self):
|
|
||||||
from rest_framework.test import APIClient
|
|
||||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
|
||||||
response = APIClient().patch(f'/api/2/events/{event.id}/', {'addresses': ['foo@bar.baz', 'foo1@bar.baz']})
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
self.assertEqual(response.json()['slug'], 'EVENT1')
|
|
||||||
self.assertEqual(response.json()['name'], 'Event 1')
|
|
||||||
self.assertEqual(2, len(response.json()['addresses']))
|
|
||||||
self.assertEqual('foo@bar.baz', response.json()['addresses'][0])
|
|
||||||
self.assertEqual('foo1@bar.baz', response.json()['addresses'][1])
|
|
||||||
self.assertEqual(len(Event.objects.all()), 1)
|
|
||||||
self.assertEqual(Event.objects.all()[0].slug, 'EVENT1')
|
|
||||||
self.assertEqual(Event.objects.all()[0].name, 'Event 1')
|
|
||||||
|
|
||||||
def test_remove_event(self):
|
def test_remove_event(self):
|
||||||
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
event = Event.objects.create(slug='EVENT1', name='Event 1')
|
||||||
Event.objects.create(slug='EVENT2', name='Event 2')
|
Event.objects.create(slug='EVENT2', name='Event 2')
|
||||||
self.assertEqual(len(Event.objects.all()), 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(response.status_code, 204)
|
||||||
self.assertEqual(len(Event.objects.all()), 1)
|
self.assertEqual(len(Event.objects.all()), 1)
|
||||||
|
|
||||||
def test_event_with_address(self):
|
def test_items2(self):
|
||||||
from mail.models import EventAddress
|
from mail.models import EventAddress
|
||||||
event1 = Event.objects.create(slug='TEST1', name='Event')
|
event1 = Event.objects.create(slug='TEST1', name='Event')
|
||||||
EventAddress.objects.create(event=event1, address='foo@bar.baz')
|
EventAddress.objects.create(event=Event.objects.get(slug='TEST1'), address='foo@bar.baz')
|
||||||
response = self.client.get('/api/2/events/')
|
response = self.client.get('/api/2/events/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(1, len(response.json()))
|
self.assertEqual(1, len(response.json()))
|
||||||
|
@ -80,15 +66,3 @@ class EventTestCase(TestCase):
|
||||||
self.assertEqual('Event', response.json()[0]['name'])
|
self.assertEqual('Event', response.json()[0]['name'])
|
||||||
self.assertEqual(1, len(response.json()[0]['addresses']))
|
self.assertEqual(1, len(response.json()[0]['addresses']))
|
||||||
|
|
||||||
def test_items_remove_addresss(self):
|
|
||||||
from mail.models import EventAddress
|
|
||||||
from rest_framework.test import APIClient
|
|
||||||
event1 = Event.objects.create(slug='TEST1', name='Event')
|
|
||||||
EventAddress.objects.create(event=event1, address='foo@bar.baz')
|
|
||||||
EventAddress.objects.create(event=event1, address='fo1o@bar.baz')
|
|
||||||
response = APIClient().patch(f'/api/2/events/{event1.id}/', {'addresses': ['foo1@bar.baz']})
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
self.assertEqual('TEST1', response.json()['slug'])
|
|
||||||
self.assertEqual('Event', response.json()['name'])
|
|
||||||
self.assertEqual(1, len(response.json()['addresses']))
|
|
||||||
self.assertEqual('foo1@bar.baz', response.json()['addresses'][0])
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
from datetime import datetime, timedelta
|
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase, Client
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
|
@ -7,11 +5,7 @@ from knox.models import AuthToken
|
||||||
|
|
||||||
from authentication.models import ExtendedUser
|
from authentication.models import ExtendedUser
|
||||||
from files.models import File
|
from files.models import File
|
||||||
from inventory.models import Event, Container, Item, Comment, ItemPlacement
|
from inventory.models import Event, Container, Item
|
||||||
|
|
||||||
from base64 import b64encode
|
|
||||||
|
|
||||||
from tickets.models import IssueThread, ItemRelation
|
|
||||||
|
|
||||||
|
|
||||||
class ItemTestCase(TestCase):
|
class ItemTestCase(TestCase):
|
||||||
|
@ -19,240 +13,142 @@ class ItemTestCase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||||
self.box1 = Container.objects.create(name='BOX1')
|
self.box = Container.objects.create(name='BOX')
|
||||||
self.box2 = Container.objects.create(name='BOX2')
|
|
||||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||||
self.user.user_permissions.add(*Permission.objects.all())
|
self.user.user_permissions.add(*Permission.objects.all())
|
||||||
self.token = AuthToken.objects.create(user=self.user)
|
self.token = AuthToken.objects.create(user=self.user)
|
||||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
||||||
self.issue = IssueThread.objects.create(
|
|
||||||
name="test issue",
|
|
||||||
event=self.event,
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.content, b'[]')
|
self.assertEqual(response.content, b'[]')
|
||||||
|
|
||||||
def test_members_and_timeline(self):
|
def test_members(self):
|
||||||
now = datetime.now()
|
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
|
||||||
comment = Comment.objects.create(
|
|
||||||
item=item,
|
|
||||||
comment="test",
|
|
||||||
timestamp=now + timedelta(seconds=3),
|
|
||||||
)
|
|
||||||
match = ItemRelation.objects.create(
|
|
||||||
issue_thread=self.issue,
|
|
||||||
item=item,
|
|
||||||
timestamp=now + timedelta(seconds=4),
|
|
||||||
)
|
|
||||||
placement = ItemPlacement.objects.create(
|
|
||||||
container=self.box2,
|
|
||||||
item=item,
|
|
||||||
timestamp=now + timedelta(seconds=5),
|
|
||||||
)
|
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 1)
|
self.assertEqual(response.json(),
|
||||||
self.assertEqual(response.json()[0]['id'], item.id)
|
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||||
self.assertEqual(response.json()[0]['description'], '1')
|
'returned': False}])
|
||||||
self.assertEqual(response.json()[0]['box'], 'BOX2')
|
|
||||||
self.assertEqual(response.json()[0]['cid'], self.box2.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]['timeline']), 4)
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'placement')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][1]['type'], 'comment')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['type'], 'issue_relation')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['type'], 'placement')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][1]['id'], comment.id)
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['id'], match.id)
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['id'], placement.id)
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][0]['box'], 'BOX1')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][0]['cid'], self.box1.id)
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][1]['comment'], 'test')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][1]['timestamp'],
|
|
||||||
comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['status'], 'possible')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['timestamp'],
|
|
||||||
match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['issue_thread']['name'], "test issue")
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['issue_thread']['event'], "EVENT")
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['issue_thread']['state'], "pending_new")
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['box'], 'BOX2')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['cid'], self.box2.id)
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['timestamp'],
|
|
||||||
placement.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
|
||||||
self.assertEqual(len(response.json()[0]['related_issues']), 1)
|
|
||||||
self.assertEqual(response.json()[0]['related_issues'][0]['name'], "test issue")
|
|
||||||
self.assertEqual(response.json()[0]['related_issues'][0]['event'], "EVENT")
|
|
||||||
self.assertEqual(response.json()[0]['related_issues'][0]['state'], "pending_new")
|
|
||||||
|
|
||||||
def test_members_with_file(self):
|
def test_members_with_file(self):
|
||||||
import base64
|
import base64
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
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'))
|
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/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 1)
|
self.assertEqual(response.json(),
|
||||||
self.assertEqual(response.json()[0]['id'], item.id)
|
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash,
|
||||||
self.assertEqual(response.json()[0]['description'], '1')
|
'returned': False}])
|
||||||
self.assertEqual(response.json()[0]['box'], 'BOX1')
|
|
||||||
self.assertEqual(response.json()[0]['cid'], self.box1.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)
|
|
||||||
|
|
||||||
def test_members_with_two_file(self):
|
|
||||||
import base64
|
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
|
||||||
file1 = File.objects.create(item=item,
|
|
||||||
data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
|
||||||
file2 = File.objects.create(item=item,
|
|
||||||
data="data:text/plain;base64," + base64.b64encode(b"bar").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'], 'BOX1')
|
|
||||||
self.assertEqual(response.json()[0]['cid'], self.box1.id)
|
|
||||||
self.assertEqual(response.json()[0]['file'], file2.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)
|
|
||||||
|
|
||||||
def test_multi_members(self):
|
def test_multi_members(self):
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='1')
|
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='2')
|
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='3')
|
Item.objects.create(container=self.box, event=self.event, description='3')
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 3)
|
self.assertEqual(len(response.json()), 3)
|
||||||
|
|
||||||
def test_create_item(self):
|
def test_create_item(self):
|
||||||
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box1.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.status_code, 201)
|
||||||
self.assertEqual(response.json()['id'], 1)
|
self.assertEqual(response.json(),
|
||||||
self.assertEqual(response.json()['description'], '1')
|
{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||||
self.assertEqual(response.json()['box'], 'BOX1')
|
'returned': False})
|
||||||
self.assertEqual(response.json()['cid'], self.box1.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(len(Item.objects.all()), 1)
|
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].description, '1')
|
||||||
self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
|
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||||
|
|
||||||
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.box1.id})
|
|
||||||
self.assertEqual(response.status_code, 400)
|
|
||||||
|
|
||||||
def test_create_item_with_file(self):
|
def test_create_item_with_file(self):
|
||||||
import base64
|
import base64
|
||||||
response = self.client.post(f'/api/2/{self.event.slug}/item/',
|
response = self.client.post(f'/api/2/{self.event.slug}/item/',
|
||||||
{'cid': self.box1.id, 'description': '1',
|
{'cid': self.box.cid, 'description': '1',
|
||||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode(
|
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode(
|
||||||
'utf-8')}, content_type='application/json')
|
'utf-8')}, content_type='application/json')
|
||||||
self.assertEqual(response.status_code, 201)
|
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()['description'], '1')
|
||||||
self.assertEqual(response.json()['box'], 'BOX1')
|
self.assertEqual(response.json()['box'], 'BOX')
|
||||||
self.assertEqual(response.json()['id'], self.box1.id)
|
self.assertEqual(response.json()['cid'], self.box.cid)
|
||||||
self.assertEqual(len(response.json()['file']), 64)
|
self.assertEqual(len(response.json()['file']), 64)
|
||||||
self.assertEqual(len(Item.objects.all()), 1)
|
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].description, '1')
|
||||||
self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
|
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||||
self.assertEqual(len(File.objects.all()), 1)
|
self.assertEqual(len(File.objects.all()), 1)
|
||||||
|
|
||||||
def test_update_item(self):
|
def test_update_item(self):
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
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')
|
content_type='application/json')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.json()['id'], item.id)
|
self.assertEqual(response.json(),
|
||||||
self.assertEqual(response.json()['description'], '2')
|
{'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None,
|
||||||
self.assertEqual(response.json()['box'], 'BOX1')
|
'returned': False})
|
||||||
self.assertEqual(response.json()['cid'], self.box1.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(len(Item.objects.all()), 1)
|
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].description, '2')
|
||||||
self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
|
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||||
|
|
||||||
def test_update_item_with_file(self):
|
def test_update_item_with_file(self):
|
||||||
import base64
|
import base64
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
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',
|
{'description': '2',
|
||||||
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode(
|
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||||
'utf-8')},
|
|
||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
self.assertEqual(response.status_code, 200)
|
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()['description'], '2')
|
||||||
self.assertEqual(response.json()['box'], 'BOX1')
|
self.assertEqual(response.json()['box'], 'BOX')
|
||||||
self.assertEqual(response.json()['id'], self.box1.id)
|
self.assertEqual(response.json()['cid'], self.box.cid)
|
||||||
self.assertEqual(len(response.json()['file']), 64)
|
self.assertEqual(len(response.json()['file']), 64)
|
||||||
self.assertEqual(len(Item.objects.all()), 1)
|
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].description, '2')
|
||||||
self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
|
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid)
|
||||||
self.assertEqual(len(File.objects.all()), 1)
|
self.assertEqual(len(File.objects.all()), 1)
|
||||||
|
|
||||||
def test_delete_item(self):
|
def test_delete_item(self):
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='2')
|
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||||
self.assertEqual(len(Item.objects.all()), 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(response.status_code, 204)
|
||||||
self.assertEqual(len(Item.objects.all()), 1)
|
self.assertEqual(len(Item.objects.all()), 1)
|
||||||
|
|
||||||
def test_delete_item2(self):
|
def test_delete_item2(self):
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='1')
|
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
item2 = Item.objects.create(container=self.box1, event=self.event, description='2')
|
item2 = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||||
self.assertEqual(len(Item.objects.all()), 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(response.status_code, 204)
|
||||||
self.assertEqual(len(Item.objects.all()), 1)
|
self.assertEqual(len(Item.objects.all()), 1)
|
||||||
item3 = Item.objects.create(container=self.box1, event=self.event, description='3')
|
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)
|
self.assertEqual(len(Item.objects.all()), 2)
|
||||||
|
|
||||||
def test_item_count(self):
|
def test_item_count(self):
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='1')
|
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
Item.objects.create(container=self.box1, event=self.event, description='2')
|
Item.objects.create(container=self.box, event=self.event, description='2')
|
||||||
response = self.client.get('/api/2/boxes/')
|
response = self.client.get('/api/2/boxes/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 2)
|
self.assertEqual(len(response.json()), 1)
|
||||||
self.assertEqual(response.json()[0]['itemCount'], 2)
|
self.assertEqual(response.json()[0]['itemCount'], 2)
|
||||||
self.assertEqual(response.json()[1]['itemCount'], 0)
|
|
||||||
|
|
||||||
def test_item_nonexistent(self):
|
def test_item_nonexistent(self):
|
||||||
response = self.client.get(f'/api/2/NOEVENT/item/')
|
response = self.client.get(f'/api/2/NOEVENT/item/')
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
def test_item_return(self):
|
def test_item_return(self):
|
||||||
item = Item.objects.create(container=self.box1, event=self.event, description='1')
|
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
self.assertEqual(item.returned_at, None)
|
self.assertEqual(item.returned_at, None)
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 1)
|
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')
|
content_type='application/json')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
item.refresh_from_db()
|
item.refresh_from_db()
|
||||||
|
@ -262,8 +158,8 @@ class ItemTestCase(TestCase):
|
||||||
self.assertEqual(len(response.json()), 0)
|
self.assertEqual(len(response.json()), 0)
|
||||||
|
|
||||||
def test_item_show_not_returned(self):
|
def test_item_show_not_returned(self):
|
||||||
item1 = Item.objects.create(container=self.box1, event=self.event, description='1')
|
item1 = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||||
item2 = Item.objects.create(container=self.box1, event=self.event, description='2')
|
item2 = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 2)
|
self.assertEqual(len(response.json()), 2)
|
||||||
|
@ -272,74 +168,4 @@ class ItemTestCase(TestCase):
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
response = self.client.get(f'/api/2/{self.event.slug}/item/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(len(response.json()), 1)
|
self.assertEqual(len(response.json()), 1)
|
||||||
self.assertEqual(response.json()[0]['id'], item1.id)
|
self.assertEqual(response.json()[0]['uid'], item1.uid)
|
||||||
|
|
||||||
|
|
||||||
class ItemSearchTestCase(TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super().setUp()
|
|
||||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
|
||||||
self.box1 = Container.objects.create(name='BOX1')
|
|
||||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
|
||||||
self.user.user_permissions.add(*Permission.objects.all())
|
|
||||||
self.token = AuthToken.objects.create(user=self.user)
|
|
||||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
|
||||||
self.item1 = Item.objects.create(container=self.box1, event=self.event, description='abc def')
|
|
||||||
self.item2 = Item.objects.create(container=self.box1, event=self.event, description='def ghi')
|
|
||||||
self.item3 = Item.objects.create(container=self.box1, event=self.event, description='jkl mno pqr')
|
|
||||||
self.item4 = Item.objects.create(container=self.box1, event=self.event, description='stu vwx')
|
|
||||||
|
|
||||||
def test_search(self):
|
|
||||||
search_query = b64encode(b'abc').decode('utf-8')
|
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
|
||||||
self.assertEqual(200, response.status_code)
|
|
||||||
self.assertEqual(1, len(response.json()))
|
|
||||||
self.assertEqual(self.item1.id, response.json()[0]['id'])
|
|
||||||
self.assertEqual('abc def', response.json()[0]['description'])
|
|
||||||
self.assertEqual('BOX1', response.json()[0]['box'])
|
|
||||||
self.assertEqual(self.box1.id, response.json()[0]['cid'])
|
|
||||||
self.assertEqual(1, response.json()[0]['search_score'])
|
|
||||||
|
|
||||||
def test_search2(self):
|
|
||||||
search_query = b64encode(b'def').decode('utf-8')
|
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
|
||||||
self.assertEqual(200, response.status_code)
|
|
||||||
self.assertEqual(2, len(response.json()))
|
|
||||||
self.assertEqual(self.item1.id, response.json()[0]['id'])
|
|
||||||
self.assertEqual('abc def', response.json()[0]['description'])
|
|
||||||
self.assertEqual('BOX1', response.json()[0]['box'])
|
|
||||||
self.assertEqual(self.box1.id, response.json()[0]['cid'])
|
|
||||||
self.assertEqual(1, response.json()[0]['search_score'])
|
|
||||||
self.assertEqual(self.item2.id, response.json()[1]['id'])
|
|
||||||
self.assertEqual('def ghi', response.json()[1]['description'])
|
|
||||||
self.assertEqual('BOX1', response.json()[1]['box'])
|
|
||||||
self.assertEqual(self.box1.id, response.json()[1]['cid'])
|
|
||||||
self.assertEqual(1, response.json()[0]['search_score'])
|
|
||||||
|
|
||||||
def test_search3(self):
|
|
||||||
search_query = b64encode(b'jkl').decode('utf-8')
|
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
|
||||||
self.assertEqual(200, response.status_code)
|
|
||||||
self.assertEqual(1, len(response.json()))
|
|
||||||
self.assertEqual(self.item3.id, response.json()[0]['id'])
|
|
||||||
self.assertEqual('jkl mno pqr', response.json()[0]['description'])
|
|
||||||
self.assertEqual('BOX1', response.json()[0]['box'])
|
|
||||||
self.assertEqual(self.box1.id, response.json()[0]['cid'])
|
|
||||||
self.assertEqual(1, response.json()[0]['search_score'])
|
|
||||||
|
|
||||||
def test_search4(self):
|
|
||||||
search_query = b64encode(b'abc def').decode('utf-8')
|
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/items/{search_query}/')
|
|
||||||
self.assertEqual(200, response.status_code)
|
|
||||||
self.assertEqual(2, len(response.json()))
|
|
||||||
self.assertEqual(self.item1.id, response.json()[0]['id'])
|
|
||||||
self.assertEqual('abc def', response.json()[0]['description'])
|
|
||||||
self.assertEqual('BOX1', response.json()[0]['box'])
|
|
||||||
self.assertEqual(self.box1.id, response.json()[0]['cid'])
|
|
||||||
self.assertEqual(2, response.json()[0]['search_score'])
|
|
||||||
self.assertEqual(self.item2.id, response.json()[1]['id'])
|
|
||||||
self.assertEqual('def ghi', response.json()[1]['description'])
|
|
||||||
self.assertEqual('BOX1', response.json()[1]['box'])
|
|
||||||
self.assertEqual(self.box1.id, response.json()[1]['cid'])
|
|
||||||
self.assertEqual(1, response.json()[1]['search_score'])
|
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
# Generated by Django 4.2.7 on 2024-11-08 20:37
|
|
||||||
from django.core.files.base import ContentFile
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
dependencies = [
|
|
||||||
('mail', '0005_alter_eventaddress_event'),
|
|
||||||
]
|
|
||||||
|
|
||||||
def move_raw_mails_to_file(apps, schema_editor):
|
|
||||||
Email = apps.get_model('mail', 'Email')
|
|
||||||
for email in Email.objects.all():
|
|
||||||
raw_content = email.raw
|
|
||||||
path = "mail_{}".format(email.id)
|
|
||||||
if len(raw_content):
|
|
||||||
email.raw_file.save(path, ContentFile(raw_content))
|
|
||||||
email.save()
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='email',
|
|
||||||
name='raw_file',
|
|
||||||
field=models.FileField(null=True, upload_to='raw_mail/'),
|
|
||||||
),
|
|
||||||
migrations.RunPython(move_raw_mails_to_file),
|
|
||||||
migrations.RemoveField(
|
|
||||||
model_name='email',
|
|
||||||
name='raw',
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name='email',
|
|
||||||
name='raw_file',
|
|
||||||
field=models.FileField(upload_to='raw_mail/'),
|
|
||||||
),
|
|
||||||
]
|
|
|
@ -3,7 +3,7 @@ import random
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django_softdelete.models import SoftDeleteModel
|
from django_softdelete.models import SoftDeleteModel
|
||||||
|
|
||||||
from core.settings import MAIL_DOMAIN, ACTIVE_SPAM_TRAINING
|
from core.settings import MAIL_DOMAIN
|
||||||
from files.models import AbstractFile
|
from files.models import AbstractFile
|
||||||
from inventory.models import Event
|
from inventory.models import Event
|
||||||
from tickets.models import IssueThread
|
from tickets.models import IssueThread
|
||||||
|
@ -18,7 +18,7 @@ class Email(SoftDeleteModel):
|
||||||
recipient = models.CharField(max_length=255)
|
recipient = models.CharField(max_length=255)
|
||||||
reference = models.CharField(max_length=255, null=True, unique=True)
|
reference = models.CharField(max_length=255, null=True, unique=True)
|
||||||
in_reply_to = models.CharField(max_length=255, null=True)
|
in_reply_to = models.CharField(max_length=255, null=True)
|
||||||
raw_file = models.FileField(upload_to='raw_mail/')
|
raw = models.TextField()
|
||||||
issue_thread = models.ForeignKey(IssueThread, models.SET_NULL, null=True, related_name='emails')
|
issue_thread = models.ForeignKey(IssueThread, models.SET_NULL, null=True, related_name='emails')
|
||||||
event = models.ForeignKey(Event, models.SET_NULL, null=True)
|
event = models.ForeignKey(Event, models.SET_NULL, null=True)
|
||||||
|
|
||||||
|
@ -28,18 +28,6 @@ class Email(SoftDeleteModel):
|
||||||
self.reference = f'<{random.randint(0, 1000000000):09}@{MAIL_DOMAIN}>'
|
self.reference = f'<{random.randint(0, 1000000000):09}@{MAIL_DOMAIN}>'
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def train_spam(self):
|
|
||||||
if ACTIVE_SPAM_TRAINING and self.raw_file.path:
|
|
||||||
import subprocess
|
|
||||||
path = self.raw_file.path
|
|
||||||
subprocess.run(["rspamc", "learn_spam", path])
|
|
||||||
|
|
||||||
def train_ham(self):
|
|
||||||
if ACTIVE_SPAM_TRAINING and self.raw_file.path:
|
|
||||||
import subprocess
|
|
||||||
path = self.raw_file.path
|
|
||||||
subprocess.run(["rspamc", "learn_ham", path])
|
|
||||||
|
|
||||||
|
|
||||||
class EventAddress(models.Model):
|
class EventAddress(models.Model):
|
||||||
id = models.AutoField(primary_key=True)
|
id = models.AutoField(primary_key=True)
|
||||||
|
|
|
@ -10,6 +10,8 @@ from mail.models import Email, EventAddress, EmailAttachment
|
||||||
from notify_sessions.models import SystemEvent
|
from notify_sessions.models import SystemEvent
|
||||||
from tickets.models import IssueThread
|
from tickets.models import IssueThread
|
||||||
|
|
||||||
|
from core.settings import SMTP_GATEWAY_HOST, SMTP_GATEWAY_PORT
|
||||||
|
|
||||||
|
|
||||||
class SpecialMailException(Exception):
|
class SpecialMailException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -88,10 +90,10 @@ def make_reply(reply_email, references=None, event=None):
|
||||||
|
|
||||||
|
|
||||||
async def send_smtp(message):
|
async def send_smtp(message):
|
||||||
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
await aiosmtplib.send(message, hostname=SMTP_GATEWAY_HOST, port=SMTP_GATEWAY_PORT, use_tls=False, start_tls=False)
|
||||||
|
|
||||||
|
|
||||||
def find_active_issue_thread(in_reply_to, address, subject, event):
|
def find_active_issue_thread(in_reply_to, address, subject):
|
||||||
from re import match
|
from re import match
|
||||||
uuid_match = match(r'^ticket\+([a-f0-9-]{36})@', address)
|
uuid_match = match(r'^ticket\+([a-f0-9-]{36})@', address)
|
||||||
if uuid_match:
|
if uuid_match:
|
||||||
|
@ -102,7 +104,7 @@ def find_active_issue_thread(in_reply_to, address, subject, event):
|
||||||
if reply_to.exists():
|
if reply_to.exists():
|
||||||
return reply_to.first().issue_thread, False
|
return reply_to.first().issue_thread, False
|
||||||
else:
|
else:
|
||||||
issue = IssueThread.objects.create(name=subject, event=event)
|
issue = IssueThread.objects.create(name=subject)
|
||||||
return issue, True
|
return issue, True
|
||||||
|
|
||||||
|
|
||||||
|
@ -202,14 +204,11 @@ def receive_email(envelope, log=None):
|
||||||
subject = unescape_and_decode_base64(subject)
|
subject = unescape_and_decode_base64(subject)
|
||||||
target_event = find_target_event(recipient)
|
target_event = find_target_event(recipient)
|
||||||
|
|
||||||
active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject, target_event)
|
active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject)
|
||||||
|
|
||||||
from hashlib import sha256
|
|
||||||
random_filename = 'mail-' + sha256(envelope.content).hexdigest()
|
|
||||||
|
|
||||||
email = Email.objects.create(
|
email = Email.objects.create(
|
||||||
sender=sender, recipient=recipient, body=body, subject=subject, reference=header_message_id,
|
sender=sender, recipient=recipient, body=body, subject=subject, reference=header_message_id,
|
||||||
in_reply_to=header_in_reply_to, raw_file=ContentFile(envelope.content, name=random_filename), event=target_event,
|
in_reply_to=header_in_reply_to, raw=envelope.content, event=target_event,
|
||||||
issue_thread=active_issue_thread)
|
issue_thread=active_issue_thread)
|
||||||
for attachment in attachments:
|
for attachment in attachments:
|
||||||
email.attachments.add(attachment)
|
email.attachments.add(attachment)
|
||||||
|
|
|
@ -142,7 +142,6 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test
|
||||||
aiosmtplib.send.assert_called_once()
|
aiosmtplib.send.assert_called_once()
|
||||||
self.assertEqual('test ä', Email.objects.all()[0].subject)
|
self.assertEqual('test ä', Email.objects.all()[0].subject)
|
||||||
self.assertEqual('Text mit Quoted-Printable-Kodierung: äöüß', Email.objects.all()[0].body)
|
self.assertEqual('Text mit Quoted-Printable-Kodierung: äöüß', Email.objects.all()[0].body)
|
||||||
self.assertTrue( Email.objects.all()[0].raw_file.path)
|
|
||||||
|
|
||||||
def test_handle_quoted_printable_2(self):
|
def test_handle_quoted_printable_2(self):
|
||||||
from aiosmtpd.smtp import Envelope
|
from aiosmtpd.smtp import Envelope
|
||||||
|
@ -163,7 +162,6 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test
|
||||||
aiosmtplib.send.assert_called_once()
|
aiosmtplib.send.assert_called_once()
|
||||||
self.assertEqual('suche_Mütze', Email.objects.all()[0].subject)
|
self.assertEqual('suche_Mütze', Email.objects.all()[0].subject)
|
||||||
self.assertEqual('Text mit Quoted-Printable-Kodierung: äöüß', Email.objects.all()[0].body)
|
self.assertEqual('Text mit Quoted-Printable-Kodierung: äöüß', Email.objects.all()[0].body)
|
||||||
self.assertTrue( Email.objects.all()[0].raw_file.path)
|
|
||||||
|
|
||||||
def test_handle_base64(self):
|
def test_handle_base64(self):
|
||||||
from aiosmtpd.smtp import Envelope
|
from aiosmtpd.smtp import Envelope
|
||||||
|
@ -184,7 +182,6 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test
|
||||||
aiosmtplib.send.assert_called_once()
|
aiosmtplib.send.assert_called_once()
|
||||||
self.assertEqual('test', Email.objects.all()[0].subject)
|
self.assertEqual('test', Email.objects.all()[0].subject)
|
||||||
self.assertEqual('Text mit Base64-Kodierung: äöüß', Email.objects.all()[0].body)
|
self.assertEqual('Text mit Base64-Kodierung: äöüß', Email.objects.all()[0].body)
|
||||||
self.assertTrue( Email.objects.all()[0].raw_file.path)
|
|
||||||
|
|
||||||
def test_handle_client_reply(self):
|
def test_handle_client_reply(self):
|
||||||
issue_thread = IssueThread.objects.create(
|
issue_thread = IssueThread.objects.create(
|
||||||
|
@ -232,7 +229,6 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test
|
||||||
self.assertEqual(IssueThread.objects.all()[0].name, 'test')
|
self.assertEqual(IssueThread.objects.all()[0].name, 'test')
|
||||||
self.assertEqual(IssueThread.objects.all()[0].state, 'pending_new')
|
self.assertEqual(IssueThread.objects.all()[0].state, 'pending_new')
|
||||||
self.assertEqual(IssueThread.objects.all()[0].assigned_to, None)
|
self.assertEqual(IssueThread.objects.all()[0].assigned_to, None)
|
||||||
self.assertTrue( Email.objects.all()[2].raw_file.path)
|
|
||||||
|
|
||||||
def test_handle_client_reply_2(self):
|
def test_handle_client_reply_2(self):
|
||||||
issue_thread = IssueThread.objects.create(
|
issue_thread = IssueThread.objects.create(
|
||||||
|
@ -285,7 +281,6 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test
|
||||||
self.assertEqual(IssueThread.objects.all()[0].name, 'test')
|
self.assertEqual(IssueThread.objects.all()[0].name, 'test')
|
||||||
self.assertEqual(IssueThread.objects.all()[0].state, 'pending_open')
|
self.assertEqual(IssueThread.objects.all()[0].state, 'pending_open')
|
||||||
self.assertEqual(IssueThread.objects.all()[0].assigned_to, None)
|
self.assertEqual(IssueThread.objects.all()[0].assigned_to, None)
|
||||||
self.assertTrue( Email.objects.all()[2].raw_file.path)
|
|
||||||
|
|
||||||
def test_mail_reply(self):
|
def test_mail_reply(self):
|
||||||
issue_thread = IssueThread.objects.create(
|
issue_thread = IssueThread.objects.create(
|
||||||
|
@ -389,7 +384,6 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test
|
||||||
states = StateChange.objects.filter(issue_thread=IssueThread.objects.all()[0])
|
states = StateChange.objects.filter(issue_thread=IssueThread.objects.all()[0])
|
||||||
self.assertEqual(1, len(states))
|
self.assertEqual(1, len(states))
|
||||||
self.assertEqual('pending_new', states[0].state)
|
self.assertEqual('pending_new', states[0].state)
|
||||||
self.assertEqual(event, IssueThread.objects.all()[0].event)
|
|
||||||
|
|
||||||
def test_mail_html_body(self):
|
def test_mail_html_body(self):
|
||||||
from aiosmtpd.smtp import Envelope
|
from aiosmtpd.smtp import Envelope
|
||||||
|
|
|
@ -73,5 +73,3 @@ watchfiles==0.21.0
|
||||||
websockets==12.0
|
websockets==12.0
|
||||||
yarl==1.9.4
|
yarl==1.9.4
|
||||||
zope.interface==6.1
|
zope.interface==6.1
|
||||||
django-prometheus==2.3.1
|
|
||||||
prometheus_client==0.21.0
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from base64 import b64decode
|
import logging
|
||||||
|
|
||||||
from django.urls import re_path
|
from django.urls import re_path
|
||||||
from django.contrib.auth.decorators import permission_required
|
from django.contrib.auth.decorators import permission_required
|
||||||
|
@ -14,24 +14,13 @@ from inventory.models import Event
|
||||||
from mail.models import Email
|
from mail.models import Email
|
||||||
from mail.protocol import send_smtp, make_reply, collect_references
|
from mail.protocol import send_smtp, make_reply, collect_references
|
||||||
from notify_sessions.models import SystemEvent
|
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, SearchResultSerializer
|
from tickets.serializers import IssueSerializer, CommentSerializer, ShippingVoucherSerializer
|
||||||
from tickets.shared_serializers import RelationSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class IssueViewSet(viewsets.ModelViewSet):
|
class IssueViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = IssueSerializer
|
serializer_class = IssueSerializer
|
||||||
queryset = IssueThread.objects.all().prefetch_related('state_changes', 'comments', 'emails', 'emails__attachments', 'assignments', 'item_relation_changes', 'shipping_vouchers')
|
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):
|
class ShippingVoucherViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -67,7 +56,7 @@ def reply(request, pk):
|
||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
@permission_required('tickets.add_issuethread_manual', raise_exception=True)
|
@permission_required('tickets.add_issuethread_manual', raise_exception=True)
|
||||||
def manual_ticket(request, event_slug):
|
def manual_ticket(request):
|
||||||
if 'name' not in request.data:
|
if 'name' not in request.data:
|
||||||
return Response({'status': 'error', 'message': 'missing name'}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({'status': 'error', 'message': 'missing name'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
if 'sender' not in request.data:
|
if 'sender' not in request.data:
|
||||||
|
@ -77,16 +66,8 @@ def manual_ticket(request, event_slug):
|
||||||
if 'body' not in request.data:
|
if 'body' not in request.data:
|
||||||
return Response({'status': 'error', 'message': 'missing body'}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({'status': 'error', 'message': 'missing body'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
event = None
|
|
||||||
if event_slug != 'none':
|
|
||||||
try:
|
|
||||||
event = Event.objects.get(slug=event_slug)
|
|
||||||
except:
|
|
||||||
return Response({'status': 'error', 'message': 'invalid event'}, status=status.HTTP_400_BAD_REQUEST)
|
|
||||||
|
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name=request.data['name'],
|
name=request.data['name'],
|
||||||
event=event,
|
|
||||||
manually_created=True,
|
manually_created=True,
|
||||||
)
|
)
|
||||||
email = Email.objects.create(
|
email = Email.objects.create(
|
||||||
|
@ -136,40 +117,13 @@ def add_comment(request, pk):
|
||||||
return Response(CommentSerializer(comment).data, status=status.HTTP_201_CREATED)
|
return Response(CommentSerializer(comment).data, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
||||||
def filter_issues(issues, query):
|
|
||||||
query_tokens = query.split(' ')
|
|
||||||
for issue in issues:
|
|
||||||
value = 0
|
|
||||||
for token in query_tokens:
|
|
||||||
if token in issue.description:
|
|
||||||
value += 1
|
|
||||||
if value > 0:
|
|
||||||
yield {'search_score': value, 'issue': issue}
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
|
||||||
@permission_classes([IsAuthenticated])
|
|
||||||
def search_issues(request, event_slug, query):
|
|
||||||
try:
|
|
||||||
event = Event.objects.get(slug=event_slug)
|
|
||||||
if not request.user.has_event_perm(event, 'view_issuethread'):
|
|
||||||
return Response(status=403)
|
|
||||||
items = filter_issues(IssueThread.objects.filter(event=event), b64decode(query).decode('utf-8'))
|
|
||||||
return Response(SearchResultSerializer(items, many=True).data)
|
|
||||||
except Event.DoesNotExist:
|
|
||||||
return Response(status=404)
|
|
||||||
|
|
||||||
|
|
||||||
router = routers.SimpleRouter()
|
router = routers.SimpleRouter()
|
||||||
router.register(r'tickets', IssueViewSet, basename='issues')
|
router.register(r'tickets', IssueViewSet, basename='issues')
|
||||||
router.register(r'matches', RelationViewSet, basename='matches')
|
|
||||||
router.register(r'shipping_vouchers', ShippingVoucherViewSet, basename='shipping_vouchers')
|
router.register(r'shipping_vouchers', ShippingVoucherViewSet, basename='shipping_vouchers')
|
||||||
|
|
||||||
urlpatterns = ([
|
urlpatterns = ([
|
||||||
re_path(r'tickets/states/$', get_available_states, name='get_available_states'),
|
|
||||||
re_path(r'^tickets/(?P<pk>\d+)/reply/$', reply, name='reply'),
|
re_path(r'^tickets/(?P<pk>\d+)/reply/$', reply, name='reply'),
|
||||||
re_path(r'^tickets/(?P<pk>\d+)/comment/$', add_comment, name='add_comment'),
|
re_path(r'^tickets/(?P<pk>\d+)/comment/$', add_comment, name='add_comment'),
|
||||||
re_path(r'^(?P<event_slug>[\w-]+)/tickets/manual/$', manual_ticket, name='manual_ticket'),
|
re_path(r'^tickets/manual/$', manual_ticket, name='manual_ticket'),
|
||||||
re_path(r'^(?P<event_slug>[\w-]+)/tickets/(?P<query>[-A-Za-z0-9+/]*={0,3})/$', search_issues,
|
re_path(r'^tickets/states/$', get_available_states, name='get_available_states'),
|
||||||
name='search_issues'),
|
|
||||||
] + router.urls)
|
] + router.urls)
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
# Generated by Django 4.2.7 on 2024-06-23 02:17
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
import django.db.models.deletion
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
dependencies = [
|
|
||||||
('mail', '0006_email_raw_file'),
|
|
||||||
('tickets', '0010_issuethread_event_itemrelation_and_more'),
|
|
||||||
]
|
|
||||||
|
|
||||||
def train_old_mails(apps, schema_editor):
|
|
||||||
from tickets.models import IssueThread
|
|
||||||
for t in IssueThread.objects.all():
|
|
||||||
try:
|
|
||||||
state = t.state
|
|
||||||
i = 0
|
|
||||||
for e in t.emails.all():
|
|
||||||
if e.raw_file:
|
|
||||||
if state == 'closed_spam' and i == 0:
|
|
||||||
e.train_spam()
|
|
||||||
else:
|
|
||||||
e.train_ham()
|
|
||||||
i += 1
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RunPython(train_old_mails),
|
|
||||||
]
|
|
|
@ -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.db import models
|
||||||
|
from django.utils import timezone
|
||||||
from django_softdelete.models import SoftDeleteModel
|
from django_softdelete.models import SoftDeleteModel
|
||||||
|
|
||||||
from authentication.models import ExtendedUser
|
from authentication.models import ExtendedUser
|
||||||
|
@ -45,6 +43,7 @@ class IssueThread(SoftDeleteModel):
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
event = models.ForeignKey(Event, null=True, on_delete=models.SET_NULL, related_name='issue_threads')
|
event = models.ForeignKey(Event, null=True, on_delete=models.SET_NULL, related_name='issue_threads')
|
||||||
manually_created = models.BooleanField(default=False)
|
manually_created = models.BooleanField(default=False)
|
||||||
|
related_items = models.ManyToManyField(Item, through='ItemRelation')
|
||||||
|
|
||||||
def short_uuid(self):
|
def short_uuid(self):
|
||||||
return self.uuid[:8]
|
return self.uuid[:8]
|
||||||
|
@ -52,11 +51,7 @@ class IssueThread(SoftDeleteModel):
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
try:
|
try:
|
||||||
state_changes = sorted(self.state_changes.all(), key=lambda x: x.timestamp, reverse=True)
|
return self.state_changes.order_by('-timestamp').first().state
|
||||||
if state_changes:
|
|
||||||
return state_changes[0].state
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return 'none'
|
return 'none'
|
||||||
|
|
||||||
|
@ -65,17 +60,11 @@ class IssueThread(SoftDeleteModel):
|
||||||
if self.state == value:
|
if self.state == value:
|
||||||
return
|
return
|
||||||
self.state_changes.create(state=value)
|
self.state_changes.create(state=value)
|
||||||
if value == 'closed_spam' and self.emails.exists():
|
|
||||||
self.emails.first().train_spam()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def assigned_to(self):
|
def assigned_to(self):
|
||||||
try:
|
try:
|
||||||
assignments = sorted(self.assignments.all(), key=lambda x: x.timestamp, reverse=True)
|
return self.assignments.order_by('-timestamp').first().assigned_to
|
||||||
if assignments:
|
|
||||||
return assignments[0].assigned_to
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -85,11 +74,6 @@ class IssueThread(SoftDeleteModel):
|
||||||
return
|
return
|
||||||
self.assignments.create(assigned_to=value)
|
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):
|
def __str__(self):
|
||||||
return '[' + str(self.id) + '][' + self.short_uuid() + '] ' + self.name
|
return '[' + str(self.id) + '][' + self.short_uuid() + '] ' + self.name
|
||||||
|
|
||||||
|
@ -146,8 +130,8 @@ class Assignment(models.Model):
|
||||||
|
|
||||||
class ItemRelation(models.Model):
|
class ItemRelation(models.Model):
|
||||||
id = models.AutoField(primary_key=True)
|
id = models.AutoField(primary_key=True)
|
||||||
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='item_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='issue_relation_changes')
|
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='issues')
|
||||||
timestamp = models.DateTimeField(auto_now_add=True)
|
timestamp = models.DateTimeField(auto_now_add=True)
|
||||||
status = models.CharField(max_length=255, choices=RELATION_STATUS_CHOICES, default='possible')
|
status = models.CharField(max_length=255, choices=RELATION_STATUS_CHOICES, default='possible')
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from authentication.models import ExtendedUser
|
from authentication.models import ExtendedUser
|
||||||
from inventory.models import Event
|
|
||||||
from inventory.shared_serializers import BasicItemSerializer
|
|
||||||
from mail.api_v2 import AttachmentSerializer
|
from mail.api_v2 import AttachmentSerializer
|
||||||
from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher
|
from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher
|
||||||
from tickets.shared_serializers import BasicIssueSerializer
|
from inventory.serializers import ItemSerializer
|
||||||
|
|
||||||
|
|
||||||
class CommentSerializer(serializers.ModelSerializer):
|
class CommentSerializer(serializers.ModelSerializer):
|
||||||
|
@ -38,20 +36,24 @@ class ShippingVoucherSerializer(serializers.ModelSerializer):
|
||||||
read_only_fields = ('id', 'timestamp', 'used_at')
|
read_only_fields = ('id', 'timestamp', 'used_at')
|
||||||
|
|
||||||
|
|
||||||
class IssueSerializer(BasicIssueSerializer):
|
class IssueSerializer(serializers.ModelSerializer):
|
||||||
timeline = serializers.SerializerMethodField()
|
timeline = serializers.SerializerMethodField()
|
||||||
last_activity = 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)
|
||||||
|
related_items = ItemSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = IssueThread
|
model = IssueThread
|
||||||
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items', 'event')
|
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items')
|
||||||
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
|
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
ret = super().to_internal_value(data)
|
ret = super().to_internal_value(data)
|
||||||
if 'state' in data:
|
if 'state' in data:
|
||||||
ret['state'] = data['state']
|
ret['state'] = data['state']
|
||||||
|
# if 'assigned_to' in data:
|
||||||
|
# ret['assigned_to'] = data['assigned_to']
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
|
@ -63,12 +65,14 @@ class IssueSerializer(BasicIssueSerializer):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_last_activity(self):
|
def get_last_activity(self):
|
||||||
try:
|
try:
|
||||||
last_state_change = max([t.timestamp for t in self.state_changes.all()]) if self.state_changes.exists() else None
|
last_state_change = self.state_changes.order_by('-timestamp').first().timestamp \
|
||||||
last_comment = max([t.timestamp for t in self.comments.all()]) if self.comments.exists() else None
|
if self.state_changes.count() > 0 else None
|
||||||
last_mail = max([t.timestamp for t in self.emails.all()]) if self.emails.exists() else None
|
last_comment = self.comments.order_by('-timestamp').first().timestamp if self.comments.count() > 0 else None
|
||||||
last_assignment = max([t.timestamp for t in self.assignments.all()]) if self.assignments.exists() else None
|
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 \
|
||||||
last_relation = max([t.timestamp for t in self.item_relation_changes.all()]) if self.item_relation_changes.exists() else None
|
self.assignments.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
|
args = [x for x in [last_state_change, last_comment, last_mail, last_assignment, last_relation] if
|
||||||
x is not None]
|
x is not None]
|
||||||
return max(args)
|
return max(args)
|
||||||
|
@ -110,13 +114,13 @@ class IssueSerializer(BasicIssueSerializer):
|
||||||
'timestamp': assignment.timestamp,
|
'timestamp': assignment.timestamp,
|
||||||
'assigned_to': assignment.assigned_to.username,
|
'assigned_to': assignment.assigned_to.username,
|
||||||
})
|
})
|
||||||
for relation in (obj.item_relation_changes.all()):
|
for relation in obj.item_relations.all():
|
||||||
timeline.append({
|
timeline.append({
|
||||||
'type': 'item_relation',
|
'type': 'item_relation',
|
||||||
'id': relation.id,
|
'id': relation.id,
|
||||||
'status': relation.status,
|
'status': relation.status,
|
||||||
'timestamp': relation.timestamp,
|
'timestamp': relation.timestamp,
|
||||||
'item': BasicItemSerializer(relation.item).data,
|
'item': ItemSerializer(relation.item).data,
|
||||||
})
|
})
|
||||||
for shipping_voucher in obj.shipping_vouchers.all():
|
for shipping_voucher in obj.shipping_vouchers.all():
|
||||||
timeline.append({
|
timeline.append({
|
||||||
|
@ -128,14 +132,5 @@ class IssueSerializer(BasicIssueSerializer):
|
||||||
})
|
})
|
||||||
return sorted(timeline, key=lambda x: x['timestamp'])
|
return sorted(timeline, key=lambda x: x['timestamp'])
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
return IssueThread.objects.all().order_by('-last_activity')
|
||||||
class SearchResultSerializer(serializers.Serializer):
|
|
||||||
search_score = serializers.IntegerField()
|
|
||||||
item = IssueSerializer()
|
|
||||||
|
|
||||||
def to_representation(self, instance):
|
|
||||||
return {**IssueSerializer(instance['item']).data, 'search_score': instance['search_score']}
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = IssueThread
|
|
||||||
|
|
|
@ -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,14 +3,11 @@ from datetime import datetime, timedelta
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase, Client
|
||||||
|
|
||||||
from authentication.models import ExtendedUser
|
from authentication.models import ExtendedUser
|
||||||
from inventory.models import Event, Container, Item
|
|
||||||
from mail.models import Email, EmailAttachment
|
from mail.models import Email, EmailAttachment
|
||||||
from tickets.models import IssueThread, StateChange, Comment, ItemRelation, Assignment
|
from tickets.models import IssueThread, StateChange, Comment
|
||||||
from django.contrib.auth.models import Permission
|
from django.contrib.auth.models import Permission
|
||||||
from knox.models import AuthToken
|
from knox.models import AuthToken
|
||||||
|
|
||||||
from base64 import b64encode
|
|
||||||
|
|
||||||
|
|
||||||
class IssueApiTest(TestCase):
|
class IssueApiTest(TestCase):
|
||||||
|
|
||||||
|
@ -19,9 +16,6 @@ class IssueApiTest(TestCase):
|
||||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||||
self.user.user_permissions.add(*Permission.objects.all())
|
self.user.user_permissions.add(*Permission.objects.all())
|
||||||
self.user.save()
|
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.token = AuthToken.objects.create(user=self.user)
|
||||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
||||||
|
|
||||||
|
@ -34,7 +28,6 @@ class IssueApiTest(TestCase):
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
mail1 = Email.objects.create(
|
mail1 = Email.objects.create(
|
||||||
subject='test',
|
subject='test',
|
||||||
|
@ -53,24 +46,14 @@ class IssueApiTest(TestCase):
|
||||||
in_reply_to=mail1.reference,
|
in_reply_to=mail1.reference,
|
||||||
timestamp=now + timedelta(seconds=2),
|
timestamp=now + timedelta(seconds=2),
|
||||||
)
|
)
|
||||||
assignment = Assignment.objects.create(
|
|
||||||
issue_thread=issue,
|
|
||||||
assigned_to=self.user,
|
|
||||||
timestamp=now + timedelta(seconds=3),
|
|
||||||
)
|
|
||||||
comment = Comment.objects.create(
|
comment = Comment.objects.create(
|
||||||
issue_thread=issue,
|
issue_thread=issue,
|
||||||
comment="test",
|
comment="test",
|
||||||
timestamp=now + timedelta(seconds=4),
|
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('pending_new', issue.state)
|
||||||
self.assertEqual('test issue', issue.name)
|
self.assertEqual('test issue', issue.name)
|
||||||
self.assertEqual(self.user, issue.assigned_to)
|
self.assertEqual(None, issue.assigned_to)
|
||||||
self.assertEqual(36, len(issue.uuid))
|
self.assertEqual(36, len(issue.uuid))
|
||||||
response = self.client.get('/api/2/tickets/')
|
response = self.client.get('/api/2/tickets/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
@ -78,17 +61,14 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual(response.json()[0]['id'], issue.id)
|
self.assertEqual(response.json()[0]['id'], issue.id)
|
||||||
self.assertEqual(response.json()[0]['name'], "test issue")
|
self.assertEqual(response.json()[0]['name'], "test issue")
|
||||||
self.assertEqual(response.json()[0]['state'], "pending_new")
|
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]['assigned_to'], self.user.username)
|
|
||||||
self.assertEqual(response.json()[0]['uuid'], issue.uuid)
|
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(response.json()[0]['last_activity'], comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||||
self.assertEqual(len(response.json()[0]['timeline']), 6)
|
self.assertEqual(len(response.json()[0]['timeline']), 4)
|
||||||
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state')
|
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state')
|
||||||
self.assertEqual(response.json()[0]['timeline'][1]['type'], 'mail')
|
self.assertEqual(response.json()[0]['timeline'][1]['type'], 'mail')
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['type'], 'mail')
|
self.assertEqual(response.json()[0]['timeline'][2]['type'], 'mail')
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['type'], 'assignment')
|
self.assertEqual(response.json()[0]['timeline'][3]['type'], 'comment')
|
||||||
self.assertEqual(response.json()[0]['timeline'][4]['type'], 'comment')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][5]['type'], 'item_relation')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][1]['id'], mail1.id)
|
self.assertEqual(response.json()[0]['timeline'][1]['id'], mail1.id)
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['id'], mail2.id)
|
self.assertEqual(response.json()[0]['timeline'][2]['id'], mail2.id)
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['id'], comment.id)
|
self.assertEqual(response.json()[0]['timeline'][3]['id'], comment.id)
|
||||||
|
@ -105,35 +85,20 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['body'], 'test')
|
self.assertEqual(response.json()[0]['timeline'][2]['body'], 'test')
|
||||||
self.assertEqual(response.json()[0]['timeline'][2]['timestamp'],
|
self.assertEqual(response.json()[0]['timeline'][2]['timestamp'],
|
||||||
mail2.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
mail2.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['assigned_to'], self.user.username)
|
self.assertEqual(response.json()[0]['timeline'][3]['comment'], 'test')
|
||||||
self.assertEqual(response.json()[0]['timeline'][3]['timestamp'],
|
self.assertEqual(response.json()[0]['timeline'][3]['timestamp'],
|
||||||
assignment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][4]['comment'], 'test')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][4]['timestamp'],
|
|
||||||
comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||||
self.assertEqual(response.json()[0]['timeline'][5]['status'], 'possible')
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][5]['timestamp'],
|
|
||||||
match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][5]['item']['description'], "foo")
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][5]['item']['event'], "evt")
|
|
||||||
self.assertEqual(response.json()[0]['timeline'][5]['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):
|
def test_issues_incomplete_timeline(self):
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
issue1 = IssueThread.objects.create(
|
issue1 = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
issue2 = IssueThread.objects.create(
|
issue2 = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
issue3 = IssueThread.objects.create(
|
issue3 = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
mail1 = Email.objects.create(
|
mail1 = Email.objects.create(
|
||||||
subject='test',
|
subject='test',
|
||||||
|
@ -153,11 +118,8 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual(200, response.status_code)
|
self.assertEqual(200, response.status_code)
|
||||||
self.assertEqual(3, len(response.json()))
|
self.assertEqual(3, len(response.json()))
|
||||||
self.assertEqual(issue1.id, response.json()[0]['id'])
|
self.assertEqual(issue1.id, response.json()[0]['id'])
|
||||||
self.assertEqual("evt", response.json()[0]['event'])
|
|
||||||
self.assertEqual(issue2.id, response.json()[1]['id'])
|
self.assertEqual(issue2.id, response.json()[1]['id'])
|
||||||
self.assertEqual("evt", response.json()[1]['event'])
|
|
||||||
self.assertEqual(issue3.id, response.json()[2]['id'])
|
self.assertEqual(issue3.id, response.json()[2]['id'])
|
||||||
self.assertEqual("evt", response.json()[2]['event'])
|
|
||||||
self.assertEqual(issue1.state_changes.first().timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
|
self.assertEqual(issue1.state_changes.first().timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
|
||||||
response.json()[0]['last_activity'])
|
response.json()[0]['last_activity'])
|
||||||
self.assertEqual(mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
|
self.assertEqual(mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
|
||||||
|
@ -191,7 +153,6 @@ class IssueApiTest(TestCase):
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
mail1 = Email.objects.create(
|
mail1 = Email.objects.create(
|
||||||
subject='test',
|
subject='test',
|
||||||
|
@ -228,7 +189,6 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual(200, response.status_code)
|
self.assertEqual(200, response.status_code)
|
||||||
self.assertEqual(1, len(response.json()))
|
self.assertEqual(1, len(response.json()))
|
||||||
self.assertEqual(issue.id, response.json()[0]['id'])
|
self.assertEqual(issue.id, response.json()[0]['id'])
|
||||||
self.assertEqual("evt", response.json()[0]['event'])
|
|
||||||
self.assertEqual('pending_new', response.json()[0]['state'])
|
self.assertEqual('pending_new', response.json()[0]['state'])
|
||||||
self.assertEqual('test issue', response.json()[0]['name'])
|
self.assertEqual('test issue', response.json()[0]['name'])
|
||||||
self.assertEqual(None, response.json()[0]['assigned_to'])
|
self.assertEqual(None, response.json()[0]['assigned_to'])
|
||||||
|
@ -270,14 +230,13 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual(file2.hash, response.json()[0]['timeline'][1]['attachments'][1]['hash'])
|
self.assertEqual(file2.hash, response.json()[0]['timeline'][1]['attachments'][1]['hash'])
|
||||||
|
|
||||||
def test_manual_creation(self):
|
def test_manual_creation(self):
|
||||||
response = self.client.post('/api/2/evt/tickets/manual/',
|
response = self.client.post('/api/2/tickets/manual/',
|
||||||
{'name': 'test issue', 'sender': 'test', 'recipient': 'test', 'body': 'test'},
|
{'name': 'test issue', 'sender': 'test', 'recipient': 'test', 'body': 'test'},
|
||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
self.assertEqual(response.status_code, 201)
|
self.assertEqual(response.status_code, 201)
|
||||||
self.assertEqual(response.json()['state'], 'pending_new')
|
self.assertEqual(response.json()['state'], 'pending_new')
|
||||||
self.assertEqual(response.json()['name'], 'test issue')
|
self.assertEqual(response.json()['name'], 'test issue')
|
||||||
self.assertEqual(response.json()['assigned_to'], None)
|
self.assertEqual(response.json()['assigned_to'], None)
|
||||||
self.assertEqual("evt", response.json()['event'])
|
|
||||||
timeline = response.json()['timeline']
|
timeline = response.json()['timeline']
|
||||||
self.assertEqual(len(timeline), 2)
|
self.assertEqual(len(timeline), 2)
|
||||||
self.assertEqual(timeline[0]['type'], 'state')
|
self.assertEqual(timeline[0]['type'], 'state')
|
||||||
|
@ -288,35 +247,9 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual(timeline[1]['subject'], 'test issue')
|
self.assertEqual(timeline[1]['subject'], 'test issue')
|
||||||
self.assertEqual(timeline[1]['body'], 'test')
|
self.assertEqual(timeline[1]['body'], 'test')
|
||||||
|
|
||||||
def test_manual_creation_none(self):
|
|
||||||
response = self.client.post('/api/2/none/tickets/manual/',
|
|
||||||
{'name': 'test issue', 'sender': 'test', 'recipient': 'test', 'body': 'test'},
|
|
||||||
content_type='application/json')
|
|
||||||
self.assertEqual(response.status_code, 201)
|
|
||||||
self.assertEqual(response.json()['state'], 'pending_new')
|
|
||||||
self.assertEqual(response.json()['name'], 'test issue')
|
|
||||||
self.assertEqual(response.json()['assigned_to'], None)
|
|
||||||
self.assertEqual(None, response.json()['event'])
|
|
||||||
timeline = response.json()['timeline']
|
|
||||||
self.assertEqual(len(timeline), 2)
|
|
||||||
self.assertEqual(timeline[0]['type'], 'state')
|
|
||||||
self.assertEqual(timeline[0]['state'], 'pending_new')
|
|
||||||
self.assertEqual(timeline[1]['type'], 'mail')
|
|
||||||
self.assertEqual(timeline[1]['sender'], 'test')
|
|
||||||
self.assertEqual(timeline[1]['recipient'], 'test')
|
|
||||||
self.assertEqual(timeline[1]['subject'], 'test issue')
|
|
||||||
self.assertEqual(timeline[1]['body'], 'test')
|
|
||||||
|
|
||||||
def test_manual_creation_invalid(self):
|
|
||||||
response = self.client.post('/api/2/foobar/tickets/manual/',
|
|
||||||
{'name': 'test issue', 'sender': 'test', 'recipient': 'test', 'body': 'test'},
|
|
||||||
content_type='application/json')
|
|
||||||
self.assertEqual(response.status_code, 400)
|
|
||||||
|
|
||||||
def test_post_comment_altenative(self):
|
def test_post_comment_altenative(self):
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
response = self.client.post(f'/api/2/tickets/{issue.id}/comment/', {'comment': 'test'})
|
response = self.client.post(f'/api/2/tickets/{issue.id}/comment/', {'comment': 'test'})
|
||||||
self.assertEqual(response.status_code, 201)
|
self.assertEqual(response.status_code, 201)
|
||||||
|
@ -327,7 +260,6 @@ class IssueApiTest(TestCase):
|
||||||
def test_post_alt_comment_empty(self):
|
def test_post_alt_comment_empty(self):
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
response = self.client.post(f'/api/2/tickets/{issue.id}/comment/', {'comment': ''})
|
response = self.client.post(f'/api/2/tickets/{issue.id}/comment/', {'comment': ''})
|
||||||
self.assertEqual(response.status_code, 400)
|
self.assertEqual(response.status_code, 400)
|
||||||
|
@ -335,7 +267,6 @@ class IssueApiTest(TestCase):
|
||||||
def test_state_change(self):
|
def test_state_change(self):
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'state': 'pending_open'},
|
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'state': 'pending_open'},
|
||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
|
@ -353,7 +284,6 @@ class IssueApiTest(TestCase):
|
||||||
def test_state_change_invalid_state(self):
|
def test_state_change_invalid_state(self):
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'state': 'invalid'},
|
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'state': 'invalid'},
|
||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
|
@ -362,14 +292,12 @@ class IssueApiTest(TestCase):
|
||||||
def test_assign_user(self):
|
def test_assign_user(self):
|
||||||
issue = IssueThread.objects.create(
|
issue = IssueThread.objects.create(
|
||||||
name="test issue",
|
name="test issue",
|
||||||
event=self.event,
|
|
||||||
)
|
)
|
||||||
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'assigned_to': self.user.username},
|
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'assigned_to': self.user.username},
|
||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
self.assertEqual(200, response.status_code)
|
self.assertEqual(200, response.status_code)
|
||||||
self.assertEqual('pending_new', response.json()['state'])
|
self.assertEqual('pending_new', response.json()['state'])
|
||||||
self.assertEqual('test issue', response.json()['name'])
|
self.assertEqual('test issue', response.json()['name'])
|
||||||
self.assertEqual("evt", response.json()['event'])
|
|
||||||
self.assertEqual(self.user.username, response.json()['assigned_to'])
|
self.assertEqual(self.user.username, response.json()['assigned_to'])
|
||||||
timeline = response.json()['timeline']
|
timeline = response.json()['timeline']
|
||||||
self.assertEqual(2, len(timeline))
|
self.assertEqual(2, len(timeline))
|
||||||
|
@ -377,21 +305,3 @@ class IssueApiTest(TestCase):
|
||||||
self.assertEqual('pending_new', timeline[0]['state'])
|
self.assertEqual('pending_new', timeline[0]['state'])
|
||||||
self.assertEqual('assignment', timeline[1]['type'])
|
self.assertEqual('assignment', timeline[1]['type'])
|
||||||
self.assertEqual(self.user.username, timeline[1]['assigned_to'])
|
self.assertEqual(self.user.username, timeline[1]['assigned_to'])
|
||||||
|
|
||||||
|
|
||||||
class IssueSearchTest(TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super().setUp()
|
|
||||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
|
||||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
|
||||||
self.user.user_permissions.add(*Permission.objects.all())
|
|
||||||
self.user.save()
|
|
||||||
self.token = AuthToken.objects.create(user=self.user)
|
|
||||||
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
|
||||||
|
|
||||||
def test_search(self):
|
|
||||||
search_query = b64encode(b'abc').decode('utf-8')
|
|
||||||
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
|
|
||||||
self.assertEqual(200, response.status_code)
|
|
||||||
self.assertEqual([], response.json())
|
|
||||||
|
|
|
@ -12,5 +12,3 @@ c3lf-nodes:
|
||||||
main_email: <main_email>
|
main_email: <main_email>
|
||||||
legacy_api_user: <legacy_api_user>
|
legacy_api_user: <legacy_api_user>
|
||||||
legacy_api_password: <legacy_api_password>
|
legacy_api_password: <legacy_api_password>
|
||||||
debug_mode_active: false
|
|
||||||
django_secret_key: 'django-insecure-tm*$w_14iqbiy-!7(8#ba7j+_@(7@rf2&a^!=shs&$03b%2*rv'
|
|
|
@ -10,6 +10,3 @@ LEGACY_API_USER={{ legacy_api_user }}
|
||||||
LEGACY_API_PASSWORD={{ legacy_api_password }}
|
LEGACY_API_PASSWORD={{ legacy_api_password }}
|
||||||
MEDIA_ROOT=/var/www/c3lf-sys3/userfiles
|
MEDIA_ROOT=/var/www/c3lf-sys3/userfiles
|
||||||
STATIC_ROOT=/var/www/c3lf-sys3/staticfiles
|
STATIC_ROOT=/var/www/c3lf-sys3/staticfiles
|
||||||
ACTIVE_SPAM_TRAINING=True
|
|
||||||
DEBUG_MODE_ACTIVE={{ debug_mode_active }}
|
|
||||||
DJANGO_SECRET_KEY={{ django_secret_key }}
|
|
||||||
|
|
|
@ -70,13 +70,6 @@ server {
|
||||||
alias /var/www/c3lf-sys3/staticfiles/;
|
alias /var/www/c3lf-sys3/staticfiles/;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /metrics {
|
|
||||||
allow 95.156.226.90;
|
|
||||||
allow 127.0.0.1;
|
|
||||||
allow ::1;
|
|
||||||
deny all;
|
|
||||||
}
|
|
||||||
|
|
||||||
listen 443 ssl http2; # managed by Certbot
|
listen 443 ssl http2; # managed by Certbot
|
||||||
ssl_certificate /etc/letsencrypt/live/{{ web_domain }}/fullchain.pem; # managed by Certbot
|
ssl_certificate /etc/letsencrypt/live/{{ web_domain }}/fullchain.pem; # managed by Certbot
|
||||||
ssl_certificate_key /etc/letsencrypt/live/{{ web_domain }}/privkey.pem; # managed by Certbot
|
ssl_certificate_key /etc/letsencrypt/live/{{ web_domain }}/privkey.pem; # managed by Certbot
|
||||||
|
|
|
@ -16,6 +16,12 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- "3306:3306"
|
- "3306:3306"
|
||||||
|
|
||||||
|
mailpit:
|
||||||
|
image: axllent/mailpit
|
||||||
|
ports:
|
||||||
|
- "8025:8025"
|
||||||
|
- "1025:1025"
|
||||||
|
|
||||||
core:
|
core:
|
||||||
build:
|
build:
|
||||||
context: ../../core
|
context: ../../core
|
||||||
|
|
|
@ -1,124 +0,0 @@
|
||||||
<template>
|
|
||||||
<table class="table table-striped table-dark">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>
|
|
||||||
</th>
|
|
||||||
<th scope="col" v-for="(column, index) in columns" :key="index">
|
|
||||||
<div class="input-group">
|
|
||||||
<div class="input-group-prepend">
|
|
||||||
<button
|
|
||||||
:class="[ 'btn', column === sortBy ? 'btn-outline-info' : 'btn-outline-secondary' ]"
|
|
||||||
@click="toggleSort(column)"
|
|
||||||
>
|
|
||||||
{{ column }}
|
|
||||||
<span :class="{ 'text-info': column === sortBy }">
|
|
||||||
<font-awesome-icon :icon="getSortIcon(column)"/>
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
class="form-control"
|
|
||||||
placeholder="filter"
|
|
||||||
:value="filters[column]"
|
|
||||||
@input="changeFilter(column, $event.target.value)"
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
<slot name="header_actions"/>
|
|
||||||
</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody v-for="(item, index) in internalItems" :key="item[keyName]" style="border-top: none">
|
|
||||||
<tr @click="toggle(index)">
|
|
||||||
<td>
|
|
||||||
<font-awesome-icon icon="angle-right" style="width: 1em;" v-if="collapsed[index]"/>
|
|
||||||
<font-awesome-icon icon="angle-down" style="width: 1em;" v-else/>
|
|
||||||
</td>
|
|
||||||
<td v-for="(column, index) in columns" :key="index">{{ item[column] }}</td>
|
|
||||||
<td>
|
|
||||||
<slot v-bind:id="index" v-bind:item="item" name="actions"/>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr v-if="!collapsed[index]">
|
|
||||||
<td :colspan="columns.length + 2">
|
|
||||||
<slot v-bind:id="index" v-bind:item="item" name="detail"/>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import DataContainer from '@/mixins/data-container';
|
|
||||||
import router from '../router';
|
|
||||||
import {mapGetters} from "vuex";
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'ExpandableTable',
|
|
||||||
mixins: [DataContainer],
|
|
||||||
created() {
|
|
||||||
this.columns.map(e => ({
|
|
||||||
k: e,
|
|
||||||
v: this.$store.getters.getFilters[e]
|
|
||||||
})).filter(e => e.v).forEach(e => this.setFilter(e.k, e.v));
|
|
||||||
|
|
||||||
const query = this.route ? (this.route.query ? this.route.query.collapsed : null) : null;
|
|
||||||
if (query !== null && query !== undefined) {
|
|
||||||
this.collapsed = this.unpackInt(parseInt(query), this.internalItems.length);
|
|
||||||
} else {
|
|
||||||
this.collapsed = this.internalItems.map(() => true);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
collapsed: [],
|
|
||||||
};
|
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
...mapGetters(['route']),
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
changeFilter(col, val) {
|
|
||||||
this.setFilter(col, val);
|
|
||||||
let newquery = Object.entries({
|
|
||||||
...this.$store.getters.getFilters,
|
|
||||||
[col]: val
|
|
||||||
}).reduce((a, [k, v]) => (v ? {...a, [k]: v} : a), {});
|
|
||||||
router.push({query: newquery});
|
|
||||||
},
|
|
||||||
packInt(arr) {
|
|
||||||
return arr.reduce((a, e, i) => a + (e ? 0 : 2 ** i), 0);
|
|
||||||
},
|
|
||||||
unpackInt(n, l) {
|
|
||||||
return [...Array(l)].map((e, i) => (n & 2 ** i) === 0);
|
|
||||||
},
|
|
||||||
toggle(index) {
|
|
||||||
const collapsed = [...this.collapsed]
|
|
||||||
collapsed[index] = !collapsed[index];
|
|
||||||
this.collapsed = collapsed;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
collapsed: {
|
|
||||||
handler() {
|
|
||||||
const encoded = this.packInt(this.collapsed).toString()
|
|
||||||
if (this.route.query.collapsed !== encoded)
|
|
||||||
this.$router.push({
|
|
||||||
...this.route,
|
|
||||||
query: {...this.route.query, collapsed: encoded}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
deep: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
.table-body-move {
|
|
||||||
transition: transform 1s;
|
|
||||||
}
|
|
||||||
</style>
|
|
|
@ -6,8 +6,7 @@
|
||||||
{{ getEventSlug }}
|
{{ getEventSlug }}
|
||||||
</button>
|
</button>
|
||||||
<div class="dropdown-menu bg-dark" aria-labelledby="dropdownMenuButton">
|
<div class="dropdown-menu bg-dark" aria-labelledby="dropdownMenuButton">
|
||||||
<a class="dropdown-item text-light" href="#" v-for="(event, index) in selectableEvents"
|
<a class="dropdown-item text-light" href="#" v-for="(event, index) in events" v-bind:key="index"
|
||||||
v-bind:key="index"
|
|
||||||
:class="{ active: event.slug === getEventSlug }" @click="changeEvent(event)">{{ event.slug }}</a>
|
:class="{ active: event.slug === getEventSlug }" @click="changeEvent(event)">{{ event.slug }}</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -49,12 +48,12 @@
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<button type="button" class="btn text-nowrap btn-success mr-1" @click="$emit('addItemClicked')"
|
<button type="button" class="btn text-nowrap btn-success mr-1" @click="$emit('addItemClicked')"
|
||||||
v-if="isItemView() && getEventSlug !== 'all'">
|
v-if="isItemView()">
|
||||||
<font-awesome-icon icon="plus"/>
|
<font-awesome-icon icon="plus"/>
|
||||||
<span class="d-none d-md-inline"> Add Item</span>
|
<span class="d-none d-md-inline"> Add Item</span>
|
||||||
</button>
|
</button>
|
||||||
<button type="button" class="btn text-nowrap btn-success mr-1" @click="$emit('addTicketClicked')"
|
<button type="button" class="btn text-nowrap btn-success mr-1" @click="$emit('addTicketClicked')"
|
||||||
v-if="isTicketView() && getEventSlug !== 'all'">
|
v-if="isTicketView()">
|
||||||
<font-awesome-icon icon="plus"/>
|
<font-awesome-icon icon="plus"/>
|
||||||
<span class="d-none d-md-inline"> Add Ticket</span>
|
<span class="d-none d-md-inline"> Add Ticket</span>
|
||||||
</button>
|
</button>
|
||||||
|
@ -65,6 +64,19 @@
|
||||||
</button>
|
</button>
|
||||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||||
<ul class="navbar-nav ml-auto">
|
<ul class="navbar-nav ml-auto">
|
||||||
|
<!--li class="nav-item dropdown">
|
||||||
|
<button class="btn nav-link dropdown-toggle" type="button" id="dropdownMenuButton2"
|
||||||
|
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||||
|
{{ getActiveView }}
|
||||||
|
</button>
|
||||||
|
<ul class="dropdown-menu bg-dark" aria-labelledby="dropdownMenuButton2">
|
||||||
|
<li class="" v-for="(link, index) in views" v-bind:key="index"
|
||||||
|
:class="{ active: link.path === getActiveView }">
|
||||||
|
<a class="nav-link text-nowrap" href="#" @click="changeView(link)">{{ link.title }}</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li-->
|
||||||
|
|
||||||
<li class="nav-item" v-for="(link, index) in links" v-bind:key="index">
|
<li class="nav-item" v-for="(link, index) in links" v-bind:key="index">
|
||||||
<a class="nav-link text-nowrap" :href="link.path" @click.prevent="navigateTo(link.path)">
|
<a class="nav-link text-nowrap" :href="link.path" @click.prevent="navigateTo(link.path)">
|
||||||
{{ link.title }}
|
{{ link.title }}
|
||||||
|
@ -103,9 +115,6 @@ export default {
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(['events']),
|
...mapState(['events']),
|
||||||
...mapGetters(['getEventSlug', 'getActiveView', "checkPermission", "hasPermissions", "layout", "route"]),
|
...mapGetters(['getEventSlug', 'getActiveView', "checkPermission", "hasPermissions", "layout", "route"]),
|
||||||
selectableEvents() {
|
|
||||||
return [{slug: 'all'}, ...this.events, {slug: 'none'}];
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions(['changeEvent', 'changeView']),
|
...mapActions(['changeEvent', 'changeView']),
|
||||||
|
|
279
web/src/store.js
279
web/src/store.js
|
@ -3,7 +3,7 @@ import router from './router';
|
||||||
|
|
||||||
import * as base64 from 'base-64';
|
import * as base64 from 'base-64';
|
||||||
import * as utf8 from 'utf8';
|
import * as utf8 from 'utf8';
|
||||||
import {ticketStateColorLookup, ticketStateIconLookup, http, http_session} from "@/utils";
|
import {ticketStateColorLookup, ticketStateIconLookup, http} from "@/utils";
|
||||||
import sharedStatePlugin from "@/shared-state-plugin";
|
import sharedStatePlugin from "@/shared-state-plugin";
|
||||||
import persistentStatePlugin from "@/persistent-state-plugin";
|
import persistentStatePlugin from "@/persistent-state-plugin";
|
||||||
|
|
||||||
|
@ -11,18 +11,17 @@ const store = createStore({
|
||||||
state: {
|
state: {
|
||||||
keyIncrement: 0,
|
keyIncrement: 0,
|
||||||
events: [],
|
events: [],
|
||||||
items: [],
|
loadedItems: [],
|
||||||
|
itemCache: {},
|
||||||
loadedBoxes: [],
|
loadedBoxes: [],
|
||||||
toasts: [],
|
toasts: [],
|
||||||
|
tickets: [],
|
||||||
users: [],
|
users: [],
|
||||||
groups: [],
|
groups: [],
|
||||||
state_options: [],
|
state_options: [],
|
||||||
shippingVouchers: [],
|
shippingVouchers: [],
|
||||||
|
|
||||||
loadedItems: {},
|
lastEvent: '37C3',
|
||||||
loadedTickets: {},
|
|
||||||
|
|
||||||
lastEvent: 'all',
|
|
||||||
lastUsed: {},
|
lastUsed: {},
|
||||||
searchQuery: '',
|
searchQuery: '',
|
||||||
remember: false,
|
remember: false,
|
||||||
|
@ -63,14 +62,7 @@ const store = createStore({
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
route: state => router.currentRoute.value,
|
route: state => router.currentRoute.value,
|
||||||
session: state => http_session(state.user.token),
|
|
||||||
getEventSlug: state => router.currentRoute.value.params.event ? router.currentRoute.value.params.event : state.lastEvent,
|
getEventSlug: state => router.currentRoute.value.params.event ? router.currentRoute.value.params.event : state.lastEvent,
|
||||||
getAllItems: state => Object.values(state.loadedItems).flat(),
|
|
||||||
getAllTickets: state => Object.values(state.loadedTickets).flat(),
|
|
||||||
getEventItems: (state, getters) => getters.getEventSlug === 'all' ? getters.getAllItems : getters.getAllItems.filter(t => t.event === getters.getEventSlug || (t.event == null && getters.getEventSlug === 'none')),
|
|
||||||
getEventTickets: (state, getters) => getters.getEventSlug === 'all' ? getters.getAllTickets : getters.getAllTickets.filter(t => t.event === getters.getEventSlug || (t.event == null && getters.getEventSlug === 'none')),
|
|
||||||
isItemsLoaded: (state, getters) => (getters.getEventSlug === 'all' || getters.getEventSlug === 'none') ? !!state.loadedItems : Object.keys(state.loadedItems).includes(getters.getEventSlug),
|
|
||||||
isTicketsLoaded: (state, getters) => (getters.getEventSlug === 'all' || getters.getEventSlug === 'none') ? !!state.loadedTickets : Object.keys(state.loadedTickets).includes(getters.getEventSlug),
|
|
||||||
getActiveView: state => router.currentRoute.value.name || 'items',
|
getActiveView: state => router.currentRoute.value.name || 'items',
|
||||||
getFilters: state => router.currentRoute.value.query,
|
getFilters: state => router.currentRoute.value.query,
|
||||||
getBoxes: state => state.loadedBoxes,
|
getBoxes: state => state.loadedBoxes,
|
||||||
|
@ -138,48 +130,35 @@ const store = createStore({
|
||||||
changeView(state, {view, slug}) {
|
changeView(state, {view, slug}) {
|
||||||
router.push({path: `/${slug}/${view}`});
|
router.push({path: `/${slug}/${view}`});
|
||||||
},
|
},
|
||||||
|
replaceLoadedItems(state, newItems) {
|
||||||
|
state.loadedItems = newItems;
|
||||||
|
state.fetchedData = {...state.fetchedData, items: Date.now()}; // TODO: manage caching items for different events and search results correctly
|
||||||
|
},
|
||||||
|
setItemCache(state, {slug, items}) {
|
||||||
|
state.itemCache[slug] = items;
|
||||||
|
},
|
||||||
replaceBoxes(state, loadedBoxes) {
|
replaceBoxes(state, loadedBoxes) {
|
||||||
state.loadedBoxes = loadedBoxes;
|
state.loadedBoxes = loadedBoxes;
|
||||||
state.fetchedData = {...state.fetchedData, boxes: Date.now()};
|
state.fetchedData = {...state.fetchedData, boxes: Date.now()};
|
||||||
},
|
},
|
||||||
setItems(state, {slug, items}) {
|
|
||||||
state.loadedItems[slug] = items;
|
|
||||||
state.loadedItems = {...state.loadedItems};
|
|
||||||
console.log(state.loadedItems)
|
|
||||||
},
|
|
||||||
replaceItems(state, items) {
|
|
||||||
const groups = Object.groupBy(items, i => i.event ? i.event : 'none')
|
|
||||||
for (const [key, value] of Object.entries(groups)) state.loadedItems[key] = value;
|
|
||||||
state.loadedItems = {...state.loadedItems};
|
|
||||||
console.log(state.loadedItems)
|
|
||||||
},
|
|
||||||
updateItem(state, updatedItem) {
|
updateItem(state, updatedItem) {
|
||||||
const item = state.loadedItems[updatedItem.event?updatedItem.event:'none'].filter(
|
const item = state.loadedItems.filter(({uid}) => uid === updatedItem.uid)[0];
|
||||||
({uid}) => uid === updatedItem.uid)[0];
|
|
||||||
Object.assign(item, updatedItem);
|
Object.assign(item, updatedItem);
|
||||||
},
|
},
|
||||||
removeItem(state, item) {
|
removeItem(state, item) {
|
||||||
state.loadedItems[item.event?item.event:'none'] = state.loadedItems[item.event].filter(it => it !== item);
|
state.loadedItems = state.loadedItems.filter(it => it !== item);
|
||||||
},
|
},
|
||||||
appendItem(state, item) {
|
appendItem(state, item) {
|
||||||
state.loadedItems[item.event?item.event:'none'].push(item);
|
state.loadedItems.push(item);
|
||||||
},
|
|
||||||
setTickets(state, {slug, tickets}) {
|
|
||||||
state.loadedTickets[slug] = tickets;
|
|
||||||
state.loadedTickets = {...state.loadedTickets};
|
|
||||||
console.log(state.loadedTickets)
|
|
||||||
},
|
},
|
||||||
replaceTickets(state, tickets) {
|
replaceTickets(state, tickets) {
|
||||||
const groups = Object.groupBy(tickets, t => t.event ? t.event : 'none')
|
state.tickets = tickets;
|
||||||
for (const [key, value] of Object.entries(groups)) state.loadedTickets[key] = value;
|
state.fetchedData = {...state.fetchedData, tickets: Date.now()};
|
||||||
state.loadedTickets = {...state.loadedTickets};
|
|
||||||
console.log(state.loadedTickets)
|
|
||||||
},
|
},
|
||||||
updateTicket(state, updatedTicket) {
|
updateTicket(state, updatedTicket) {
|
||||||
const ticket = state.loadedTickets[updatedTicket.event?updatedTicket.event:'none'].filter(
|
const ticket = state.tickets.filter(({id}) => id === updatedTicket.id)[0];
|
||||||
({id}) => id === updatedTicket.id)[0];
|
|
||||||
Object.assign(ticket, updatedTicket);
|
Object.assign(ticket, updatedTicket);
|
||||||
state.loadedTickets = {...state.loadedTickets};
|
state.tickets = [...state.tickets];
|
||||||
},
|
},
|
||||||
replaceUsers(state, users) {
|
replaceUsers(state, users) {
|
||||||
state.users = users;
|
state.users = users;
|
||||||
|
@ -270,7 +249,7 @@ const store = createStore({
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async reloadToken({commit, state}) {
|
async reloadToken({commit, state, getters}) {
|
||||||
try {
|
try {
|
||||||
if (state.user.username && state.user.password) {
|
if (state.user.username && state.user.password) {
|
||||||
const data = await fetch('/api/2/login/', {
|
const data = await fetch('/api/2/login/', {
|
||||||
|
@ -318,58 +297,59 @@ const store = createStore({
|
||||||
async fetchImage({state}, url) {
|
async fetchImage({state}, url) {
|
||||||
return await fetch(url, {headers: {'Authorization': `Token ${state.user.token}`}});
|
return await fetch(url, {headers: {'Authorization': `Token ${state.user.token}`}});
|
||||||
},
|
},
|
||||||
async loadUserInfo({commit, getters}) {
|
async loadUserInfo({commit, state}) {
|
||||||
const {data, success} = await getters.session.get('/2/self/');
|
const {data, success} = await http.get('/2/self/', state.user.token);
|
||||||
commit('setPermissions', data.permissions);
|
commit('setPermissions', data.permissions);
|
||||||
},
|
},
|
||||||
async loadEvents({commit, state, getters}) {
|
async loadEvents({commit, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.events > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.events > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/events/');
|
const {data, success} = await http.get('/2/events/', state.user.token);
|
||||||
if (data && success) commit('replaceEvents', data);
|
if (data && success)
|
||||||
|
commit('replaceEvents', data);
|
||||||
},
|
},
|
||||||
async createEvent({commit, dispatch, state, getters}, event) {
|
async createEvent({commit, dispatch, state}, event) {
|
||||||
const {data, success} = await getters.session.post('/2/events/', event);
|
const {data, success} = await http.post('/2/events/', event, state.user.token);
|
||||||
if (data && success) commit('replaceEvents', [...state.events, data]);
|
if (data && success)
|
||||||
|
commit('replaceEvents', [...state.events, data]);
|
||||||
},
|
},
|
||||||
async deleteEvent({commit, dispatch, state, getters}, event_id) {
|
async deleteEvent({commit, dispatch, state}, event_id) {
|
||||||
const {data, success} = await getters.session.delete(`/2/events/${event_id}/`);
|
const {data, success} = await http.delete(`/2/events/${event_id}/`, state.user.token);
|
||||||
if (success) {
|
if (success) {
|
||||||
await dispatch('loadEvents')
|
await dispatch('loadEvents')
|
||||||
commit('replaceEvents', [...state.events.filter(e => e.eid !== event_id)])
|
commit('replaceEvents', [...state.events.filter(e => e.eid !== event_id)])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async updateEvent({commit, dispatch, state}, {id, partial_event}){
|
async fetchTicketStates({commit, state}) {
|
||||||
console.log(id, partial_event);
|
|
||||||
const {data, success} = await http.patch(`/2/events/${id}/`, partial_event, state.user.token);
|
|
||||||
if (success) {
|
|
||||||
commit('replaceEvents', [...state.events.filter(e => e.eid !== id), data])
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async fetchTicketStates({commit, state, getters}) {
|
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.states > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.states > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/tickets/states/');
|
const {data, success} = await http.get('/2/tickets/states/', state.user.token);
|
||||||
if (data && success) commit('replaceTicketStates', data);
|
if (data && success)
|
||||||
|
commit('replaceTicketStates', data);
|
||||||
},
|
},
|
||||||
async changeEvent({dispatch, getters, commit}, eventName) {
|
changeEvent({dispatch, getters, commit}, eventName) {
|
||||||
await router.push({path: `/${eventName.slug}/${getters.getActiveView}/`});
|
router.push({path: `/${eventName.slug}/${getters.getActiveView}/`});
|
||||||
//dispatch('loadEventItems');
|
dispatch('loadEventItems');
|
||||||
},
|
},
|
||||||
async changeView({getters}, link) {
|
changeView({getters}, link) {
|
||||||
await router.push({path: `/${getters.getEventSlug}/${link.path}/`});
|
router.push({path: `/${getters.getEventSlug}/${link.path}/`});
|
||||||
},
|
},
|
||||||
async showBoxContent({getters}, box) {
|
showBoxContent({getters}, box) {
|
||||||
await router.push({path: `/${getters.getEventSlug}/items/`, query: {box}});
|
router.push({path: `/${getters.getEventSlug}/items/`, query: {box}});
|
||||||
},
|
},
|
||||||
async loadEventItems({commit, getters, state}) {
|
async loadEventItems({commit, getters, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.items > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.items > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
try {
|
try {
|
||||||
|
commit('replaceLoadedItems', []);
|
||||||
const slug = getters.getEventSlug;
|
const slug = getters.getEventSlug;
|
||||||
const {data, success} = await getters.session.get(`/2/${slug}/items/`);
|
if (slug in state.itemCache) {
|
||||||
|
commit('replaceLoadedItems', state.itemCache[slug]);
|
||||||
|
}
|
||||||
|
const {data, success} = await http.get(`/2/${slug}/items/`, state.user.token);
|
||||||
if (data && success) {
|
if (data && success) {
|
||||||
commit('setItems', {slug, items: data});
|
commit('replaceLoadedItems', data);
|
||||||
|
commit('setItemCache', {slug, items: data});
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Error loading items");
|
console.error("Error loading items");
|
||||||
|
@ -377,124 +357,129 @@ const store = createStore({
|
||||||
},
|
},
|
||||||
async searchEventItems({commit, getters, state}, query) {
|
async searchEventItems({commit, getters, state}, query) {
|
||||||
const encoded_query = base64.encode(utf8.encode(query));
|
const encoded_query = base64.encode(utf8.encode(query));
|
||||||
const slug = getters.getEventSlug;
|
|
||||||
const {
|
const {data, success} = await http.get(`/2/${getters.getEventSlug}/items/${encoded_query}/`, state.user.token);
|
||||||
data, success
|
if (data && success)
|
||||||
} = await getters.session.get(`/2/${slug}/items/${encoded_query}/`);
|
commit('replaceLoadedItems', data);
|
||||||
if (data && success) {
|
|
||||||
commit('setItems', {slug, items: data});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
async loadBoxes({commit, state, getters}) {
|
async loadBoxes({commit, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.boxes > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.boxes > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/boxes/');
|
const {data, success} = await http.get('/2/boxes/', state.user.token);
|
||||||
if (data && success) commit('replaceBoxes', data);
|
if (data && success)
|
||||||
|
commit('replaceBoxes', data);
|
||||||
},
|
},
|
||||||
async createBox({commit, dispatch, state, getters}, box) {
|
async createBox({commit, dispatch, state}, box) {
|
||||||
const {data, success} = await getters.session.post('/2/boxes/', box);
|
const {data, success} = await http.post('/2/boxes/', box, state.user.token);
|
||||||
commit('replaceBoxes', data);
|
commit('replaceBoxes', data);
|
||||||
dispatch('loadBoxes').then(() => {
|
dispatch('loadBoxes').then(() => {
|
||||||
commit('closeAddBoxModal');
|
commit('closeAddBoxModal');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
async deleteBox({commit, dispatch, state, getters}, box_id) {
|
async deleteBox({commit, dispatch, state}, box_id) {
|
||||||
await getters.session.delete(`/2/boxes/${box_id}/`);
|
await http.delete(`/2/boxes/${box_id}/`, state.user.token);
|
||||||
dispatch('loadBoxes');
|
dispatch('loadBoxes');
|
||||||
},
|
},
|
||||||
async updateItem({commit, getters, state}, item) {
|
async updateItem({commit, getters, state}, item) {
|
||||||
const {
|
const {
|
||||||
data, success
|
data,
|
||||||
} = await getters.session.put(`/2/${getters.getEventSlug}/item/${item.uid}/`, item);
|
success
|
||||||
|
} = await http.put(`/2/${getters.getEventSlug}/item/${item.uid}/`, item, state.user.token);
|
||||||
commit('updateItem', data);
|
commit('updateItem', data);
|
||||||
},
|
},
|
||||||
async markItemReturned({commit, getters, state}, item) {
|
async markItemReturned({commit, getters, state}, item) {
|
||||||
await getters.session.patch(`/2/${getters.getEventSlug}/item/${item.uid}/`, {returned: true},
|
await http.patch(`/2/${getters.getEventSlug}/item/${item.uid}/`, {returned: true}, state.user.token);
|
||||||
state.user.token);
|
|
||||||
commit('removeItem', item);
|
commit('removeItem', item);
|
||||||
},
|
},
|
||||||
async deleteItem({commit, getters, state}, item) {
|
async deleteItem({commit, getters, state}, item) {
|
||||||
await getters.session.delete(`/2/${getters.getEventSlug}/item/${item.uid}/`, item);
|
await http.delete(`/2/${getters.getEventSlug}/item/${item.uid}/`, item, state.user.token);
|
||||||
commit('removeItem', item);
|
commit('removeItem', item);
|
||||||
},
|
},
|
||||||
async postItem({commit, getters, state}, item) {
|
async postItem({commit, getters, state}, item) {
|
||||||
commit('updateLastUsed', {box: item.box, cid: item.cid});
|
commit('updateLastUsed', {box: item.box, cid: item.cid});
|
||||||
const {data, success} = await getters.session.post(`/2/${getters.getEventSlug}/item/`, item);
|
const {data, success} = await http.post(`/2/${getters.getEventSlug}/item/`, item, state.user.token);
|
||||||
commit('appendItem', data);
|
commit('appendItem', data);
|
||||||
},
|
},
|
||||||
async loadTickets({commit, state, getters}) {
|
async loadTickets({commit, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
//if (state.fetchedData.tickets > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.tickets > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/tickets/');
|
const {data, success} = await http.get('/2/tickets/', state.user.token);
|
||||||
if (data && success) commit('replaceTickets', data);
|
if (data && success)
|
||||||
|
commit('replaceTickets', data);
|
||||||
},
|
},
|
||||||
async searchEventTickets({commit, getters, state}, query) {
|
async searchEventTickets({commit, getters, state}, query) {
|
||||||
const encoded_query = base64.encode(utf8.encode(query));
|
const encoded_query = base64.encode(utf8.encode(query));
|
||||||
|
|
||||||
const {
|
const {data, success} = await http.get(`/2/${getters.getEventSlug}/tickets/${encoded_query}/`, state.user.token);
|
||||||
data, success
|
if (data && success)
|
||||||
} = await getters.session.get(`/2/${getters.getEventSlug}/tickets/${encoded_query}/`);
|
commit('replaceTickets', data);
|
||||||
if (data && success) commit('replaceTickets', data);
|
|
||||||
},
|
},
|
||||||
async sendMail({commit, dispatch, state, getters}, {id, message}) {
|
async sendMail({commit, dispatch, state}, {id, message}) {
|
||||||
const {data, success} = await getters.session.post(`/2/tickets/${id}/reply/`, {message},
|
const {data, success} = await http.post(`/2/tickets/${id}/reply/`, {message}, state.user.token);
|
||||||
state.user.token);
|
|
||||||
if (data && success) {
|
if (data && success) {
|
||||||
state.fetchedData.tickets = 0;
|
state.fetchedData.tickets = 0;
|
||||||
await dispatch('loadTickets');
|
await dispatch('loadTickets');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async postManualTicket({commit, dispatch, state, getters}, {sender, message, title,}) {
|
async postManualTicket({commit, dispatch, state}, {sender, message, title,}) {
|
||||||
const {data, success} = await getters.session.post(`/2/tickets/manual/`, {
|
const {data, success} = await http.post(`/2/tickets/manual/`, {
|
||||||
name: title, sender, body: message, recipient: 'mail@c3lf.de'
|
name: title,
|
||||||
});
|
sender,
|
||||||
|
body: message,
|
||||||
|
recipient: 'mail@c3lf.de'
|
||||||
|
}, state.user.token);
|
||||||
await dispatch('loadTickets');
|
await dispatch('loadTickets');
|
||||||
},
|
},
|
||||||
async postComment({commit, dispatch, state, getters}, {id, message}) {
|
async postComment({commit, dispatch, state}, {id, message}) {
|
||||||
const {data, success} = await getters.session.post(`/2/tickets/${id}/comment/`, {comment: message});
|
const {data, success} = await http.post(`/2/tickets/${id}/comment/`, {comment: message}, state.user.token);
|
||||||
if (data && success) {
|
if (data && success) {
|
||||||
state.fetchedData.tickets = 0;
|
state.fetchedData.tickets = 0;
|
||||||
await dispatch('loadTickets');
|
await dispatch('loadTickets');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async loadUsers({commit, state, getters}) {
|
async loadUsers({commit, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.users > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.users > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/users/');
|
const {data, success} = await http.get('/2/users/', state.user.token);
|
||||||
if (data && success) commit('replaceUsers', data);
|
if (data && success)
|
||||||
|
commit('replaceUsers', data);
|
||||||
},
|
},
|
||||||
async loadGroups({commit, state, getters}) {
|
async loadGroups({commit, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.groups > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.groups > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/groups/');
|
const {data, success} = await http.get('/2/groups/', state.user.token);
|
||||||
if (data && success) commit('replaceGroups', data);
|
if (data && success)
|
||||||
|
commit('replaceGroups', data);
|
||||||
},
|
},
|
||||||
async updateTicket({commit, state, getters}, ticket) {
|
async updateTicket({commit, state}, ticket) {
|
||||||
const {data, success} = await getters.session.put(`/2/tickets/${ticket.id}/`, ticket);
|
const {data, success} = await http.put(`/2/tickets/${ticket.id}/`, ticket, state.user.token);
|
||||||
commit('updateTicket', data);
|
commit('updateTicket', data);
|
||||||
},
|
},
|
||||||
async updateTicketPartial({commit, state, getters}, {id, ...ticket}) {
|
async updateTicketPartial({commit, state}, {id, ...ticket}) {
|
||||||
const {data, success} = await getters.session.patch(`/2/tickets/${id}/`, ticket);
|
const {data, success} = await http.patch(`/2/tickets/${id}/`, ticket, state.user.token);
|
||||||
commit('updateTicket', data);
|
commit('updateTicket', data);
|
||||||
},
|
},
|
||||||
async fetchShippingVouchers({commit, state, getters}) {
|
async fetchShippingVouchers({commit, state}) {
|
||||||
if (!state.user.token) return;
|
if (!state.user.token) return;
|
||||||
if (state.fetchedData.shippingVouchers > Date.now() - 1000 * 60 * 60 * 24) return;
|
if (state.fetchedData.shippingVouchers > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||||
const {data, success} = await getters.session.get('/2/shipping_vouchers/');
|
const {data, success} = await http.get('/2/shipping_vouchers/', state.user.token);
|
||||||
if (data && success) {
|
if (data && success) {
|
||||||
commit('setShippingVouchers', data);
|
commit('setShippingVouchers', data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async createShippingVoucher({dispatch, state, getters}, code) {
|
async createShippingVoucher({dispatch, state}, code) {
|
||||||
const {data, success} = await getters.session.post('/2/shipping_vouchers/', code);
|
const {data, success} = await http.post('/2/shipping_vouchers/', code, state.user.token);
|
||||||
if (data && success) {
|
if (data && success) {
|
||||||
state.fetchedData.shippingVouchers = 0;
|
state.fetchedData.shippingVouchers = 0;
|
||||||
dispatch('fetchShippingVouchers');
|
dispatch('fetchShippingVouchers');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async claimShippingVoucher({dispatch, state, getters}, {ticket, shipping_voucher_type}) {
|
async claimShippingVoucher({dispatch, state}, {ticket, shipping_voucher_type}) {
|
||||||
const id = state.shippingVouchers.filter(voucher => voucher.type === shipping_voucher_type && voucher.issue_thread === null)[0].id;
|
const id = state.shippingVouchers.filter(voucher => voucher.type === shipping_voucher_type && voucher.issue_thread === null)[0].id;
|
||||||
const {data, success} = await getters.session.patch(`/2/shipping_vouchers/${id}/`, {issue_thread: ticket});
|
const {
|
||||||
|
data,
|
||||||
|
success
|
||||||
|
} = await http.patch(`/2/shipping_vouchers/${id}/`, {issue_thread: ticket}, state.user.token);
|
||||||
if (data && success) {
|
if (data && success) {
|
||||||
state.fetchedData.shippingVouchers = 0;
|
state.fetchedData.shippingVouchers = 0;
|
||||||
state.fetchedData.tickets = 0;
|
state.fetchedData.tickets = 0;
|
||||||
|
@ -502,30 +487,58 @@ const store = createStore({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
plugins: [persistentStatePlugin({ // TODO change remember to some kind of enable field
|
plugins: [
|
||||||
|
persistentStatePlugin({ // TODO change remember to some kind of enable field
|
||||||
prefix: "lf_",
|
prefix: "lf_",
|
||||||
debug: false,
|
debug: false,
|
||||||
isLoadedKey: "persistent_loaded",
|
isLoadedKey: "persistent_loaded",
|
||||||
state: ["remember", "user", "events", "lastUsed",]
|
state: [
|
||||||
}), sharedStatePlugin({
|
"remember",
|
||||||
|
"user",
|
||||||
|
"events",
|
||||||
|
"lastUsed",
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
sharedStatePlugin({
|
||||||
debug: false,
|
debug: false,
|
||||||
isLoadedKey: "shared_loaded",
|
isLoadedKey: "shared_loaded",
|
||||||
clearingMutation: "logout",
|
clearingMutation: "logout",
|
||||||
afterInit: "afterSharedInit",
|
afterInit: "afterSharedInit",
|
||||||
state: ["test", "state_options", "fetchedData", "loadedItems", "users", "groups", "loadedBoxes", "loadedTickets", "shippingVouchers",],
|
state: [
|
||||||
watch: ["test", "state_options", "fetchedData", "loadedItems", "users", "groups", "loadedBoxes", "loadedTickets", "shippingVouchers",],
|
"test",
|
||||||
mutations: [//"replaceTickets",
|
"state_options",
|
||||||
|
"fetchedData",
|
||||||
|
"tickets",
|
||||||
|
"users",
|
||||||
|
"groups",
|
||||||
|
"loadedBoxes",
|
||||||
|
"loadedItems",
|
||||||
|
"shippingVouchers",
|
||||||
|
],
|
||||||
|
watch: [
|
||||||
|
"test",
|
||||||
|
"state_options",
|
||||||
|
"fetchedData",
|
||||||
|
"tickets",
|
||||||
|
"users",
|
||||||
|
"groups",
|
||||||
|
"loadedBoxes",
|
||||||
|
"loadedItems",
|
||||||
|
"shippingVouchers",
|
||||||
|
],
|
||||||
|
mutations: [
|
||||||
|
//"replaceTickets",
|
||||||
|
],
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
}),],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
store.watch((state) => state.user, (user) => {
|
store.watch((state) => state.user, (user) => {
|
||||||
if (store.getters.isLoggedIn) {
|
if (store.getters.isLoggedIn) {
|
||||||
if (router.currentRoute.value.name === 'login' && router.currentRoute.value.query.redirect) {
|
if (router.currentRoute.value.name === 'login' && router.currentRoute.value.query.redirect)
|
||||||
router.push(router.currentRoute.value.query.redirect);
|
router.push(router.currentRoute.value.query.redirect);
|
||||||
} else if (router.currentRoute.value.name === 'login') {
|
else if (router.currentRoute.value.name === 'login')
|
||||||
router.push('/');
|
router.push('/');
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (router.currentRoute.value.name !== 'login') {
|
if (router.currentRoute.value.name !== 'login') {
|
||||||
router.push({
|
router.push({
|
||||||
|
|
|
@ -100,12 +100,4 @@ const http = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const http_session = token => ({
|
export {ticketStateColorLookup, ticketStateIconLookup, http};
|
||||||
get: async (url) => await http.get(url, token),
|
|
||||||
post: async (url, data) => await http.post(url, data, token),
|
|
||||||
put: async (url, data) => await http.put(url, data, token),
|
|
||||||
patch: async (url, data) => await http.patch(url, data, token),
|
|
||||||
delete: async (url) => await http.delete(url, token),
|
|
||||||
});
|
|
||||||
|
|
||||||
export {ticketStateColorLookup, ticketStateIconLookup, http, http_session};
|
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<AsyncLoader :loaded="isItemsLoaded">
|
<AsyncLoader :loaded="loadedItems.length > 0">
|
||||||
<div class="container-fluid px-xl-5 mt-3">
|
<div class="container-fluid px-xl-5 mt-3">
|
||||||
<Modal title="Edit Item" v-if="editingItem" @close="closeEditingModal()">
|
<Modal title="Edit Item" v-if="editingItem" @close="closeEditingModal()">
|
||||||
<template #body>
|
<template #body>
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
<div class="col-xl-8 offset-xl-2">
|
<div class="col-xl-8 offset-xl-2">
|
||||||
<Table
|
<Table
|
||||||
:columns="['uid', 'description', 'box']"
|
:columns="['uid', 'description', 'box']"
|
||||||
:items="getEventItems"
|
:items="loadedItems"
|
||||||
:keyName="'uid'"
|
:keyName="'uid'"
|
||||||
@itemActivated="openLightboxModalWith($event)"
|
@itemActivated="openLightboxModalWith($event)"
|
||||||
>
|
>
|
||||||
|
@ -44,7 +44,7 @@
|
||||||
<Cards
|
<Cards
|
||||||
v-if="layout === 'cards'"
|
v-if="layout === 'cards'"
|
||||||
:columns="['uid', 'description', 'box']"
|
:columns="['uid', 'description', 'box']"
|
||||||
:items="getEventItems"
|
:items="loadedItems"
|
||||||
:keyName="'uid'"
|
:keyName="'uid'"
|
||||||
v-slot="{ item }"
|
v-slot="{ item }"
|
||||||
@itemActivated="openLightboxModalWith($event)"
|
@itemActivated="openLightboxModalWith($event)"
|
||||||
|
@ -97,8 +97,8 @@ export default {
|
||||||
}),
|
}),
|
||||||
components: {AsyncLoader, AuthenticatedImage, Lightbox, Table, Cards, Modal, EditItem},
|
components: {AsyncLoader, AuthenticatedImage, Lightbox, Table, Cards, Modal, EditItem},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState([]),
|
...mapState(['loadedItems']),
|
||||||
...mapGetters(['getEventItems', 'isItemsLoaded', 'layout']),
|
...mapGetters(['layout']),
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions(['deleteItem', 'markItemReturned', 'loadEventItems', 'updateItem', 'scheduleAfterInit']),
|
...mapActions(['deleteItem', 'markItemReturned', 'loadEventItems', 'updateItem', 'scheduleAfterInit']),
|
||||||
|
|
|
@ -80,11 +80,11 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(['state_options', 'users']),
|
...mapState(['tickets', 'state_options', 'users']),
|
||||||
...mapGetters(['availableShippingVoucherTypes', 'getAllTickets', 'route']),
|
...mapGetters(['availableShippingVoucherTypes']),
|
||||||
ticket() {
|
ticket() {
|
||||||
const id = parseInt(this.route.params.id)
|
const id = parseInt(this.$route.params.id)
|
||||||
const ret = this.getAllTickets.find(ticket => ticket.id === id);
|
const ret = this.tickets.find(ticket => ticket.id === id);
|
||||||
return ret ? ret : {};
|
return ret ? ret : {};
|
||||||
},
|
},
|
||||||
shippingEmail() {
|
shippingEmail() {
|
||||||
|
@ -124,12 +124,7 @@ export default {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.scheduleAfterInit(() => [Promise.all([this.fetchTicketStates(), this.loadTickets(), this.loadUsers(), this.fetchShippingVouchers()]).then(() => {
|
this.scheduleAfterInit(() => [Promise.all([this.fetchTicketStates(), this.loadTickets(), this.loadUsers(), this.fetchShippingVouchers()]).then(()=>{
|
||||||
if (this.ticket.state == "pending_new") {
|
|
||||||
this.selected_state = "pending_open";
|
|
||||||
this.changeTicketStatus(this.ticket)
|
|
||||||
}
|
|
||||||
;
|
|
||||||
this.selected_state = this.ticket.state;
|
this.selected_state = this.ticket.state;
|
||||||
this.selected_assignee = this.ticket.assigned_to
|
this.selected_assignee = this.ticket.assigned_to
|
||||||
})]);
|
})]);
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
<template>
|
<template>
|
||||||
<AsyncLoader :loaded="isTicketsLoaded">
|
<AsyncLoader :loaded="tickets.length > 0">
|
||||||
<div class="container-fluid px-xl-5 mt-3">
|
<div class="container-fluid px-xl-5 mt-3">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xl-8 offset-xl-2">
|
<div class="col-xl-8 offset-xl-2">
|
||||||
<Table
|
<Table
|
||||||
:columns="['id', 'name', 'state', 'last_activity', 'assigned_to',
|
:columns="['id', 'name', 'state', 'last_activity', 'assigned_to', 'actions', 'actions2']"
|
||||||
...(getEventSlug==='all'?['event']:[])]"
|
:items="tickets.map(formatTicket)"
|
||||||
:items="getEventTickets.map(formatTicket)"
|
|
||||||
:keyName="'id'"
|
:keyName="'id'"
|
||||||
v-if="layout === 'table'"
|
v-if="layout === 'table'"
|
||||||
>
|
>
|
||||||
|
@ -22,9 +21,8 @@
|
||||||
</Table>
|
</Table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<CollapsableCards v-if="layout === 'tasks'" :items="getEventTickets"
|
<CollapsableCards v-if="layout === 'tasks'" :items="tickets"
|
||||||
:columns="['id', 'name', 'last_activity', 'assigned_to',
|
:columns="['id', 'name', 'last_activity', 'assigned_to']"
|
||||||
...(getEventSlug==='all'?['event']:[])]"
|
|
||||||
:keyName="'state'" :sections="['pending_new', 'pending_open','pending_shipping',
|
:keyName="'state'" :sections="['pending_new', 'pending_open','pending_shipping',
|
||||||
'pending_physical_confirmation','pending_return','pending_postponed'].map(stateInfo)">
|
'pending_physical_confirmation','pending_return','pending_postponed'].map(stateInfo)">
|
||||||
<template #section_header="{index, section, count}">
|
<template #section_header="{index, section, count}">
|
||||||
|
@ -36,7 +34,6 @@
|
||||||
<td>{{ item.name }}</td>
|
<td>{{ item.name }}</td>
|
||||||
<td>{{ item.last_activity }}</td>
|
<td>{{ item.last_activity }}</td>
|
||||||
<td>{{ item.assigned_to }}</td>
|
<td>{{ item.assigned_to }}</td>
|
||||||
<td v-if="getEventSlug==='all'">{{ item.event }}</td>
|
|
||||||
<td>
|
<td>
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<a class="btn btn-primary" :href="'/'+ getEventSlug + '/ticket/' + item.id" title="view"
|
<a class="btn btn-primary" :href="'/'+ getEventSlug + '/ticket/' + item.id" title="view"
|
||||||
|
@ -67,7 +64,8 @@ export default {
|
||||||
name: 'Tickets',
|
name: 'Tickets',
|
||||||
components: {AsyncLoader, Lightbox, Table, Cards, Modal, EditItem, CollapsableCards},
|
components: {AsyncLoader, Lightbox, Table, Cards, Modal, EditItem, CollapsableCards},
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters(['getEventTickets', 'isTicketsLoaded', 'stateInfo', 'getEventSlug', 'layout']),
|
...mapState(['tickets']),
|
||||||
|
...mapGetters(['stateInfo', 'getEventSlug', 'layout']),
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions(['loadTickets', 'fetchTicketStates', 'scheduleAfterInit']),
|
...mapActions(['loadTickets', 'fetchTicketStates', 'scheduleAfterInit']),
|
||||||
|
@ -81,8 +79,7 @@ export default {
|
||||||
state: this.stateInfo(ticket.state).text,
|
state: this.stateInfo(ticket.state).text,
|
||||||
stateColor: this.stateInfo(ticket.state).color,
|
stateColor: this.stateInfo(ticket.state).color,
|
||||||
last_activity: ticket.last_activity,
|
last_activity: ticket.last_activity,
|
||||||
assigned_to: ticket.assigned_to,
|
assigned_to: ticket.assigned_to
|
||||||
event: ticket.event
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
<template>
|
<template>
|
||||||
<AsyncLoader :loaded="events.length > 0">
|
<Table
|
||||||
<ExpandableTable v-if="!!events" :columns="['slug', 'name']" :items="events" :keyName="'slug'">
|
:columns="['slug', 'name']"
|
||||||
|
:items="events"
|
||||||
|
:keyName="'slug'"
|
||||||
|
>
|
||||||
<template v-slot:header_actions>
|
<template v-slot:header_actions>
|
||||||
<button class="btn btn-success" @click.prevent="openAddEventModal">
|
<button class="btn btn-success" @click.prevent="openAddEventModal">
|
||||||
<font-awesome-icon icon="plus"/>
|
<font-awesome-icon icon="plus"/>
|
||||||
|
@ -9,94 +12,39 @@
|
||||||
</template>
|
</template>
|
||||||
<template v-slot:actions="{ item }">
|
<template v-slot:actions="{ item }">
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<button class="btn btn-secondary" @click.stop="changeEvent(item)" style="white-space: nowrap;">
|
<button class="btn btn-secondary" @click.stop="changeEvent(item)">
|
||||||
<font-awesome-icon icon="archive"/> use
|
<font-awesome-icon icon="archive"/>
|
||||||
|
use
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-danger" @click.stop="safeDeleteEvent(item.eid)" style="white-space: nowrap;">
|
<button class="btn btn-danger" @click.stop="safeDeleteEvent(item.eid)">
|
||||||
<font-awesome-icon icon="trash"/> delete
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<template v-slot:detail="{id, item }">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col">
|
|
||||||
<input type="date" class="form-control form-control-sm" title="Buildup Start"
|
|
||||||
v-model="item.pre_start" disabled style="opacity: 1"
|
|
||||||
:max="item.start" @focus="prepare_date_field">
|
|
||||||
</div>
|
|
||||||
<div class="col">
|
|
||||||
<input type="date" class="form-control form-control-sm" title="Official Event Start"
|
|
||||||
v-model="item.start" disabled style="opacity: 1"
|
|
||||||
:min="item.pre_start" :max="item.end" @focus="prepare_date_field">
|
|
||||||
</div>
|
|
||||||
<div class="col">
|
|
||||||
<input type="date" class="form-control form-control-sm" title="Official Event End"
|
|
||||||
v-model="item.end" disabled style="opacity: 1"
|
|
||||||
:min="item.start" :max="item.post_end" @focus="prepare_date_field">
|
|
||||||
</div>
|
|
||||||
<div class="col">
|
|
||||||
<input type="date" class="form-control form-control-sm" title="Teardown End"
|
|
||||||
v-model="item.post_end" disabled style="opacity: 1"
|
|
||||||
:min="item.end" @focus="prepare_date_field">
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="mt-3">
|
|
||||||
<label class="mr-3">Addresses: </label>
|
|
||||||
<div v-for="(address, a_id) in item.addresses" class="btn-group btn-group-sm mr-3"
|
|
||||||
@click.stop="deleteAddress(id, a_id)">
|
|
||||||
<button class="btn btn-secondary" disabled style="opacity: 1">
|
|
||||||
{{ address }}
|
|
||||||
</button>
|
|
||||||
<button class="btn btn-danger">
|
|
||||||
<font-awesome-icon icon="trash"/>
|
<font-awesome-icon icon="trash"/>
|
||||||
|
delete
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="btn-group btn-group-sm">
|
|
||||||
<input type="text" v-model="new_address[id]">
|
|
||||||
<button class="btn btn-secondary" @click.stop="addAddress(id)" style="white-space: nowrap;">
|
|
||||||
<font-awesome-icon icon="envelope"/> add
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
</ExpandableTable>
|
</Table>
|
||||||
</AsyncLoader>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import {mapActions, mapMutations, mapState} from 'vuex';
|
import {mapActions, mapMutations, mapState} from 'vuex';
|
||||||
import ExpandableTable from "@/components/ExpandableTable.vue";
|
import Table from '@/components/Table';
|
||||||
import AsyncLoader from "@/components/AsyncLoader.vue";
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Events',
|
name: 'Events',
|
||||||
components: {AsyncLoader, ExpandableTable},
|
components: {Table},
|
||||||
computed: mapState(['events']),
|
computed: mapState(['events']),
|
||||||
data() {
|
|
||||||
return {new_address: []}
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions(['changeEvent', 'deleteEvent', 'updateEvent']),
|
...mapActions(['changeEvent', 'deleteEvent']),
|
||||||
...mapMutations(['openAddEventModal']),
|
...mapMutations(['openAddEventModal']),
|
||||||
safeDeleteEvent(id) {
|
safeDeleteEvent(id) {
|
||||||
if (confirm('do you want to completely delete this event and related data?')) {
|
if (confirm('do you want to completely delete this event and related data?')) {
|
||||||
this.deleteEvent(id)
|
this.deleteEvent(id)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
addAddress(id) {
|
|
||||||
const a = this.new_address[id];
|
|
||||||
if (!this.events[id].addresses.includes(a))
|
|
||||||
this.events[id].addresses.push(a)
|
|
||||||
this.new_address[id] = ""
|
|
||||||
this.updateEvent({id: this.events[id].eid, partial_event: {addresses: this.events[id].addresses}});
|
|
||||||
},
|
|
||||||
deleteAddress(id, a_id) {
|
|
||||||
this.events[id].addresses = this.events[id].addresses.filter((e, i) => i !== a_id);
|
|
||||||
this.updateEvent({id: this.events[id].eid, partial_event: {addresses: this.events[id].addresses}});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|
||||||
</style>
|
</style>
|
Loading…
Reference in a new issue