Completed
Pull Request — master (#41)
by Paolo
06:52
created

submissions.helpers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A send_message() 0 26 2
A is_target_in_message() 0 10 3
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