Ejemplo n.º 1
0
 def testRenderRecentUsersActivity(self):
     """
     L{RecentUsersActivityResource.deferred_render_GET} renders a response
     with recent activity data by the users returned by the given query.
     """
     objectID = ObjectAPI(self.user).create(u'object1')
     self.store.commit()
     TagValueAPI(self.user).set({objectID: {u'username/tag1': u'A'}})
     runDataImportHandler(self.client.url)
     request = FakeRequest(
         args={'query': ['fluiddb/users/username = "******"']})
     with login(u'username', self.user.objectID, self.transact) as session:
         resource = RecentUsersActivityResource(self.facade, session)
         body = yield resource.deferred_render_GET(request)
         body = json.loads(body)
         expected = [{
             u'about': u'object1',
             u'id': str(objectID),
             u'tag': u'username/tag1',
             u'username': u'username',
             u'value': u'A'
         }]
         # Clean up timestamps.
         for item in body:
             del item['updated-at']
         self.assertEqual(expected, body)
         self.assertEqual(http.OK, request.code)
Ejemplo n.º 2
0
 def testRenderRecentUserActivity(self):
     """
     L{RecentUserActivityResource.deferred_render_GET} renders a response
     with recent activity data for the given user.
     """
     objectID = ObjectAPI(self.user).create(u'object1')
     TagValueAPI(self.user).set({objectID: {u'username/tag1': u'A'}})
     self.store.commit()
     TagValueAPI(self.user).set({objectID: {u'username/tag2': u'B'}})
     self.store.commit()
     request = FakeRequest()
     with login(u'username', self.user.objectID, self.transact) as session:
         resource = RecentUserActivityResource(self.facade, session,
                                               u'username')
         body = yield resource.deferred_render_GET(request)
         body = json.loads(body)
         expected = [{
             u'username': u'username',
             u'about': u'object1',
             u'id': str(objectID),
             u'tag': u'username/tag2',
             u'value': u'B'
         }, {
             u'username': u'username',
             u'about': u'object1',
             u'id': str(objectID),
             u'tag': u'username/tag1',
             u'value': u'A'
         }]
         # Clean up timestamps.
         for item in body:
             del item['updated-at']
         self.assertEqual(expected, body)
         self.assertEqual(http.OK, request.code)
Ejemplo n.º 3
0
def setVersionTag(version):
    """Updates the fluiddb/version tag.

    @param version: The new version string.
    """
    user = getUser(u'fluiddb')
    objectID = ObjectAPI(user).create(u'fluidinfo')
    releaseDate = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
    values = {
        objectID: {
            u'fluiddb/api-version': {
                'mime-type': 'text/plain',
                'contents': version
            },
            u'fluiddb/release-date': {
                'mime-type': 'text/plain',
                'contents': releaseDate + '\n'
            }
        }
    }
    TagValueAPI(user).set(values)
    PermissionAPI(user).set([
        (u'fluiddb/api-version', Operation.READ_TAG_VALUE, Policy.OPEN, []),
        (u'fluiddb/release-date', Operation.READ_TAG_VALUE, Policy.OPEN, [])
    ])

    try:
        transaction.commit()
    except:
        transaction.abort()
        raise
Ejemplo n.º 4
0
    def getObjectAPI(self, user):
        """Get an L{ObjectAPI} instance for the specified user.

        @param user: The L{User} to configure the L{ObjectAPI} instance.
        @return: An L{ObjectAPI} instance.
        """
        return ObjectAPI(user)
Ejemplo n.º 5
0
 def setUp(self):
     super(DataImportHandlerTest, self).setUp()
     createSystemData()
     UserAPI().create([(u'user', u'secret', u'User', u'*****@*****.**')])
     self.user = getUser(u'user')
     self.objects = ObjectAPI(self.user)
     self.values = TagValueAPI(self.user)
     self.index = ObjectIndex(self.client)
Ejemplo n.º 6
0
 def setUp(self):
     super(FluidinfoGlobalTrendingHashtagTest, self).setUp()
     createSystemData()
     UserAPI().create([
         (u'username', u'password', u'User', u'*****@*****.**'),
         (u'fluidinfo.com', u'secret', u'Fluidinfo', u'*****@*****.**')
     ])
     self.user = getUser(u'username')
     self.comments = CommentAPI(self.user)
     objectAPI = ObjectAPI(self.user)
     self.fluidinfObjectID = objectAPI.create(u'fluidinfo.com')
     self.tag = u'fluidinfo.com/trending-hashtags'
