Ejemplo n.º 1
0
def unfollow(follower, followed, delay_commit=False):
    followed = get_member(followed)
    follower = get_member(follower)

    if not followed:
        raise action_error(_('unable to find followed'), code=404)
    if not follower:
        raise action_error(_('unable to find follower'), code=404)
    #if followed not in follower.following:
    # GregM: can unfollow to remove trusted invite
    if not follower.is_following(
            followed) and not follower.is_follow_trusted_inviter(followed):
        raise action_error(_('not currently following'), code=400)

    #follower.following.remove(followed)
    follow = Session.query(Follow).filter(
        Follow.member_id == followed.id).filter(
            Follow.follower_id == follower.id).one()
    Session.delete(follow)

    if not delay_commit:
        Session.commit()

    #invalidate_member(follower)
    #invalidate_member(followed)

    followed.send_notification(
        messages.follow_stop(member=follower, you=followed))

    return True
Ejemplo n.º 2
0
def set_password(user, new_token, delay_commit=False):
    """
    Set password
    WARNING! We assume the user has already been authenticated
    - remove old password (if found)
    - create new password record
    """
    # search for existing record and remove it
    #
    try:
        for existing_login in [login for login in user.login_details if login.type == 'password']:
            log.debug("removing password for %s" % user.username)
            #if existing_login.token == old_token: raise Exception('old password token does not match - aborting password change')
            Session.delete(existing_login)
            log.debug("removed ok")
    #try: Session.execute(UserLogin.__table__.delete().where(and_(UserLogin.__table__.c.member_id == user.id, UserLogin.__table__.c.token == token)))
    except Exception:
        pass
    # Set new password
    u_login = UserLogin()
    u_login.user   = user
    u_login.type   = 'password'
    u_login.token  = new_token
    Session.add(u_login)
    
    if not delay_commit:
        Session.commit()
Ejemplo n.º 3
0
def rate_content(content, member, rating):
    content = get_content(content)
    member = get_member(member)
    rating_value = int(rating)

    if not content:
        raise action_error(_('unable to find content'), code=404)
    if not member:
        raise action_error(_('unable to find member'), code=404)
    if rating and rating_value < 0 or rating_value > 5:
        raise action_error(_("Ratings can only be in the range 0 to 5"),
                           code=400)

    # remove any existing ratings
    # we need to commit after removal, otherwise SQLAlchemy
    # will optimise remove->add as modify-existing, and the
    # SQL trigger will break
    try:
        existing = Session.query(Rating).filter(
            Rating.content == content).filter(Rating.member == member).one()
        Session.delete(existing)
        Session.commit()
    except NoResultFound:
        pass

    # rating = 0 = remove vote
    # add a new one
    if rating_value > 0:
        r = Rating()
        r.content = content
        r.member = member
        r.rating = rating_value
        Session.add(r)
        Session.commit()
Ejemplo n.º 4
0
def del_group(group):
    group = get_group(group)
    from pylons import tmpl_context as c
    for member in [member_role.member for member_role in group.members_roles]:
        member.send_notification(
            messages.group_deleted(group=group, admin=c.logged_in_user)
        )  # AllanC - We cant use the standard group.send_notification because the group wont exisit after this line!
    Session.delete(group)
    Session.commit()
Ejemplo n.º 5
0
def unboom_content(content, member, delay_commit=False):
    boom = has_boomed(content, member)
    if boom:
        Session.delete(boom)
    else:
        raise action_error(_(
            "%s has not boomed previously boomed this _content" % member),
                           code=400)

    if not delay_commit:
        Session.commit()
Ejemplo n.º 6
0
 def delete_content(self, id, force=False):
     if force:  # ignore logged in user and just get rid of it
         Session.delete(Session.query(Content).get(id))
         Session.commit()
     else:
         self.assertIn('delete', self.get_actions(id))
         response = self.app.post(url('content', id=id, format="json"),
                                  params={
                                      '_method': 'delete',
                                      '_authentication_token':
                                      self.auth_token,
                                  },
                                  status=200)
Ejemplo n.º 7
0
def remove_member(group, member, delay_commit=False):
    group = get_group(group)
    member = get_member(member)
    membership = get_membership(group, member)

    if not group:
        raise action_error(_('unable to find group'), code=404)
    if not member:
        raise action_error(_('unable to find member'), code=404)
    if not membership:
        raise action_error(_('not a member of group'), code=400)
    # AllanC - permissions moved to controller
    #if member!=c.logged_in_persona and not group.is_admin(c.logged_in_persona):
    #    raise action_error('current user has no permissions for this group', 403)
    #AllanC - integrety moved to model
    #if membership.role=="admin" and num_admins<=1:
    #    raise action_error('cannot remove last admin', 400)

    # Notifications
    if membership.status == "active":  # member removed
        from pylons import tmpl_context as c
        if member != c.logged_in_user:
            membership.member.send_notification(
                messages.group_remove_member_to_member(admin=c.logged_in_user,
                                                       group=group))
        group.send_notification(
            messages.group_remove_member_to_group(admin=c.logged_in_user,
                                                  group=group,
                                                  member=member))
    elif membership.status == "invite":  # invitation declined
        group.send_notification(
            messages.group_invitation_declined(member=member, group=group))
    elif membership.status == "request":  # request declined
        membership.member.send_notification(
            messages.group_request_declined(group=group))

    Session.delete(membership)

    if not delay_commit:
        Session.commit()

    #invalidate_member(group)
    #invalidate_member(member)

    return True
