Beispiel #1
0
    def index(self, *args, **kw):
        try:
            log.info('User registration: %s'%str(kw))
            new_reg = Registration()
            new_reg.email_address = kw['email_address']
            new_reg.user_name = kw['email_address']
            new_reg.password = kw['password']
            new_reg.code = Registration.generate_code(kw['email_address'])
            DBSession.add(new_reg)
            DBSession.flush()

            email_data = {'sender':config['registration.email_sender'],
                      'subject':_('Message from Jminee'),
                      'body':'''
Please click on this link to confirm your registration

%s
''' % (url_for(self.mount_point+'/activate', code=new_reg.code, email=new_reg.email_address, qualified=True))}

        
            send_email(new_reg.email_address, email_data['sender'], email_data['subject'], email_data['body'])
        except Exception as e:
            log.exception('Got exception')
            return dict(success=False)
        return dict(success=True, email=kw['email_address'])
Beispiel #2
0
    def forget_password(self, *args, **kw):
        try:
            email_address = kw['email_address']
            user=DBSession.query(User).filter_by(email_address=email_address).first()
            if not user:
                return dict(success=False, error_code=ErrorCode.NONEXISTEDUSER)
                                                
            log.info("Reset password: %s"%email_address)
            reset_pwd = ResetPassword()
            reset_pwd.email_address=email_address
            reset_pwd.code = Registration.generate_code(email_address)
            
            DBSession.add(reset_pwd)
            DBSession.flush()
            
            email_data = {'sender':config['registration.email_sender'],
                      'subject':_('Message from Jminee'),
                      'body':'''
Please click on this link to reset your password

%s
''' % (url_for(self.mount_point+'/confirm_reset', code=reset_pwd.code, email=reset_pwd.email_address, qualified=True))}

        
            send_email(reset_pwd.email_address, email_data['sender'], email_data['subject'], email_data['body'])
        except Exception as e:
            log.exception('Got exception')
            return dict(success=False)
        return dict(success=True)
Beispiel #3
0
    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)  
Beispiel #4
0
 def setUp(self):
     """Prepare model test fixture."""
     try:
         new_attrs = {}
         new_attrs.update(self.attrs)
         new_attrs.update(self.do_get_dependencies())
         self.obj = self.klass(**new_attrs)
         DBSession.add(self.obj)
         DBSession.flush()
         return self.obj
     except:
         DBSession.rollback()
         raise
Beispiel #5
0
 def reset_password(self, *agrs, **kw):
     try:
         log.info('User reset password: %s'%str(kw))
         ResetPassword.clear_expired_user(kw['email_address'])
         reset = ResetPassword.get_inactive(kw['email_address'], kw['code'])
         if not reset:
             return dict(success=False, error_code=ErrorCode.NORESETRECORD)
         
         user=DBSession.query(User).filter_by(email_address=kw['email_address']).first()
         user.password = kw['password']
         
         reset.email = kw['email_address'] 
         reset.reset = datetime.now()
     
         DBSession.flush()
         return dict(success=True)
     except Exception as e:
         log.exception('Got exception')
         return dict(success=False, error_code=ErrorCode.OTHERS)
Beispiel #6
0
 def create_topic(self, *args, **kw):
     topic = Topic()
     topic.creator_id = request.identity['user'].user_id
     
     log.debug("Query string: %s"%kw)
     
     if kw.has_key('title'):
         topic.title = kw['title'] 
     else:
         #TODO: create topic named after all member
         return dict(success=False)
                 
     if kw.has_key('logourl'):
         topic.logourl = kw['logourl']
         
     #creator is always a member of the topic            
     membertopic = MemberTopic(role='c', local_title=topic.title, member_id=topic.creator_id)
     topic.members.append(membertopic)
     
     #do not change the order of the following if clause
     members = []
     if kw.has_key('members'):
         if isinstance(kw['members'], list):
            members = kw['members']
         elif kw['members']!='' and isinstance(kw['members'], unicode):
             members = [kw['members']]        
     try:
         # check if each member is in the User table
         # if not add him to nonexists list
         nonregistered_users = []
         registered_users = DBSession.query(User.email_address, User.user_id)\
                             .filter(User.email_address.in_(members)).all()
         registered_email_address = [user[0] for user in registered_users]
         registered_user_ids = [user[1] for user in registered_users]
         
         log.debug(registered_users)
         if len(registered_users)!=len(members):
             for user in members:
                 if user not in registered_email_address:
                     log.info("User %s is not in the database"%user)
                     nonregistered_users.append(user)   
         
         for member_id in registered_user_ids:
             if member_id == topic.creator_id:
                 continue
             
             member_topic = MemberTopic(role='c', 
                                       local_title=topic.title, 
                                       member_id=member_id)
             topic.members.append(member_topic)
                     
         DBSession.add(topic)
         DBSession.flush()
                     
         main_res = dict()
         #if there is subject to be created, then create it, return error_code if failed
         if kw.has_key('subject'):
             if kw.has_key('message'):
                 res=self.create_subject(topic_id=topic.uid, title=kw['subject'], content=kw['message'])
             else:
                 res=self.create_subject(topic_id=topic.uid, title=kw['subject'])
                 
             if res['success']==False:
                 main_res['error_code'] = ErrorCode.CREATSUBJECTFAILED
         
         #TODO: check main_res before sending notification
         #send notification to users
         notif=dict(type='new_topic',topic=topic.title,registered_users=members,
                    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))
 
         if len(nonregistered_users):
             main_res.update(dict(success=True,
                         topic=dict(uid=topic.uid, time=topic.time), 
                         nonregistered_users=nonregistered_users))
         else:
             main_res.update(dict(success=True,
                     topic=dict(uid=topic.uid, time=topic.time, 
                                creator_id=topic.creator_id, update_time=topic.time,
                                title=topic.title, logourl=topic.logourl)))            
         return main_res
     except Exception as e:
         log.exception("Got exception: %s"%e)
         return dict(success=False)                
Beispiel #7
0
    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)