Compare commits

...
Sign in to create a new pull request.

57 commits

Author SHA1 Message Date
0fa52645c2 handle plain base64 as transfer-encoding in incoming mails
All checks were successful
/ test (push) Successful in 2m31s
/ deploy (push) Successful in 3m19s
2025-01-18 22:15:04 +01:00
65755feb2f add UI for backend search
All checks were successful
/ test (push) Successful in 2m25s
/ deploy (push) Successful in 3m19s
2025-01-13 16:29:37 +01:00
ebb13b2a85 fix bug in tickets search api and add tests 2025-01-13 16:29:37 +01:00
5f0d9b8626 add more tests for encodings
All checks were successful
/ test (push) Successful in 2m52s
2025-01-12 13:29:44 +01:00
3635a55e39 fix error in mail parsing: not all 'inline' elements ara attachments
All checks were successful
/ test (push) Successful in 2m43s
2025-01-09 17:19:31 +01:00
598f758332 reduce shadow logouts, by actually logging out when token fails or is expired
All checks were successful
/ deploy (push) Successful in 4m33s
/ test (push) Successful in 2m56s
2024-12-24 15:56:24 +01:00
66a03a9dca metrics: Fix inverted return state in metrics
All checks were successful
/ test (push) Successful in 2m37s
/ deploy (push) Successful in 4m25s
2024-12-24 07:05:47 +01:00
f266133d14 bugfix: use correct url in manual ticket creation
All checks were successful
/ test (pull_request) Successful in 4m9s
/ deploy (push) Successful in 5m17s
/ test (push) Successful in 2m45s
2024-12-23 17:16:17 +01:00
b393767871 deploy: Fix hostname in postfix config
All checks were successful
/ test (push) Successful in 2m38s
/ deploy (push) Successful in 4m29s
/ test (pull_request) Successful in 2m39s
2024-12-23 15:02:41 +01:00
02d0c1498d deploy: Add dkim signing with rspamd
Some checks are pending
/ deploy (push) Blocked by required conditions
/ test (push) Successful in 2m38s
/ test (pull_request) Successful in 2m37s
2024-12-23 14:57:42 +01:00
198a9ccaa0 deploy: Remove mail relay host 2024-12-23 14:56:38 +01:00
22ac146110 metrics: Make labels more speakable
All checks were successful
/ test (push) Successful in 2m40s
/ deploy (push) Successful in 4m35s
2024-12-23 14:36:08 +01:00
2886d887f5 metrics: Make metrics actually usable
All checks were successful
/ test (push) Successful in 2m40s
/ deploy (push) Successful in 4m25s
With this commit, we should get stats for the item count per event per
returned-state.
2024-12-23 13:20:44 +00:00
06c0d6cf44 metrics: Add the first (simple) custom metrics 2024-12-23 13:20:44 +00:00
1e23151021 deploy: Fix dkim-key selection in rspamd
All checks were successful
/ test (push) Successful in 2m41s
/ deploy (push) Successful in 4m52s
2024-12-22 21:26:45 +01:00
ed7186cbbd add testdata to docker setup. use sqlite in 'dev' scenario and mailpit in 'testing'
All checks were successful
/ test (push) Successful in 2m44s
/ deploy (push) Successful in 3m33s
2024-12-22 21:12:57 +01:00
c512da2660 use id instead of eid in Events.vue and use index independent of sorting 2024-12-22 21:12:57 +01:00
e4fa48eb75 force loading of items after switching to another event 2024-12-22 21:12:57 +01:00
b31f3758e0 cicd: Fix failure when .ssh exists
All checks were successful
/ test (push) Successful in 2m38s
/ deploy (push) Successful in 4m23s
2024-12-19 13:44:17 +00:00
9141752fdf fix container selection in AddItemModal.vue
Some checks failed
/ test (push) Successful in 3m39s
/ deploy (push) Failing after 1m59s
2024-12-19 14:25:36 +01:00
f7fda52fd0 fix 500 error if files does not exist on disk
All checks were successful
/ deploy (push) Successful in 4m48s
/ test (pull_request) Successful in 50s
/ test (push) Successful in 2m45s
2024-12-05 00:50:52 +01:00
385620717c add basic view for item history 2024-12-05 00:50:48 +01:00
ec6e872163 update prefetched fields 2024-12-05 00:50:12 +01:00
c03d298a58 add /item/comment endpoint and prefetch related models 2024-12-05 00:50:12 +01:00
52cf00ef9c deploy: Fix metrics in nginx 2024-12-05 00:50:12 +01:00
0cb9b6cd95 update admin interface 2024-12-05 00:50:12 +01:00
a51036d6f2 add timeline information to the /items endpoint 2024-12-05 00:50:12 +01:00
01b022128d add frontend to edit event details 2024-12-05 00:50:12 +01:00
690a9c954e ensure creation file date 2024-12-05 00:50:12 +01:00
f57f0796eb fix untested error in /tickets endpoint 2024-12-05 00:50:12 +01:00
385831d735 Speed up sql
Some checks failed
/ test (push) Has been cancelled
2024-12-05 00:50:12 +01:00
72ff5aea12 implement simple backend search for items and tickets 2024-12-05 00:50:12 +01:00
797ea3ae12 add /matches endpoint ad return match information in tickets and /item endpoints 2024-12-05 00:50:12 +01:00
aaa11c3b60 drop v1 API and rename id columns 2024-12-05 00:50:12 +01:00
6968c38e68 test on every push 2024-12-05 00:50:12 +01:00
450562f534 show tickets filtered by active event 2024-12-05 00:50:12 +01:00
8dd56dd859 partition tickets by event 2024-12-05 00:50:12 +01:00
90f47fa9d6 match incoming mail to event 2024-12-05 00:30:05 +01:00
d18fa897a0 add simple issue templates 2024-12-05 00:30:05 +01:00
48a0f3819c deploy: Simple protection for metrics endpoint 2024-12-05 00:30:05 +01:00
80558a241d cicd: run on every pull request, but only deploy on testing 2024-12-05 00:30:05 +01:00
d9729a68ff cicd: Run tests automatically 2024-12-05 00:30:05 +01:00
f3550d7dc0 cicd: Deploy testing automatically 2024-12-05 00:30:05 +01:00
e023f87b35 change the ticket.state in the backend too 2024-12-05 00:30:05 +01:00
706d80c2c8 ticket state changes to pending_open on first view 2024-12-05 00:30:04 +01:00
6ac300d737 train spam on state change to 'closed_spam' 2024-12-05 00:30:04 +01:00
737aa69024 add functions to train mails as spam/ham 2024-12-05 00:30:04 +01:00
e9c8f1049e save raw_mails as file 2024-12-05 00:30:04 +01:00
38a24e4e06 add docker env for integration testing 2024-12-05 00:30:04 +01:00
9f5b420095 Add django standard metrics 2024-12-05 00:30:04 +01:00
031e146499 fixed race contidion on ticket view loading 2024-12-05 00:30:04 +01:00
00c01ac934 enforce startup order in docker-compose.yml 2024-12-05 00:30:04 +01:00
df345e36a1 Add dev environment using docker 2024-12-05 00:30:04 +01:00
43802196ca cleanup admin 'dashboard' 2024-12-05 00:30:04 +01:00
413a18a5d0 add event mail addresses to the /events endpoints 2024-12-05 00:30:04 +01:00
f94e2d8191 extract the search box into its own component 2024-12-05 00:30:04 +01:00
bd00f03411 use an 'AsyncButton' that waits for completion of an async onClick handler for mail replies and comments in tickets 2024-12-05 00:29:52 +01:00
96 changed files with 3771 additions and 1531 deletions

View file

@ -0,0 +1,35 @@
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

View file

@ -0,0 +1,27 @@
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

View file

@ -0,0 +1,60 @@
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 -p ~/.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 -p /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

View file

@ -0,0 +1,21 @@
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

14
core/.coveragerc Normal file
View file

