-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from Amsterdam/feature/display-zds-data
Feature/display zds data
- Loading branch information
Showing
54 changed files
with
2,080 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from rest_framework import serializers | ||
from zds_client.client import ClientError | ||
|
||
from signals.apps.signals.models import Signal | ||
|
||
|
||
class SignalZDSSerializer(serializers.ModelSerializer): | ||
"""Read-only serializer for `Signal` object.""" | ||
id = serializers.IntegerField(label='ID', read_only=True) | ||
zds_case = serializers.SerializerMethodField() | ||
zds_statusses = serializers.SerializerMethodField() | ||
zds_images = serializers.SerializerMethodField() | ||
|
||
def get_zds_case(self, obj): | ||
if hasattr(obj, 'case'): | ||
try: | ||
return obj.case.get_case() | ||
except ClientError: | ||
return {} | ||
return {} | ||
|
||
def get_zds_statusses(self, obj): | ||
if hasattr(obj, 'case'): | ||
try: | ||
return obj.case.get_statusses() | ||
except ClientError: | ||
return [] | ||
return [] | ||
|
||
def get_zds_images(self, obj): | ||
if hasattr(obj, 'case'): | ||
try: | ||
return obj.case.get_images() | ||
except ClientError: | ||
return [] | ||
return [] | ||
|
||
class Meta(object): | ||
model = Signal | ||
fields = ( | ||
'id', | ||
'zds_case', | ||
'zds_statusses', | ||
'zds_images', | ||
) | ||
read_only_fields = ( | ||
'id', | ||
'zds_case', | ||
'zds_statusses', | ||
'zds_images', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
Views that are used exclusively by the V0 API | ||
""" | ||
from datapunt_api.rest import DatapuntViewSet | ||
|
||
from signals.apps.signals.models import Signal | ||
from signals.auth.backend import JWTAuthBackend | ||
|
||
from .serializers import SignalZDSSerializer | ||
|
||
|
||
class SignalZDSViewSet(DatapuntViewSet): | ||
authentication_classes = (JWTAuthBackend, ) | ||
queryset = Signal.objects.order_by('-created_at') | ||
serializer_detail_class = SignalZDSSerializer | ||
serializer_class = SignalZDSSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
api/app/signals/apps/zds/management/commands/add_signals_to_case.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import logging | ||
from datetime import timedelta | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from signals.apps.signals.models import Signal | ||
from signals.apps.zds.exceptions import ( | ||
CaseConnectionException, | ||
CaseNotCreatedException, | ||
DocumentConnectionException, | ||
DocumentNotCreatedException, | ||
StatusNotCreatedException | ||
) | ||
from signals.apps.zds.tasks import ( | ||
add_document_to_case, | ||
add_status_to_case, | ||
connect_signal_to_case, | ||
create_case, | ||
create_document | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Sync the signals with the ZDS components" | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Only select the signals that are 10 minutes old. | ||
Select signals that have no case or have not been done syncing. | ||
""" | ||
ten_minutes_ago = timezone.now() - timedelta(seconds=600) | ||
signals_id1 = list(Signal.objects.filter(case__isnull=True).values_list('pk', flat=True)) | ||
signals_id2 = list(Signal.objects.filter(case__isnull=False).filter( | ||
case__sync_completed=False).values_list('pk', flat=True)) | ||
signal_ids = signals_id1 + signals_id2 | ||
|
||
signals = Signal.objects.filter(pk__in=signal_ids, created_at__lte=ten_minutes_ago) | ||
for signal in signals: | ||
self.sync_case(signal) | ||
|
||
def sync_case(self, signal): | ||
""" | ||
Push the case to the ZDS components. | ||
If the case already exists, it will continue to connect the signal to the case. | ||
If everything went correctly, continue to add the statusses to the ZDS components. | ||
""" | ||
try: | ||
create_case(signal) | ||
|
||
try: | ||
connect_signal_to_case(signal) | ||
self.sync_status(signal) | ||
except CaseConnectionException as case_exception: | ||
logger.exception(case_exception) | ||
self.stderr.write(repr(case_exception)) | ||
except CaseNotCreatedException as case_exception: | ||
logger.exception(case_exception) | ||
self.stderr.write(repr(case_exception)) | ||
|
||
def sync_status(self, signal): | ||
""" | ||
Push all the statusses that are connected to the signal to the ZDS components. | ||
If a status already exists, it will skip the status and continue with the other statusses. | ||
If everything went correctly, continue to add the documents to the ZDS components. | ||
""" | ||
try: | ||
for status in signal.statuses.order_by('created_at'): | ||
add_status_to_case(signal, status) | ||
|
||
self.sync_document(signal) | ||
except StatusNotCreatedException as status_exception: | ||
logger.exception(status_exception) | ||
self.stderr.write(repr(status_exception)) | ||
|
||
def sync_document(self, signal): | ||
""" | ||
Push all the images that are connected to the signal to the ZDS components. | ||
If the document already exists, set signal sync to completed. | ||
""" | ||
if signal.image: | ||
try: | ||
case_document = create_document(signal) | ||
|
||
try: | ||
add_document_to_case(signal, case_document) | ||
signal.case.sync_completed = True | ||
signal.case.save() | ||
except DocumentConnectionException as document_exception: | ||
logger.exception(document_exception) | ||
self.stderr.write(repr(document_exception)) | ||
except DocumentNotCreatedException as document_exception: | ||
logger.exception(document_exception) | ||
self.stderr.write(repr(document_exception)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
api/app/signals/apps/zds/migrations/0006_auto_20181213_1740.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 2.1.2 on 2018-12-13 16:40 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('signals', '0028_auto_20181204_0915'), | ||
('zds', '0005_auto_20181211_1503'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='casesignal', | ||
name='sync_completed', | ||
field=models.BooleanField(blank=True, default=False), | ||
), | ||
migrations.AddField( | ||
model_name='casestatus', | ||
name='status', | ||
field=models.OneToOneField( | ||
null=True, on_delete=django.db.models.deletion.CASCADE, | ||
related_name='case_status', to='signals.Status' | ||
), | ||
), | ||
] |
Oops, something went wrong.