| Total Complexity | 5 |
| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | #!/usr/bin/env python3 |
||
| 2 | # -*- coding: utf-8 -*- |
||
| 3 | """ |
||
| 4 | Created on Thu Jun 27 11:52:37 2019 |
||
| 5 | |||
| 6 | @author: Paolo Cozzi <[email protected]> |
||
| 7 | """ |
||
| 8 | |||
| 9 | import asyncio |
||
| 10 | import re |
||
| 11 | |||
| 12 | from common.constants import STATUSES |
||
| 13 | from common.helpers import send_message_to_websocket |
||
| 14 | |||
| 15 | |||
| 16 | def send_message(submission_obj, validation_message=None): |
||
| 17 | """ |
||
| 18 | Update submission.status and submission message using django |
||
| 19 | channels |
||
| 20 | |||
| 21 | Args: |
||
| 22 | submission_obj (image_app.models.Submission): an UID submission |
||
| 23 | object |
||
| 24 | validation_message (dict): set validation message |
||
| 25 | """ |
||
| 26 | |||
| 27 | # define a message to send |
||
| 28 | message = { |
||
| 29 | 'message': STATUSES.get_value_display(submission_obj.status), |
||
| 30 | 'notification_message': submission_obj.message, |
||
| 31 | } |
||
| 32 | |||
| 33 | # if validation message is needed, add to the final message |
||
| 34 | if validation_message: |
||
| 35 | message['validation_message'] = validation_message |
||
| 36 | |||
| 37 | # now send the message to its submission |
||
| 38 | asyncio.get_event_loop().run_until_complete( |
||
| 39 | send_message_to_websocket( |
||
| 40 | message, |
||
| 41 | submission_obj.pk |
||
| 42 | ) |
||
| 43 | ) |
||
| 44 | |||
| 45 | |||
| 46 | def is_target_in_message(target, messages): |
||
| 47 | """ |
||
| 48 | This function will return true if target in message |
||
| 49 | Args: |
||
| 50 | target (str): target to search |
||
| 51 | """ |
||
| 52 | for message in messages: |
||
| 53 | if re.search(message, target): |
||
| 54 | return True |
||
| 55 | return False |
||
| 56 |