@ -0,0 +1,14 @@
[run]
source = .
[report]
fail_under = 100
show_missing = True
skip_covered = True
omit =
*/tests/*
*/migrations/*
core/asgi.py
core/wsgi.py
core/settings.py
manage.py

View file

@ -12,25 +12,7 @@ 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):
@ -38,26 +20,17 @@ 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):

View file

@ -0,0 +1,32 @@
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()]

35
core/core/metrics.py Normal file
View file

@ -0,0 +1,35 @@
from django.apps import apps
from prometheus_client.core import CounterMetricFamily, REGISTRY
from django.db.models import Case, Value, When, BooleanField, Count
from inventory.models import Item
class ItemCountCollector(object):
def collect(self):
counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state'])
yield counter
if not apps.models_ready or not apps.apps_ready:
return
queryset = (
Item.all_objects
.annotate(
returned=Case(
When(returned_at__isnull=True, then=Value(False)),
default=Value(True),
output_field=BooleanField()
)
)
.values('event__slug', 'returned', 'event_id')
.annotate(amount=Count('id'))
.order_by('event__slug', 'returned') # Optional: order by slug and returned
)
for e in queryset:
counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"])
yield counter
REGISTRY.register(ItemCountCollector())

View file

@ -15,6 +15,11 @@ 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
@ -24,12 +29,14 @@ 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 = 'django-insecure-tm*$w_14iqbiy-!7(8#ba7j+_@(7@rf2&a^!=shs&$03b%2*rv' SECRET_KEY = os.getenv('DJANGO_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 = True DEBUG = truthy_str(os.getenv('DEBUG_MODE_ACTIVE', 'False'))
ALLOWED_HOSTS = [os.getenv('HTTP_HOST', 'localhost')] PRIMARY_HOST = os.getenv('HTTP_HOST', 'localhost')
ALLOWED_HOSTS = [PRIMARY_HOST]
MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost') MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost')
@ -40,6 +47,8 @@ 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 = [
@ -50,6 +59,7 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django_extensions', 'django_extensions',
'django_prometheus',
'rest_framework', 'rest_framework',
'knox', 'knox',
'drf_yasg', 'drf_yasg',
@ -85,6 +95,7 @@ SWAGGER_SETTINGS = {
} }
MIDDLEWARE = [ MIDDLEWARE = [
'django_prometheus.middleware.PrometheusBeforeMiddleware',
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
@ -92,6 +103,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_prometheus.middleware.PrometheusAfterMiddleware',
] ]
ROOT_URLCONF = 'core.urls' ROOT_URLCONF = 'core.urls'
@ -112,19 +124,12 @@ TEMPLATES = [
}, },
] ]
WSGI_APPLICATION = 'core.wsgi.application' ASGI_APPLICATION = 'core.asgi.application'
# Database # Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
if 'test' in sys.argv: if os.getenv('DB_HOST') is not None:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
else:
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.mysql', 'ENGINE': 'django.db.backends.mysql',
@ -134,8 +139,23 @@ 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'"
} }
},
}
elif os.getenv('DB_FILE') is not None:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.getenv('DB_FILE', 'local.db'),
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
} }
} }
@ -204,10 +224,12 @@ CHANNEL_LAYERS = {
'default': { 'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer', 'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': { 'CONFIG': {
'hosts': [('localhost', 6379)], 'hosts': [(os.getenv('REDIS_HOST', 'localhost'), 6379)],
}, },
} }
} }
PROMETHEUS_METRIC_NAMESPACE = 'c3lf'
TEST_RUNNER = 'core.test_runner.FastTestRunner' TEST_RUNNER = 'core.test_runner.FastTestRunner'

View file

@ -19,11 +19,10 @@ from django.urls import path, include
from .version import get_info from .version import get_info
from .metrics import *
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')),
@ -32,4 +31,5 @@ urlpatterns = [
path('api/2/', include('notify_sessions.api_v2')), path('api/2/', include('notify_sessions.api_v2')),
path('api/2/', include('authentication.api_v2')), path('api/2/', include('authentication.api_v2')),
path('api/', get_info), path('api/', get_info),
path('', include('django_prometheus.urls')),
] ]

View file

@ -1,16 +0,0 @@
"""
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()

View file

@ -1,27 +0,0 @@
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

View file

@ -1,65 +0,0 @@
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),
]

View file

@ -38,6 +38,8 @@ def media_urls(request, hash):
'Age': 0, 'Age': 0,
'ETag': file.hash, 'ETag': file.hash,
}) })
except FileNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except File.DoesNotExist: except File.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_404_NOT_FOUND)
except EmailAttachment.DoesNotExist: except EmailAttachment.DoesNotExist:
@ -80,6 +82,8 @@ def thumbnail_urls(request, size, hash):
'ETag': file.hash + "_" + str(size), 'ETag': file.hash + "_" + str(size),
}) })
except FileNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except File.DoesNotExist: except File.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_404_NOT_FOUND)
except EmailAttachment.DoesNotExist: except EmailAttachment.DoesNotExist:

View file

@ -0,0 +1,24 @@
# 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),
]

View file

@ -1,68 +0,0 @@
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)

View file

@ -1,6 +1,6 @@
from django.contrib import admin from django.contrib import admin
from inventory.models import Item, Container, Event from inventory.models import Item, Container, ItemPlacement, Comment, Event
class ItemAdmin(admin.ModelAdmin): class ItemAdmin(admin.ModelAdmin):
@ -22,3 +22,17 @@ 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)

View file

@ -1,150 +0,0 @@
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),
]

View file

@ -1,12 +1,15 @@
from django.urls import path from django.urls import re_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, status
from rest_framework.decorators import api_view, permission_classes from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response 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, Comment
from inventory.serializers import EventSerializer, ContainerSerializer, ItemSerializer from inventory.serializers import EventSerializer, ContainerSerializer, CommentSerializer, ItemSerializer, \
SearchResultSerializer
from base64 import b64decode
class EventViewSet(viewsets.ModelViewSet): class EventViewSet(viewsets.ModelViewSet):
@ -20,18 +23,40 @@ class ContainerViewSet(viewsets.ModelViewSet):
queryset = Container.objects.all() queryset = Container.objects.all()
class ItemViewSet(viewsets.ModelViewSet):
serializer_class = ItemSerializer
def prefetch_queryset(self, queryset):
serializer = self.get_serializer_class()
if hasattr(serializer, 'Meta') and hasattr(serializer.Meta, 'prefetch_related_fields'):
queryset = queryset.prefetch_related(*serializer.Meta.prefetch_related_fields)
return queryset
def get_queryset(self):
queryset = Item.objects.all()
return self.prefetch_queryset(queryset)
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)
query_tokens = query.split(' ') if not request.user.has_event_perm(event, 'view_item'):
q = Item.objects.filter(event=event) return Response(status=403)
for token in query_tokens: items = filter_items(Item.objects.filter(event=event), b64decode(query).decode('utf-8'))
if token: return Response(SearchResultSerializer(items, many=True).data)
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)
@ -39,12 +64,15 @@ def search_items(request, event_slug, query):
@api_view(['GET', 'POST']) @api_view(['GET', 'POST'])
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
def item(request, event_slug): def item(request, event_slug):
vs = ItemViewSet()
try: try:
event = Event.objects.get(slug=event_slug) event = None
if event_slug != 'none':
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'):
return Response(status=403) return Response(status=403)
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data) return Response(ItemSerializer(vs.prefetch_queryset(Item.objects.filter(event=event)), many=True).data)
elif request.method == 'POST': elif request.method == 'POST':
if not request.user.has_event_perm(event, 'add_item'): if not request.user.has_event_perm(event, 'add_item'):
return Response(status=403) return Response(status=403)
@ -52,8 +80,30 @@ 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(['POST'])
@permission_classes([IsAuthenticated])
@permission_required('tickets.add_comment', raise_exception=True)
def add_comment(request, event_slug, id):
event = None
if event_slug != 'none':
event = Event.objects.get(slug=event_slug)
item = Item.objects.get(event=event, id=id)
if not request.user.has_event_perm(event, 'view_item'):
return Response(status=403)
if 'comment' not in request.data or request.data['comment'] == '':
return Response({'status': 'error', 'message': 'missing comment'}, status=status.HTTP_400_BAD_REQUEST)
comment = Comment.objects.create(
item=item,
comment=request.data['comment'],
)
return Response(CommentSerializer(comment).data, status=status.HTTP_201_CREATED)
@api_view(['GET', 'PUT', 'DELETE', 'PATCH']) @api_view(['GET', 'PUT', 'DELETE', 'PATCH'])
@ -61,7 +111,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, uid=id) item = Item.objects.get(event=event, id=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)
@ -99,8 +149,9 @@ 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 + [
path('<event_slug>/items/', item), re_path(r'^(?P<event_slug>[\w-]+)/items/$', item, name='item'),
path('<event_slug>/items/<query>/', search_items), re_path(r'^(?P<event_slug>[\w-]+)/items/(?P<query>[-A-Za-z0-9+/]*={0,3})/$', search_items, name='search_items'),
path('<event_slug>/item/', item), re_path(r'^(?P<event_slug>[\w-]+)/item/$', item, name='item'),
path('<event_slug>/item/<id>/', item_by_id), re_path(r'^(?P<event_slug>[\w-]+)/item/(?P<id>\d+)/comment/$', add_comment, name='add_comment'),
re_path(r'^(?P<event_slug>[\w-]+)/item/(?P<id>\d+)/$', item_by_id, name='item_by_id'),
] ]

View file

@ -0,0 +1,52 @@
# 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')},
),
]

View file

@ -0,0 +1,17 @@
# 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',
),
]

View file

@ -0,0 +1,52 @@
# 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',
),
]

View file

@ -1,56 +1,106 @@
from django.core.files.base import ContentFile from itertools import groupby
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):
if 'uid' in kwargs: container = kwargs.pop('container')
raise ValueError('uid must not be set manually') if 'uid_deprecated' in kwargs:
uid = Item.all_objects.filter(event=kwargs['event']).count() + 1 raise ValueError('uid_deprecated must not be set manually')
kwargs['uid'] = uid uid_deprecated = Item.all_objects.filter(event=kwargs['event']).count() + 1
return super().create(**kwargs) kwargs['uid_deprecated'] = uid_deprecated
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):
iid = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
uid = models.IntegerField() uid_deprecated = models.IntegerField()
description = models.TextField() description = models.TextField()
event = models.ForeignKey('Event', models.CASCADE, db_column='eid') event = models.ForeignKey('Event', models.CASCADE)
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:
history = sorted(self.container_history.all(), key=lambda x: x.timestamp, reverse=True)
if history:
return history[0].container
else:
return None
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', 'event'),) unique_together = (('uid_deprecated', 'event'),)
permissions = [ permissions = [
('match_item', 'Can match item') ('match_item', 'Can match item')
] ]
def __str__(self): def __str__(self):
return '[' + str(self.uid) + ']' + self.description return '[' + str(self.id) + ']' + self.description
class Container(SoftDeleteModel): class Container(SoftDeleteModel):
cid = models.AutoField(primary_key=True) id = 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.cid) + ']' + self.name return '[' + str(self.id) + ']' + 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):
eid = models.AutoField(primary_key=True) id = 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)
@ -62,3 +112,6 @@ 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'

View file

@ -1,15 +1,29 @@
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, Comment
from inventory.shared_serializers import BasicItemSerializer
from mail.models import EventAddress
from tickets.shared_serializers import BasicIssueSerializer
class EventSerializer(serializers.ModelSerializer): class EventSerializer(serializers.ModelSerializer):
addresses = SlugRelatedField(many=True, slug_field='address', queryset=EventAddress.objects.all())
class Meta: class Meta:
model = Event model = Event
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end'] fields = ['id', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
read_only_fields = ['eid'] read_only_fields = ['id']
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):
@ -17,44 +31,47 @@ class ContainerSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Container model = Container
fields = ['cid', 'name', 'itemCount'] fields = ['id', 'name', 'itemCount']
read_only_fields = ['cid', 'itemCount'] read_only_fields = ['id', 'itemCount']
def get_itemCount(self, instance): def get_itemCount(self, instance):
return Item.objects.filter(container=instance.cid).count() return len(instance.items)
class ItemSerializer(serializers.ModelSerializer): class CommentSerializer(serializers.ModelSerializer):
def validate(self, attrs):
if 'comment' not in attrs or attrs['comment'] == '':
raise serializers.ValidationError('comment cannot be empty')
return attrs
class Meta:
model = Comment
fields = ('id', 'comment', 'timestamp', 'item')
class ItemSerializer(BasicItemSerializer):
timeline = serializers.SerializerMethodField()
dataImage = serializers.CharField(write_only=True, required=False) dataImage = serializers.CharField(write_only=True, required=False)
cid = serializers.SerializerMethodField() related_issues = BasicIssueSerializer(many=True, read_only=True)
box = serializers.SerializerMethodField()
file = serializers.SerializerMethodField()
returned = serializers.SerializerMethodField(required=False)
class Meta: class Meta:
model = Item model = Item
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage', 'returned'] fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues',
read_only_fields = ['uid'] 'timeline']
read_only_fields = ['id']
def get_cid(self, instance): prefetch_related_fields = ['comments', 'issue_relation_changes', 'container_history',
return instance.container.cid 'container_history__container', 'files', 'event',
'issue_relation_changes__issue_thread',
def get_box(self, instance): 'issue_relation_changes__issue_thread__event',
return instance.container.name 'issue_relation_changes__issue_thread__state_changes',
'issue_relation_changes__issue_thread__assignments']
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(cid=data['cid']) container = Container.objects.get(id=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)
@ -65,6 +82,8 @@ class ItemSerializer(serializers.ModelSerializer):
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):
@ -86,3 +105,42 @@ class ItemSerializer(serializers.ModelSerializer):
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

View file

@ -0,0 +1,31 @@
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

View file

@ -1,34 +0,0 @@
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(), [])

View file

@ -1,59 +0,0 @@
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)

View file

@ -1,56 +0,0 @@
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)

View file

@ -1,133 +0,0 @@
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)

View file

@ -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]['cid'], 1) self.assertEqual(response.json()[0]['id'], 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()['cid'], 1) self.assertEqual(response.json()['id'], 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].cid, 1) self.assertEqual(Container.objects.all()[0].id, 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.cid}/', {'name': 'BOX 2'}, content_type='application/json') response = self.client.put(f'/api/2/box/{box.id}/', {'name': 'BOX 2'}, content_type='application/json')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['cid'], 1) self.assertEqual(response.json()['id'], 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].cid, 1) self.assertEqual(Container.objects.all()[0].id, 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.cid}/') response = self.client.delete(f'/api/2/box/{box.id}/')
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)

View file

@ -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.eid}/', {'slug': 'EVENT2', 'name': 'Event 2 new'}) response = APIClient().put(f'/api/2/events/{event.id}/', {'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,10 +47,48 @@ 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.eid}/') response = client.delete(f'/api/2/events/{event.id}/')
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):
from mail.models import EventAddress
event1 = Event.objects.create(slug='TEST1', name='Event')
EventAddress.objects.create(event=event1, address='foo@bar.baz')
response = self.client.get('/api/2/events/')
self.assertEqual(response.status_code, 200)
self.assertEqual(1, len(response.json()))
self.assertEqual('TEST1', response.json()[0]['slug'])
self.assertEqual('Event', response.json()[0]['name'])
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])

View file

@ -1,3 +1,5 @@
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
@ -5,7 +7,11 @@ 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 from inventory.models import Event, Container, Item, Comment, ItemPlacement
from base64 import b64encode
from tickets.models import IssueThread, ItemRelation
class ItemTestCase(TestCase): class ItemTestCase(TestCase):
@ -13,142 +19,240 @@ 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.box = Container.objects.create(name='BOX') self.box1 = Container.objects.create(name='BOX1')
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(self): def test_members_and_timeline(self):
item = Item.objects.create(container=self.box, event=self.event, description='1') now = datetime.now()
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(response.json(), self.assertEqual(len(response.json()), 1)
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None, self.assertEqual(response.json()[0]['id'], item.id)
'returned': False}]) self.assertEqual(response.json()[0]['description'], '1')
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.box, event=self.event, description='1') item = Item.objects.create(container=self.box1, 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(response.json(), self.assertEqual(len(response.json()), 1)
[{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': file.hash, self.assertEqual(response.json()[0]['id'], item.id)
'returned': False}]) 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'], 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.box, event=self.event, description='1') Item.objects.create(container=self.box1, event=self.event, description='1')
Item.objects.create(container=self.box, event=self.event, description='2') Item.objects.create(container=self.box1, event=self.event, description='2')
Item.objects.create(container=self.box, event=self.event, description='3') Item.objects.create(container=self.box1, 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.box.cid, 'description': '1'}) response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.box1.id, 'description': '1'})
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), self.assertEqual(response.json()['id'], 1)
{'uid': 1, 'description': '1', 'box': 'BOX', 'cid': self.box.cid, 'file': None, self.assertEqual(response.json()['description'], '1')
'returned': False}) self.assertEqual(response.json()['box'], 'BOX1')
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].uid, 1) self.assertEqual(Item.objects.all()[0].id, 1)
self.assertEqual(Item.objects.all()[0].description, '1') self.assertEqual(Item.objects.all()[0].description, '1')
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
def test_create_item_without_container(self):
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'description': '1'})
self.assertEqual(response.status_code, 400)
def test_create_item_without_description(self):
response = self.client.post(f'/api/2/{self.event.slug}/item/', {'cid': self.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.box.cid, 'description': '1', {'cid': self.box1.id, '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()['uid'], 1) self.assertEqual(response.json()['id'], 1)
self.assertEqual(response.json()['description'], '1') self.assertEqual(response.json()['description'], '1')
self.assertEqual(response.json()['box'], 'BOX') self.assertEqual(response.json()['box'], 'BOX1')
self.assertEqual(response.json()['cid'], self.box.cid) self.assertEqual(response.json()['id'], self.box1.id)
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].uid, 1) self.assertEqual(Item.objects.all()[0].id, 1)
self.assertEqual(Item.objects.all()[0].description, '1') self.assertEqual(Item.objects.all()[0].description, '1')
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
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.box, event=self.event, description='1') item = Item.objects.create(container=self.box1, event=self.event, description='1')
response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/', {'description': '2'}, response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/', {'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(), self.assertEqual(response.json()['id'], item.id)
{'uid': 1, 'description': '2', 'box': 'BOX', 'cid': self.box.cid, 'file': None, self.assertEqual(response.json()['description'], '2')
'returned': False}) self.assertEqual(response.json()['box'], 'BOX1')
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].uid, 1) self.assertEqual(Item.objects.all()[0].id, 1)
self.assertEqual(Item.objects.all()[0].description, '2') self.assertEqual(Item.objects.all()[0].description, '2')
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
def test_update_item_with_file(self): def test_update_item_with_file(self):
import base64 import base64
item = Item.objects.create(container=self.box, event=self.event, description='1') item = Item.objects.create(container=self.box1, event=self.event, description='1')
response = self.client.put(f'/api/2/{self.event.slug}/item/{item.uid}/', response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/',
{'description': '2', {'description': '2',
'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')}, 'dataImage': "data:text/plain;base64," + base64.b64encode(b"foo").decode(
content_type='application/json') 'utf-8')},
content_type='application/json')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['uid'], 1) self.assertEqual(response.json()['id'], 1)
self.assertEqual(response.json()['description'], '2') self.assertEqual(response.json()['description'], '2')
self.assertEqual(response.json()['box'], 'BOX') self.assertEqual(response.json()['box'], 'BOX1')
self.assertEqual(response.json()['cid'], self.box.cid) self.assertEqual(response.json()['id'], self.box1.id)
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].uid, 1) self.assertEqual(Item.objects.all()[0].id, 1)
self.assertEqual(Item.objects.all()[0].description, '2') self.assertEqual(Item.objects.all()[0].description, '2')
self.assertEqual(Item.objects.all()[0].container.cid, self.box.cid) self.assertEqual(Item.objects.all()[0].container.id, self.box1.id)
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.box, event=self.event, description='1') item = Item.objects.create(container=self.box1, event=self.event, description='1')
Item.objects.create(container=self.box, event=self.event, description='2') Item.objects.create(container=self.box1, 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.uid}/') response = self.client.delete(f'/api/2/{self.event.slug}/item/{item.id}/')
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.box, event=self.event, description='1') Item.objects.create(container=self.box1, event=self.event, description='1')
item2 = Item.objects.create(container=self.box, event=self.event, description='2') item2 = Item.objects.create(container=self.box1, 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.uid}/') response = self.client.delete(f'/api/2/{self.event.slug}/item/{item2.id}/')
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.box, event=self.event, description='3') item3 = Item.objects.create(container=self.box1, event=self.event, description='3')
self.assertEqual(item3.uid, 3) self.assertEqual(item3.id, 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.box, event=self.event, description='1') Item.objects.create(container=self.box1, event=self.event, description='1')
Item.objects.create(container=self.box, event=self.event, description='2') Item.objects.create(container=self.box1, 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()), 1) self.assertEqual(len(response.json()), 2)
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.box, event=self.event, description='1') item = Item.objects.create(container=self.box1, 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.uid}/', {'returned': True}, response = self.client.patch(f'/api/2/{self.event.slug}/item/{item.id}/', {'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()
@ -158,8 +262,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.box, event=self.event, description='1') item1 = Item.objects.create(container=self.box1, event=self.event, description='1')
item2 = Item.objects.create(container=self.box, event=self.event, description='2') item2 = Item.objects.create(container=self.box1, 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)
@ -168,4 +272,74 @@ 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]['uid'], item1.uid) self.assertEqual(response.json()[0]['id'], item1.id)
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'])

View file

@ -0,0 +1,20 @@
# Generated by Django 4.2.7 on 2024-11-03 18:30
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'),
('mail', '0004_alter_emailattachment_file'),
]
operations = [
migrations.AlterField(
model_name='eventaddress',
name='event',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='addresses', to='inventory.event'),
),
]

View file

@ -0,0 +1,36 @@
# 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/'),
),
]

View file

@ -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 from core.settings import MAIL_DOMAIN, ACTIVE_SPAM_TRAINING
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 = models.TextField() raw_file = models.FileField(upload_to='raw_mail/')
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,10 +28,22 @@ 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)
event = models.ForeignKey(Event, models.SET_NULL, null=True) event = models.ForeignKey(Event, models.SET_NULL, null=True, related_name='addresses')
address = models.CharField(max_length=255) address = models.CharField(max_length=255)

View file

@ -1,4 +1,5 @@
import logging import logging
from re import match
import aiosmtplib import aiosmtplib
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
@ -10,6 +11,10 @@ from notify_sessions.models import SystemEvent
from tickets.models import IssueThread from tickets.models import IssueThread
class SpecialMailException(Exception):
pass
def find_quoted_printable(s, marker): def find_quoted_printable(s, marker):
positions = [i for i in range(len(s)) if s.lower().startswith('=?utf-8?' + marker + '?', i)] positions = [i for i in range(len(s)) if s.lower().startswith('=?utf-8?' + marker + '?', i)]
for pos in positions: for pos in positions:
@ -43,9 +48,15 @@ def unescape_and_decode_base64(s):
return decoded return decoded
def unescape_simplified_quoted_printable(s): def unescape_simplified_quoted_printable(s, encoding='utf-8'):
import quopri import quopri
return quopri.decodestring(s).decode('utf-8') return quopri.decodestring(s).decode(encoding)
def ascii_strip(s):
if not s:
return None
return ''.join([c for c in str(s) if 128 > ord(c) > 31])
def collect_references(issue_thread): def collect_references(issue_thread):
@ -86,7 +97,7 @@ 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="127.0.0.1", port=25, use_tls=False, start_tls=False)
def find_active_issue_thread(in_reply_to, address, subject): def find_active_issue_thread(in_reply_to, address, subject, event):
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:
@ -97,7 +108,7 @@ def find_active_issue_thread(in_reply_to, address, subject):
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) issue = IssueThread.objects.create(name=subject, event=event)
return issue, True return issue, True
@ -111,6 +122,23 @@ def find_target_event(address):
return None return None
def decode_email_segment(segment, charset, transfer_encoding):
decode_as = 'utf-8'
if charset == 'windows-1251':
decode_as = 'cp1251'
elif charset == 'iso-8859-1':
decode_as = 'latin1'
if transfer_encoding == 'quoted-printable':
segment = unescape_simplified_quoted_printable(segment, decode_as)
elif transfer_encoding == 'base64':
import base64
segment = base64.b64decode(segment).decode('utf-8')
else:
segment = unescape_and_decode_quoted_printable(segment)
segment = unescape_and_decode_base64(segment)
return segment
def parse_email_body(raw, log=None): def parse_email_body(raw, log=None):
import email import email
from hashlib import sha256 from hashlib import sha256
@ -122,21 +150,24 @@ def parse_email_body(raw, log=None):
if parsed.is_multipart(): if parsed.is_multipart():
for part in parsed.walk(): for part in parsed.walk():
ctype = part.get_content_type() ctype = part.get_content_type()
charset = part.get_content_charset()
cdispo = str(part.get('Content-Disposition')) cdispo = str(part.get('Content-Disposition'))
if ctype == 'multipart/mixed':
log.debug("Ignoring Multipart %s %s", ctype, cdispo)
# skip any text/plain (txt) attachments # skip any text/plain (txt) attachments
if ctype == 'text/plain' and 'attachment' not in cdispo: elif ctype == 'text/plain' and 'attachment' not in cdispo:
segment = part.get_payload() segment = part.get_payload()
if not segment: if not segment:
continue continue
segment = unescape_and_decode_quoted_printable(segment) segment = decode_email_segment(segment, charset, part.get('Content-Transfer-Encoding'))
segment = unescape_and_decode_base64(segment)
if part.get('Content-Transfer-Encoding') == 'quoted-printable':
segment = unescape_simplified_quoted_printable(segment)
log.debug(segment) log.debug(segment)
body = body + segment body = body + segment
elif 'attachment' in cdispo or 'inline' in cdispo: elif 'attachment' in cdispo or 'inline' in cdispo:
file = ContentFile(part.get_payload(decode=True)) content = part.get_payload(decode=True)
if content is None:
continue
file = ContentFile(content)
chash = sha256(file.read()).hexdigest() chash = sha256(file.read()).hexdigest()
name = part.get_filename() name = part.get_filename()
if name is None: if name is None:
@ -162,10 +193,7 @@ def parse_email_body(raw, log=None):
else: else:
log.warning("Unknown content type %s", parsed.get_content_type()) log.warning("Unknown content type %s", parsed.get_content_type())
body = "Unknown content type" body = "Unknown content type"
body = unescape_and_decode_quoted_printable(body) body = decode_email_segment(body, parsed.get_content_charset(), parsed.get('Content-Transfer-Encoding'))
body = unescape_and_decode_base64(body)
if parsed.get('Content-Transfer-Encoding') == 'quoted-printable':
body = unescape_simplified_quoted_printable(body)
log.debug(body) log.debug(body)
return parsed, body, attachments return parsed, body, attachments
@ -177,31 +205,35 @@ def receive_email(envelope, log=None):
header_from = parsed.get('From') header_from = parsed.get('From')
header_to = parsed.get('To') header_to = parsed.get('To')
header_in_reply_to = parsed.get('In-Reply-To') header_in_reply_to = ascii_strip(parsed.get('In-Reply-To'))
header_message_id = parsed.get('Message-ID') header_message_id = ascii_strip(parsed.get('Message-ID'))
if header_from != envelope.mail_from: if match(r'^([a-zA-Z ]*<)?MAILER-DAEMON@', header_from) and envelope.mail_from.strip("<>") == "":
log.warning("Header from does not match envelope from") log.warning("Ignoring mailer daemon")
log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}") raise SpecialMailException("Ignoring mailer daemon")
if header_to != envelope.rcpt_tos[0]: if Email.objects.filter(reference=header_message_id).exists(): # break before issue thread is created
log.warning("Header to does not match envelope to") log.warning("Email already exists")
log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}") raise Exception("Email already exists")
recipient = envelope.rcpt_tos[0].lower() if envelope.rcpt_tos else header_to.lower() recipient = envelope.rcpt_tos[0].lower() if envelope.rcpt_tos else header_to.lower()
sender = envelope.mail_from if envelope.mail_from else header_from sender = envelope.mail_from if envelope.mail_from else header_from
subject = parsed.get('Subject') subject = ascii_strip(parsed.get('Subject'))
if not subject: if not subject:
subject = "No subject" subject = "No subject"
subject = unescape_and_decode_quoted_printable(subject) subject = unescape_and_decode_quoted_printable(subject)
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) active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject, target_event)
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=envelope.content, event=target_event, in_reply_to=header_in_reply_to, raw_file=ContentFile(envelope.content, name=random_filename),
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)
@ -224,7 +256,7 @@ do not create a new request.
Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid()) Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid())
reply_email = Email.objects.create( reply_email = Email.objects.create(
sender=recipient, recipient=sender, body=body, subject=subject, sender=recipient, recipient=sender, body=body, subject=ascii_strip(subject),
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread) in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
reply = make_reply(reply_email, references, event=target_event.slug if target_event else None) reply = make_reply(reply_email, references, event=target_event.slug if target_event else None)
else: else:
@ -233,7 +265,7 @@ Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid
active_issue_thread.state = 'pending_open' active_issue_thread.state = 'pending_open'
active_issue_thread.save() active_issue_thread.save()
return email, new, reply return email, new, reply, active_issue_thread
class LMTPHandler: class LMTPHandler:
@ -255,7 +287,7 @@ class LMTPHandler:
content = None content = None
try: try:
content = envelope.content content = envelope.content
email, new, reply = await receive_email(envelope, log) email, new, reply, thread = await receive_email(envelope, log)
log.info(f"Created email {email.id}") log.info(f"Created email {email.id}")
systemevent = await database_sync_to_async(SystemEvent.objects.create)(type='email received', systemevent = await database_sync_to_async(SystemEvent.objects.create)(type='email received',
reference=email.id) reference=email.id)
@ -263,14 +295,20 @@ class LMTPHandler:
channel_layer = get_channel_layer() channel_layer = get_channel_layer()
await channel_layer.group_send( await channel_layer.group_send(
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id, 'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
"message": "email received"} "message": "email received"})
)
log.info(f"Sent message to frontend") log.info(f"Sent message to frontend")
if new and reply: if new and reply:
log.info('Sending message to %s' % reply['To']) log.info('Sending message to %s' % reply['To'])
await send_smtp(reply) await send_smtp(reply)
log.info("Sent auto reply") log.info("Sent auto reply")
return '250 Message accepted for delivery'
except SpecialMailException as e:
import uuid
random_filename = 'special-' + str(uuid.uuid4())
with open(random_filename, 'wb') as f:
f.write(content)
log.warning(f"Special mail exception: {e} saved to {random_filename}")
return '250 Message accepted for delivery' return '250 Message accepted for delivery'
except Exception as e: except Exception as e:
from hashlib import sha256 from hashlib import sha256

View file

@ -142,6 +142,7 @@ 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
@ -162,8 +163,9 @@ 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_inline(self):
from aiosmtpd.smtp import Envelope from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
import aiosmtplib import aiosmtplib
@ -182,6 +184,36 @@ 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_base64_transfer_encoding(self):
from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync
import aiosmtplib
aiosmtplib.send = make_mocked_coro()
handler = LMTPHandler()
server = mock.Mock()
session = mock.Mock()
envelope = Envelope()
envelope.mail_from = 'test1@test'
envelope.rcpt_tos = ['test2@test']
envelope.content = b'''Subject: test
From: test3@test
To: test4@test
Message-ID: <1@test>
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
VGVzdCBtaXQgQmFzZTY0LUtvZGllcnVuZzogw6TDtsO8w58='''
result = async_to_sync(handler.handle_DATA)(server, session, envelope)
self.assertEqual(result, '250 Message accepted for delivery')
self.assertEqual(len(Email.objects.all()), 2)
self.assertEqual(len(IssueThread.objects.all()), 1)
aiosmtplib.send.assert_called_once()
self.assertEqual('test', Email.objects.all()[0].subject)
self.assertEqual('Test 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(
@ -229,6 +261,7 @@ 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(
@ -281,6 +314,7 @@ 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(
@ -384,6 +418,7 @@ 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
@ -760,7 +795,6 @@ dGVzdGltYWdl
response = self.client.post(f'/api/2/tickets/{issue_thread.id}/reply/', { response = self.client.post(f'/api/2/tickets/{issue_thread.id}/reply/', {
'message': 'test' 'message': 'test'
}) })
aiosmtplib.send.assert_called_once()
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
self.assertEqual(5, len(Email.objects.all())) self.assertEqual(5, len(Email.objects.all()))
self.assertEqual(5, len(Email.objects.filter(issue_thread=issue_thread))) self.assertEqual(5, len(Email.objects.filter(issue_thread=issue_thread)))
@ -776,6 +810,7 @@ dGVzdGltYWdl
self.assertEqual('test subject', IssueThread.objects.all()[0].name) self.assertEqual('test subject', IssueThread.objects.all()[0].name)
self.assertEqual('pending_new', IssueThread.objects.all()[0].state) self.assertEqual('pending_new', IssueThread.objects.all()[0].state)
self.assertEqual(None, IssueThread.objects.all()[0].assigned_to) self.assertEqual(None, IssueThread.objects.all()[0].assigned_to)
aiosmtplib.send.assert_called_once()
def test_mail_4byte_unicode_emoji(self): def test_mail_4byte_unicode_emoji(self):
from aiosmtpd.smtp import Envelope from aiosmtpd.smtp import Envelope
@ -881,6 +916,59 @@ hello \xe4\xf6\xfc'''
self.assertEqual(1, len(states)) self.assertEqual(1, len(states))
self.assertEqual('pending_new', states[0].state) self.assertEqual('pending_new', states[0].state)
def test_mail_windows_1252(self):
from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync
import aiosmtplib
aiosmtplib.send = make_mocked_coro()
handler = LMTPHandler()
server = mock.Mock()
session = mock.Mock()
envelope = Envelope()
envelope.mail_from = 'test1@test'
envelope.rcpt_tos = ['test2@test']
envelope.content = b'''Subject: test
From: test1@test
To: test2@test
Message-ID: <1@test>
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
=0D=0Ahello='''
result = async_to_sync(handler.handle_DATA)(server, session, envelope)
self.assertEqual('250 Message accepted for delivery', result)
self.assertEqual(2, len(Email.objects.all()))
self.assertEqual(1, len(IssueThread.objects.all()))
aiosmtplib.send.assert_called_once()
self.assertEqual('test', Email.objects.all()[0].subject)
self.assertEqual('test1@test', Email.objects.all()[0].sender)
self.assertEqual('test2@test', Email.objects.all()[0].recipient)
self.assertEqual('\r\nhello', Email.objects.all()[0].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[0].issue_thread)
self.assertEqual('<1@test>', Email.objects.all()[0].reference)
self.assertEqual(None, Email.objects.all()[0].in_reply_to)
self.assertEqual(expected_auto_reply_subject.format('test', IssueThread.objects.all()[0].short_uuid()),
Email.objects.all()[1].subject)
self.assertEqual('test2@test', Email.objects.all()[1].sender)
self.assertEqual('test1@test', Email.objects.all()[1].recipient)
self.assertEqual(expected_auto_reply.format(IssueThread.objects.all()[0].short_uuid()),
Email.objects.all()[1].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[1].issue_thread)
self.assertTrue(Email.objects.all()[1].reference.startswith("<"))
self.assertTrue(Email.objects.all()[1].reference.endswith("@localhost>"))
self.assertEqual("<1@test>", Email.objects.all()[1].in_reply_to)
self.assertEqual('test', IssueThread.objects.all()[0].name)
self.assertEqual('pending_new', IssueThread.objects.all()[0].state)
self.assertEqual(None, IssueThread.objects.all()[0].assigned_to)
states = StateChange.objects.filter(issue_thread=IssueThread.objects.all()[0])
self.assertEqual(1, len(states))
self.assertEqual('pending_new', states[0].state)
def test_mail_quoted_printable_transfer_encoding(self): def test_mail_quoted_printable_transfer_encoding(self):
from aiosmtpd.smtp import Envelope from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
@ -933,3 +1021,146 @@ hello =C3=A4=C3=B6=C3=BC'''
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)
def test_text_with_attachment2(self):
from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync
import aiosmtplib
aiosmtplib.send = make_mocked_coro()
handler = LMTPHandler()
server = mock.Mock()
session = mock.Mock()
envelope = Envelope()
envelope.mail_from = 'test1@test'
envelope.rcpt_tos = ['test2@test']
envelope.content = b'''Subject: test
From: test1@test
To: test2@test
Message-ID: <1@test>
Content-Type: multipart/mixed; boundary="abc"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
--abc
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
test1
--abc
Content-Type: image/jpeg; name="test.jpg"
Content-Disposition: attachment; filename="test.jpg"
Content-Transfer-Encoding: base64
Content-ID: <1>
X-Attachment-Id: 1
dGVzdGltYWdl
--abc--'''
result = async_to_sync(handler.handle_DATA)(server, session, envelope)
self.assertEqual(result, '250 Message accepted for delivery')
self.assertEqual(len(Email.objects.all()), 2)
self.assertEqual(len(IssueThread.objects.all()), 1)
aiosmtplib.send.assert_called_once()
self.assertEqual('test', Email.objects.all()[0].subject)
self.assertEqual('test1@test', Email.objects.all()[0].sender)
self.assertEqual('test2@test', Email.objects.all()[0].recipient)
self.assertEqual('test1\n', Email.objects.all()[0].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[0].issue_thread)
self.assertEqual('<1@test>', Email.objects.all()[0].reference)
self.assertEqual(None, Email.objects.all()[0].in_reply_to)
self.assertEqual(expected_auto_reply_subject.format('test', IssueThread.objects.all()[0].short_uuid()),
Email.objects.all()[1].subject)
self.assertEqual('test2@test', Email.objects.all()[1].sender)
self.assertEqual('test1@test', Email.objects.all()[1].recipient)
self.assertEqual(expected_auto_reply.format(IssueThread.objects.all()[0].short_uuid()),
Email.objects.all()[1].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[1].issue_thread)
self.assertTrue(Email.objects.all()[1].reference.startswith("<"))
self.assertTrue(Email.objects.all()[1].reference.endswith("@localhost>"))
self.assertEqual("<1@test>", Email.objects.all()[1].in_reply_to)
self.assertEqual('test', IssueThread.objects.all()[0].name)
self.assertEqual('pending_new', IssueThread.objects.all()[0].state)
self.assertEqual(None, IssueThread.objects.all()[0].assigned_to)
states = StateChange.objects.filter(issue_thread=IssueThread.objects.all()[0])
self.assertEqual(1, len(states))
self.assertEqual('pending_new', states[0].state)
self.assertEqual(1, len(EmailAttachment.objects.all()))
self.assertEqual(1, EmailAttachment.objects.all()[0].id)
self.assertEqual('image/jpeg', EmailAttachment.objects.all()[0].mime_type)
self.assertEqual('test.jpg', EmailAttachment.objects.all()[0].name)
file_content = EmailAttachment.objects.all()[0].file.read()
self.assertEqual(b'testimage', file_content)
def test_text_non_utf8_in_multipart(self):
from aiosmtpd.smtp import Envelope
from asgiref.sync import async_to_sync
import aiosmtplib
aiosmtplib.send = make_mocked_coro()
handler = LMTPHandler()
server = mock.Mock()
session = mock.Mock()
envelope = Envelope()
envelope.mail_from = 'test1@test'
envelope.rcpt_tos = ['test2@test']
envelope.content = b'''Subject: test
From: test1@test
To: test2@test
Message-ID: <1@test>
Content-Type: multipart/alternative; boundary="abc"
--abc
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
test1
--abc
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
hello =E4
--abc
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
=0D=0Ahello
--abc--'''
result = async_to_sync(handler.handle_DATA)(server, session, envelope)
self.assertEqual(result, '250 Message accepted for delivery')
self.assertEqual(len(Email.objects.all()), 2)
self.assertEqual(len(IssueThread.objects.all()), 1)
aiosmtplib.send.assert_called_once()
self.assertEqual('test', Email.objects.all()[0].subject)
self.assertEqual('test1@test', Email.objects.all()[0].sender)
self.assertEqual('test2@test', Email.objects.all()[0].recipient)
self.assertEqual('test1\nhello ä\n\r\nhello\n', Email.objects.all()[0].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[0].issue_thread)
self.assertEqual('<1@test>', Email.objects.all()[0].reference)
self.assertEqual(None, Email.objects.all()[0].in_reply_to)
self.assertEqual(expected_auto_reply_subject.format('test', IssueThread.objects.all()[0].short_uuid()),
Email.objects.all()[1].subject)
self.assertEqual('test2@test', Email.objects.all()[1].sender)
self.assertEqual('test1@test', Email.objects.all()[1].recipient)
self.assertEqual(expected_auto_reply.format(IssueThread.objects.all()[0].short_uuid()),
Email.objects.all()[1].body)
self.assertEqual(IssueThread.objects.all()[0], Email.objects.all()[1].issue_thread)
self.assertTrue(Email.objects.all()[1].reference.startswith("<"))
self.assertTrue(Email.objects.all()[1].reference.endswith("@localhost>"))
self.assertEqual("<1@test>", Email.objects.all()[1].in_reply_to)
self.assertEqual('test', IssueThread.objects.all()[0].name)
self.assertEqual('pending_new', IssueThread.objects.all()[0].state)
self.assertEqual(None, IssueThread.objects.all()[0].assigned_to)
states = StateChange.objects.filter(issue_thread=IssueThread.objects.all()[0])
self.assertEqual(1, len(states))
self.assertEqual('pending_new', states[0].state)

View file

@ -1,3 +1,6 @@
aiodns==3.2.0
aiohttp==3.9.5
aiosignal==1.3.1
aiosmtpd==1.4.4.post2 aiosmtpd==1.4.4.post2
aiosmtplib==3.0.1 aiosmtplib==3.0.1
anyio==4.1.0 anyio==4.1.0
@ -28,6 +31,7 @@ django-rest-knox==4.2.0
django-soft-delete==0.9.21 django-soft-delete==0.9.21
djangorestframework==3.14.0 djangorestframework==3.14.0
drf-yasg==1.21.7 drf-yasg==1.21.7
frozenlist==1.4.1
h11==0.14.0 h11==0.14.0
hyperlink==21.0.0 hyperlink==21.0.0
idna==3.4 idna==3.4
@ -38,11 +42,13 @@ Jinja2==3.1.2
MarkupSafe==2.1.3 MarkupSafe==2.1.3
msgpack==1.0.7 msgpack==1.0.7
msgpack-python==0.5.6 msgpack-python==0.5.6
multidict==6.0.5
openapi-codec==1.3.2 openapi-codec==1.3.2
packaging==23.2 packaging==23.2
Pillow==10.1.0 Pillow==10.1.0
pyasn1==0.5.1 pyasn1==0.5.1
pyasn1-modules==0.3.0 pyasn1-modules==0.3.0
pycares==4.4.0
pycparser==2.21 pycparser==2.21
pyOpenSSL==23.3.0 pyOpenSSL==23.3.0
python-dotenv==1.0.0 python-dotenv==1.0.0
@ -65,4 +71,7 @@ urllib3==2.1.0
uvicorn==0.24.0.post1 uvicorn==0.24.0.post1
watchfiles==0.21.0 watchfiles==0.21.0
websockets==12.0 websockets==12.0
yarl==1.9.4
zope.interface==6.1 zope.interface==6.1
django-prometheus==2.3.1
prometheus_client==0.21.0

View file

@ -1,3 +1,6 @@
aiodns==3.2.0
aiohttp==3.9.5
aiosignal==1.3.1
aiosmtpd==1.4.4.post2 aiosmtpd==1.4.4.post2
aiosmtplib==3.0.1 aiosmtplib==3.0.1
asgiref==3.7.2 asgiref==3.7.2
@ -38,3 +41,5 @@ urllib3==2.1.0
uvicorn==0.24.0.post1 uvicorn==0.24.0.post1
watchfiles==0.21.0 watchfiles==0.21.0
websockets==12.0 websockets==12.0
django-prometheus==2.3.1
prometheus_client==0.21.0

View file

@ -1,4 +1,4 @@
import logging from base64 import b64decode
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
@ -10,16 +10,34 @@ from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
from core.settings import MAIL_DOMAIN from core.settings import MAIL_DOMAIN
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 from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher, ItemRelation
from tickets.serializers import IssueSerializer, CommentSerializer, ShippingVoucherSerializer from tickets.serializers import IssueSerializer, CommentSerializer, ShippingVoucherSerializer, SearchResultSerializer
from tickets.shared_serializers import RelationSerializer
class IssueViewSet(viewsets.ModelViewSet): class IssueViewSet(viewsets.ModelViewSet):
serializer_class = IssueSerializer serializer_class = IssueSerializer
queryset = IssueThread.objects.all()
def get_queryset(self):
queryset = IssueThread.objects.all()
serializer = self.get_serializer_class()
if hasattr(serializer, 'Meta') and hasattr(serializer.Meta, 'prefetch_related_fields'):
queryset = queryset.prefetch_related(*serializer.Meta.prefetch_related_fields)
return queryset
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):
@ -55,7 +73,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): def manual_ticket(request, event_slug):
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:
@ -65,8 +83,16 @@ def manual_ticket(request):
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(
@ -116,13 +142,67 @@ 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.lower().split(' ')
for issue in issues:
value = 0
if issue.short_uuid() in query:
value += 10
if "T#" + str(issue.id) in query:
value += 10
elif "#" + str(issue.id) in query:
value += 9
for item in issue.related_items:
if "I#" + str(item.id) in query:
value += 8
elif "#" + str(item.id) in query:
value += 5
for token in query_tokens:
if token in item.description.lower():
value += 1
for token in query_tokens:
if token in issue.name.lower():
value += 1
for comment in issue.comments.all():
for token in query_tokens:
if token in comment.comment.lower():
value += 1
for email in issue.emails.all():
for token in query_tokens:
if token in email.subject.lower():
value += 1
if token in email.body.lower():
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)
serializer = IssueSerializer()
queryset = IssueThread.objects.filter(event=event)
items = filter_issues(queryset.prefetch_related(*serializer.Meta.prefetch_related_fields),
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'^tickets/manual/$', manual_ticket, name='manual_ticket'), re_path(r'^(?P<event_slug>[\w-]+)/tickets/manual/$', manual_ticket, name='manual_ticket'),
re_path(r'^tickets/states/$', get_available_states, name='get_available_states'), re_path(r'^(?P<event_slug>[\w-]+)/tickets/(?P<query>[-A-Za-z0-9+/]*={0,3})/$', search_issues,
name='search_issues'),
] + router.urls) ] + router.urls)

View file

@ -0,0 +1,31 @@
# 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),
]

View file

@ -0,0 +1,29 @@
# 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'),
),
]

View file

@ -1,5 +1,7 @@
from django.db import models from itertools import groupby
from django.utils import timezone from django.utils import timezone
from django.db import models
from django_softdelete.models import SoftDeleteModel from django_softdelete.models import SoftDeleteModel
from authentication.models import ExtendedUser from authentication.models import ExtendedUser
@ -43,7 +45,6 @@ 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]
@ -51,7 +52,11 @@ class IssueThread(SoftDeleteModel):
@property @property
def state(self): def state(self):
try: try:
return self.state_changes.order_by('-timestamp').first().state state_changes = sorted(self.state_changes.all(), key=lambda x: x.timestamp, reverse=True)
if state_changes:
return state_changes[0].state
else:
return None
except AttributeError: except AttributeError:
return 'none' return 'none'
@ -60,11 +65,17 @@ 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:
return self.assignments.order_by('-timestamp').first().assigned_to assignments = sorted(self.assignments.all(), key=lambda x: x.timestamp, reverse=True)
if assignments:
return assignments[0].assigned_to
else:
return None
except AttributeError: except AttributeError:
return None return None
@ -74,6 +85,11 @@ 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
@ -130,8 +146,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_relations') issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='item_relation_changes')
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='issues') item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='issue_relation_changes')
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')

View file

@ -1,9 +1,11 @@
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 inventory.serializers import ItemSerializer from tickets.shared_serializers import BasicIssueSerializer
class CommentSerializer(serializers.ModelSerializer): class CommentSerializer(serializers.ModelSerializer):
@ -36,24 +38,26 @@ class ShippingVoucherSerializer(serializers.ModelSerializer):
read_only_fields = ('id', 'timestamp', 'used_at') read_only_fields = ('id', 'timestamp', 'used_at')
class IssueSerializer(serializers.ModelSerializer): class IssueSerializer(BasicIssueSerializer):
timeline = serializers.SerializerMethodField() timeline = serializers.SerializerMethodField()
last_activity = serializers.SerializerMethodField() last_activity = serializers.SerializerMethodField()
assigned_to = serializers.SlugRelatedField(slug_field='username', queryset=ExtendedUser.objects.all(), related_items = BasicItemSerializer(many=True, read_only=True)
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') fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items', 'event')
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items') read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
prefetch_related_fields = ['state_changes', 'comments', 'emails', 'emails__attachments', 'assignments',
'item_relation_changes', 'shipping_vouchers', 'item_relation_changes__item',
'item_relation_changes__item__container_history', 'event',
'item_relation_changes__item__container_history__container',
'item_relation_changes__item__files', 'item_relation_changes__item__event',
'item_relation_changes__item__event']
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):
@ -65,14 +69,14 @@ class IssueSerializer(serializers.ModelSerializer):
@staticmethod @staticmethod
def get_last_activity(self): def get_last_activity(self):
try: try:
last_state_change = self.state_changes.order_by('-timestamp').first().timestamp \ last_state_change = max(
if self.state_changes.count() > 0 else None [t.timestamp for t in self.state_changes.all()]) if self.state_changes.exists() else None
last_comment = self.comments.order_by('-timestamp').first().timestamp if self.comments.count() > 0 else None last_comment = max([t.timestamp for t in self.comments.all()]) if self.comments.exists() else None
last_mail = self.emails.order_by('-timestamp').first().timestamp if self.emails.count() > 0 else None last_mail = max([t.timestamp for t in self.emails.all()]) if self.emails.exists() else None
last_assignment = self.assignments.order_by('-timestamp').first().timestamp if \ last_assignment = max([t.timestamp for t in self.assignments.all()]) if self.assignments.exists() else None
self.assignments.count() > 0 else None
last_relation = self.item_relations.order_by('-timestamp').first().timestamp if \ last_relation = max([t.timestamp for t in
self.item_relations.count() > 0 else None self.item_relation_changes.all()]) if self.item_relation_changes.exists() 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)
@ -114,13 +118,13 @@ class IssueSerializer(serializers.ModelSerializer):
'timestamp': assignment.timestamp, 'timestamp': assignment.timestamp,
'assigned_to': assignment.assigned_to.username, 'assigned_to': assignment.assigned_to.username,
}) })
for relation in obj.item_relations.all(): for relation in (obj.item_relation_changes.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': ItemSerializer(relation.item).data, 'item': BasicItemSerializer(relation.item).data,
}) })
for shipping_voucher in obj.shipping_vouchers.all(): for shipping_voucher in obj.shipping_vouchers.all():
timeline.append({ timeline.append({
@ -132,5 +136,13 @@ class IssueSerializer(serializers.ModelSerializer):
}) })
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()
issue = IssueSerializer()
def to_representation(self, instance):
return {**IssueSerializer(instance['issue']).data, 'search_score': instance['search_score']}
class Meta:
model = IssueThread

View file

@ -0,0 +1,23 @@
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')

View file

@ -0,0 +1,149 @@
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']))

View file

@ -3,11 +3,14 @@ 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 from tickets.models import IssueThread, StateChange, Comment, ItemRelation, Assignment
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):
@ -16,6 +19,9 @@ 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]})
@ -28,6 +34,7 @@ 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',
@ -46,14 +53,24 @@ 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=3), timestamp=now + timedelta(seconds=4),
)
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(None, issue.assigned_to) self.assertEqual(self.user, 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)
@ -61,14 +78,17 @@ 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]['assigned_to'], None) self.assertEqual(response.json()[0]['event'], "evt")
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'], comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) self.assertEqual(response.json()[0]['last_activity'], match.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
self.assertEqual(len(response.json()[0]['timeline']), 4) self.assertEqual(len(response.json()[0]['timeline']), 6)
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'], 'comment') self.assertEqual(response.json()[0]['timeline'][3]['type'], 'assignment')
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)
@ -85,20 +105,35 @@ 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]['comment'], 'test') self.assertEqual(response.json()[0]['timeline'][3]['assigned_to'], self.user.username)
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',
@ -118,8 +153,11 @@ 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'),
@ -153,6 +191,7 @@ 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',
@ -189,6 +228,7 @@ 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'])
@ -230,13 +270,14 @@ 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/tickets/manual/', response = self.client.post('/api/2/evt/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')
@ -247,9 +288,35 @@ 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)
@ -260,6 +327,7 @@ 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)
@ -267,6 +335,7 @@ 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')
@ -284,6 +353,7 @@ 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')
@ -292,12 +362,14 @@ 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))
@ -305,3 +377,91 @@ 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.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='EVENT', name='Event')
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]})
def test_search_empty_result(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())
def test_search(self):
now = datetime.now()
issue = IssueThread.objects.create(
name="test issue Abc",
event=self.event,
)
mail1 = Email.objects.create(
subject='test',
body='test aBc',
sender='test',
recipient='test',
issue_thread=issue,
timestamp=now,
)
mail2 = Email.objects.create(
subject='test',
body='test',
sender='test',
recipient='test',
issue_thread=issue,
in_reply_to=mail1.reference,
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(
issue_thread=issue,
comment="test deF",
timestamp=now + timedelta(seconds=4),
)
match = ItemRelation.objects.create(
issue_thread=issue,
item=self.item,
timestamp=now + timedelta(seconds=5),
)
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(1, len(response.json()))
self.assertEqual(issue.id, response.json()[0]['id'])
score2 = response.json()[0]['search_score']
search_query = b64encode(b'dEf').decode('utf-8')
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
self.assertEqual(200, response.status_code)
self.assertEqual(1, len(response.json()))
self.assertEqual(issue.id, response.json()[0]['id'])
score1 = response.json()[0]['search_score']
search_query = b64encode(b'ghi').decode('utf-8')
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
self.assertEqual(200, response.status_code)
self.assertEqual(0, len(response.json()))
search_query = b64encode(b'Abc def').decode('utf-8')
response = self.client.get(f'/api/2/{self.event.slug}/tickets/{search_query}/')
self.assertEqual(200, response.status_code)
self.assertEqual(1, len(response.json()))
self.assertEqual(issue.id, response.json()[0]['id'])
score3 = response.json()[0]['search_score']
self.assertGreater(score3, score2)
self.assertGreater(score2, score1)
self.assertGreater(score1, 0)

View file

@ -11,4 +11,6 @@ c3lf-nodes:
mail_domain: <mail_domain> mail_domain: <mail_domain>
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'

View file

@ -345,6 +345,13 @@
notify: notify:
- restart postfix - restart postfix
- name: configure rspamd dkim
template:
src: templates/rspamd-dkim.cf.j2
dest: /etc/rspamd/local.d/dkim_signing.conf
notify:
- restart rspamd
- name: configure rspamd - name: configure rspamd
copy: copy:
content: | content: |

View file

@ -1,3 +1,4 @@
REDIS_HOST=localhost
DB_HOST=localhost DB_HOST=localhost
DB_PORT=3306 DB_PORT=3306
DB_NAME=c3lf_sys3 DB_NAME=c3lf_sys3
@ -9,3 +10,6 @@ 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 }}

View file

@ -70,6 +70,21 @@ 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;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://c3lf-sys3;
}
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

View file

@ -32,12 +32,11 @@ smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = polaris.c3lf.de myhostname = polaris.lab.or.it
alias_maps = hash:/etc/aliases alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases alias_database = hash:/etc/aliases
myorigin = /etc/mailname myorigin = /etc/mailname
mydestination = $myhostname, , localhost mydestination = $myhostname, , localhost
relayhost = firefly.lab.or.it
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0 mailbox_size_limit = 0
recipient_delimiter = + recipient_delimiter = +

View file

@ -0,0 +1,79 @@
# local.d/dkim_signing.conf
enabled = true;
# If false, messages with empty envelope from are not signed
allow_envfrom_empty = true;
# If true, envelope/header domain mismatch is ignored
allow_hdrfrom_mismatch = false;
# If true, multiple from headers are allowed (but only first is used)
allow_hdrfrom_multiple = false;
# If true, username does not need to contain matching domain
allow_username_mismatch = false;
# Default path to key, can include '$domain' and '$selector' variables
path = "/var/lib/rspamd/dkim/$domain.$selector.key";
# Default selector to use
selector = "dkim";
# If false, messages from authenticated users are not selected for signing
sign_authenticated = true;
# If false, messages from local networks are not selected for signing
sign_local = true;
# Map file of IP addresses/subnets to consider for signing
# sign_networks = "/some/file"; # or url
# Symbol to add when message is signed
symbol = "DKIM_SIGNED";
# Whether to fallback to global config
try_fallback = true;
# Domain to use for DKIM signing: can be "header" (MIME From), "envelope" (SMTP From), "recipient" (SMTP To), "auth" (SMTP username) or directly specified domain name
use_domain = "header";
# Domain to use for DKIM signing when sender is in sign_networks ("header"/"envelope"/"auth")
#use_domain_sign_networks = "header";
# Domain to use for DKIM signing when sender is a local IP ("header"/"envelope"/"auth")
#use_domain_sign_local = "header";
# Whether to normalise domains to eSLD
use_esld = true;
# Whether to get keys from Redis
use_redis = false;
# Hash for DKIM keys in Redis
key_prefix = "DKIM_KEYS";
# map of domains -> names of selectors (since rspamd 1.5.3)
#selector_map = "/etc/rspamd/dkim_selectors.map";
# map of domains -> paths to keys (since rspamd 1.5.3)
#path_map = "/etc/rspamd/dkim_paths.map";
# If `true` get pubkey from DNS record and check if it matches private key
check_pubkey = false;
# Set to `false` if you want to skip signing if public and private keys mismatch
allow_pubkey_mismatch = true;
# Domain specific settings
domain {
# Domain name is used as key
c3lf.de {
# Private key path
path = "/var/lib/rspamd/dkim/{{ mail_domain }}.key";
# Selector
selector = "{{ mail_domain }}";
}
}

View file

@ -0,0 +1,13 @@
FROM python:3.11-bookworm
LABEL authors="lagertonne"
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.dev.txt /code/
COPY requirements.prod.txt /code/
RUN apt update && apt install -y mariadb-client
RUN pip install -r requirements.dev.txt
RUN pip install -r requirements.prod.txt
RUN pip install mysqlclient
COPY .. /code/

View file

@ -0,0 +1,6 @@
FROM docker.io/node:22
RUN mkdir /web
WORKDIR /web
COPY package.json /web/
RUN npm install

View file

@ -0,0 +1,28 @@
services:
core:
build:
context: ../../core
dockerfile: ../deploy/dev/Dockerfile.backend
command: bash -c 'python manage.py migrate && python testdata.py && python manage.py runserver 0.0.0.0:8000'
environment:
- HTTP_HOST=core
- DB_FILE=dev.db
volumes:
- ../../core:/code
- ../testdata.py:/code/testdata.py
ports:
- "8000:8000"
frontend:
build:
context: ../../web
dockerfile: ../deploy/dev/Dockerfile.frontend
command: npm run serve
volumes:
- ../../web:/web:ro
- /web/node_modules
- ./vue.config.js:/web/vue.config.js
ports:
- "8080:8080"
depends_on:
- core

27
deploy/dev/vue.config.js Normal file
View file

@ -0,0 +1,27 @@
// vue.config.js
module.exports = {
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*"
},
proxy: {
'^/media/2': {
target: 'http://core:8000/',
},
'^/api/2': {
target: 'http://core:8000/',
},
'^/api/1': {
target: 'http://core:8000/',
},
'^/ws/2': {
target: 'http://core:8000/',
ws: true,
logLevel: 'debug',
},
}
}
}

88
deploy/testdata.py Normal file
View file

@ -0,0 +1,88 @@
import os
def setup():
from authentication.models import ExtendedUser, EventPermission
from inventory.models import Event
from django.contrib.auth.models import Permission, Group
permissions = ['add_item', 'view_item', 'view_file', 'delete_item', 'change_item']
if not ExtendedUser.objects.filter(username='admin').exists():
admin = ExtendedUser.objects.create_superuser('admin', 'admin@example.com', 'admin')
admin.set_password('admin')
admin.user_permissions.add(*Permission.objects.all())
admin.save()
if not ExtendedUser.objects.filter(username='testuser').exists():
testuser = ExtendedUser.objects.create_user('testuser', 'testuser@example.com', 'testuser')
testuser.set_password('testuser')
testuser.user_permissions.add(*Permission.objects.all())
testuser.save()
team = Group.objects.get(name='Team')
team.permissions.add(
*Permission.objects.all()
)
if not ExtendedUser.objects.filter(username='testuser2').exists():
testuser2 = ExtendedUser.objects.create_user('testuser2', 'testuser2@example.com', 'testuser2')
testuser2.set_password('testuser2')
testuser2.groups.add(team)
testuser2.save()
event1 = Event.objects.get_or_create(id=1, name='first test event', slug='TEST1',
start='2023-12-18 00:00:00.000000', end='2023-12-27 00:00:00.000000',
pre_start='2023-12-31 00:00:00.000000', post_end='2024-01-04 00:00:00.000000')[
0]
event2 = Event.objects.get_or_create(id=2, name='second test event', slug='TEST2',
start='2024-12-18 00:00:00.000000', end='2024-12-27 00:00:00.000000',
pre_start='2024-12-31 00:00:00.000000', post_end='2025-01-04 00:00:00.000000')[
0]
# for permission in permissions:
# EventPermission.objects.create(event=event_37c3, user=foo,
# permission=Permission.objects.get(codename=permission))
from tickets.models import IssueThread
from mail.models import Email
issue_thread = IssueThread.objects.get_or_create(
id=1,
name="test",
event=Event.objects.get(slug='TEST1')
)[0]
mail1 = Email.objects.get_or_create(
id=1,
subject='test subject',
body='test',
sender='test1@test',
recipient='test2@test',
issue_thread=issue_thread,
)[0]
mail1_reply = Email.objects.get_or_create(
id=2,
subject='Message received',
body='Thank you for your message.',
sender='test2@test',
recipient='test1@test',
in_reply_to=mail1.reference,
issue_thread=issue_thread,
)[0]
def main():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
import django
django.setup()
from django.core.management import call_command
call_command('migrate')
setup()
print('testdata initialised')
if __name__ == '__main__':
main()

View file

@ -0,0 +1,11 @@
FROM python:3.11-bookworm
LABEL authors="lagertonne"
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.prod.txt /code/
RUN apt update && apt install -y mariadb-client
RUN pip install -r requirements.prod.txt
RUN pip install mysqlclient
COPY .. /code/

View file

@ -0,0 +1,6 @@
FROM docker.io/node:22
RUN mkdir /web
WORKDIR /web
COPY package.json /web/
RUN npm install

View file

@ -0,0 +1,72 @@
services:
redis:
image: redis
ports:
- "6379:6379"
db:
image: mariadb
environment:
MARIADB_RANDOM_ROOT_PASSWORD: true
MARIADB_DATABASE: system3
MARIADB_USER: system3
MARIADB_PASSWORD: system3
volumes:
- mariadb_data:/var/lib/mysql
ports:
- "3306:3306"
core:
build:
context: ../../core
dockerfile: ../deploy/testing/Dockerfile.backend
command: bash -c 'python manage.py migrate && python testdata.py && python /code/server.py'
environment:
- HTTP_HOST=core
- REDIS_HOST=redis
- DB_HOST=db
- DB_PORT=3306
- DB_NAME=system3
- DB_USER=system3
- DB_PASSWORD=system3
- MAIL_DOMAIN=mail:1025
volumes:
- ../../core:/code
- ../testdata.py:/code/testdata.py
ports:
- "8000:8000"
depends_on:
- db
- redis
- mail
frontend:
build:
context: ../../web
dockerfile: ../deploy/testing/Dockerfile.frontend
command: npm run serve
volumes:
- ../../web:/web:ro
- /web/node_modules
- ./vue.config.js:/web/vue.config.js
ports:
- "8080:8080"
depends_on:
- core
mail:
image: docker.io/axllent/mailpit
volumes:
- mailpit_data:/data
ports:
- 8025:8025
- 1025:1025
environment:
MP_MAX_MESSAGES: 5000
MP_DATABASE: /data/mailpit.db
MP_SMTP_AUTH_ACCEPT_ANY: 1
MP_SMTP_AUTH_ALLOW_INSECURE: 1
volumes:
mariadb_data:
mailpit_data:

View file

@ -0,0 +1,27 @@
// vue.config.js
module.exports = {
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*"
},
proxy: {
'^/media/2': {
target: 'http://core:8000/',
},
'^/api/2': {
target: 'http://core:8000/',
},
'^/api/1': {
target: 'http://core:8000/',
},
'^/ws/2': {
target: 'http://core:8000/',
ws: true,
logLevel: 'debug',
},
}
}
}

View file

@ -1,5 +1,6 @@
<template> <template>
<div style="min-height: 100vh; display: flex; flex-direction: column;"> <div style="min-height: 100vh; display: flex; flex-direction: column;">
<Lightbox v-if="lightboxHash" :hash="lightboxHash" @close="openLightboxModalWith(null)"/>
<AddItemModal v-if="addItemModalOpen && isLoggedIn" @close="closeAddItemModal()" isModal="true"/> <AddItemModal v-if="addItemModalOpen && isLoggedIn" @close="closeAddItemModal()" isModal="true"/>
<AddTicketModal v-if="addTicketModalOpen && isLoggedIn" @close="closeAddTicketModal()" isModal="true"/> <AddTicketModal v-if="addTicketModalOpen && isLoggedIn" @close="closeAddTicketModal()" isModal="true"/>
<AddBoxModal v-if="showAddBoxModal && isLoggedIn" @close="closeAddBoxModal()" isModal="true"/> <AddBoxModal v-if="showAddBoxModal && isLoggedIn" @close="closeAddBoxModal()" isModal="true"/>
@ -16,20 +17,22 @@ import {mapState, mapMutations, mapActions, mapGetters} from 'vuex';
import AddTicketModal from "@/components/AddTicketModal.vue"; import AddTicketModal from "@/components/AddTicketModal.vue";
import AddBoxModal from "@/components/AddBoxModal.vue"; import AddBoxModal from "@/components/AddBoxModal.vue";
import AddEventModal from "@/components/AddEventModal.vue"; import AddEventModal from "@/components/AddEventModal.vue";
import Lightbox from "@/components/Lightbox.vue";
export default { export default {
name: 'app', name: 'app',
components: {AddBoxModal, AddEventModal, Navbar, AddItemModal, AddTicketModal}, components: {Lightbox, AddBoxModal, AddEventModal, Navbar, AddItemModal, AddTicketModal},
computed: { computed: {
...mapState(['loadedItems', 'layout', 'toasts', 'showAddBoxModal', 'showAddEventModal']), ...mapState(['loadedItems', 'layout', 'toasts', 'showAddBoxModal', 'showAddEventModal', 'lightboxHash']),
...mapGetters(['isLoggedIn']), ...mapGetters(['isLoggedIn']),
}, },
data: () => ({ data: () => ({
addItemModalOpen: false, addItemModalOpen: false,
addTicketModalOpen: false addTicketModalOpen: false,
}), }),
methods: { methods: {
...mapMutations(['removeToast', 'createToast', 'closeAddBoxModal', 'openAddBoxModal', 'closeAddEventModal']), ...mapMutations(['removeToast', 'createToast', 'closeAddBoxModal', 'openAddBoxModal', 'closeAddEventModal',
'openLightboxModalWith']),
...mapActions(['loadEvents', 'scheduleAfterInit']), ...mapActions(['loadEvents', 'scheduleAfterInit']),
openAddItemModal() { openAddItemModal() {
this.addItemModalOpen = true; this.addItemModalOpen = true;
@ -42,7 +45,7 @@ export default {
}, },
closeAddTicketModal() { closeAddTicketModal() {
this.addTicketModalOpen = false; this.addTicketModalOpen = false;
} },
}, },
created: function () { created: function () {
document.title = document.location.hostname; document.title = document.location.hostname;

View file

@ -2,7 +2,29 @@
<div> <div>
<Modal v-if="isModal" title="Add Item" @close="$emit('close')"> <Modal v-if="isModal" title="Add Item" @close="$emit('close')">
<template #body> <template #body>
<EditItem :item="item"/> <div>
<InputPhoto
:model="item"
field="file"
:on-capture="storeImage"
/>
<InputString
label="description"
:model="item"
field="description"
:validation-fn="str => str && str.length > 0"
/>
<div class="form-group">
<label for="box">box</label>
<InputCombo
label="box"
:model="item"
nameKey="box"
uniqueKey="cid"
:options="boxes"
/>
</div>
</div>
</template> </template>
<template #buttons> <template #buttons>
<button type="button" class="btn btn-secondary" @click="$emit('close')">Cancel</button> <button type="button" class="btn btn-secondary" @click="$emit('close')">Cancel</button>
@ -13,33 +35,40 @@
</template> </template>
<script> <script>
import {mapActions, mapGetters, mapState} from "vuex";
import Modal from '@/components/Modal'; import Modal from '@/components/Modal';
import EditItem from '@/components/EditItem'; import InputCombo from "@/components/inputs/InputCombo.vue";
import {mapActions, mapState} from "vuex"; import InputPhoto from "@/components/inputs/InputPhoto.vue";
import InputString from "@/components/inputs/InputString.vue";
export default { export default {
name: 'AddItemModal', name: 'AddItemModal',
components: {Modal, EditItem}, components: {InputString, InputPhoto, InputCombo, Modal},
props: ['isModal'], props: ['isModal'],
data: () => ({ data: () => ({
item: {} item: {}
}), }),
computed: { computed: {
...mapState(['lastUsed']) ...mapState(['lastUsed']),
...mapGetters(['getBoxes']),
boxes({getBoxes}) {
return getBoxes.map(obj => ({cid: obj.id, box: obj.name}));
}
}, },
methods: { methods: {
...mapActions(['postItem', 'loadBoxes', 'scheduleAfterInit']), ...mapActions(['postItem', 'loadBoxes', 'scheduleAfterInit']),
saveNewItem() { async saveNewItem() {
this.postItem(this.item).then(() => { await this.postItem(this.item);
this.$emit('close'); this.$emit('close');
}); },
storeImage(image) {
this.item.dataImage = image;
} }
}, },
created() {
this.item = {box: this.lastUsed.box || '', cid: this.lastUsed.cid || ''};
},
mounted() { mounted() {
this.scheduleAfterInit(() => [this.loadBoxes()]); this.scheduleAfterInit(() => [this.loadBoxes().then(() => {
this.item = {box: this.lastUsed.box || '', cid: this.lastUsed.cid || ''};
})])
} }
}; };
</script> </script>

View file

@ -19,11 +19,10 @@
<script> <script>
import {mapActions} from 'vuex'; import {mapActions} from 'vuex';
import Modal from '@/components/Modal'; import Modal from '@/components/Modal';
import EditItem from '@/components/EditItem';
export default { export default {
name: 'AddTicketModal', name: 'AddTicketModal',
components: {Modal, EditItem}, components: {Modal},
props: ['isModal'], props: ['isModal'],
data: () => ({ data: () => ({
ticket: { ticket: {

View file

@ -42,19 +42,27 @@ export default {
url: this.src, url: this.src,
data: this.image_data data: this.image_data
}); });
},
deferImage() {
setTimeout(() => {
if (this.cached) {
const c = this.getThumbnail(this.src);
if (c) {
this.image_data = c;
return;
}
}
this.loadImage();
}, 0);
}
},
watch: {
src: function (newVal, oldVal) {
this.deferImage()
} }
}, },
mounted() { mounted() {
setTimeout(() => { this.deferImage();
if (this.cached) {
const c = this.getThumbnail(this.src);
if (c) {
this.image_data = c;
return;
}
}
this.loadImage();
}, 0);
} }
} }
</script> </script>

View file

@ -52,7 +52,7 @@ export default {
}; };
}, },
created() { created() {
const query = this.$router.currentRoute ? (this.$router.currentRoute.query ? this.$router.currentRoute.query.collapsed : null) : null; const query = this.route ? (this.route.query ? this.route.query.collapsed : null) : null;
if (query !== null && query !== undefined) { if (query !== null && query !== undefined) {
this.collapsed = this.unpackInt(parseInt(query), this.sections.length); this.collapsed = this.unpackInt(parseInt(query), this.sections.length);
} else { } else {
@ -84,8 +84,8 @@ export default {
const encoded = this.packInt(this.collapsed).toString() const encoded = this.packInt(this.collapsed).toString()
if (this.route.query.collapsed !== encoded) if (this.route.query.collapsed !== encoded)
this.$router.push({ this.$router.push({
...this.$router.currentRoute, ...this.route,
query: {...this.$router.currentRoute.query, collapsed: encoded} query: {...this.route.query, collapsed: encoded}
}); });
}, },
deep: true, deep: true,

View file

@ -1,47 +0,0 @@
<template>
<div>
<h6>Editing Item <span class="badge badge-secondary">#{{ item.uid }}</span></h6>
<InputPhoto
:model="item"
field="file"
:on-capture="storeImage"
/>
<InputString
label="description"
:model="item"
field="description"
:validation-fn="str => str && str.length > 0"
/>
<InputCombo
label="box"
:model="item"
nameKey="box"
uniqueKey="cid"
:options="boxes"
/>
</div>
</template>
<script>
import InputString from './inputs/InputString';
import InputCombo from './inputs/InputCombo';
import {mapGetters} from 'vuex';
import InputPhoto from './inputs/InputPhoto';
export default {
name: 'EditItem',
components: {InputPhoto, InputCombo, InputString},
props: ['item'],
computed: {
...mapGetters(['getBoxes']),
boxes({getBoxes}) {
return getBoxes.map(obj => ({cid: obj.cid, box: obj.name}));
}
},
methods: {
storeImage(image) {
this.item.dataImage = image;
}
}
};
</script>

View file

@ -0,0 +1,124 @@
<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>

View file

@ -6,7 +6,8 @@
{{ 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 events" v-bind:key="index" <a class="dropdown-item text-light" href="#" v-for="(event, index) in selectableEvents"
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>
@ -29,16 +30,7 @@
</router-link> </router-link>
</li> </li>
</ul> </ul>
<form class="form-inline mt-1 my-lg-auto my-xl-auto w-100 d-inline mr-1" v-if="hasPermissions"> <SearchBox v-if="hasPermissions" class="mt-1 my-lg-auto my-xl-auto w-100 d-inline mr-1"/>
<input
class="form-control w-100"
type="search"
placeholder="Search"
aria-label="Search"
@input="searchEventItems($event.target.value)"
disabled
>
</form>
<div class="custom-control-inline mr-1" v-if="hasPermissions"> <div class="custom-control-inline mr-1" v-if="hasPermissions">
<div class="btn-group btn-group-toggle mr-1" v-if="isItemView()"> <div class="btn-group btn-group-toggle mr-1" v-if="isItemView()">
<button :class="['btn', 'btn-info', { active: layout === 'cards' }]" @click="setLayout('cards')"> <button :class="['btn', 'btn-info', { active: layout === 'cards' }]" @click="setLayout('cards')">
@ -57,12 +49,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()"> v-if="isItemView() && getEventSlug !== 'all'">
<font-awesome-icon icon="plus"/> <font-awesome-icon icon="plus"/>
<span class="d-none d-md-inline">&nbsp;Add Item</span> <span class="d-none d-md-inline">&nbsp;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()"> v-if="isTicketView() && getEventSlug !== 'all'">
<font-awesome-icon icon="plus"/> <font-awesome-icon icon="plus"/>
<span class="d-none d-md-inline">&nbsp;Add Ticket</span> <span class="d-none d-md-inline">&nbsp;Add Ticket</span>
</button> </button>
@ -73,19 +65,6 @@
</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 +82,13 @@
<script> <script>
import {mapState, mapActions, mapMutations, mapGetters} from 'vuex'; import {mapState, mapActions, mapMutations, mapGetters} from 'vuex';
import SearchBox from "@/components/inputs/SearchBox.vue";
export default { export default {
name: 'Navbar', name: 'Navbar',
components: {
SearchBox
},
data: () => ({ data: () => ({
views: [ views: [
{'title': 'items', 'path': 'items'}, {'title': 'items', 'path': 'items'},
@ -120,19 +103,22 @@ 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', 'searchEventItems']), ...mapActions(['changeEvent', 'changeView']),
...mapMutations(['logout']), ...mapMutations(['logout']),
navigateTo(link) { navigateTo(link) {
if (this.route.path !== link) if (this.route.path !== link)
this.$router.push(link); this.$router.push(link);
}, },
isItemView() { isItemView() {
return this.getActiveView === 'items' || this.getActiveView === 'item'; return this.getActiveView === 'items' || this.getActiveView === 'item' || this.getActiveView === 'item_search';
}, },
isTicketView() { isTicketView() {
return this.getActiveView === 'tickets' || this.getActiveView === 'ticket'; return this.getActiveView === 'tickets' || this.getActiveView === 'ticket' || this.getActiveView === 'ticket_search';
}, },
setLayout(layout) { setLayout(layout) {
if (this.route.query.layout === layout) if (this.route.query.layout === layout)

View file

@ -21,6 +21,9 @@
<span class="timeline-item-icon faded-icon" v-else-if="item.type === 'shipping_voucher'"> <span class="timeline-item-icon faded-icon" v-else-if="item.type === 'shipping_voucher'">
<font-awesome-icon icon="truck"/> <font-awesome-icon icon="truck"/>
</span> </span>
<span class="timeline-item-icon faded-icon" v-else-if="item.type === 'placement'">
<font-awesome-icon icon="archive"/>
</span>
<span class="timeline-item-icon faded-icon" v-else> <span class="timeline-item-icon faded-icon" v-else>
<font-awesome-icon icon="pen"/> <font-awesome-icon icon="pen"/>
</span> </span>
@ -30,40 +33,15 @@
<TimelineAssignment v-else-if="item.type === 'assignment'" :item="item"/> <TimelineAssignment v-else-if="item.type === 'assignment'" :item="item"/>
<TimelineRelatedItem v-else-if="item.type === 'item_relation'" :item="item"/> <TimelineRelatedItem v-else-if="item.type === 'item_relation'" :item="item"/>
<TimelineShippingVoucher v-else-if="item.type === 'shipping_voucher'" :item="item"/> <TimelineShippingVoucher v-else-if="item.type === 'shipping_voucher'" :item="item"/>
<TimelinePlacement v-else-if="item.type === 'placement'" :item="item"/>
<TimelineRelatedTicket v-else-if="item.type === 'issue_relation'" :item="item"/>
<p v-else>{{ item }}</p> <p v-else>{{ item }}</p>
</li> </li>
<li class="timeline-item"> <li class="timeline-item">
<span class="timeline-item-icon | faded-icon"> <slot name="timeline_action1"/>
<font-awesome-icon icon="comment"/>
</span>
<div class="new-comment card bg-dark">
<div class="">
<textarea placeholder="add comment..." v-model="newComment" class="form-control">
</textarea>
<button class="btn btn-primary float-right" @click="addCommentAndClear">
<font-awesome-icon icon="comment"/>
Save Comment
</button>
</div>
</div>
</li> </li>
<li class="timeline-item"> <li class="timeline-item">
<span class="timeline-item-icon | faded-icon"> <slot name="timeline_action2"/>
<font-awesome-icon icon="envelope"/>
</span>
<div class="new-mail card bg-dark">
<div class="card-header">
{{ newestMailSubject }}
</div>
<div>
<textarea placeholder="reply mail..." v-model="newMail" class="form-control">
</textarea>
<button class="btn btn-primary float-right" @click="sendMailAndClear">
<font-awesome-icon icon="envelope"/>
Send Mail
</button>
</div>
</div>
</li> </li>
</ol> </ol>
</template> </template>
@ -77,12 +55,21 @@ import {mapActions, mapGetters} from "vuex";
import TimelineAssignment from "@/components/TimelineAssignment.vue"; import TimelineAssignment from "@/components/TimelineAssignment.vue";
import TimelineRelatedItem from "@/components/TimelineRelatedItem.vue"; import TimelineRelatedItem from "@/components/TimelineRelatedItem.vue";
import TimelineShippingVoucher from "@/components/TimelineShippingVoucher.vue"; import TimelineShippingVoucher from "@/components/TimelineShippingVoucher.vue";
import AsyncButton from "@/components/inputs/AsyncButton.vue";
import TimelinePlacement from "@/components/TimelinePlacement.vue";
import TimelineRelatedTicket from "@/components/TimelineRelatedTicket.vue";
export default { export default {
name: 'Timeline', name: 'Timeline',
components: { components: {
TimelineRelatedTicket,
TimelinePlacement,
TimelineShippingVoucher, TimelineShippingVoucher,
TimelineRelatedItem, TimelineAssignment, TimelineStateChange, TimelineComment, TimelineMail TimelineRelatedItem,
TimelineAssignment,
TimelineStateChange,
TimelineComment,
TimelineMail
}, },
props: { props: {
timeline: { timeline: {
@ -90,36 +77,13 @@ export default {
default: () => [] default: () => []
} }
}, },
emits: ['sendMail', 'addComment'],
data: () => ({
newMail: "",
newComment: ""
}),
computed: { computed: {
...mapGetters(['stateInfo']), ...mapGetters(['stateInfo'])
newestMailSubject() {
const mail = this.timeline.filter(item => item.type === 'mail').pop();
return mail ? mail.subject : "";
},
}, },
methods: {
...mapActions(['fetchShippingVouchers']),
sendMailAndClear: function () {
this.$emit('sendMail', this.newMail);
this.newMail = "";
},
addCommentAndClear: function () {
this.$emit('addComment', this.newComment);
this.newComment = "";
}
},
mounted() {
this.fetchShippingVouchers();
}
}; };
</script> </script>
<style lang="scss" scoped> <style lang="scss">
*, *,
*:before, *:before,
@ -138,10 +102,10 @@ a {
color: inherit; color: inherit;
} }
img { /*img {
display: block; display: block;
max-width: 100%; max-width: 100%;
} }*/
/* End basic CSS override */ /* End basic CSS override */