Ejemplo n.º 7
0
    def testGetRecentAboutActivity(self):
        """
        L{FacadeRecentActivityMixin.getRecentAboutActivity} returns a C{dict}
        with information about the recent tag values on the given object.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag2': u'D'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag1': u'E'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag2': u'F'}})
        self.store.commit()

        expected = [
            {'tag': u'user/tag2',
             'id': str(objectID1),
             'about': u'object1',
             'value': u'B',
             'username': u'username'},

            {'tag': u'user/tag1',
             'id': str(objectID1),
             'about': u'object1',
             'value': u'A',
             'username': u'username'},

            {'tag': u'fluiddb/about',
             'id': str(objectID1),
             'about': u'object1',
             'value': u'object1',
             'username': u'fluiddb'}]

        with login(self.user.username, uuid4(), self.transact) as session:
            result = yield self.facade.getRecentAboutActivity(
                session, 'object1')

            # Remove the creation times from the result.
            for item in result:
                del item['updated-at']

            self.assertEqual(expected, result)
Ejemplo n.º 8
0
def extractTrendingHashtags(store, limit=10, duration=None):
    """Extract information about trending hashtags and store it in FluidDB.

    @param store: The storm store to query and to save our result to.
    @param limit: Optionally, the number of objects to retrieve.
    @param duration: Optionally, the recent time period to look at when
        determining which hashtags are trending.  Default is 28 days.

    The storm query below results in SQL like:

        SELECT COUNT(DISTINCT comments.object_id) AS count,
               about_tag_values.value,
               array_agg(ROW(comments.username, comments.creation_time))
        FROM about_tag_values, comment_object_link, comments
        WHERE about_tag_values.value LIKE '#%' AND
              about_tag_values.object_id = comment_object_link.object_id AND
              comments.object_id = comment_object_link.comment_id AND
              comments.creation_time >= '2012-11-09 07:42:40'::TIMESTAMP AND
              CHAR_LENGTH(about_tag_values.value) >= 2
        GROUP BY about_tag_values.value
        ORDER BY count DESC
        LIMIT 10
    """
    duration = timedelta(days=28) if duration is None else duration
    startTime = datetime.utcnow() - duration
    count = Alias(Count(Comment.objectID, distinct=True))
    result = store.find(
        (count, AboutTagValue.value,
         Func('array_agg', Row(Comment.username, Comment.creationTime))),
        Like(AboutTagValue.value,
             u'#%'), AboutTagValue.objectID == CommentObjectLink.objectID,
        Comment.objectID == CommentObjectLink.commentID,
        Comment.creationTime >= startTime,
        Func('CHAR_LENGTH', AboutTagValue.value) >= 2)
    result.group_by(AboutTagValue.value)
    result.order_by(Desc(count))
    result.config(limit=limit)

    data = [{
        'count': count,
        'usernames': _sortUsernames(usernames),
        'value': hashtag
    } for count, hashtag, usernames in result]

    user = getUser(u'fluidinfo.com')
    tagValues = TagValueAPI(user)
    objectID = ObjectAPI(user).create(u'fluidinfo.com')
    tagValues.set(
        {objectID: {
            u'fluidinfo.com/trending-hashtags': json.dumps(data)
        }})
    store.commit()
Ejemplo n.º 9
0
 def testRenderRecentObjectActivityWithNoQuery(self):
     """
     L{RecentUsersActivityResource.deferred_render_GET} raises
     L{MissingArgument} if the C{query} argument is not given.
     """
     objectID = ObjectAPI(self.user).create(u'object1')
     self.store.commit()
     TagValueAPI(self.user).set({objectID: {u'username/tag1': u'A'}})
     runDataImportHandler(self.client.url)
     request = FakeRequest()
     with login(u'username', self.user.objectID, self.transact) as session:
         resource = RecentUsersActivityResource(self.facade, session)
         deferred = resource.deferred_render_GET(request)
         yield self.assertFailure(deferred, MissingArgument)
Ejemplo n.º 10
0
    def testGetForUsersReturnsOnlyAllowedTags(self):
        """
        L{SecureRecentActivityAPI.getForUser} returns all the tags for the
        superuser.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()

        UserAPI().create([(u'user2', u'secret', u'User', u'*****@*****.**')])
        tagValues = TagValueAPI(getUser(u'user2'))

        tagValues.set({objectID1: {u'user2/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user2/tag2': u'D'}})
        self.store.commit()

        UserAPI().create([(u'user3', u'secret', u'User', u'*****@*****.**')])
        tagValues = TagValueAPI(getUser(u'user3'))

        tagValues.set({objectID1: {u'user3/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user3/tag2': u'D'}})
        self.store.commit()

        self.permissions.set([
            (u'user/tag2', Operation.READ_TAG_VALUE, Policy.OPEN, [u'user']),
            (u'user2/tag2', Operation.READ_TAG_VALUE, Policy.CLOSED, [])
        ])

        expected = [(u'user2/tag2', objectID2, None, u'D', u'user2'),
                    (u'user2/tag1', objectID1, u'object1', u'C', u'user2'),
                    (u'user/tag2', objectID1, u'object1', u'B', u'user'),
                    (u'user/tag1', objectID1, u'object1', u'A', u'user')]

        result = self.recentActivity.getForUsers([u'user', u'user2'])
        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 11
