Created
August 26, 2014 09:41
-
-
Save nl5887/41b8e78627de45803fc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
from pyramid.response import Response | |
from pyramid.view import view_config, view_defaults | |
from sqlalchemy.sql.expression import and_, or_, not_ | |
from sqlalchemy.orm.exc import NoResultFound | |
from .models import ( | |
DBSession, | |
User, | |
) | |
log = logging.getLogger(__name__) | |
@view_defaults() | |
class sns(object): | |
def __init__(self, request): | |
self.request = request | |
pass | |
@view_config(route_name='sns', request_method='POST', renderer='json') | |
def post(self): | |
notification = self.request.json_body | |
message = json.loads(notification['Message']) | |
if message.get('bounce') and message.get('bounce').get('bounceType')=='Permanent': | |
for br in message.get('bounce').get('bouncedRecipients'): | |
if br.get('action')!='failed': | |
continue | |
email = br.get('emailAddress') | |
user = None | |
try: | |
user = DBSession.query(User).filter(and_(User.email==email)).one() | |
except NoResultFound as e: | |
pass | |
if user: | |
user.notifications_publications = False | |
log.info("Changed notification settings for user {0} because of bounce message.".format(email)) | |
else: | |
raise Exception("Email not found [0}.", email) | |
if message.get('complaint'): | |
for cr in message.get('complaint').get('complainedRecipients'): | |
email = cr.get('emailAddress') | |
user = None | |
try: | |
user = DBSession.query(User).filter(and_(User.email==email)).one() | |
except NoResultFound as e: | |
pass | |
if user: | |
user.notifications_publications = False | |
log.info("Changed notification settings for user {0} because of abuse message {1}.".format(email, cr.get('complaintFeedbackType'))) | |
else: | |
raise Exception("Email not found [0}.", email) | |
return Response("OK") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment