<commit_before>#  Copyright 2016 Google Inc. All Rights Reserved.
#  
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#  
#      http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License. 

from django.db.models.signals import post_save
from django.utils import timezone
from main.models import Alert
from django.dispatch import receiver
from main.models import AlertStyle, Alert
from fanfunding.models import FanFundingEvent, AlertConfig
import json

def config_to_alert(alert, info, test=False):
    if alert.blacklist:
        blacklist_strings = map(lambda x: x.strip(), alert.blacklist.split(","))
    else:
        blacklist_strings = []
    for s in blacklist_strings:
        if s and s.lower() in info['name'].lower():
            return
    text = alert.alert_text
    text = text.replace("[[name]]", info['name'])
    text = text.replace("[[amount]]", info['amount'])
    text += "[[br]]" + info['comment']
    style = AlertStyle(image=alert.image_url, sound=alert.sound_url, font=alert.font, font_size=alert.font_size, font_color=alert.font_color)
    style.save()
    a = Alert(text=text, time=timezone.now(), user=alert.user, style=style, test=test, config=alert)
    a.save()

@receiver(post_save, sender=FanFundingEvent)
def event(instance, **kwargs):
    user = instance.updater.credentials.user
    details = json.loads(instance.details)
    info = instance.as_dict()
    alerts = AlertConfig.objects.filter(user=user).order_by("filter_type", "-filter_amount")
    amount_micros = info['amount_micros']
    for alert in alerts:
        if alert.filter_type == "1equal":
            if alert.filter_amount * 1000000 == amount_micros:
                config_to_alert(alert, info)
                break
        elif alert.filter_type == "2gt":
            if alert.filter_amount * 1000000 < amount_micros:
                config_to_alert(alert, info)
                break
        else:
            config_to_alert(alert, info)

if __name__ == "__main__":
    # simple testing.
    import django
    django.setup()
    event(FanFundingEvent.objects.all()[0])<commit_msg>Fix case where someone enters a filter but no amount.<commit_after>#  Copyright 2016 Google Inc. All Rights Reserved.
#  
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#  
#      http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License. 

from django.db.models.signals import post_save
from django.utils import timezone
from main.models import Alert
from django.dispatch import receiver
from main.models import AlertStyle, Alert
from fanfunding.models import FanFundingEvent, AlertConfig
import json

def config_to_alert(alert, info, test=False):
    if alert.blacklist:
        blacklist_strings = map(lambda x: x.strip(), alert.blacklist.split(","))
    else:
        blacklist_strings = []
    for s in blacklist_strings:
        if s and s.lower() in info['name'].lower():
            return
    text = alert.alert_text
    text = text.replace("[[name]]", info['name'])
    text = text.replace("[[amount]]", info['amount'])
    text += "[[br]]" + info['comment']
    style = AlertStyle(image=alert.image_url, sound=alert.sound_url, font=alert.font, font_size=alert.font_size, font_color=alert.font_color)
    style.save()
    a = Alert(text=text, time=timezone.now(), user=alert.user, style=style, test=test, config=alert)
    a.save()

@receiver(post_save, sender=FanFundingEvent)
def event(instance, **kwargs):
    user = instance.updater.credentials.user
    details = json.loads(instance.details)
    info = instance.as_dict()
    alerts = AlertConfig.objects.filter(user=user).order_by("filter_type", "-filter_amount")
    amount_micros = info['amount_micros']
    for alert in alerts:
        if alert.filter_type == "1equal" and alert.filter_amount:
            if alert.filter_amount * 1000000 == amount_micros:
                config_to_alert(alert, info)
                break
        elif alert.filter_type == "2gt" and alert.filter_amount:
            if alert.filter_amount * 1000000 < amount_micros:
                config_to_alert(alert, info)
                break
        elif alert.filter_type == "3default":
            config_to_alert(alert, info)

if __name__ == "__main__":
    # simple testing.
    import django
    django.setup()
    event(FanFundingEvent.objects.get(id=2120))
{'fraction_non_alphanumeric': 0.10614849187935035, 'fraction_numerical': 0.010440835266821345, 'mean_word_length': 3.402553191489362, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '214484', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1491, 'n_words': 446}