0
    def testGetForObjectsReturnsOnlyAllowedTags(self):
        """
        L{SecureRecentActivityAPI.getForObjects} will return all the tags for
        which the user has C{Operation.READ_TAG_VALUE} permissions, but not
        those for which the user doesn't have.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')
        objectID2 = uuid4()

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag1': u'A'}})
        self.store.commit()
        tagValues.set({objectID1: {u'user/tag2': u'B'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag1': u'C'}})
        self.store.commit()
        tagValues.set({objectID2: {u'user/tag2': u'D'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag1': u'E'}})
        self.store.commit()
        tagValues.set({uuid4(): {u'user/tag2': u'F'}})
        self.store.commit()

        self.permissions.set([(u'user/tag2', Operation.READ_TAG_VALUE,
                               Policy.OPEN, [u'anon'])])

        expected = [(u'user/tag1', objectID2, None, u'C', u'user'),
                    (u'user/tag1', objectID1, u'object1', u'A', u'user'),
                    (u'fluiddb/about', objectID1, u'object1', u'object1',
                     u'fluiddb')]

        result = self.recentActivity.getForObjects([objectID1, objectID2])

        # Remove the creation times from the result, with the order is enough.
        result = [(path, objectID, about, value, username)
                  for path, objectID, about, value, username, time in result]
        self.assertEqual(expected, result)
Ejemplo n.º 12
0
    def testGetRecentUserActivityForQueryWithObjectsNotUsers(self):
        """
        L{FacadeRecentActivityMixin.getRecentUserActivityForQuery} returns an
        empty result if the objects returned by the query are not users.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/following': u'A'}})
        self.store.commit()

        runDataImportHandler(self.client.url)

        with login(self.user.username, uuid4(), self.transact) as session:
            result = yield self.facade.getRecentUserActivityForQuery(
                session, u'has user/following')

            # Remove the creation times from the result.
            for item in result:
                del item['updated-at']

            self.assertEqual([], result)
Ejemplo n.º 13
0
    def testGetRecentActivityForQuery(self):
        """
        L{FacadeRecentActivityMixin.getRecentActivityForQuery} returns a
        C{dict} with information about the recent tag values on the objects
        returned by the given query.
        """
        tagValues = TagValueAPI(self.user)
        objectID1 = ObjectAPI(self.user).create(u'object1')

        # Use commit() frequently to have different timestamps on each value.
        self.store.commit()
        tagValues.set({objectID1: {u'user/following': u'A'}})
        self.store.commit()

        runDataImportHandler(self.client.url)

        expected = [{'about': u'object1',
                     'id': str(objectID1),
                     'tag': u'user/following',
                     'username': u'username',
                     'value': u'A'},
                    {'about': u'object1',
                     'id': str(objectID1),
                     'tag': u'fluiddb/about',
                     'username': u'fluiddb',
                     'value': u'object1'}]

        with login(self.user.username, uuid4(), self.transact) as session:
            result = yield self.facade.getRecentActivityForQuery(
                session, u'has user/following')

            # Remove the creation times from the result.
            for item in result:
                del item['updated-at']

            self.assertEqual(expected, result)
Ejemplo n.º 14
0
 def __init__(self, user):
     self._api = ObjectAPI(user, factory=CachingAPIFactory())
     self._cache = ObjectCache()
     self._user = user
Ejemplo n.º 15
0
 def objects(self, user):
     """Get a new L{ObjectAPI} instance."""
     from fluiddb.model.object import ObjectAPI
     return ObjectAPI(user)
Ejemplo n.º 16
0
 def setUp(self):
     super(ObjectAPITest, self).setUp()
     self.system = createSystemData()
     UserAPI().create([(u'user', u'secret', u'User', u'*****@*****.**')])
     self.user = getUser(u'user')
     self.objects = ObjectAPI(self.user)
Ejemplo n.º 17
0
    store = setupStore('postgres:///fluidinfo', 'main')
    setConfig(setupConfig(None))

    aboutTag = getTags(paths=[u'fluiddb/about']).one()
    superUser = getUsers(usernames=[u'fluiddb']).one()

    result = store.find(AboutTagValue, AboutTagValue.value.like(u'@%'))
    for aboutValue in result:
        if aboutValue.value == aboutValue.value.lower():
            continue
        print 'Migrating mixed cased', aboutValue.value.encode('utf-8')

        newAbout = u'@%s' % aboutValue.value.lower()
        oldObjectID = aboutValue.objectID
        newObjectID = ObjectAPI(superUser).create(newAbout)
        result = store.find(TagValue,
                            TagValue.objectID == oldObjectID,
                            TagValue.tagID != aboutTag.id)
        for tagValue in result:
            existingValue = getTagValues([(newObjectID, tagValue.tagID)]).one()
            if existingValue is not None:
                error = ('ERROR: Cannot migrate value {path} on {about} '
                         'because the value already exist.')
                print error.format(path=tagValue.tag.path,
                                   about=aboutValue.value.encode('utf-8'))
            else:
                tagValue.objectID = newObjectID

    store.commit()