示例#1
0
文件: topic.py 项目: mbbui/Jminee
    def create_comment(self, **kw):         
        try:
            #check if this user can create a comment in this topic
            #for now every member of the topic can create a comment
            creator = DBSession.query(MemberTopic).\
                           filter(MemberTopic.topic_id==kw['topic_id'], 
                                  MemberTopic.member_id==request.identity['user'].user_id).\
                           first()      
            
            if not creator:
                #TODO: this should never happen, log this event and return only False
                return dict(success=False, error_code=ErrorCode.UNAUTHORIZED)
            
            comment = Comment()
            comment.subject_id = kw['subject_id']
            comment.creator_id = request.identity['user'].user_id
            comment.content = kw['content']
            
            DBSession.add(comment)
            DBSession.flush()
            
            #TODO: should not query then update, should only update
            topic = DBSession.query(Topic).\
                            filter(Topic.uid==kw['topic_id']).first()
            #TODO: need to rethink this time var, how about subject update_time
            topic.update_time = comment.time                
                            
            log.info("User %s creates comment %s"%(comment.creator_id, comment))
            
            subject_title = DBSession.query(Subject.title).\
                            filter(Subject.uid==kw['subject_id']).first()[0]
                            
            members = DBSession.query(User.email_address).\
                           filter(MemberTopic.topic_id==kw['topic_id'],
                                  MemberTopic.member_id!=creator.member_id, 
                                  MemberTopic.deleted==False,
                                  MemberSubject.subject_id==kw['subject_id'],
                                  MemberSubject.muted==False).\
                           join(MemberTopic,MemberSubject).\
                           all()
            member_emails = [member[0] for member in members]
                           
            #send notification to users
            notif=dict(type='new_comment',topic=topic.title,subject=subject_title,comment=comment.content,
                       registered_users=member_emails,user_name=request.identity['user'].user_name)
            log.info('Sending SNS notification: %s'%notif)
            sns=SNSConnection(config.get('AWS_ACCESS_KEY_ID'), config.get("AWS_SECRET_ACCESS_KEY"))
            sns.publish(config.get('sqs_user_notification'),js.dumps(notif))

            return dict(success=True, comment=dict(uid=comment.uid, time=comment.time))
                        
        except Exception as e:
            log.exception('Got exception %s'%e)
            #traceback.print_exc(file=sys.stdout)
            return dict(success=False)  
示例#2
0
文件: topic.py 项目: mbbui/Jminee
    def create_subject(self, **kw):         
        try:
            #check if this user can create a subject in this topic
            creator = DBSession.query(MemberTopic).\
                           filter(MemberTopic.topic_id==kw['topic_id'], 
                                  MemberTopic.member_id==request.identity['user'].user_id,
                                  'role = "c" or role = "s"').\
                           first()      
            
            if not creator:
                #TODO: this should never happen, log this event and return only False
                return dict(success=False, error_code=ErrorCode.UNAUTHORIZED)
            
            #add new subject to the database
            subject = Subject()
            subject.topic_id = kw['topic_id']
            subject.title = kw['title']
            subject.creator_id = request.identity['user'].user_id
            
            DBSession.add(subject)
            DBSession.flush()
        
            comment = None
            if kw.has_key('content'):
                comment = Comment()
                comment.subject_id = subject.uid
                comment.creator_id = request.identity['user'].user_id
                comment.content = kw['content']
            
                DBSession.add(comment)
                DBSession.flush()
            
            
            #add creator to member_subject
            if comment:
                member_subject = MemberSubject(member_id = creator.member_id,
                                           subject_id = subject.uid,
                                           last_read = comment.uid)                
            else:
                member_subject = MemberSubject(member_id = creator.member_id,
                                           subject_id = subject.uid,
                                           last_read = 0)
            subject.members.append(member_subject)
            
#            registered_users = DBSession.query(User.email_address).filter(User.email_address.in_(members)).all()
            members = DBSession.query(MemberTopic.member_id,User.email_address).\
                           filter(MemberTopic.topic_id==kw['topic_id'],
                                  MemberTopic.member_id!=creator.member_id, 
                                  MemberTopic.deleted == False).\
                           join(User).\
                           all()
            
            #add other members to member_subject table
            member_ids = [member[0] for member in members]               
            for member_id in member_ids:
                member_subject = MemberSubject(member_id = member_id,
                                           subject_id = subject.uid,
                                           last_read = 0) 
                subject.members.append(member_subject)
            
                        
            topic = DBSession.query(Topic).\
                            filter(Topic.uid==kw['topic_id']).first()
            #TODO: need to rename/rethink this time vars
            topic.update_time = subject.time                
            log.info("User %s creates subject %s"%(creator.member_id, subject))
            
            member_emails = [member[1] for member in members]
            
            #send notification to users
            if len(member_emails)>0:
                notif=dict(type='new_subject',topic=topic.title,subject=subject.title,
                           registered_users=member_emails,user_name=request.identity['user'].user_name)
                log.info('Sending SNS notification: %s'%notif)
                sns=SNSConnection(config.get('AWS_ACCESS_KEY_ID'), config.get("AWS_SECRET_ACCESS_KEY"))
                sns.publish(config.get('sqs_user_notification'),js.dumps(notif))

            return dict(success=True, subject=dict(uid=subject.uid, time=subject.time))
                        
        except Exception as e:
            log.exception('Got exception %s'%e)
            #traceback.print_exc(file=sys.stdout)
            return dict(success=False)