import logging
from website.notifications.exceptions import InvalidSubscriptionError
from website.notifications.utils import subscribe_user_to_notifications, subscribe_user_to_global_notifications
from website.project.signals import contributor_added, project_created
from framework.auth.signals import user_confirmed

logger = logging.getLogger(__name__)

@project_created.connect
def subscribe_creator(node):
    if node.is_collection or node.is_deleted:
        return None
    try:
        subscribe_user_to_notifications(node, node.creator)
    except InvalidSubscriptionError as err:
        user = node.creator._id if node.creator else 'None'
        logger.warn('Skipping subscription of user {} to node {}'.format(user, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@contributor_added.connect
def subscribe_contributor(node, contributor, auth=None, *args, **kwargs):
    try:
        subscribe_user_to_notifications(node, contributor)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to node {}'.format(contributor, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@user_confirmed.connect
def subscribe_confirmed_user(user):
    try:
        subscribe_user_to_global_notifications(user)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to global subscriptions'.format(user))
        logger.warn('Reason: {}'.format(str(err)))

import logging
from website.notifications.exceptions import InvalidSubscriptionError
from website.notifications.utils import subscribe_user_to_notifications, subscribe_user_to_global_notifications
from website.project.signals import contributor_added, project_created
from framework.auth.signals import user_confirmed

logger = logging.getLogger(__name__)

@project_created.connect
def subscribe_creator(node):
    if node.institution_id or node.is_collection or node.is_deleted:
        return None
    try:
        subscribe_user_to_notifications(node, node.creator)
    except InvalidSubscriptionError as err:
        user = node.creator._id if node.creator else 'None'
        logger.warn('Skipping subscription of user {} to node {}'.format(user, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@contributor_added.connect
def subscribe_contributor(node, contributor, auth=None, *args, **kwargs):
    try:
        subscribe_user_to_notifications(node, contributor)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to node {}'.format(contributor, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@user_confirmed.connect
def subscribe_confirmed_user(user):
    try:
        subscribe_user_to_global_notifications(user)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to global subscriptions'.format(user))
        logger.warn('Reason: {}'.format(str(err)))

Revert "Remove incorrect check for institution_id"
Revert "Remove incorrect check for institution_id"

This reverts commit 617df13670573b858b6c23249f4287786807d8b6.

  import logging
  from website.notifications.exceptions import InvalidSubscriptionError
  from website.notifications.utils import subscribe_user_to_notifications, subscribe_user_to_global_notifications
  from website.project.signals import contributor_added, project_created
  from framework.auth.signals import user_confirmed
  
  logger = logging.getLogger(__name__)
  
  @project_created.connect
  def subscribe_creator(node):
-     if node.is_collection or node.is_deleted:
+     if node.institution_id or node.is_collection or node.is_deleted:
          return None
      try:
          subscribe_user_to_notifications(node, node.creator)
      except InvalidSubscriptionError as err:
          user = node.creator._id if node.creator else 'None'
          logger.warn('Skipping subscription of user {} to node {}'.format(user, node._id))
          logger.warn('Reason: {}'.format(str(err)))
  
  @contributor_added.connect
  def subscribe_contributor(node, contributor, auth=None, *args, **kwargs):
      try:
          subscribe_user_to_notifications(node, contributor)
      except InvalidSubscriptionError as err:
          logger.warn('Skipping subscription of user {} to node {}'.format(contributor, node._id))
          logger.warn('Reason: {}'.format(str(err)))
  
  @user_confirmed.connect
  def subscribe_confirmed_user(user):
      try:
          subscribe_user_to_global_notifications(user)
      except InvalidSubscriptionError as err:
          logger.warn('Skipping subscription of user {} to global subscriptions'.format(user))
          logger.warn('Reason: {}'.format(str(err)))
  
Revert "Remove incorrect check for institution_id"
## Code Before:
import logging
from website.notifications.exceptions import InvalidSubscriptionError
from website.notifications.utils import subscribe_user_to_notifications, subscribe_user_to_global_notifications
from website.project.signals import contributor_added, project_created
from framework.auth.signals import user_confirmed

logger = logging.getLogger(__name__)

@project_created.connect
def subscribe_creator(node):
    if node.is_collection or node.is_deleted:
        return None
    try:
        subscribe_user_to_notifications(node, node.creator)
    except InvalidSubscriptionError as err:
        user = node.creator._id if node.creator else 'None'
        logger.warn('Skipping subscription of user {} to node {}'.format(user, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@contributor_added.connect
def subscribe_contributor(node, contributor, auth=None, *args, **kwargs):
    try:
        subscribe_user_to_notifications(node, contributor)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to node {}'.format(contributor, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@user_confirmed.connect
def subscribe_confirmed_user(user):
    try:
        subscribe_user_to_global_notifications(user)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to global subscriptions'.format(user))
        logger.warn('Reason: {}'.format(str(err)))

## Instruction:
Revert "Remove incorrect check for institution_id"
## Code After:
import logging
from website.notifications.exceptions import InvalidSubscriptionError
from website.notifications.utils import subscribe_user_to_notifications, subscribe_user_to_global_notifications
from website.project.signals import contributor_added, project_created
from framework.auth.signals import user_confirmed

logger = logging.getLogger(__name__)

@project_created.connect
def subscribe_creator(node):
    if node.institution_id or node.is_collection or node.is_deleted:
        return None
    try:
        subscribe_user_to_notifications(node, node.creator)
    except InvalidSubscriptionError as err:
        user = node.creator._id if node.creator else 'None'
        logger.warn('Skipping subscription of user {} to node {}'.format(user, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@contributor_added.connect
def subscribe_contributor(node, contributor, auth=None, *args, **kwargs):
    try:
        subscribe_user_to_notifications(node, contributor)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to node {}'.format(contributor, node._id))
        logger.warn('Reason: {}'.format(str(err)))

@user_confirmed.connect
def subscribe_confirmed_user(user):
    try:
        subscribe_user_to_global_notifications(user)
    except InvalidSubscriptionError as err:
        logger.warn('Skipping subscription of user {} to global subscriptions'.format(user))
        logger.warn('Reason: {}'.format(str(err)))
