<commit_before># Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: <EMAIL>
# Maintained By: <EMAIL>


"""
 GGRC email notification module hook to prepares email, email digest, notify email to recipients 
"""


from flask import current_app
from google.appengine.api import mail
from ggrc.models import Person, NotificationConfig, Notification, NotificationObject, NotificationRecipient
from datetime import datetime, timedelta
from ggrc import db
from ggrc import settings


def getAppEngineEmail():
  appengine_email = getattr(settings, 'APPENGINE_EMAIL') 
  if appengine_email is not None and appengine_email != '' and appengine_email !=" ":
    return appengine_email
  else:
    return None
   
def isNotificationEnabled(person_id, notif_type):
  if notif_type == 'Email_Deferred':
    notif_type='Email_Now'
  elif notif_type == 'Email_Digest_Deferred':
    notif_type='Email_Digest'
  notification_config=NotificationConfig.query.\
    filter(NotificationConfig.person_id==person_id).\
    filter(NotificationConfig.notif_type==notif_type).\
    first()
  if notification_config is None:
    if notif_type == 'Email_Digest':
      return True
    else:
      return False
  else:
    return notification_config.enable_flag
  
class NotificationBase(object):
  notif_type=None
  notif_pri=None
  appengine_email=None

  def __init__(self, notif_type):
    self.notif_type=notif_type
    self.appengine_email = getAppEngineEmail()

  def prepare(self, target_objs, sender, recipients, subject, content, override):
    return None

  def notify(self):
    pass

  def notify_one(self, notification, notify_custom_message):
    pass


class EmailNotification(NotificationBase):
  def __init__(self, notif_type='Email_Now'):
    super(EmailNotification, self).__init__(notif_type)

  def get_ignored_notifications(self, notifs):
    ignored_notifs={}
    #ToDo(Mouli): The valid states mapping must be set outside the generic email notification module
    ignored_states={
      'CycleTaskGroupObjectTask': ['Assigned', 'Finished', 'InProgress', 'Verified'],
      'CycleTaskGroup': ['InProgress', 'Verified'],
    }
    for notif in notifs:
      for notif_object in notif.notification_object:
        if ignored_states.has_key(notif_object.object_type):
          states=ignored_states[notif_object.object_type]
          if notif_object.status in states:
            ignored_notifs[notif.id]=notif

    return ignored_notifs

  def get_skipped_notifications(self, notifs_by_target):
    skipped_notifs={}
    #ToDo(Mouli): The valid states mapping must be set outside the generic email notification module
    valid_states={
      'CycleTaskGroupObjectTask': ['Assigned', 'InProgress', 'Finished', 'Verified', 'Declined'],
    }
    valid_transition_task={
     'Assigned': 'InProgress',
     'InProgress': 'Finished',
     'Finished': 'Declined',
     'Finished': 'Verified',
    }
    valid_transition={
      'CycleTaskGroupObjectTask': valid_transition_task,
    }
    undo_transition_task={
     'InProgress': 'Assigned',
     'Finished': 'InProgress',
     'Declined':'Finished',
     'Verified': 'Finished',
    }
    undo_transition={
      'CycleTaskGroupObjectTask': undo_transition_task,
    }
    for key, target_notifs in notifs_by_target.items():
      type=key[0]
      cnt = 0
      if not valid_states.has_key(type):
        current_app.logger.error(
          "EmailDeferredNotification: Error occured in handling state " + 
          "transitions"
          )
        for notif in target_notifs:
          skipped_notifs[notif.id]=notif
        continue

      for valid_state in valid_states[type]:
        next_cnt=find_matching_entry(target_notifs, cnt, valid_state, valid_state)
        if cnt != next_cnt:
          for index_cnt in range(next_cnt-cnt):
            notif=target_notifs[cnt+index_cnt][0]
            skipped_notifs[notif.id]=notif
        cnt=next_cnt
        if undo_transition[type].has_key(valid_state):
          next_cnt=find_matching_entry(target_notifs, cnt, valid_state, undo_transition[type][valid_state])
          if cnt != next_cnt:
            for index_cnt in range(next_cnt-cnt):
              notif=target_notifs[cnt+index_cnt][0]
              skipped_notifs[notif.id]=notif
        cnt=next_cnt
        if valid_transition[type].has_key(valid_state):
          cnt=find_matching_entry(target_notifs, cnt, valid_state, valid_transition[type][valid_state])
    return skipped_notifs

  def prepare(self, target_objs, sender, recipients, subject, content, override=False):
    if self.appengine_email is None:
      return None
    enable_notif={}
    existing_recipients={}
    updated_recipients=[]
    for recipient in recipients:
      recipient_id=recipient.id
      existing_recipients[recipient.id]=recipient
      if recipient_id is None:<commit_msg>Handle error transitions gracefully<commit_after># Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: <EMAIL>
# Maintained By: <EMAIL>


"""
 GGRC email notification module hook to prepares email, email digest, notify email to recipients 
"""


from flask import current_app
from google.appengine.api import mail
from ggrc.models import Person, NotificationConfig, Notification, NotificationObject, NotificationRecipient
from datetime import datetime, timedelta
from ggrc import db
from ggrc import settings


def getAppEngineEmail():
  appengine_email = getattr(settings, 'APPENGINE_EMAIL') 
  if appengine_email is not None and appengine_email != '' and appengine_email !=" ":
    return appengine_email
  else:
    return None
   
def isNotificationEnabled(person_id, notif_type):
  if notif_type == 'Email_Deferred':
    notif_type='Email_Now'
  elif notif_type == 'Email_Digest_Deferred':
    notif_type='Email_Digest'
  notification_config=NotificationConfig.query.\
    filter(NotificationConfig.person_id==person_id).\
    filter(NotificationConfig.notif_type==notif_type).\
    first()
  if notification_config is None:
    if notif_type == 'Email_Digest':
      return True
    else:
      return False
  else:
    return notification_config.enable_flag
  
class NotificationBase(object):
  notif_type=None
  notif_pri=None
  appengine_email=None

  def __init__(self, notif_type):
    self.notif_type=notif_type
    self.appengine_email = getAppEngineEmail()

  def prepare(self, target_objs, sender, recipients, subject, content, override):
    return None

  def notify(self):
    pass

  def notify_one(self, notification, notify_custom_message):
    pass


class EmailNotification(NotificationBase):
  def __init__(self, notif_type='Email_Now'):
    super(EmailNotification, self).__init__(notif_type)

  def get_ignored_notifications(self, notifs):
    ignored_notifs={}
    #ToDo(Mouli): The valid states mapping must be set outside the generic email notification module
    ignored_states={
      'CycleTaskGroupObjectTask': ['Assigned', 'Finished', 'InProgress', 'Verified'],
      'CycleTaskGroup': ['InProgress', 'Verified'],
    }
    for notif in notifs:
      for notif_object in notif.notification_object:
        if ignored_states.has_key(notif_object.object_type):
          states=ignored_states[notif_object.object_type]
          if notif_object.status in states:
            ignored_notifs[notif.id]=notif

    return ignored_notifs

  def get_skipped_notifications(self, notifs_by_target):
    skipped_notifs={}
    #ToDo(Mouli): The valid states mapping must be set outside the generic email notification module
    valid_states={
      'CycleTaskGroupObjectTask': ['Assigned', 'InProgress', 'Finished', 'Verified', 'Declined'],
    }
    valid_transition_task={
     'Assigned': 'InProgress',
     'InProgress': 'Finished',
     'Finished': 'Declined',
     'Finished': 'Verified',
    }
    valid_transition={
      'CycleTaskGroupObjectTask': valid_transition_task,
    }
    undo_transition_task={
     'InProgress': 'Assigned',
     'Finished': 'InProgress',
     'Declined':'Finished',
     'Verified': 'Finished',
    }
    undo_transition={
      'CycleTaskGroupObjectTask': undo_transition_task,
    }
    for key, target_notifs in notifs_by_target.items():
      type=key[0]
      cnt = 0
      if not valid_states.has_key(type):
        current_app.logger.error(
          "EmailDeferredNotification: Error occured in handling state " + 
          "transitions"
          )
        for (notif, notif_object) in target_notifs:
          skipped_notifs[notif.id]=notif
        continue

      for valid_state in valid_states[type]:
        next_cnt=find_matching_entry(target_notifs, cnt, valid_state, valid_state)
        if cnt != next_cnt:
          for index_cnt in range(next_cnt-cnt):
            notif=target_notifs[cnt+index_cnt][0]
            skipped_notifs[notif.id]=notif
        cnt=next_cnt
        if undo_transition[type].has_key(valid_state):
          next_cnt=find_matching_entry(target_notifs, cnt, valid_state, undo_transition[type][valid_state])
          if cnt != next_cnt:
            for index_cnt in range(next_cnt-cnt):
              notif=target_notifs[cnt+index_cnt][0]
              skipped_notifs[notif.id]=notif
        cnt=next_cnt
        if valid_transition[type].has_key(valid_state):
          cnt=find_matching_entry(target_notifs, cnt, valid_state, valid_transition[type][valid_state])
    return skipped_notifs

  def prepare(self, target_objs, sender, recipients, subject, content, override=False):
    if self.appengine_email is None:
      return None
    enable_notif={}
    existing_recipients={}
    updated_recipients=[]
    for recipient in recipients:
      recipient_id=recipient.id
      existing_recipients[recipient.id]=recipient
      if recipient_id is None:
{'fraction_non_alphanumeric': 0.09311945117029863, 'fraction_numerical': 0.0020177562550443904, 'mean_word_length': 3.7958393807450412, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, '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': '6414174', 'n_tokens_mistral': 3092, 'n_tokens_neox': 3001, 'n_words': 681}