View file

@ -1,6 +1,5 @@
<template> <template>
<div class="timeline-item-wrapper"> <div class="timeline-item-wrapper">
<Lightbox v-if="lightboxHash" :hash="lightboxHash" @close="closeLightboxModal()"/>
<div class="timeline-item-description"> <div class="timeline-item-description">
<i class="avatar | small"> <i class="avatar | small">
<font-awesome-icon icon="user"/> <font-awesome-icon icon="user"/>
@ -23,7 +22,7 @@
</div> </div>
<div class="card-footer" v-if="item.attachments.length"> <div class="card-footer" v-if="item.attachments.length">
<ul> <ul>
<li v-for="attachment in item.attachments" @click="openLightboxModalWith(attachment)"> <li v-for="attachment in item.attachments" @click="openLightboxModalWith(attachment.hash)">
<AuthenticatedImage :src="`/media/2/256/${attachment.hash}/`" :alt="attachment.name" <AuthenticatedImage :src="`/media/2/256/${attachment.hash}/`" :alt="attachment.name"
v-if="attachment.mime_type.startsWith('image/')" cached/> v-if="attachment.mime_type.startsWith('image/')" cached/>
<AuthenticatedDataLink :href="`/media/2/${attachment.hash}/`" :download="attachment.name" <AuthenticatedDataLink :href="`/media/2/${attachment.hash}/`" :download="attachment.name"
@ -32,26 +31,6 @@
</ul> </ul>
</div> </div>
</div> </div>
<!--button class="show-replies">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-forward"
width="44" height="44" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M15 11l4 4l-4 4m4 -4h-11a4 4 0 0 1 0 -8h1"/>
</svg>
Show 3 replies
<span class="avatar-list">
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
</span>
</button-->
</div> </div>
</template> </template>
@ -59,16 +38,11 @@
import AuthenticatedImage from "@/components/AuthenticatedImage.vue"; import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
import AuthenticatedDataLink from "@/components/AuthenticatedDataLink.vue"; import AuthenticatedDataLink from "@/components/AuthenticatedDataLink.vue";
import Lightbox from "@/components/Lightbox.vue"; import {mapMutations} from "vuex";
export default { export default {
name: 'TimelineMail', name: 'TimelineMail',
components: {Lightbox, AuthenticatedImage, AuthenticatedDataLink}, components: {AuthenticatedImage, AuthenticatedDataLink},
data() {
return {
lightboxHash: null,
}
},
props: { props: {
'item': { 'item': {
type: Object, type: Object,
@ -85,12 +59,7 @@ export default {
}, },
methods: { methods: {
openLightboxModalWith(attachment) { ...mapMutations(['openLightboxModalWith'])
this.lightboxHash = attachment.hash;
},
closeLightboxModal() { // Closes the editing modal and discards the edited copy of the item.
this.lightboxHash = null;
},
}, },
}; };
</script> </script>

View file

@ -0,0 +1,85 @@
<template>
<div class="timeline-item-description">
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
<span><a href="#">$USER</a> has placed item in '{{item.box}}' (#{{item.cid}}) at <time
:datetime="timestamp">{{ timestamp }}</time>
</span>
</div>
</template>
<script>
export default {
name: 'TimelinePlacement',
props: {
'item': {
type: Object,
required: true
}
},
computed: {
'timestamp': function () {
return new Date(this.item.timestamp).toLocaleString();
},
}
};
</script>
<style scoped>
a {
color: inherit;
}
/* End basic CSS override */
.timeline-item-description {
display: flex;
padding-top: 6px;
gap: 8px;
color: var(--gray);
img {
flex-shrink: 0;
}
a {
/*color: var(--c-grey-500);*/
font-weight: 500;
text-decoration: none;
&:hover,
&:focus {
outline: 0; /* Don't actually do this */
color: var(--info);
}
}
}
.avatar {
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
overflow: hidden;
aspect-ratio: 1 / 1;
flex-shrink: 0;
width: 40px;
height: 40px;
&.small {
width: 28px;
height: 28px;
}
img {
object-fit: cover;
}
}
</style>

View file

@ -1,12 +1,15 @@
<template> <template>
<div class="timeline-item-wrapper"> <div class="timeline-item-wrapper">
<Lightbox v-if="lightboxHash" :hash="lightboxHash" @close="closeLightboxModal()"/>
<div class="timeline-item-description"> <div class="timeline-item-description">
<i class="avatar | small"> <i class="avatar | small">
<font-awesome-icon icon="user"/> <font-awesome-icon icon="user"/>
</i> </i>
<span><!--a href="#">$USER</a--> linked item <span class="badge badge-secondary">#{{ item.item.uid }} </span> on <time <span><!--a href="#">$USER</a--> linked item <span class="badge badge-secondary">#{{
:datetime="timestamp">{{ timestamp }}</time> as <span class="badge badge-primary">{{ item.status }}</span> item.item.id
}} </span> on <time
:datetime="timestamp">{{ timestamp }}</time> as <span class="badge badge-primary">{{
item.status
}}</span>
</span> </span>
</div> </div>
<div class="card bg-dark"> <div class="card bg-dark">
@ -15,56 +18,19 @@
<AuthenticatedImage v-if="item.item.file" cached <AuthenticatedImage v-if="item.item.file" cached
:src="`/media/2/256/${item.item.file}/`" :src="`/media/2/256/${item.item.file}/`"
class="d-block card-img-left" class="d-block card-img-left"
@click="openLightboxModalWith(item.item)" @click="openLightboxModalWith(item.item.file)"
/> />
</div> </div>
<div class="col"> <div class="col">
<div class="card-body"> <div class="card-body">
<h6 class="card-subtitle text-secondary">uid: {{ item.item.uid }} box: {{ item.item.box }}</h6> <h6 class="card-subtitle text-secondary">id: {{ item.item.id }} box: {{ item.item.box }}</h6>
<h6 class="card-title">{{ item.item.description }}</h6> <router-link :to="{name: 'item', params: {id: item.item.id}}">
<!--div class="row mx-auto mt-2"> <h6 class="card-title">{{ item.item.description }}</h6>
<div class="btn-group"> </router-link>
<button class="btn btn-outline-success"
@click.stop="confirm('return Item?') && markItemReturned(item.item)"
title="returned">
<font-awesome-icon icon="check"/>
</button>
<button class="btn btn-outline-secondary" @click.stop="openEditingModalWith(item.item)"
title="edit">
<font-awesome-icon icon="edit"/>
</button>
<button class="btn btn-outline-danger"
@click.stop="confirm('delete Item?') && deleteItem(item.item)"
title="delete">
<font-awesome-icon icon="trash"/>
</button>
</div>
</div>
<p>{{ item }}</p-->
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<!--button class="show-replies">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-forward"
width="44" height="44" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M15 11l4 4l-4 4m4 -4h-11a4 4 0 0 1 0 -8h1"/>
</svg>
Show 3 replies
<span class="avatar-list">
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
</span>
</button-->
</div> </div>
</template> </template>
@ -72,16 +38,11 @@
import AuthenticatedImage from "@/components/AuthenticatedImage.vue"; import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
import AuthenticatedDataLink from "@/components/AuthenticatedDataLink.vue"; import AuthenticatedDataLink from "@/components/AuthenticatedDataLink.vue";
import Lightbox from "@/components/Lightbox.vue"; import {mapMutations} from "vuex";
export default { export default {
name: 'TimelineRelatedItem', name: 'TimelineRelatedItem',
components: {Lightbox, AuthenticatedImage, AuthenticatedDataLink}, components: {AuthenticatedImage, AuthenticatedDataLink},
data() {
return {
lightboxHash: null,
}
},
props: { props: {
'item': { 'item': {
type: Object, type: Object,
@ -98,13 +59,8 @@ export default {
}, },
methods: { methods: {
openLightboxModalWith(attachment) { ...mapMutations(['openLightboxModalWith'])
this.lightboxHash = attachment.hash; }
},
closeLightboxModal() { // Closes the editing modal and discards the edited copy of the item.
this.lightboxHash = null;
},
},
}; };
</script> </script>

View file

@ -0,0 +1,94 @@
<template>
<div class="timeline-item-wrapper">
<div class="timeline-item-description">
<i class="avatar | small">
<font-awesome-icon icon="user"/>
</i>
<span> linked ticket <span class="badge badge-secondary">#{{ item.issue_thread.id }} </span> on
<time :datetime="timestamp">{{ timestamp }}</time> as
<span class="badge badge-primary">{{ item.status }}</span>
</span>
</div>
<div class="timeline-item-description">
<router-link :to="{name: 'ticket', params: {id: item.issue_thread.id}}">
<h6 class="card-title">Ticket #{{ item.issue_thread.id }} - {{ item.issue_thread.name }}</h6>
</router-link>
</div>
</div>
</template>
<script>
export default {
name: 'TimelineRelatedTicket',
components: {},
props: {
'item': {
type: Object,
required: true
}
},
computed: {
'timestamp': function () {
return new Date(this.item.timestamp).toLocaleString();
}
},
};
</script>
<style scoped>
a {
color: inherit;
}
.timeline-item-description {
display: flex;
padding-top: 6px;
gap: 8px;
color: var(--gray);
img {
flex-shrink: 0;
}
a {
/*color: var(--c-grey-500);*/
font-weight: 500;
text-decoration: none;
&:hover,
&:focus {
outline: 0; /* Don't actually do this */
color: var(--info);
}
}
}
.card {
border: 1px solid var(--gray);
}
.avatar {
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
overflow: hidden;
aspect-ratio: 1 / 1;
flex-shrink: 0;
width: 40px;
height: 40px;
&.small {
width: 28px;
height: 28px;
}
img {
object-fit: cover;
}
}
</style>

View file

@ -0,0 +1,46 @@
<template>
<button @click.stop="handleClick" :disabled="disabled">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"
:class="{'d-none': !disabled}"></span>
<span class="ml-2" :class="{'d-none': !disabled}">In Progress...</span>
<span :class="{'d-none': disabled}"><slot></slot></span>
</button>
</template>
<script>
export default {
name: 'AsyncButton',
data() {
return {
disabled: false,
};
},
props: {
task: {
type: Function,
required: true,
},
},
methods: {
async handleClick() {
if (this.task && typeof this.task === 'function') {
this.disabled = true;
try {
await this.task();
} catch (e) {
console.error(e);
} finally {
this.disabled = false;
}
}
},
}
};
</script>
<style scoped>
.spinner-border {
vertical-align: -0.125em;
}
</style>

View file

@ -1,39 +1,36 @@
<template> <template>
<div class="form-group"> <div class="input-group">
<label :for="label">{{ label }}</label> <div class="input-group-prepend">
<div class="input-group"> <button
<div class="input-group-prepend"> class="btn btn-outline-secondary dropdown-toggle"
<button type="button"
class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown"
type="button" >Search
data-toggle="dropdown" </button>
>Search <div class="dropdown-menu">
</button> <a
<div class="dropdown-menu"> v-for="(option, index) in sortedOptions"
<a :key="index"
v-for="(option, index) in sortedOptions" class="dropdown-item"
:key="index" @click="setInternalValue(option)"
class="dropdown-item" :class="{ active: option == selectedOption }"
@click="setInternalValue(option)"
:class="{ active: option == selectedOption }"
>
{{ option[nameKey] }}
</a>
</div>
</div>
<input type="text" class="form-control" :id="label" v-model="internalName">
<div class="input-group-append">
<button
class="btn"
:class="{ 'btn-info disabled': isValid, 'btn-success': !isValid }"
v-if="!isValid"
@click="addOption()"
> >
<font-awesome-icon icon="plus"/> {{ option[nameKey] }}
</button> </a>
</div> </div>
<Addon type="Combo Box" :is-valid="isValid"/>
</div> </div>
<input type="text" class="form-control" :id="label" v-model="internalName">
<div class="input-group-append">
<button
class="btn"
:class="{ 'btn-info disabled': isValid, 'btn-success': !isValid }"
v-if="!isValid"
@click="addOption()"
>
<font-awesome-icon icon="plus"/>
</button>
</div>
<Addon type="Combo Box" :is-valid="isValid"/>
</div> </div>
</template> </template>
@ -46,11 +43,11 @@ export default {
props: ['label', 'model', 'nameKey', 'uniqueKey', 'options', 'onOptionAdd'], props: ['label', 'model', 'nameKey', 'uniqueKey', 'options', 'onOptionAdd'],
data: ({options, model, nameKey, uniqueKey}) => ({ data: ({options, model, nameKey, uniqueKey}) => ({
internalName: model[nameKey], internalName: model[nameKey],
selectedOption: options.filter(e => e[uniqueKey] == model[uniqueKey])[0], selectedOption: options.filter(e => e[uniqueKey] === model[uniqueKey])[0],
addingOption: false addingOption: false
}), }),
computed: { computed: {
isValid: ({options, nameKey, internalName}) => options.some(e => e[nameKey] == internalName), isValid: ({options, nameKey, internalName}) => options.some(e => e[nameKey] === internalName),
sortedOptions: ({ sortedOptions: ({
options, options,
nameKey nameKey
@ -59,7 +56,7 @@ export default {
watch: { watch: {
internalName(newValue) { internalName(newValue) {
if (this.isValid) { if (this.isValid) {
if (!this.selectedOption || newValue != this.selectedOption[this.nameKey]) { if (!this.selectedOption || newValue !== this.selectedOption[this.nameKey]) {
this.selectedOption = this.options.filter(e => e[this.nameKey] === newValue)[0]; this.selectedOption = this.options.filter(e => e[this.nameKey] === newValue)[0];
} }
this.model[this.nameKey] = this.selectedOption[this.nameKey]; this.model[this.nameKey] = this.selectedOption[this.nameKey];

View file

@ -2,9 +2,10 @@
<div> <div>
<img <img
v-if="!capturing" v-if="!capturing"
class="img-fluid rounded mx-auto d-block mb-3 img-preview" :class="imgClass || ['img-fluid', 'rounded', 'mx-auto', 'd-block', 'mb-3', 'img-preview']"
:src="dataImage" :src="dataImage"
alt="Image not available." alt="Image not available."
@click="e=>$emit('detail', e)"
/> />
<video <video
v-if="capturing" v-if="capturing"
@ -44,19 +45,21 @@
</template> </template>
<script> <script>
import {mapMutations} from 'vuex'; import {mapActions, mapMutations} from 'vuex';
export default { export default {
name: 'InputPhoto', name: 'InputPhoto',
props: ['model', 'field', 'onCapture'], props: ['model', 'field', 'onCapture', 'imgClass'],
data: () => ({ data: () => ({
capturing: false, capturing: false,
streaming: false, streaming: false,
stream: undefined, stream: undefined,
dataImage: undefined dataImage: undefined
}), }),
emits: ['detail'],
methods: { methods: {
...mapMutations(['createToast']), ...mapMutations(['createToast']),
...mapActions(['fetchImage']),
openStream() { openStream() {
if (!this.capturing) { if (!this.capturing) {
this.capturing = true; this.capturing = true;

View file

@ -0,0 +1,57 @@
<template>
<input
class="form-control w-100"
type="search"
placeholder="Search"
aria-label="Search"
v-model="search_query"
@keyup.enter="dispatchSearch"
>
</template>
<script>
import {mapActions, mapGetters} from "vuex";
import router from "@/router";
export default {
name: 'SearchBox',
data() {
return {
search_query: ''
}
},
computed: {
...mapGetters(['getActiveView', 'route']),
},
watch: {
route() {
this.search_query = this.route.params.search || '';
}
},
methods: {
...mapActions(['searchEventItems', 'searchEventTickets']),
isItemView() {
return this.getActiveView === 'items' || this.getActiveView === 'item' || this.getActiveView === 'item_search';
},
isTicketView() {
return this.getActiveView === 'tickets' || this.getActiveView === 'ticket' || this.getActiveView === 'ticket_search';
},
dispatchSearch() {
if (this.isItemView()) {
router.push({
name: "item_search",
query: this.route.query,
params: {...this.route.params, search: this.search_query}
});
} else if (this.isTicketView()) {
router.push({
name: "ticket_search",
query: this.route.query,
params: {...this.route.params, search: this.search_query}
});
}
}
}
};
</script>

View file

@ -20,6 +20,9 @@ export default (config) => (store) => {
} }
}); });
store.state[config.isLoadedKey] = true; store.state[config.isLoadedKey] = true;
if ('validate' in config) {
config.validate(store.state);
}
} }
const reload = initialize; const reload = initialize;

View file

@ -1,14 +1,17 @@
import {createRouter, createWebHistory} from 'vue-router' import {createRouter, createWebHistory} from 'vue-router'
import store from '@/store'; import store from '@/store';
import Items from './views/Items'; import Item from "@/views/Item.vue";
import Boxes from './views/Boxes'; import ItemSearch from "@/views/ItemSearch.vue";
import Files from './views/Files'; import Items from '@/views/Items';
import HowTo from './views/HowTo'; import Boxes from '@/views/Boxes';
import Files from '@/views/Files';
import HowTo from '@/views/HowTo';
import Login from '@/views/Login.vue'; import Login from '@/views/Login.vue';
import Register from '@/views/Register.vue'; import Register from '@/views/Register.vue';
import Debug from "@/views/admin/Debug.vue"; import Dashboard from "@/views/admin/Dashboard.vue";
import Tickets from "@/views/Tickets.vue"; import Tickets from "@/views/Tickets.vue";
import TicketSearch from "@/views/TicketSearch.vue";
import Ticket from "@/views/Ticket.vue"; import Ticket from "@/views/Ticket.vue";
import Admin from "@/views/admin/Admin.vue"; import Admin from "@/views/admin/Admin.vue";
import Empty from "@/views/Empty.vue"; import Empty from "@/views/Empty.vue";
@ -27,7 +30,11 @@ const routes = [
{requiresAuth: true, requiresPermission: 'view_item'} {requiresAuth: true, requiresPermission: 'view_item'}
}, },
{ {
path: '/:event/item/:uid/', name: 'item', component: Items, meta: path: '/:event/items/:search', name: 'item_search', component: ItemSearch, meta:
{requiresAuth: true, requiresPermission: 'view_item'}
},
{
path: '/:event/item/:id/', name: 'item', component: Item, meta:
{requiresAuth: true, requiresPermission: 'view_item'} {requiresAuth: true, requiresPermission: 'view_item'}
}, },
{ {
@ -42,6 +49,10 @@ const routes = [
path: '/:event/tickets/', name: 'tickets', component: Tickets, meta: path: '/:event/tickets/', name: 'tickets', component: Tickets, meta:
{requiresAuth: true, requiresPermission: 'view_issuethread'} {requiresAuth: true, requiresPermission: 'view_issuethread'}
}, },
{
path: '/:event/tickets/:search', name: 'ticket_search', component: TicketSearch, meta:
{requiresAuth: true, requiresPermission: 'view_issuethread'}
},
{ {
path: '/:event/ticket/:id/', name: 'ticket', component: Ticket, meta: path: '/:event/ticket/:id/', name: 'ticket', component: Ticket, meta:
{requiresAuth: true, requiresPermission: 'view_issuethread'} {requiresAuth: true, requiresPermission: 'view_issuethread'}
@ -59,7 +70,7 @@ const routes = [
{requiresAuth: true, requiresPermission: 'delete_event'} {requiresAuth: true, requiresPermission: 'delete_event'}
}, },
{ {
path: '', name: 'admin', component: Debug, meta: path: '', name: 'admin', component: Dashboard, meta:
{requiresAuth: true, requiresPermission: 'delete_event'} {requiresAuth: true, requiresPermission: 'delete_event'}
}, },
{ {

View file

@ -3,25 +3,31 @@ 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} from "@/utils"; import {ticketStateColorLookup, ticketStateIconLookup, http, http_session} 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";
const store = createStore({ const store = createStore({
state: { state: {
//shared
keyIncrement: 0, keyIncrement: 0,
events: [], events: [],
loadedItems: [], items: [],
itemCache: {},
loadedBoxes: [], loadedBoxes: [],
toasts: [], toasts: [],
tickets: [],
users: [], users: [],
groups: [], groups: [],
state_options: [], state_options: [],
shippingVouchers: [], shippingVouchers: [],
lastEvent: '37C3', loadedItems: {},
loadedTickets: {},
loadedItemSearchResults: {},
loadedTicketSearchResults: {},
//local
lastEvent: 'all',
lastUsed: {}, lastUsed: {},
remember: false, remember: false,
user: { user: {
@ -32,6 +38,7 @@ const store = createStore({
expiry: null, expiry: null,
}, },
lightboxHash: null,
thumbnailCache: {}, thumbnailCache: {},
fetchedData: { fetchedData: {
events: 0, events: 0,
@ -61,7 +68,19 @@ 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,
searchQuery: state => router.currentRoute.value.params.search,
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),
getItemsSearchResults: (state, getters) => state.loadedItemSearchResults[getters.getEventSlug + '/' + base64.encode(utf8.encode(getters.searchQuery))] || [],
getTicketsSearchResults: (state, getters) => state.loadedTicketSearchResults[getters.getEventSlug + '/' + base64.encode(utf8.encode(getters.searchQuery))] || [],
isItemsSearchLoaded: (state, getters) => Object.keys(state.loadedItemSearchResults).includes(getters.getEventSlug + '/' + base64.encode(utf8.encode(getters.searchQuery))),
isTicketsSearchLoaded: (state, getters) => Object.keys(state.loadedTicketSearchResults).includes(getters.getEventSlug + '/' + base64.encode(utf8.encode(getters.searchQuery))),
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,
@ -96,9 +115,9 @@ const store = createStore({
layout: (state, getters) => { layout: (state, getters) => {
if (router.currentRoute.value.query.layout) if (router.currentRoute.value.query.layout)
return router.currentRoute.value.query.layout; return router.currentRoute.value.query.layout;
if (getters.getActiveView === 'items') if (getters.getActiveView === 'items' || getters.getActiveView === 'item_search')
return 'cards'; return 'cards';
if (getters.getActiveView === 'tickets') if (getters.getActiveView === 'tickets' || getters.getActiveView === 'ticket_search')
return 'tasks'; return 'tasks';
}, },
isLoggedIn(state) { isLoggedIn(state) {
@ -129,35 +148,52 @@ 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};
},
setItemSearchResults(state, {slug, query, items}) {
state.loadedItemSearchResults[slug + '/' + query] = items;
state.loadedItemSearchResults = {...state.loadedItemSearchResults};
},
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};
},
updateItem(state, updatedItem) { updateItem(state, updatedItem) {
const item = state.loadedItems.filter(({uid}) => uid === updatedItem.uid)[0]; const item = state.loadedItems[updatedItem.event ? updatedItem.event : 'none'].filter(
({id}) => id === updatedItem.id)[0];
Object.assign(item, updatedItem); Object.assign(item, updatedItem);
}, },
removeItem(state, item) { removeItem(state, item) {
state.loadedItems = state.loadedItems.filter(it => it !== item); state.loadedItems[item.event ? item.event : 'none'] = state.loadedItems[item.event].filter(it => it !== item);
}, },
appendItem(state, item) { appendItem(state, item) {
state.loadedItems.push(item); state.loadedItems[item.event ? item.event : 'none'].push(item);
},
setTickets(state, {slug, tickets}) {
state.loadedTickets[slug] = tickets;
state.loadedTickets = {...state.loadedTickets};
},
setTicketSearchResults(state, {slug, query, items}) {
state.loadedTicketSearchResults[slug + '/' + query] = items;
state.loadedTicketSearchResults = {...state.loadedTicketSearchResults};
}, },
replaceTickets(state, tickets) { replaceTickets(state, tickets) {
state.tickets = tickets; const groups = Object.groupBy(tickets, t => t.event ? t.event : 'none')
state.fetchedData = {...state.fetchedData, tickets: Date.now()}; for (const [key, value] of Object.entries(groups)) state.loadedTickets[key] = value;
state.loadedTickets = {...state.loadedTickets};
}, },
updateTicket(state, updatedTicket) { updateTicket(state, updatedTicket) {
const ticket = state.tickets.filter(({id}) => id === updatedTicket.id)[0]; const ticket = state.loadedTickets[updatedTicket.event ? updatedTicket.event : 'none'].filter(
({id}) => id === updatedTicket.id)[0];
Object.assign(ticket, updatedTicket); Object.assign(ticket, updatedTicket);
state.tickets = [...state.tickets]; state.loadedTickets = {...state.loadedTickets};
}, },
replaceUsers(state, users) { replaceUsers(state, users) {
state.users = users; state.users = users;
@ -167,6 +203,9 @@ const store = createStore({
state.groups = groups; state.groups = groups;
state.fetchedData = {...state.fetchedData, groups: Date.now()}; state.fetchedData = {...state.fetchedData, groups: Date.now()};
}, },
openLightboxModalWith(state, hash) {
state.lightboxHash = hash;
},
openAddBoxModal(state) { openAddBoxModal(state) {
state.showAddBoxModal = true; state.showAddBoxModal = true;
}, },
@ -248,7 +287,7 @@ const store = createStore({
return false; return false;
} }
}, },
async reloadToken({commit, state, getters}) { async reloadToken({commit, state}) {
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/', {
@ -296,183 +335,194 @@ 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, state}) { async loadUserInfo({commit, getters}) {
const {data, success} = await http.get('/2/self/', state.user.token); const {data, success} = await getters.session.get('/2/self/');
commit('setPermissions', data.permissions); commit('setPermissions', data.permissions);
}, },
async loadEvents({commit, state}) { async loadEvents({commit, state, getters}) {
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 http.get('/2/events/', state.user.token); const {data, success} = await getters.session.get('/2/events/');
if (data && success) if (data && success) commit('replaceEvents', data);
commit('replaceEvents', data);
}, },
async createEvent({commit, dispatch, state}, event) { async createEvent({commit, dispatch, state, getters}, event) {
const {data, success} = await http.post('/2/events/', event, state.user.token); const {data, success} = await getters.session.post('/2/events/', event);
if (data && success) if (data && success) commit('replaceEvents', [...state.events, data]);
commit('replaceEvents', [...state.events, data]);
}, },
async deleteEvent({commit, dispatch, state}, event_id) { async deleteEvent({commit, dispatch, state, getters}, event_id) {
const {data, success} = await http.delete(`/2/events/${event_id}/`, state.user.token); const {data, success} = await getters.session.delete(`/2/events/${event_id}/`);
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.id !== event_id)])
} }
}, },
async fetchTicketStates({commit, state}) { async updateEvent({commit, dispatch, state}, {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.id !== 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 http.get('/2/tickets/states/', state.user.token); const {data, success} = await getters.session.get('/2/tickets/states/');
if (data && success) if (data && success) commit('replaceTicketStates', data);
commit('replaceTicketStates', data);
}, },
changeEvent({dispatch, getters, commit}, eventName) { async changeEvent({dispatch, getters, commit}, eventName) {
router.push({path: `/${eventName.slug}/${getters.getActiveView}/`}); await router.push({path: `/${eventName.slug}/${getters.getActiveView}/`});
dispatch('loadEventItems');
}, },
changeView({getters}, link) { async changeView({getters}, link) {
router.push({path: `/${getters.getEventSlug}/${link.path}/`}); await router.push({path: `/${getters.getEventSlug}/${link.path}/`});
}, },
showBoxContent({getters}, box) { async showBoxContent({getters}, box) {
router.push({path: `/${getters.getEventSlug}/items/`, query: {box}}); await 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;
if (slug in state.itemCache) { const {data, success} = await getters.session.get(`/2/${slug}/items/`);
commit('replaceLoadedItems', state.itemCache[slug]);
}
const {data, success} = await http.get(`/2/${slug}/items/`, state.user.token);
if (data && success) { if (data && success) {
commit('replaceLoadedItems', data); commit('setItems', {slug, items: data});
commit('setItemCache', {slug, items: data});
} }
} catch (e) { } catch (e) {
console.error("Error loading items"); console.error("Error loading items");
} }
}, },
async searchEventItems({commit, getters, state}, query) { async searchEventItems({commit, getters, state}, query) {
const foo = utf8.encode(query); const encoded_query = base64.encode(utf8.encode(query));
const bar = base64.encode(foo); const slug = getters.getEventSlug;
if (Object.keys(state.loadedItemSearchResults).includes(slug + '/' + encoded_query)) return;
const {data, success} = await http.get(`/2/${getters.getEventSlug}/items/${bar}/`, state.user.token); const {
if (data && success) data, success
commit('replaceLoadedItems', data); } = await getters.session.get(`/2/${slug}/items/${encoded_query}/`);
if (data && success) {
commit('setItemSearchResults', {slug, query: encoded_query, items: data});
}
}, },
async loadBoxes({commit, state}) { async loadBoxes({commit, state, getters}) {
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 http.get('/2/boxes/', state.user.token); const {data, success} = await getters.session.get('/2/boxes/');
if (data && success) if (data && success) commit('replaceBoxes', data);
commit('replaceBoxes', data);
}, },
async createBox({commit, dispatch, state}, box) { async createBox({commit, dispatch, state, getters}, box) {
const {data, success} = await http.post('/2/boxes/', box, state.user.token); const {data, success} = await getters.session.post('/2/boxes/', box);
commit('replaceBoxes', data); commit('replaceBoxes', data);
dispatch('loadBoxes').then(() => { dispatch('loadBoxes').then(() => {
commit('closeAddBoxModal'); commit('closeAddBoxModal');
}); });
}, },
async deleteBox({commit, dispatch, state}, box_id) { async deleteBox({commit, dispatch, state, getters}, box_id) {
await http.delete(`/2/boxes/${box_id}/`, state.user.token); await getters.session.delete(`/2/boxes/${box_id}/`);
dispatch('loadBoxes'); dispatch('loadBoxes');
}, },
async updateItem({commit, getters, state}, item) { async updateItem({commit, getters, state}, item) {
const { const {
data, data, success
success } = await getters.session.put(`/2/${getters.getEventSlug}/item/${item.id}/`, item);
} = 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 http.patch(`/2/${getters.getEventSlug}/item/${item.uid}/`, {returned: true}, state.user.token); await getters.session.patch(`/2/${getters.getEventSlug}/item/${item.id}/`, {returned: true},
state.user.token);
commit('removeItem', item); commit('removeItem', item);
}, },
async deleteItem({commit, getters, state}, item) { async deleteItem({commit, getters, state}, item) {
await http.delete(`/2/${getters.getEventSlug}/item/${item.uid}/`, item, state.user.token); await getters.session.delete(`/2/${getters.getEventSlug}/item/${item.id}/`, item);
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 http.post(`/2/${getters.getEventSlug}/item/`, item, state.user.token); const {data, success} = await getters.session.post(`/2/${getters.getEventSlug}/item/`, item);
commit('appendItem', data); commit('appendItem', data);
}, },
async loadTickets({commit, state}) { async loadTickets({commit, state, getters}) {
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 http.get('/2/tickets/', state.user.token); const {data, success} = await getters.session.get('/2/tickets/');
if (data && success) if (data && success) commit('replaceTickets', data);
commit('replaceTickets', data);
}, },
async sendMail({commit, dispatch, state}, {id, message}) { async searchEventTickets({commit, getters, state}, query) {
const {data, success} = await http.post(`/2/tickets/${id}/reply/`, {message}, state.user.token); const encoded_query = base64.encode(utf8.encode(query));
const slug = getters.getEventSlug;
if (Object.keys(state.loadedTicketSearchResults).includes(slug + '/' + encoded_query)) return;
const {
data, success
} = await getters.session.get(`/2/${slug}/tickets/${encoded_query}/`);
if (data && success) commit('setTicketSearchResults', {slug, query: encoded_query, items: data});
},
async sendMail({commit, dispatch, state, getters}, {id, message}) {
const {data, success} = await getters.session.post(`/2/tickets/${id}/reply/`, {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 postManualTicket({commit, dispatch, state}, {sender, message, title,}) { async postManualTicket({commit, dispatch, state, getters}, {sender, message, title,}) {
const {data, success} = await http.post(`/2/tickets/manual/`, { const slug = getters.getEventSlug;
name: title, const {data, success} = await getters.session.post(`/2/${slug !== 'all' ? slug : 'none'}/tickets/manual/`, {
sender, name: title, sender, body: message, recipient: 'mail@c3lf.de'
body: message, });
recipient: 'mail@c3lf.de'
}, state.user.token);
await dispatch('loadTickets'); await dispatch('loadTickets');
}, },
async postComment({commit, dispatch, state}, {id, message}) { async postComment({commit, dispatch, state, getters}, {id, message}) {
const {data, success} = await http.post(`/2/tickets/${id}/comment/`, {comment: message}, state.user.token); const {data, success} = await getters.session.post(`/2/tickets/${id}/comment/`, {comment: message});
if (data && success) { if (data && success) {
state.fetchedData.tickets = 0; state.fetchedData.tickets = 0;
await dispatch('loadTickets'); await dispatch('loadTickets');
} }
}, },
async loadUsers({commit, state}) { async postItemComment({commit, dispatch, state, getters}, {id, message}) {
const {
data,
success
} = await getters.session.post(`/2/${getters.getEventSlug}/item/${id}/comment/`, {comment: message});
if (data && success) {
state.fetchedData.items = 0;
await dispatch('loadEventItems');
}
},
async loadUsers({commit, state, getters}) {
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 http.get('/2/users/', state.user.token); const {data, success} = await getters.session.get('/2/users/');
if (data && success) if (data && success) commit('replaceUsers', data);
commit('replaceUsers', data);
}, },
async loadGroups({commit, state}) { async loadGroups({commit, state, getters}) {
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 http.get('/2/groups/', state.user.token); const {data, success} = await getters.session.get('/2/groups/');
if (data && success) if (data && success) commit('replaceGroups', data);
commit('replaceGroups', data);
}, },
async updateTicket({commit, state}, ticket) { async updateTicket({commit, state, getters}, ticket) {
const {data, success} = await http.put(`/2/tickets/${ticket.id}/`, ticket, state.user.token); const {data, success} = await getters.session.put(`/2/tickets/${ticket.id}/`, ticket);
commit('updateTicket', data); commit('updateTicket', data);
}, },
async updateTicketPartial({commit, state}, {id, ...ticket}) { async updateTicketPartial({commit, state, getters}, {id, ...ticket}) {
const {data, success} = await http.patch(`/2/tickets/${id}/`, ticket, state.user.token); const {data, success} = await getters.session.patch(`/2/tickets/${id}/`, ticket);
commit('updateTicket', data); commit('updateTicket', data);
}, },
async fetchShippingVouchers({commit, state}) { async fetchShippingVouchers({commit, state, getters}) {
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 http.get('/2/shipping_vouchers/', state.user.token); const {data, success} = await getters.session.get('/2/shipping_vouchers/');
if (data && success) { if (data && success) {
commit('setShippingVouchers', data); commit('setShippingVouchers', data);
} }
}, },
async createShippingVoucher({dispatch, state}, code) { async createShippingVoucher({dispatch, state, getters}, code) {
const {data, success} = await http.post('/2/shipping_vouchers/', code, state.user.token); const {data, success} = await getters.session.post('/2/shipping_vouchers/', code);
if (data && success) { if (data && success) {
state.fetchedData.shippingVouchers = 0; state.fetchedData.shippingVouchers = 0;
dispatch('fetchShippingVouchers'); dispatch('fetchShippingVouchers');
} }
}, },
async claimShippingVoucher({dispatch, state}, {ticket, shipping_voucher_type}) { async claimShippingVoucher({dispatch, state, getters}, {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 { const {data, success} = await getters.session.patch(`/2/shipping_vouchers/${id}/`, {issue_thread: ticket});
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;
@ -480,58 +530,39 @@ const store = createStore({
} }
} }
}, },
plugins: [ plugins: [persistentStatePlugin({ // TODO change remember to some kind of enable field
persistentStatePlugin({ // TODO change remember to some kind of enable field prefix: "lf_",
prefix: "lf_", debug: false,
debug: false, isLoadedKey: "persistent_loaded",
isLoadedKey: "persistent_loaded", validate: (state) => {
state: [ if (state.user && state.user.expiry && state.user.token) {
"remember", const as_date = new Date(state.user.expiry);
"user", if (as_date < new Date()) {
"events", state.user.token = null;
"lastUsed", state.user.expiry = null;
] }
}), }
sharedStatePlugin({ },
debug: false, state: ["remember", "user", "events", "lastUsed",]
isLoadedKey: "shared_loaded", }), sharedStatePlugin({
clearingMutation: "logout", debug: false,
afterInit: "afterSharedInit", isLoadedKey: "shared_loaded",
state: [ clearingMutation: "logout",
"test", afterInit: "afterSharedInit",
"state_options", state: ["test", "state_options", "fetchedData", "loadedItems", "users", "groups", "loadedBoxes", "loadedTickets", "shippingVouchers",],
"fetchedData", watch: ["test", "state_options", "fetchedData", "loadedItems", "users", "groups", "loadedBoxes", "loadedTickets", "shippingVouchers",],
"tickets", mutations: [//"replaceTickets",
"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({

View file

@ -1,3 +1,5 @@
import store from '@/store'
function ticketStateColorLookup(ticket) { function ticketStateColorLookup(ticket) {
if (ticket.startsWith('closed_')) { if (ticket.startsWith('closed_')) {
return 'secondary'; return 'secondary';
@ -36,6 +38,8 @@ const http = {
"Authorization": `Token ${token}`, "Authorization": `Token ${token}`,
}, },
}); });
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201; const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success}; return {data: await response.json() || {}, success};
}, },
@ -51,6 +55,8 @@ const http = {
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201; const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success}; return {data: await response.json() || {}, success};
}, },
@ -66,6 +72,8 @@ const http = {
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201; const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success}; return {data: await response.json() || {}, success};
}, },
@ -81,6 +89,8 @@ const http = {
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 200 || response.status === 201; const success = response.status === 200 || response.status === 201;
return {data: await response.json() || {}, success}; return {data: await response.json() || {}, success};
}, },
@ -95,9 +105,34 @@ const http = {
"Authorization": `Token ${token}`, "Authorization": `Token ${token}`,
}, },
}); });
if (response.status === 401)
throw {http_status: response.status};
const success = response.status === 204; const success = response.status === 204;
return {data: await response.text() || {}, success}; return {data: await response.text() || {}, success};
} }
} }
export {ticketStateColorLookup, ticketStateIconLookup, http}; const http_session = token => ({
get: async (url) => await http.get(url, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
post: async (url, data) => await http.post(url, data, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
put: async (url, data) => await http.put(url, data, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
patch: async (url, data) => await http.patch(url, data, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
delete: async (url) => await http.delete(url, token).catch((e) => {
if (e.http_status === 401) store.commit('logout');
return {data: {}, success: false};
}),
});
export {ticketStateColorLookup, ticketStateIconLookup, http, http_session};

184
web/src/views/Item.vue Normal file
View file

@ -0,0 +1,184 @@
<template>
<AsyncLoader :loaded="!!item.id">
<div class="container-fluid px-xl-5 mt-3">
<div class="row">
<div class="col-lg-3 col-xl-2">
<div class="card bg-dark text-light mb-2" id="filters">
<div class="card bg-dark">
<InputPhoto
v-if="!!editingItem"
:model="editingItem"
field="file"
:on-capture="storeImage"
imgClass="d-block card-img"
/>
<div class="card-body">
<h6 class="card-subtitle text-secondary">id: {{ item.id }} box: {{ item.box }}</h6>
<h6 class="card-title">{{ item.description }}</h6>
</div>
<div class="card-footer">
<InputString
v-if="!!editingItem"
label="description"
:model="editingItem"
field="description"
:validation-fn="str => str && str.length > 0"
/>
</div>
</div>
</div>
</div>
<div class="col-xl-8">
<div class="card bg-dark text-light mb-2" id="filters">
<div class="card-header">
<h3>Item #{{ item.id }} - {{ item.description }}</h3>
</div>
<Timeline :timeline="item.timeline">
<template v-slot:timeline_action1>
<span class="timeline-item-icon | faded-icon">
<font-awesome-icon icon="comment"/>
</span>
<div class="new-comment card bg-dark">
<div class="">
<textarea placeholder="add comment..." v-model="newComment"
class="form-control">
</textarea>
<AsyncButton class="btn btn-primary float-right" :task="addCommentAndClear">
<font-awesome-icon icon="comment"/>
Save Comment
</AsyncButton>
</div>
</div>
</template>
</Timeline>
<div class="card-footer d-flex justify-content-between">
<div class="btn-group">
<button class="btn btn-outline-success"
@click.stop="confirm('return Item?') && markItemReturnedAndClose(item)"
title="returned">
<font-awesome-icon icon="check"/>&nbsp;mark&nbsp;returned
</button>
<button class="btn btn-outline-danger"
@click.stop="confirm('delete Item?') && deleteItemAndClose(item)"
title="delete">
<font-awesome-icon icon="trash"/>&nbsp;delete
</button>
</div>
<InputCombo
v-if="!!editingItem"
label="box"
:model="editingItem"
nameKey="box"
uniqueKey="cid"
:options="boxes"
style="width: auto;"
/>
<button type="button" class="btn btn-success" @click="saveEditingItem()">Save Changes
</button>
</div>
</div>
</div>
<div class="col-lg-3 col-xl-2" v-if="item.related_issues && item.related_issues.length">
<div class="card bg-dark text-light mb-2" id="filters">
<div class="card-body">
<h5 class="card-title text-info">Related</h5>
</div>
<div class="card bg-dark" v-for="issue in item.related_issues" v-bind:key="item.id">
<div class="card-body">
<router-link :to="{name: 'ticket', params: {id: issue.id}}">
<h6 class="card-title">Ticket #{{ issue.id }} - {{ issue.name }}</h6>
</router-link>
<h6 class="card-subtitle text-secondary">state: {{ issue.state }}</h6>
</div>
</div>
</div>
</div>
</div>
</div>
</AsyncLoader>
</template>
<script>
import {mapActions, mapGetters, mapMutations, mapState} from 'vuex';
import router from "@/router";
import Timeline from "@/components/Timeline.vue";
import ClipboardButton from "@/components/inputs/ClipboardButton.vue";
import AsyncLoader from "@/components/AsyncLoader.vue";
import InputCombo from "@/components/inputs/InputCombo.vue";
import InputString from "@/components/inputs/InputString.vue";
import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
import InputPhoto from "@/components/inputs/InputPhoto.vue";
import Modal from "@/components/Modal.vue";
import AsyncButton from "@/components/inputs/AsyncButton.vue";
export default {
name: 'Item',
components: {
AsyncButton,
Modal, InputPhoto, AuthenticatedImage, InputString, InputCombo, AsyncLoader, ClipboardButton, Timeline
},
data() {
return {
newComment: "",
editingItem: null,
}
},
computed: {
...mapState(['state_options', 'users']),
...mapGetters(['availableShippingVoucherTypes', 'getAllItems', 'route', 'getBoxes']),
item() {
const id = parseInt(this.route.params.id)
const ret = this.getAllItems.find(item => item.id === id);
return ret ? ret : {};
},
boxes() {
return this.getBoxes.map(obj => ({cid: obj.id, box: obj.name}));
}
},
methods: {
...mapActions(['deleteItem', 'markItemReturned', 'updateTicketPartial', 'postItemComment']),
...mapActions(['loadTickets', 'fetchTicketStates', 'loadUsers', 'scheduleAfterInit', 'updateItem']),
...mapActions(['claimShippingVoucher', 'fetchShippingVouchers', 'loadEventItems', 'loadBoxes']),
...mapMutations(['openLightboxModalWith']),
async addCommentAndClear() {
await this.postItemComment({
id: this.item.id,
message: this.newComment
})
this.newComment = "";
},
async saveEditingItem() { // Saves the edited copy of the item.
await this.updateItem(this.editingItem);
this.editingItem = {...this.item}
},
storeImage(image) {
this.editingItem.dataImage = image;
},
confirm(message) {
return window.confirm(message);
},
async markItemReturnedAndClose(item) {
await this.markItemReturned(item);
router.back();
},
async deleteItemAndClose(item) {
await this.deleteItem(item);
router.back();
}
},
mounted() {
this.scheduleAfterInit(() => [Promise.all([this.loadEventItems(), this.loadBoxes()]).then(() => {
this.selected_state = this.item.state;
this.selected_assignee = this.item.assigned_to
this.editingItem = {...this.item}
})]);
}
};
</script>
<style scoped>
</style>

View file

@ -0,0 +1,114 @@
<template>
<AsyncLoader :loaded="isItemsSearchLoaded">
<div class="container-fluid px-xl-5 mt-3">
<div class="row" v-if="layout === 'table'">
<div class="col-xl-8 offset-xl-2">
<Table
:columns="['id', 'description', 'box']"
:items="getItemsSearchResults"
:keyName="'id'"
@itemActivated="showItemDetail"
>
<template #actions="{ item }">
<div class="btn-group">
<button class="btn btn-success"
@click.stop="confirm('return Item?') && markItemReturned(item)"
title="returned">
<font-awesome-icon icon="check"/>
</button>
<button class="btn btn-secondary" @click.stop="openEditingModalWith(item)" title="edit">
<font-awesome-icon icon="edit"/>
</button>
<button class="btn btn-danger" @click.stop="confirm('delete Item?') && deleteItem(item)"
title="delete">
<font-awesome-icon icon="trash"/>
</button>
</div>
</template>
</Table>
</div>
</div>
<Cards
v-if="layout === 'cards'"
:columns="['id', 'description', 'box']"
:items="getItemsSearchResults"
:keyName="'id'"
v-slot="{ item }"
@itemActivated="item => openLightboxModalWith(item.file)"
>
<AuthenticatedImage v-if="item.file" cached
:src="`/media/2/256/${item.file}/`"
class="card-img-top img-fluid"
/>
<div class="card-body">
<h6 class="card-title">{{ item.description }}</h6>
<h6 class="card-subtitle text-secondary">id: {{ item.id }} box: {{ item.box }}</h6>
<div class="row mx-auto mt-2">
<div class="btn-group">
<button class="btn btn-outline-success"
@click.stop="confirm('return Item?') && markItemReturned(item)" title="returned">
<font-awesome-icon icon="check"/>
</button>
<button class="btn btn-outline-secondary" @click.stop="showItemDetail(item)"
title="edit">
<font-awesome-icon icon="edit"/>
</button>
<button class="btn btn-outline-danger"
@click.stop="confirm('delete Item?') && deleteItem(item)"
title="delete">
<font-awesome-icon icon="trash"/>
</button>
</div>
</div>
</div>
</Cards>
</div>
</AsyncLoader>
</template>
<script>
import {mapActions, mapGetters, mapMutations, mapState} from 'vuex';
import Table from '@/components/Table';
import Cards from '@/components/Cards';
import Modal from '@/components/Modal';
import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
import AsyncLoader from "@/components/AsyncLoader.vue";
import router from "@/router";
export default {
name: 'Items',
data: () => ({
lightboxHash: null,
editingItem: null,
}),
components: {AsyncLoader, AuthenticatedImage, Table, Cards, Modal},
computed: {
...mapGetters(['getItemsSearchResults', 'isItemsSearchLoaded', 'layout', 'getEventSlug', 'searchQuery']),
},
methods: {
...mapActions(['deleteItem', 'markItemReturned', 'searchEventItems', 'updateItem', 'scheduleAfterInit']),
...mapMutations(['openLightboxModalWith']),
showItemDetail(item) {
router.push({name: 'item', params: {id: item.id}});
},
confirm(message) {
return window.confirm(message);
}
},
watch: {
searchQuery() {
this.scheduleAfterInit(() => [this.searchEventItems(this.searchQuery)]);
},
getEventSlug() {
this.scheduleAfterInit(() => [this.searchEventItems(this.searchQuery)]);
}
},
mounted() {
this.scheduleAfterInit(() => [this.searchEventItems(this.searchQuery)]);
}
};
</script>
<style scoped>
</style>

View file

@ -1,26 +1,13 @@
<template> <template>
<AsyncLoader :loaded="loadedItems.length > 0"> <AsyncLoader :loaded="isItemsLoaded">
<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()">
<template #body>
<EditItem
:item="editingItem"
badge="uid"
/>
</template>
<template #buttons>
<button type="button" class="btn btn-secondary" @click="closeEditingModal()">Cancel</button>
<button type="button" class="btn btn-success" @click="saveEditingItem()">Save Changes</button>
</template>
</Modal>
<Lightbox v-if="lightboxHash" :hash="lightboxHash" @close="closeLightboxModal()"/>
<div class="row" v-if="layout === 'table'"> <div class="row" v-if="layout === 'table'">
<div class="col-xl-8 offset-xl-2"> <div class="col-xl-8 offset-xl-2">
<Table <Table
:columns="['uid', 'description', 'box']" :columns="['id', 'description', 'box']"
:items="loadedItems" :items="getEventItems"
:keyName="'uid'" :keyName="'id'"
@itemActivated="openLightboxModalWith($event)" @itemActivated="showItemDetail"
> >
<template #actions="{ item }"> <template #actions="{ item }">
<div class="btn-group"> <div class="btn-group">
@ -43,11 +30,11 @@
</div> </div>
<Cards <Cards
v-if="layout === 'cards'" v-if="layout === 'cards'"
:columns="['uid', 'description', 'box']" :columns="['id', 'description', 'box']"
:items="loadedItems" :items="getEventItems"
:keyName="'uid'" :keyName="'id'"
v-slot="{ item }" v-slot="{ item }"
@itemActivated="openLightboxModalWith($event)" @itemActivated="item => openLightboxModalWith(item.file)"
> >
<AuthenticatedImage v-if="item.file" cached <AuthenticatedImage v-if="item.file" cached
:src="`/media/2/256/${item.file}/`" :src="`/media/2/256/${item.file}/`"
@ -55,14 +42,14 @@
/> />
<div class="card-body"> <div class="card-body">
<h6 class="card-title">{{ item.description }}</h6> <h6 class="card-title">{{ item.description }}</h6>
<h6 class="card-subtitle text-secondary">uid: {{ item.uid }} box: {{ item.box }}</h6> <h6 class="card-subtitle text-secondary">id: {{ item.id }} box: {{ item.box }}</h6>
<div class="row mx-auto mt-2"> <div class="row mx-auto mt-2">
<div class="btn-group"> <div class="btn-group">
<button class="btn btn-outline-success" <button class="btn btn-outline-success"
@click.stop="confirm('return Item?') && markItemReturned(item)" title="returned"> @click.stop="confirm('return Item?') && markItemReturned(item)" title="returned">
<font-awesome-icon icon="check"/> <font-awesome-icon icon="check"/>
</button> </button>
<button class="btn btn-outline-secondary" @click.stop="openEditingModalWith(item)" <button class="btn btn-outline-secondary" @click.stop="showItemDetail(item)"
title="edit"> title="edit">
<font-awesome-icon icon="edit"/> <font-awesome-icon icon="edit"/>
</button> </button>
@ -80,14 +67,13 @@
</template> </template>
<script> <script>
import {mapActions, mapGetters, mapMutations, mapState} from 'vuex';
import Table from '@/components/Table'; import Table from '@/components/Table';
import Cards from '@/components/Cards'; import Cards from '@/components/Cards';
import Modal from '@/components/Modal'; import Modal from '@/components/Modal';
import EditItem from '@/components/EditItem';
import {mapActions, mapGetters, mapState} from 'vuex';
import Lightbox from '../components/Lightbox';
import AuthenticatedImage from "@/components/AuthenticatedImage.vue"; import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
import AsyncLoader from "@/components/AsyncLoader.vue"; import AsyncLoader from "@/components/AsyncLoader.vue";
import router from "@/router";
export default { export default {
name: 'Items', name: 'Items',
@ -95,33 +81,25 @@ export default {
lightboxHash: null, lightboxHash: null,
editingItem: null, editingItem: null,
}), }),
components: {AsyncLoader, AuthenticatedImage, Lightbox, Table, Cards, Modal, EditItem}, components: {AsyncLoader, AuthenticatedImage, Table, Cards, Modal},
computed: { computed: {
...mapState(['loadedItems']), ...mapGetters(['getEventItems', 'isItemsLoaded', 'layout', 'getEventSlug']),
...mapGetters(['layout']),
}, },
methods: { methods: {
...mapActions(['deleteItem', 'markItemReturned', 'loadEventItems', 'updateItem', 'scheduleAfterInit']), ...mapActions(['deleteItem', 'markItemReturned', 'loadEventItems', 'updateItem', 'scheduleAfterInit']),
openLightboxModalWith(item) { ...mapMutations(['openLightboxModalWith']),
this.lightboxHash = item.file; showItemDetail(item) {
}, router.push({name: 'item', params: {id: item.id}});
closeLightboxModal() { // Closes the editing modal and discards the edited copy of the item.
this.lightboxHash = null;
},
openEditingModalWith(item) { // Opens the editing modal with a copy of the selected item.
this.editingItem = item;
},
closeEditingModal() {
this.editingItem = null;
},
saveEditingItem() { // Saves the edited copy of the item.
this.updateItem(this.editingItem);
this.closeEditingModal();
}, },
confirm(message) { confirm(message) {
return window.confirm(message); return window.confirm(message);
} }
}, },
watch: {
getEventSlug() {
this.scheduleAfterInit(() => [this.loadEventItems()]);
}
},
mounted() { mounted() {
this.scheduleAfterInit(() => [this.loadEventItems()]); this.scheduleAfterInit(() => [this.loadEventItems()]);
} }

View file

@ -72,7 +72,7 @@ export default {
name: 'Login', name: 'Login',
data() { data() {
return { return {
msg: 'Welcome to ' + window.location.hostname, msg: 'Lost&Found Team Login',
username: '', username: '',
password: '', password: '',
remember: false remember: false

View file

@ -87,7 +87,7 @@ export default {
name: 'Register', name: 'Register',
data() { data() {
return { return {
msg: 'Register', msg: 'Register as team member',
password2: '', password2: '',
form: { form: {
username: '', username: '',

View file

@ -1,5 +1,5 @@
<template> <template>
<AsyncLoader :loaded="ticket.id"> <AsyncLoader :loaded="!!ticket.id">
<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">
@ -7,7 +7,42 @@
<div class="card-header"> <div class="card-header">
<h3>Ticket #{{ ticket.id }} - {{ ticket.name }}</h3> <h3>Ticket #{{ ticket.id }} - {{ ticket.name }}</h3>
</div> </div>
<Timeline :timeline="ticket.timeline" @sendMail="handleMail" @addComment="handleComment"/> <Timeline :timeline="ticket.timeline">
<template v-slot:timeline_action1>
<span class="timeline-item-icon | faded-icon">
<font-awesome-icon icon="comment"/>
</span>
<div class="new-comment card bg-dark">
<div>
<textarea placeholder="add comment..." v-model="newComment"
class="form-control">
</textarea>
<AsyncButton class="btn btn-primary float-right" :task="addCommentAndClear">
<font-awesome-icon icon="comment"/>
Save Comment
</AsyncButton>
</div>
</div>
</template>
<template v-slot:timeline_action2>
<span class="timeline-item-icon | faded-icon">
<font-awesome-icon icon="envelope"/>
</span>
<div class="new-mail card bg-dark">
<div class="card-header">
{{ newestMailSubject }}
</div>
<div>
<textarea placeholder="reply mail..." v-model="newMail" class="form-control">
</textarea>
<AsyncButton class="btn btn-primary float-right" :task="sendMailAndClear">
<font-awesome-icon icon="envelope"/>
Send Mail
</AsyncButton>
</div>
</div>
</template>
</Timeline>
<div class="card-footer d-flex justify-content-between"> <div class="card-footer d-flex justify-content-between">
<button class="btn btn-secondary mr-2" @click="$router.go(-1)">Back</button> <button class="btn btn-secondary mr-2" @click="$router.go(-1)">Back</button>
<!--button class="btn btn-danger" @click="deleteItem({type: 'tickets', id: ticket.id})"> <!--button class="btn btn-danger" @click="deleteItem({type: 'tickets', id: ticket.id})">
@ -16,11 +51,14 @@
</button--> </button-->
<div class="btn-group"> <div class="btn-group">
<select class="form-control" v-model="selected_assignee"> <select class="form-control" v-model="selected_assignee">
<option v-for="user in users" :value="user.username">{{ user.username }}</option> <option v-for="user in users" :value="user.username">{{
user.username
}}
</option>
</select> </select>
<button class="form-control btn btn-success" <button class="form-control btn btn-success"
@click="assignTicket(ticket)" @click="assignTicket(ticket)"
:disabled="!selected_assignee || (selected_assignee == ticket.assigned_to)"> :disabled="!selected_assignee || (selected_assignee === ticket.assigned_to)">
Assign&nbsp;Ticket Assign&nbsp;Ticket
</button> </button>
</div> </div>
@ -33,7 +71,7 @@
</select> </select>
<button class="form-control btn btn-success" <button class="form-control btn btn-success"
@click="changeTicketStatus(ticket)" @click="changeTicketStatus(ticket)"
:disabled="(selected_state == ticket.state)"> :disabled="(selected_state === ticket.state)">
Change&nbsp;Status Change&nbsp;Status
</button> </button>
</div> </div>
@ -58,88 +96,119 @@
</div> </div>
</div> </div>
</div> </div>
<div class="col-lg-3 col-xl-2 d-lg-none d-xl-block"
v-if="ticket.related_items && ticket.related_items.length">
<div class="card bg-dark text-light mb-2" id="filters">
<div class="card-body">
<h5 class="card-title text-info">Related</h5>
<div class="card bg-dark" v-for="item in ticket.related_items" v-bind:key="item.id">
<AuthenticatedImage v-if="item.file" cached
:src="`/media/2/256/${item.file}/`"
class="d-block card-img"
@click="openLightboxModalWith(item.file)"
/>
<div class="card-body">
<!--h6 class="card-title text-info"><span class="badge badge-primary">{{ item.relation_status }}</span></--h6-->
<h6 class="card-subtitle text-secondary">id: {{ item.id }} box: {{
item.box
}}</h6>
<router-link :to="{name: 'item', params: {id: item.id}}">
<h6 class="card-title">{{ item.description }}</h6>
</router-link>
</div>
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
</AsyncLoader> </AsyncLoader>
</template> </template>
<script> <script>
import {mapActions, mapGetters, mapState} from 'vuex'; import {mapActions, mapGetters, mapMutations, mapState} from 'vuex';
import Timeline from "@/components/Timeline.vue"; import Timeline from "@/components/Timeline.vue";
import ClipboardButton from "@/components/inputs/ClipboardButton.vue"; import ClipboardButton from "@/components/inputs/ClipboardButton.vue";
import AsyncLoader from "@/components/AsyncLoader.vue"; import AsyncLoader from "@/components/AsyncLoader.vue";
import AuthenticatedImage from "@/components/AuthenticatedImage.vue";
import AsyncButton from "@/components/inputs/AsyncButton.vue";
export default { export default {
name: 'Ticket', name: 'Ticket',
components: {AsyncLoader, ClipboardButton, Timeline}, components: {AsyncButton, AuthenticatedImage, AsyncLoader, ClipboardButton, Timeline},
data() { data() {
return { return {
selected_state: null, selected_state: null,
selected_assignee: null, selected_assignee: null,
shipping_voucher_type: null, shipping_voucher_type: null,
} newMail: "",
}, newComment: ""
watch: {
ticket(val) {
if (this.selected_state == null) {
this.selected_state = val.state;
}
if (this.selected_assignee == null) {
this.selected_assignee = val.assigned_to
}
} }
}, },
computed: { computed: {
...mapState(['tickets', 'state_options', 'users']), ...mapState(['state_options', 'users']),
...mapGetters(['availableShippingVoucherTypes']), ...mapGetters(['availableShippingVoucherTypes', 'getAllTickets', 'route']),
ticket() { ticket() {
const id = parseInt(this.$route.params.id) const id = parseInt(this.route.params.id)
const ret = this.tickets.find(ticket => ticket.id === id); const ret = this.getAllTickets.find(ticket => ticket.id === id);
return ret ? ret : {}; return ret ? ret : {};
}, },
shippingEmail() { shippingEmail() {
const domain = document.location.hostname; const domain = document.location.hostname;
return `ticket+${this.ticket.uuid}@${domain}`; return `ticket+${this.ticket.uuid}@${domain}`;
} },
newestMailSubject() {
const mail = this.ticket.timeline ? this.ticket.timeline.filter(item => item.type === 'mail').pop() : null;
return mail ? mail.subject : "";
},
}, },
methods: { methods: {
...mapActions(['deleteItem', 'markItemReturned', 'sendMail', 'updateTicketPartial', 'postComment']), ...mapActions(['deleteItem', 'markItemReturned', 'sendMail', 'updateTicketPartial', 'postComment']),
...mapActions(['loadTickets', 'fetchTicketStates', 'loadUsers', 'scheduleAfterInit']), ...mapActions(['loadTickets', 'fetchTicketStates', 'loadUsers', 'scheduleAfterInit']),
...mapActions(['claimShippingVoucher', 'fetchShippingVouchers']), ...mapActions(['claimShippingVoucher', 'fetchShippingVouchers']),
handleMail(mail) { ...mapMutations(['openLightboxModalWith']),
this.sendMail({ changeTicketStatus() {
id: this.ticket.id, this.ticket.state = this.selected_state;
message: mail
})
},
handleComment(comment) {
this.postComment({
id: this.ticket.id,
message: comment
})
},
changeTicketStatus(ticket) {
ticket.state = this.selected_state;
this.updateTicketPartial({ this.updateTicketPartial({
id: ticket.id, id: this.ticket.id,
state: this.selected_state, state: this.selected_state,
}) })
}, },
assignTicket(ticket) { assignTicket() {
ticket.assigned_to = this.selected_assignee; this.ticket.assigned_to = this.selected_assignee;
this.updateTicketPartial({ this.updateTicketPartial({
id: ticket.id, id: this.ticket.id,
assigned_to: this.selected_assignee assigned_to: this.selected_assignee
}) })
}, },
sendMailAndClear: async function () {
await this.sendMail({
id: this.ticket.id,
message: this.newMail,
})
this.newMail = "";
},
addCommentAndClear: async function () {
await this.postComment({
id: this.ticket.id,
message: this.newComment
})
this.newComment = "";
}
}, },
mounted() { mounted() {
this.scheduleAfterInit(() => [this.fetchTicketStates(), this.loadTickets(), this.loadUsers(), this.scheduleAfterInit(() => [Promise.all([this.fetchTicketStates(), this.loadTickets(), this.loadUsers(), this.fetchShippingVouchers()]).then(() => {
this.fetchShippingVouchers()]); if (this.ticket.state === "pending_new") {
this.selected_state = "pending_open";
this.changeTicketStatus()
}
this.selected_state = this.ticket.state;
this.selected_assignee = this.ticket.assigned_to
})]);
} }
}; };
</script> </script>
<style scoped> <style scoped>
</style> </style>

View file

@ -0,0 +1,102 @@
<template>
<AsyncLoader :loaded="isTicketsSearchLoaded">
<div class="container-fluid px-xl-5 mt-3">
<div class="row" v-if="layout === 'table'">
<div class="col-xl-8 offset-xl-2">
<Table
:columns="['id', 'name', 'state', 'last_activity', 'assigned_to',
...(getEventSlug==='all'?['event']:[])]"
:items="getTicketsSearchResults.map(formatTicket)"
:keyName="'id'"
>
<template v-slot:actions="{item}">
<div class="btn-group">
<router-link :to="{name: 'ticket', params: {id: item.id}}" class="btn btn-primary"
title="view">
<font-awesome-icon icon="eye"/>
View
</router-link>
</div>
</template>
</Table>
</div>
</div>
<CollapsableCards v-if="layout === 'tasks'" :items="getTicketsSearchResults"
:columns="['id', 'name', 'last_activity', 'assigned_to',
...(getEventSlug==='all'?['event']:[])]"
:keyName="'state'" :sections="['pending_new', 'pending_open','pending_shipping',
'pending_physical_confirmation','pending_return','pending_postponed'].map(stateInfo)">
<template #section_header="{index, section, count}">
{{ section.text }} <span class="badge badge-light ml-1">{{ count }}</span>
</template>
<template #section_body="{item}">
<tr>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.last_activity }}</td>
<td>{{ item.assigned_to }}</td>
<td v-if="getEventSlug==='all'">{{ item.event }}</td>
<td>
<div class="btn-group">
<router-link :to="{name: 'ticket', params: {id: item.id}}" class="btn btn-primary"
title="view">
<font-awesome-icon icon="eye"/>
View
</router-link>
</div>
</td>
</tr>
</template>
</CollapsableCards>
</div>
</AsyncLoader>
</template>
<script>
import Cards from '@/components/Cards';
import {mapActions, mapGetters, mapState} from 'vuex';
import Table from '@/components/Table';
import CollapsableCards from "@/components/CollapsableCards.vue";
import AsyncLoader from "@/components/AsyncLoader.vue";
import router from "@/router";
export default {
name: 'TicketSearch',
components: {AsyncLoader, Table, Cards, CollapsableCards},
computed: {
...mapGetters(['getTicketsSearchResults', 'isTicketsSearchLoaded', 'stateInfo', 'getEventSlug', 'layout', 'searchQuery']),
},
methods: {
...mapActions(['searchEventTickets', 'fetchTicketStates', 'scheduleAfterInit']),
gotoDetail(ticket) {
router.push({name: 'ticket', params: {id: ticket.id}});
},
formatTicket(ticket) {
return {
id: ticket.id,
name: ticket.name,
state: this.stateInfo(ticket.state).text,
stateColor: this.stateInfo(ticket.state).color,
last_activity: ticket.last_activity,
assigned_to: ticket.assigned_to,
event: ticket.event
};
}
},
watch: {
searchQuery() {
this.scheduleAfterInit(() => [this.searchEventTickets(this.searchQuery)]);
},
getEventSlug() {
this.scheduleAfterInit(() => [this.searchEventTickets(this.searchQuery)]);
}
},
mounted() {
this.scheduleAfterInit(() => [this.fetchTicketStates(), this.searchEventTickets(this.searchQuery)]);
}
};
</script>
<style scoped>
</style>

View file

@ -1,28 +1,30 @@
<template> <template>
<AsyncLoader :loaded="tickets.length > 0"> <AsyncLoader :loaded="isTicketsLoaded">
<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', 'actions', 'actions2']" :columns="['id', 'name', 'state', 'last_activity', 'assigned_to',
:items="tickets.map(formatTicket)" ...(getEventSlug==='all'?['event']:[])]"
:items="getEventTickets.map(formatTicket)"
:keyName="'id'" :keyName="'id'"
v-if="layout === 'table'" v-if="layout === 'table'"
> >
<template v-slot:actions="{item}"> <template v-slot:actions="{item}">
<div class="btn-group"> <div class="btn-group">
<a class="btn btn-primary" :href="'/'+ getEventSlug + '/ticket/' + item.id" title="view" <router-link :to="{name: 'ticket', params: {id: item.id}}" class="btn btn-primary"
@click.prevent="gotoDetail(item)"> title="view">
<font-awesome-icon icon="eye"/> <font-awesome-icon icon="eye"/>
View View
</a> </router-link>
</div> </div>
</template> </template>
</Table> </Table>
</div> </div>
</div> </div>
<CollapsableCards v-if="layout === 'tasks'" :items="tickets" <CollapsableCards v-if="layout === 'tasks'" :items="getEventTickets"
: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}">
@ -34,13 +36,14 @@
<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" <router-link :to="{name: 'ticket', params: {id: item.id}}" class="btn btn-primary"
@click.prevent="gotoDetail(item)"> title="view">
<font-awesome-icon icon="eye"/> <font-awesome-icon icon="eye"/>
View View
</a> </router-link>
</div> </div>
</td> </td>
</tr> </tr>
@ -52,25 +55,22 @@
<script> <script>
import Cards from '@/components/Cards'; import Cards from '@/components/Cards';
import Modal from '@/components/Modal';
import EditItem from '@/components/EditItem';
import {mapActions, mapGetters, mapState} from 'vuex'; import {mapActions, mapGetters, mapState} from 'vuex';
import Lightbox from '../components/Lightbox';
import Table from '@/components/Table'; import Table from '@/components/Table';
import CollapsableCards from "@/components/CollapsableCards.vue"; import CollapsableCards from "@/components/CollapsableCards.vue";
import AsyncLoader from "@/components/AsyncLoader.vue"; import AsyncLoader from "@/components/AsyncLoader.vue";
import router from "@/router";
export default { export default {
name: 'Tickets', name: 'Tickets',
components: {AsyncLoader, Lightbox, Table, Cards, Modal, EditItem, CollapsableCards}, components: {AsyncLoader, Table, Cards, CollapsableCards},
computed: { computed: {
...mapState(['tickets']), ...mapGetters(['getEventTickets', 'isTicketsLoaded', 'stateInfo', 'getEventSlug', 'layout']),
...mapGetters(['stateInfo', 'getEventSlug', 'layout']),
}, },
methods: { methods: {
...mapActions(['loadTickets', 'fetchTicketStates', 'scheduleAfterInit']), ...mapActions(['loadTickets', 'fetchTicketStates', 'scheduleAfterInit']),
gotoDetail(ticket) { gotoDetail(ticket) {
this.$router.push({name: 'ticket', params: {id: ticket.id}}); router.push({name: 'ticket', params: {id: ticket.id}});
}, },
formatTicket(ticket) { formatTicket(ticket) {
return { return {
@ -79,7 +79,8 @@ 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
}; };
} }
}, },

View file

@ -0,0 +1,33 @@
<template>
<div>
<h3 class="text-center">Events</h3>
<ul>
<li v-for="event in events" :key="event.id">
{{ event.slug }}
</li>
</ul>
</div>
</template>
<script>
import {mapActions, mapState} from 'vuex';
import Table from '@/components/Table';
export default {
name: 'Dashboard',
components: {},
computed: {
...mapState(['events']),
},
methods: {
...mapActions(['loadEvents']),
},
mounted() {
this.loadEvents();
}
};
</script>
<style>
</style>

View file

@ -1,62 +0,0 @@
<template>
<div>
<!--qr-code :text="qr_url" color="#000" bg-color="#fff" error-level="H" class="qr-code"></qr-code-->
<h3 class="text-center">Events</h3>
<!--p>{{ events }}</p-->
<ul>
<li v-for="event in events" :key="event.id">
{{ event.slug }}
</li>
</ul>
<h3 class="text-center">Items</h3>
<!--p>{{ loadedItems }}</p-->
<ul>
<li v-for="item in loadedItems" :key="item.id">
{{ item.description }}
</li>
</ul>
<h3 class="text-center">Boxes</h3>
<!--p>{{ loadedBoxes }}</p-->
<ul>
<li v-for="box in loadedBoxes" :key="box.id">
{{ box.name }}
</li>
</ul>
<h3 class="text-center">Issues</h3>
<!--p>{{ issues }}</p-->
<ul>
<li v-for="issue in tickets" :key="issue.id">
{{ issue.id }}
</li>
</ul>
</div>
</template>
<script>
import {mapActions, mapState} from 'vuex';
import Table from '@/components/Table';
export default {
name: 'Debug',
components: {Table},
computed: {
...mapState(['events', 'loadedItems', 'loadedBoxes', 'tickets']),
qr_url() {
return window.location.href;
}
},
methods: {
...mapActions(['changeEvent', 'loadTickets']),
},
mounted() {
this.loadTickets();
}
};
</script>
<style>
.qr-code img {
border: #fff solid 7px
}
</style>

View file

@ -1,50 +1,104 @@
<template> <template>
<Table <AsyncLoader :loaded="events.length > 0">
:columns="['slug', 'name']" <ExpandableTable v-if="!!events" :columns="['slug', 'name']" :items="events.map((e,i)=>({idx: i, ...e}))"
:items="events" :keyName="'slug'">
:keyName="'slug'" <template v-slot:header_actions>
> <button class="btn btn-success" @click.prevent="openAddEventModal">
<template v-slot:header_actions> <font-awesome-icon icon="plus"/>
<button class="btn btn-success" @click.prevent="openAddEventModal"> Create Event
<font-awesome-icon icon="plus"/>
Create Event
</button>
</template>
<template v-slot:actions="{ item }">
<div class="btn-group">
<button class="btn btn-secondary" @click.stop="changeEvent(item)">
<font-awesome-icon icon="archive"/>
use
</button> </button>
<button class="btn btn-danger" @click.stop="safeDeleteEvent(item.eid)"> </template>
<font-awesome-icon icon="trash"/> <template v-slot:actions="{ item }">
delete <div class="btn-group">
</button> <button class="btn btn-secondary" @click.stop="changeEvent(item)" style="white-space: nowrap;">
</div> <font-awesome-icon icon="archive"/>&nbsp;use
</template> </button>
</Table> <button class="btn btn-danger" @click.stop="safeDeleteEvent(item.eid)" style="white-space: nowrap;">
<font-awesome-icon icon="trash"/>&nbsp;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(item.idx, a_id)">
<button class="btn btn-secondary" disabled style="opacity: 1">
{{ address }}
</button>
<button class="btn btn-danger">
<font-awesome-icon icon="trash"/>
</button>
</div>
<div class="btn-group btn-group-sm">
<input type="text" v-model="new_address[item.idx]">
<button class="btn btn-secondary" @click.stop="addAddress(item.idx)"
style="white-space: nowrap;">
<font-awesome-icon icon="envelope"/>&nbsp;add
</button>
</div>
</div>
</template>
</ExpandableTable>
</AsyncLoader>
</template> </template>
<script> <script>
import {mapActions, mapMutations, mapState} from 'vuex'; import {mapActions, mapMutations, mapState} from 'vuex';
import Table from '@/components/Table'; import ExpandableTable from "@/components/ExpandableTable.vue";
import AsyncLoader from "@/components/AsyncLoader.vue";
export default { export default {
name: 'Events', name: 'Events',
components: {Table}, components: {AsyncLoader, ExpandableTable},
computed: mapState(['events']), computed: mapState(['events']),
data() {
return {new_address: []}
},
methods: { methods: {
...mapActions(['changeEvent', 'deleteEvent']), ...mapActions(['changeEvent', 'deleteEvent', 'updateEvent']),
...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].id, 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].id, partial_event: {addresses: this.events[id].addresses}});
}
}, },
}; };
</script> </script>
<style scoped> <style scoped>
</style> </style>