Ejemplo n.º 8
0
    def test_flag_message(self):

        self.send_member_message('unitfriend',
                                 subject='You smell',
                                 content='I hate you')

        self.log_in_as('unitfriend')

        message = self.get_messages()['list']['items'][0]
        self.assertIn('hate you', message['content'])

        num_emails = getNumEmails()
        response = self.app.post(
            url('message_action',
                action='flag',
                id=message['id'],
                format='json'),
            params={
                '_authentication_token': self.auth_token,
                'type': 'offensive',
                'comment': 'They said they hated me, and I smell ... sniff',
            },
        )

        self.assertEqual(getNumEmails(), num_emails + 1)
        email_response = getLastEmail()
        self.assertEquals(email_response.email_to, config['email.moderator'])
        self.assertIn('They said', email_response.content_text)

        # Get raised flag from DB, will error on fail, then delete/cleanup
        flag = Session.query(FlaggedEntity).filter(
            FlaggedEntity.raising_member_id == 'unitfriend').filter(
                FlaggedEntity.offending_message_id == message['id']).one()
        Session.delete(flag)

        self.delete_message(message['id'])
Ejemplo n.º 9
0
    def test_summary_emails(self):
        
        def task_summary_notification_email():
            response = self.app.get(url(controller='task', action='summary_notification_email'))
            self.assertIn(response_completed_ok, response.body)
        
        now_start = self.server_datetime()
        now = now_start
        
        # No summary emails should trigger yet because no users have setup an interval
        num_emails = getNumEmails()
        task_summary_notification_email()
        self.assertEquals(num_emails, getNumEmails())
        
        # Setup test data ------------------------------------------------------
        self.setting('summary_email_interval', 'advanced', 'hours=1') # Setup summary date
        
        # Execute timed task ---------------------------------------------------
        num_emails = getNumEmails()
        task_summary_notification_email()
        
        # Add message that should trigger in last hour and send notification
        m1 = Message()
        m1.target    = Session.query(User).get('unittest')
        m1.subject   = 'Test summary_notification_email notification'
        m1.content   = 'Test summary_notification_email notification'
        m1.timestamp = now + datetime.timedelta(minutes=30, hours=1)
        Session.add(m1)
        m2 = Message()
        m2.source    = Session.query(User).get('kitten')
        m2.target    = Session.query(User).get('unittest')
        m2.subject   = 'Test summary_notification_email message'
        m2.content   = 'Test summary_notification_email message'
        m2.timestamp = now + datetime.timedelta(minutes=15, hours=1)
        Session.add(m2)
        m3 = Message()
        m3.source    = Session.query(User).get('unittest')
        m3.target    = Session.query(User).get('kitten')
        m3.subject   = 'To Amy kitten'
        m3.content   = 'To Amy kitten'
        m3.timestamp = now + datetime.timedelta(minutes=20, hours=1)
        Session.add(m2)

        Session.commit()
        
        now = self.server_datetime(now + datetime.timedelta(hours=2))
        
        num_emails = getNumEmails()
        task_summary_notification_email()
        
        # Check sent emails ----------------------------------------------------
        
        self.assertEquals(getNumEmails(), num_emails + 1)
        email = getLastEmail().content_text
        self.assertIn   ('summary_notification_email notification', email)
        self.assertIn   ('summary_notification_email message'     , email) 
        self.assertIn   ('To Amy'                                 , email) # Check sent message appears in summary
        self.assertIn   ('Amy M'                                  , email) # Check the avatar and names of targets are aquired from DB
        self.assertNotIn('unitfriend'                             , email) # Loose check to try to catch if any other notifications bleed over into this summary email
        
        # Check no emails sent if outside interval -----------------------------
        now = self.server_datetime(now + datetime.timedelta(hours=20))
        num_emails = getNumEmails()
        task_summary_notification_email()
        self.assertEquals(num_emails, getNumEmails())
        
        # Cleanup --------------------------------------------------------------
        
        # Reset db state
        #self.setting('summary_email_interval', 'advanced', 'hours=0', assert_set_value=False) # hours=0 turns into None and this breaks the auto assertion at the end of set value
        self.setting('summary_email_interval', 'advanced', '', assert_set_value=False) #AllanC - an empty string should default to None
        
        # No summary emails should trigger yet because no users have setup an interval
        num_emails = getNumEmails()
        task_summary_notification_email()
        self.assertEquals(num_emails, getNumEmails()) # AllanC - this fails .. investigation needs to be made
        
        # Reset server datetime
        self.server_datetime(now_start)
        
        # Delete test messages
        Session.delete(m1)
        Session.delete(m2)
        Session.delete(m3)
        Session.commit()
Ejemplo n.º 10
0
def del_member(member):
    member = get_member(member)
    #invalidate_member(member) #invalidate the cache - AllanC unneeded the event triggers handle this
    Session.delete(member)
    Session.commit()
Ejemplo n.º 11
0
def del_content(content):
    content = get_content(content)
    # TODO - AllanC - send notification to group members?
    #invalidate_content(content) # invalidate the cache - AllanC unneeded the event triggers handle this
    Session.delete(content)
    Session.commit()
Ejemplo n.º 12
0
def del_member(member):
    member = get_member(member)
    Session.delete(member)
    Session.commit()
Ejemplo n.º 13
0
def del_message(message):
    Session.delete(message)
    Session.commit()