Ejemplo n.º 1
0
    def _getFileInfo(self, request):
        """Fetch the meta info on a file that is being requested to be
        downloaded. Returns the meta info of the file in question.

        Keyword Arguments:
        itemId: id of the conversation on which this file is attached.
        attachmentId: id of the file on the amazon S3 that is to be served.
        version: version of the file on the amazon S3 that the user is
            requesting.

        """
        authinfo = request.getSession(IAuthInfo)
        myId = authinfo.username
        myOrgId = authinfo.organization
        itemId = utils.getRequestArg(request, "id", sanitize=False)
        attachmentId = utils.getRequestArg(request, "fid", sanitize=False)
        version = utils.getRequestArg(request, "ver", sanitize=False) or ''
        columns = ["meta", "attachments", "participants"]

        if not (itemId and attachmentId):
            raise errors.MissingParams([])

        item = yield db.get_slice(itemId, "mConversations", columns)
        item = utils.supercolumnsToDict(item)
        if not item:
            raise errors.InvalidMessage(itemId)
        if myId not in item.get('participants', {}):
            raise errors.MessageAccessDenied(itemId)

        # Check if the attachmentId belong to item
        if attachmentId not in item['attachments'].keys():
            raise errors.InvalidAttachment(itemId, attachmentId, version)

        fileId, filetype, name = None, 'text/plain', 'file'
        if version:
            version = utils.decodeKey(version)
            try:
                cols = yield db.get(attachmentId, "attachmentVersions", version)
            except ttypes.NotFoundException:
                raise errors.InvalidAttachment(itemId, attachmentId, version)
            except ttypes.InvalidRequestException:
                raise errors.InvalidAttachment(itemId, attachmentId, version)
            cols = utils.columnsToDict([cols])
        else:
            cols = yield db.get_slice(attachmentId, "attachmentVersions", count=1, reverse=True)
            cols = utils.columnsToDict(cols)
            version = cols.keys()[0]


        fileId, name, size, filetype = cols[version].split(':')

        files = yield db.get_slice(fileId, "files", ["meta"])
        files = utils.supercolumnsToDict(files)

        url = files['meta']['uri']
        owner = files["meta"]["owner"]
        defer.returnValue([owner, url, filetype, size, name])
Ejemplo n.º 2
0
    def _listPresetTags(self, request):
        (appchange, script, args, myId) = yield self._getBasicArgs(request)
        orgId = args["orgId"]
        landing = not self._ajax

        args['title'] = 'Preset Tags'
        args['menuId'] = 'tags'
        args["viewType"] = "tags"

        if script and landing:
            t.render(request, "admin.mako", **args)

        if script and appchange:
            t.renderScriptBlock(request, "admin.mako", "layout",
                                    landing, "#mainbar", "set", **args)

        presetTags = yield db.get_slice(orgId, "orgPresetTags", count=100)
        presetTags = utils.columnsToDict(presetTags, ordered=True).values()
        if presetTags:
            tags_ = yield db.get_slice(orgId, "orgTags", presetTags)
            tags_ = utils.supercolumnsToDict(tags_)
        else:
            tags_ = {}

        args['tagsList'] = presetTags
        args['tags'] = tags_
        if script:
            t.renderScriptBlock(request, "admin.mako", "list_tags",
                                    landing, "#content", "set", **args)

        if not script:
            t.render(request, "admin.mako", **args)
Ejemplo n.º 3
0
def untag(itemId, item, tagId, tag, myId, orgId):
    if "parent" in item:
        raise errors.InvalidRequest(_("Tags cannot be applied or removed from comments"))

    if tagId not in item.get("tags", {}):
        raise errors.InvalidRequest(_("No such tag on the item"))  # No such tag on item

    d1 = db.remove(itemId, "items", tagId, "tags")
    d2 = db.remove(tagId, "tagItems", item["meta"]["uuid"])
    d3 = db.get_slice(tagId, "tagFollowers")

    try:
        itemsCountCol = yield db.get(orgId, "orgTags", "itemsCount", tagId)
        tagItemsCount = int(itemsCountCol.column.value) - 1
        if tagItemsCount % 10 == 7:
            tagItemsCount = yield db.get_count(tagId, "tagItems")
            tagItemsCount = tagItemsCount - 1
        db.insert(orgId, "orgTags", "%s" % tagItemsCount, "itemsCount", tagId)
    except ttypes.NotFoundException:
        pass
    result = yield defer.DeferredList([d1, d2, d3])
    followers = utils.columnsToDict(result[2][1]).keys()


    feedUpdateVal = "T:%s:%s::%s" % (myId, itemId, tagId)
    yield Feed.unpush(myId, orgId, itemId, item,
                      feedUpdateVal, followers + [myId])
Ejemplo n.º 4
0
    def _addPresetTag(self, request):
        orgId = request.getSession(IAuthInfo).organization
        tagNames = utils.getRequestArg(request, 'tag')
        if not tagNames:
            return

        invalidTags = []
        tagNames = [x.strip().decode('utf-8', 'replace') for x in tagNames.split(',')]
        for tagName in tagNames:
            if len(tagName) < 50 and regex.match('^[\w-]*$', tagName):
                yield tags.ensureTag(request, tagName, orgId, True)
            else:
                invalidTags.append(tagName)

        presetTags = yield db.get_slice(orgId, "orgPresetTags")
        presetTags = utils.columnsToDict(presetTags, ordered=True).values()

        tags_ = yield db.get_slice(orgId, "orgTags", presetTags)
        tags_ = utils.supercolumnsToDict(tags_)
        args = {'tags': tags_, 'tagsList': presetTags}

        handlers = {}
        if invalidTags:
            if len(invalidTags) == 1:
                message = " %s is invalid tag." % (invalidTags[0])
            else:
                message = " %s are invalid tags. " % (",".join(invalidTags))
            errorMsg = "%s <br/>Tag can contain alpha-numeric characters or hyphen only. It cannot be more than 50 characters" % (message)
            handlers = {'onload': "$$.alerts.error('%s')" % (errorMsg)}

        t.renderScriptBlock(request, "admin.mako", "list_tags",
                            False, "#content", "set", True,
                            handlers=handlers, **args)
Ejemplo n.º 5
0
    def _deletePresetTag(self, request):
        orgId = request.getSession(IAuthInfo).organization
        tagId = utils.getRequestArg(request, 'id')
        if not tagId:
            return

        try:
            tag = yield db.get(orgId, 'orgTags', super_column=tagId)
            tag = utils.supercolumnsToDict([tag])
            tagName = tag[tagId]['title']
            if 'isPreset' in tag[tagId]:
                yield db.remove(orgId, "orgTags", 'isPreset', tagId)
                yield db.remove(orgId, 'orgPresetTags', tagName)
            presetTags = yield db.get_slice(orgId, "orgPresetTags")
            presetTags = utils.columnsToDict(presetTags, ordered=True).values()
            if presetTags:
                tags_ = yield db.get_slice(orgId, "orgTags", presetTags)
                tags_ = utils.supercolumnsToDict(tags)
            else:
                tags_ = {}
            args = {'tagsList': presetTags, 'tags': tags_}
            request.write('$("#tag-%s").remove()' % (tagId))

        except ttypes.NotFoundException:
            return
Ejemplo n.º 6
0
    def _attendance(self, request):
        itemId, item = yield utils.getValidItemId(request, "id",
                                                  columns=["invitees"])
        list_type = utils.getRequestArg(request, 'type') or "yes"
        user_list = []

        if itemId and list_type in ["yes", "no", "maybe"]:
            cols = yield db.get_slice(itemId, "eventResponses")
            res = utils.columnsToDict(cols)
            for rsvp in res.keys():
                resp = rsvp.split(":")[0]
                uid = rsvp.split(":")[1]
                if resp == list_type:
                    if uid in item["invitees"] and \
                      item["invitees"][uid] == list_type:
                        user_list.insert(0, uid)
                    else:
                        user_list.append(uid)

            invited = user_list
            owner = item["meta"].get("owner")

            entities = base.EntitySet(invited+[owner])
            yield entities.fetchData()

            args = {"users": invited, "entities": entities}
            args['title'] = {"yes":_("People attending this event"),
                             "no": _("People not attending this event"),
                             "maybe": _("People who may attend this event")
                             }[list_type]

            t.renderScriptBlock(request, "item.mako", "userListDialog", False,
                                    "#invitee-dlg-%s"%(itemId), "set", **args)
Ejemplo n.º 7
0
def updateAndPublishStatus(userId, orgId, sessionId, status, user=None):
    # Check if there is a change in the online status
    results = yield db.get_slice(orgId, 'presence', super_column=userId)
    results = utils.columnsToDict(results)
    oldPublishedStatus = getMostAvailablePresence(results.values())

    # Update the online status
    if status != 'offline':
        yield db.insert(orgId, 'presence', status, sessionId, userId)
    else:
        yield db.remove(orgId, 'presence', sessionId, userId)

    # If status changed broadcast the change
    # XXX: Currently everyone on the network will get the presence update.
    #      This will not scale well with big networks
    results[sessionId] = status
    newPublishedStatus = getMostAvailablePresence(results.values())

    if oldPublishedStatus != newPublishedStatus:
        if not user:
            user = base.Entity(userId)
            yield user.fetchData()
        data = {"userId": userId, 'status': newPublishedStatus,
                'name': user.basic['name'],
                'title': user.basic['jobTitle'],
                'avatar': utils.userAvatar(userId, user, 's')}
        yield comet.publish('/presence/' + orgId, data)
Ejemplo n.º 8
0
def clearChannels(userId, sessionId):
    key = "%s:%s" % (userId, sessionId)
    cols = yield db.get_slice(key, "sessionChannelsMap")
    channels = utils.columnsToDict(cols).keys()
    for channelId in channels:
        yield db.remove(channelId, "channelSubscribers", key)
    yield db.remove(key, "sessionChannelsMap")
Ejemplo n.º 9
0
    def _getPresence(self, request):
        authInfo = request.getSession(IAuthInfo)
        orgId = authInfo.organization
        myId = authInfo.username
        data = []

        cols = yield db.get_slice(orgId, "presence")
        cols = utils.supercolumnsToDict(cols)
        if myId not in cols:
            myPresence = yield db.get_slice(orgId, "presence", super_column=myId)
            cols[myId] = utils.columnsToDict(myPresence)

        presence = {}
        for userId in cols:
            presence[userId] = getMostAvailablePresence(cols[userId].values())

        if presence[myId] == PresenceStates.OFFLINE:
            request.write(json.dumps(data))
            return

        userIds = cols.keys()
        entities = base.EntitySet(userIds)
        yield entities.fetchData()
        for entityId in entities.keys():
            entity = entities[entityId]
            _data = {"userId": entityId, "name": entity.basic['name'],
                     "status": presence.get(entityId, PresenceStates.OFFLINE),
                     "title": entity.basic["jobTitle"],
                     "avatar": utils.userAvatar(entityId,  entity, 's')}
            data.append(_data)

        request.write(json.dumps(data))
Ejemplo n.º 10
0
def tag(itemId, item, tagName, myId, orgId):
    if "parent" in item["meta"]:
        raise errors.InvalidRequest(_("Tag cannot be applied on a comment"))

    (tagId, tag) = yield tags._ensureTag(tagName, myId, orgId)
    if tagId in item.get("tags", {}):
        raise errors.InvalidRequest(_("Tag already exists on the choosen item"))

    d1 = db.insert(itemId, "items", myId, tagId, "tags")
    d2 = db.insert(tagId, "tagItems", itemId, item["meta"]["uuid"])
    d3 = db.get_slice(tagId, "tagFollowers")

    tagItemsCount = int(tag.get("itemsCount", "0")) + 1
    if tagItemsCount % 10 == 7:
        tagItemsCount = yield db.get_count(tagId, "tagItems")
        tagItemsCount += 1
    db.insert(orgId, "orgTags", "%s" % tagItemsCount, "itemsCount", tagId)

    result = yield defer.DeferredList([d1, d2, d3])
    followers = utils.columnsToDict(result[2][1]).keys()

    if followers:
        timeUUID = uuid.uuid1().bytes
        feedUpdateVal = "T:%s:%s::%s" % (myId, itemId, tagId)
        yield Feed.push(myId, orgId, itemId, item, timeUUID,
                        feedUpdateVal, feeds=followers)

    defer.returnValue((tagId, tag))
Ejemplo n.º 11
0
 def callback(result):
     cols = utils.columnsToDict(result)
     if not utils.checkpass(password, cols.get("passwordHash", "XXX")):
         return self._renderSigninForm(request, self.AUTHENTICATION_FAILED)
     if cols.has_key("isBlocked"):
         return self._renderSigninForm(request, self.USER_BLOCKED)
     if cols.has_key("isFlagged"):
         return self._renderSigninForm(request, self.USER_FLAGGED)
     self._saveSessionAndRedirect(request, cols, remember)
Ejemplo n.º 12
0
 def removeLikeFromFeeds(result):
     likes = utils.columnsToDict(result)
     removeLikeDeferreds = []
     for actorId, likeUUID in likes.items():
         likeUpdateVal = "L:%s:%s:%s" % (actorId, itemId, itemOwnerId)
         d1 = feed.deleteUserFeed(actorId, convType, likeUUID)
         d2 = Feed.unpush(actorId, orgId, convId, conv, likeUpdateVal)
         removeLikeDeferreds.extend([d1, d2])
     return defer.DeferredList(removeLikeDeferreds)
Ejemplo n.º 13
0
def getGroups(me, entity, start='', count=PEOPLE_PER_PAGE):
    """get the groups of entity. (either groups of an organization
    or groups of an user)

    keyword params:
    @me:
    @entity: org/user
    start: fetch group from @start
    count: no.of groups to fetch.
    """
    toFetchCount = count + 1
    groups = {}
    groupIds = []
    myGroupsIds = []
    groupFollowers = {}
    pendingConnections = {}
    toFetchGroups = set()
    nextPageStart = ''
    prevPageStart = ''

    #TODO: list the groups in sorted order.
    cols = yield db.get_slice(entity.id, 'entityGroupsMap',
                              start=start, count=toFetchCount)
    groupIds = [x.column.name for x in cols]
    if len(groupIds) > count:
        nextPageStart = utils.encodeKey(groupIds[-1])
        groupIds = groupIds[0:count]
    toFetchGroups.update(set([y.split(':', 1)[1] for y in groupIds]))
    if entity.id == me.id:
        myGroupsIds = [x.split(':', 1)[1] for x in groupIds]
    elif groupIds:
        cols = yield db.get_slice(me.id, "entityGroupsMap", groupIds)
        myGroupsIds = [x.column.name.split(':', 1)[1] for x in cols]
    groupIds = [x.split(':', 1)[1] for x in groupIds]

    if start:
        cols = yield db.get_slice(entity.id, 'entityGroupsMap', start=start,
                                  count=toFetchCount, reverse=True)
        if len(cols) > 1:
            prevPageStart = utils.encodeKey(cols[-1].column.name)

    if toFetchGroups:
        groups = base.EntitySet(toFetchGroups)
        yield groups.fetchData()
        groupFollowers = yield db.multiget_slice(toFetchGroups, "followers",
                                                 names=[me.id])
        groupFollowers = utils.multiColumnsToDict(groupFollowers)
        columns = reduce(lambda x, y: x + y, [["GO:%s" % (x), "GI:%s" % (x)] for x in toFetchGroups])
        cols = yield db.get_slice(me.id, 'pendingConnections', columns)
        pendingConnections = utils.columnsToDict(cols)

    data = {"entities": groups, "groupIds": groupIds,
            "pendingConnections": pendingConnections,
            "myGroups": myGroupsIds, "groupFollowers": groupFollowers,
            "nextPageStart": nextPageStart, "prevPageStart": prevPageStart}
    defer.returnValue(data)
Ejemplo n.º 14
0
def makeAdmin(emailId):
    userAuth = yield db.get_slice(emailId, "userAuth")
    if not userAuth:
        raise Exception('User does not exist')

    userAuth = utils.columnsToDict(userAuth)
    orgId = userAuth["org"]
    userId = userAuth["user"]
    yield db.insert(orgId, "entities", "", userId, "admins")
    yield db.insert(emailId, "userAuth", "True", "isAdmin")
Ejemplo n.º 15
0
def sendInvitations(sender):
    cols = yield db.get_slice(sender, "userAuth")
    senderInfo = utils.columnsToDict(cols)
    senderOrgId = senderInfo["org"]
    senderId = senderInfo["user"]

    cols = yield db.multiget_slice([senderId, senderOrgId], "entities", ["basic"])
    entities = utils.multiSuperColumnsToDict(cols)

    emails = sys.stdin.readlines()
    emails = [x.strip() for x in emails]
    yield people._sendInvitations([], emails, entities[senderId], senderId, entities[senderOrgId])
Ejemplo n.º 16
0
def edit(me, group, name, access, desc, displayPic):
    """update group meta info.
    Only group-admin can edit group meta info.

    Keyword params:
    @me:
    @group:
    @name: name of the group.
    @access: group access type (open/closed).
    @desc: description of the group.
    @displayPic: profile pic of the group.

    """
    if me.id not in group.admins:
        raise errors.PermissionDenied('Only administrator can edit group meta data')
    if name:
        start = name.lower() + ':'
        cols = yield db.get_slice(me.basic['org'], "entityGroupsMap",
                                  start=start, count=1)
        for col in cols:
            name_, groupId_ = col.column.name.split(':')
            if name_ == name.lower() and groupId_ != group.id:
                raise errors.InvalidGroupName(name)

    meta = {'basic': {}}
    if name and name != group.basic['name']:
        meta['basic']['name'] = name
    if desc and desc != group.basic.get('desc', ''):
        meta['basic']['desc'] = desc
    if access in ['closed', 'open'] and access != group.basic['access']:
        meta['basic']['access'] = access
    if displayPic:
        avatar = yield saveAvatarItem(group.id, me.basic['org'], displayPic)
        meta['basic']['avatar'] = avatar
    if name and name != group.basic["name"]:
        members = yield db.get_slice(group.id, "groupMembers")
        members = utils.columnsToDict(members).keys()
        entities = members + [me.basic['org']]
        oldColName = "%s:%s" % (group.basic["name"].lower(), group.id)
        colname = '%s:%s' % (name.lower(), group.id)
        mutations = {}
        for entity in entities:
            mutations[entity] = {'entityGroupsMap': {colname: '',
                                                     oldColName: None}}
        #XXX:notify group-members about the change in name
        yield db.batch_mutate(mutations)

    if meta['basic']:
        yield db.batch_insert(group.id, 'entities', meta)
    if not desc and group.basic.get('desc', ''):
        yield db.remove(group.id, "entities", 'desc', 'basic')
    if (not desc and group.basic.get('desc', '')) or meta['basic']:
        defer.returnValue(True)
Ejemplo n.º 17
0
def getManagedGroups(me, start, count=PEOPLE_PER_PAGE):
    """get all groups managed by me

    Keyword params:
    @me:
    @start: get groups from @start.
    @count: no.of groups to be fetched.
    """
    groups = {}
    groupIds = []
    myGroupsIds = []
    nextPageStart = ''
    prevPageStart = ''
    toFetchCount = count + 1
    toFetchGroups = set()
    groupFollowers = {}
    pendingConnections = {}

    try:
        cols = yield db.get_slice(me.id, "entities",
                                  super_column='adminOfGroups',
                                  start=start, count=toFetchCount)
        groupIds = [x.column.name for x in cols]
        toFetchGroups.update(set(groupIds))
        myGroupsIds = groupIds
        if len(groupIds) > count:
            nextPageStart = utils.encodeKey(groupIds[-1])
            groupIds = groupIds[0:count]
    except ttypes.NotFoundException:
        pass

    if start:
        cols = yield db.get_slice(me.id, "entities",
                                  super_column='adminOfGroups', start=start,
                                 count=toFetchCount, reverse=True)
        if len(cols) > 1:
            prevPageStart = utils.encodeKey(cols[-1].column.name)

    if toFetchGroups:
        groups = base.EntitySet(toFetchGroups)
        yield groups.fetchData()
        groupFollowers = yield db.multiget_slice(toFetchGroups, "followers", names=[me.id])
        groupFollowers = utils.multiColumnsToDict(groupFollowers)
        columns = reduce(lambda x, y: x + y, [["GO:%s" % (x), "GI:%s" % (x)] for x in toFetchGroups])
        cols = yield db.get_slice(me.id, 'pendingConnections', columns)
        pendingConnections = utils.columnsToDict(cols)

    data = {"entities": groups, "groupIds": groupIds,
            "pendingConnections": pendingConnections,
            "myGroups": myGroupsIds, "groupFollowers": groupFollowers,
            "nextPageStart": nextPageStart, "prevPageStart": prevPageStart}
    defer.returnValue(data)
Ejemplo n.º 18
0
    def _getFileInfo(self, request):
        authinfo = request.getSession(IAuthInfo)
        myId = authinfo.username
        myOrgId = authinfo.organization

        itemId, item = yield utils.getValidItemId(request, 'id')
        attachmentId = utils.getRequestArg(request, "fid", sanitize=False)
        version = utils.getRequestArg(request, "ver", sanitize=False)

        if not attachmentId:
            raise errors.MissingParams()

        # Check if the attachmentId belong to item
        if attachmentId not in item['attachments'].keys():
            raise errors.EntityAccessDenied("attachment", attachmentId)
        if version:
            version = utils.decodeKey(version)
            cols = yield db.get(attachmentId, "attachmentVersions", version)
            cols = utils.columnsToDict([cols])
        else:
            cols = yield db.get_slice(attachmentId, 'attachmentVersions', count=1, reverse=True)
            cols = utils.columnsToDict(cols)
            version = cols.keys()[0]

        if not cols:
            raise errors.InvalidRequest()

        fileId, fileType, name = None, 'text/plain', 'file'
        fileId, name, size, fileType = cols[version].split(':')

        files = yield db.get_slice(fileId, "files", ["meta"])
        files = utils.supercolumnsToDict(files)

        url = files['meta']['uri']
        owner = files["meta"]["owner"]
        defer.returnValue([owner, url, fileType, size, name])
Ejemplo n.º 19
0
    def _verifyProfile(self, request):
        email = utils.getRequestArg(request, 'email')
        token = utils.getRequestArg(request, 'token')

        if not (email and token):
            raise MissingParams(['Email', 'Account Verification Token'])

        cols = yield db.get_slice(email, "userAuth", ["reactivateToken", "isFlagged"])
        cols = utils.columnsToDict(cols)
        if "isFlagged" in cols:
            storedToken = cols.get("reactivateToken", None)
            if storedToken == token:
                yield db.batch_remove({"userAuth": [email]},
                                    names=["reactivateToken", "isFlagged"])

        request.redirect('/signin')
Ejemplo n.º 20
0
def getPeople(myId, entityId, orgId, start='',
              count=PEOPLE_PER_PAGE, fn=None, fetchBlocked=True):
    blockedUsers = []
    toFetchCount = count + 1
    nextPageStart = None
    prevPageStart = None
    userIds = []

    if fetchBlocked:
        cols = yield db.get_slice(orgId, "blockedUsers")
        blockedUsers = utils.columnsToDict(cols).keys()

    if not fn:
        d1 = db.get_slice(entityId, "displayNameIndex",
                          start=start, count=toFetchCount)
        d2 = db.get_slice(entityId, "displayNameIndex",
                          start=start, count=toFetchCount,
                          reverse=True) if start else None

        # Get the list of users (sorted by displayName)
        cols = yield d1
        userIds = [col.column.name.split(":")[1] for col in cols]
        if len(userIds) > count:
            nextPageStart = utils.encodeKey(cols[-1].column.name)
            userIds = userIds[0:count]

        toFetchUsers = userIds

        # Start of previous page
        if start and d2:
            prevCols = yield d2
            if len(prevCols) > 1:
                prevPageStart = utils.encodeKey(prevCols[-1].column.name)
    else:
        userIds, nextPageStart, prevPageStart\
                                = yield fn(entityId, start, toFetchCount)
        toFetchUsers = userIds

    entities = base.EntitySet(toFetchUsers)
    usersDeferred = entities.fetchData()
    relation = Relation(myId, userIds)
    results = yield defer.DeferredList([usersDeferred,
                                        relation.initSubscriptionsList()])

    defer.returnValue((entities, relation, userIds,\
                       blockedUsers, nextPageStart, prevPageStart))
Ejemplo n.º 21
0
        def callback(cols):
            avatarInfo = utils.columnsToDict(cols)
            format = avatarInfo["format"]\
                     if avatarInfo.has_key("format") else "jpg"
            data = avatarInfo[size]
            expires = formatdate(time.time() + 31536000)

            request.setHeader('Content-Type', 'image/%s' % format)
            request.setHeader('Content-Length', len(data))
            request.setHeader('Cache-Control', 'public')
            request.setHeader('Expires', expires)
            try:
                timestamp = cols[0].column.timestamp
                request.setHeader('Last-Modified', formatdate(timestamp/1e6))
            except Exception, e: pass
            request.write(data)
            request.finish()
Ejemplo n.º 22
0
    def _addKeywords(self, request):
        orgId = request.getSession(IAuthInfo).organization
        original = utils.getRequestArg(request, 'keywords', sanitize=False)
        if not original:
            return

        original = original.split(',')
        decoded = set([x.decode('utf-8', 'replace').strip() for x in original])

        # Check if any word is longer than 50 chars
        decoded = set([x for x in decoded if len(x) < 50])
        if len(decoded) != len(original):
            raise errors.InvalidRequest(_('Keywords cannot be more than 50 characters long'))

        # Check if any word has invalid characters
        decoded = set([x for x in decoded if regex.match('^[\w-]*$', x)])
        if len(decoded) != len(original):
            raise errors.InvalidRequest(_('Keywords can only include numerals, alphabet and hyphens (-)'))

        # Convert to lower case
        decoded = set([x.lower() for x in decoded])

        # Remove stopwords
        keywords = set([x.encode('utf-8') for x in decoded\
                        if x not in stopwords.words()])

        # After all this if we still have words, add them to database
        if keywords:
            for keyword in keywords:
                yield db.insert(orgId, "keywords", '', keyword)
                yield db.insert(orgId, "originalKeywords", '', keyword)

            keywords = yield db.get_slice(orgId, "originalKeywords", count=100)
            keywords = utils.columnsToDict(keywords, ordered=True)
            args = {'keywords': keywords}

            t.renderScriptBlock(request, "admin.mako", "listKeywords",
                                    False, "#content", "set", **args)

        # We may want to display an error when we removed stopwords.
        if len(keywords) < len(decoded):
            pass
Ejemplo n.º 23
0
def _sendSignupInvitation(emailId):
    if len(emailId.split('@')) != 2:
        raise InvalidEmailId()

    mailId, domain = emailId.split('@')
    if domain in blacklist:
        raise DomainBlacklisted()

    rootUrl = config.get('General', 'URL')
    brandName = config.get('Branding', 'Name')
    signature = "Flocked-in Team.\n\n\n\n"

    myOrgId = yield getOrgId(domain)
    if myOrgId:
        entities = yield db.get_slice(myOrgId, "entities", ["basic"])
        myOrg = utils.supercolumnsToDict(entities)
        orgName = myOrg['basic']['name']
    else:
        orgName = domain

    existing = yield db.get_slice(emailId, "userAuth", ["user"])
    existing = utils.columnsToDict(existing)
    if existing and existing.get('user', ''):
        subject = "[%s] Account exists" % (brandName)
        body = "You already have an account on %(brandName)s.\n"\
               "Please visit %(rootUrl)s/signin to sign-in.\n\n"
        textBody = (body + signature) % locals()
        htmlBody = t.getBlock("emails.mako", "accountExists", **locals())
    else:
        subject = "Welcome to %s" % (brandName)
        body = "Please click the following link to join %(orgName)s network on %(brandName)s\n"\
               "%(activationUrl)s\n\n"
        activationTmpl = "%(rootUrl)s/signup?email=%(emailId)s&token=%(token)s"

        token = utils.getRandomKey()
        insert_d = db.insert(domain, "invitations", emailId, token, emailId)
        activationUrl = activationTmpl % locals()
        textBody = (body + signature) % locals()
        htmlBody = t.getBlock("emails.mako", "signup", **locals())
        yield insert_d

    yield utils.sendmail(emailId, subject, textBody, htmlBody)
Ejemplo n.º 24
0
    def _ensureAuth(self, request, rsrc):
        accessToken = utils.getRequestArg(request, 'access_token')
        if not accessToken:
            authHeader = request.getHeader("Authorization")
            if authHeader and authHeader.startswith("Bearer"):
                accessToken = authHeader.split("Bearer ", 1)[1]
            else:
                request.setHeader("WWW-Authenticate", 'Bearer realm="Flocked-in API"')
                defer.returnValue(errors.APIErrorPage(401, http.RESPONSES[401]))

        accessTokenData = yield db.get_slice(accessToken, "oAuthData")
        accessTokenData = utils.columnsToDict(accessTokenData)
        if not accessTokenData:
            request.setHeader("WWW-Authenticate",
                              'Bearer realm="Flocked-in API", error="invalid_token"')
            defer.returnValue(errors.APIErrorPage(401, 'Invalid Access Token'))
        else:
            request.apiAccessToken = _APIAccessToken(accessTokenData)

        defer.returnValue(rsrc)
Ejemplo n.º 25
0
    def _render(self, request):
        (appchange, script, args, myId) = yield self._getBasicArgs(request)
        landing = not self._ajax

        title = "Applications"
        args["title"] = title
        args["detail"] = "apps"

        if script and landing:
            t.render(request, "apps.mako", **args)

        if appchange and script:
            t.renderScriptBlock(request, "apps.mako", "layout", landing, "#mainbar", "set", **args)

        if script:
            self.setTitle(request, title)

        # XXX: Currently fetching all available apps under each category.
        #      In future implement pagination here.
        appIds = yield db.get_slice(myId, "entities", ["apikeys", "apps"], count=100)
        appIds = utils.supercolumnsToDict(appIds, timestamps=True)

        appsByMe = yield db.get_slice(myId, "appsByOwner", count=100)
        appIds["my"] = utils.columnsToDict(appsByMe)

        toFetchClients = set()
        for val in appIds.values():
            toFetchClients.update(val.keys())

        clients = yield db.multiget_slice(toFetchClients, "apps")
        clients = utils.multiSuperColumnsToDict(clients)

        toFetchEntities = set([x.author for x in clients if "author" in x])
        entities = base.EntitySet(toFetchEntities)
        yield entities.fetchData()

        args.update({"entities": entities, "clients": clients, "apps": appIds})
        if script:
            t.renderScriptBlock(request, "apps.mako", "appListing", landing, "#apps-contents", "set", **args)
        else:
            t.render(request, "apps.mako", **args)
Ejemplo n.º 26
0
    def _reportUser(self, request, myId, targetId):

        entities = base.EntitySet([myId, targetId])
        yield entities.fetchData()
        reportedBy = entities[myId].basic["name"]
        email = entities[targetId].basic["emailId"]
        rootUrl = config.get('General', 'URL')
        brandName = config.get('Branding', 'Name')
        authinfo = request.getSession(IAuthInfo)
        amIAdmin = authinfo.isAdmin

        cols = yield db.get_slice(email, "userAuth", ["reactivateToken",
                                                      "isFlagged", "isAdmin"])
        cols = utils.columnsToDict(cols)
        if cols.has_key("isAdmin") and not amIAdmin:
            raise errors.PermissionDenied("Only administrators can flag other \
                                            administrators for verification")

        if cols.has_key("isFlagged"):
            token = cols.get("reactivateToken")
        else:
            token = utils.getRandomKey()
            yield db.insert(email, "userAuth", token, 'reactivateToken')
            yield db.insert(email, "userAuth", "", 'isFlagged')

        body = "%(reportedBy)s has flagged your account for verification."\
               "You can verify your account by clicking on the link below.\n"\
               "\n\n%(reactivateUrl)s\n\n"

        reactivateUrl = "%(rootUrl)s/password/verify?email=%(email)s&token=%(token)s"%(locals())
        args = {"brandName": brandName, "rootUrl": rootUrl,
                "reportedBy":reportedBy, "reactivateUrl": reactivateUrl}
        subject = "[%(brandName)s] Your profile has been flagged for review" %(locals())
        htmlBody = t.getBlock("emails.mako", "reportUser", **args)
        textBody = body %(locals())

        yield utils.sendmail(email, subject, textBody, htmlBody)

        request.write('$$.alerts.info("%s");' % _('User has been flagged for verification'))
Ejemplo n.º 27
0
    def _renderKeywordManagement(self, request):
        (appchange, script, args, myId) = yield self._getBasicArgs(request)
        orgId = args["orgId"]
        landing = not self._ajax

        args["title"] = "Keyword Monitoring"
        args["menuId"] = "keywords"

        if script and landing:
            t.render(request, "admin.mako", **args)

        if script and appchange:
            t.renderScriptBlock(request, "admin.mako", "layout",
                                    landing, "#mainbar", "set", **args)

        keywords = yield db.get_slice(orgId, "originalKeywords")
        keywords = utils.columnsToDict(keywords, ordered=True)
        args['keywords'] = keywords

        if script:
            t.renderScriptBlock(request, "admin.mako", "listKeywords",
                                landing, "#content", "set", **args)
Ejemplo n.º 28
0
def getAllInvitations(me, start='', count=PEOPLE_PER_PAGE):
    """get all group invitations sent to @me starting from @start.

    Keyword params:
    @me:
    @start: fetch invitations starting from @start.
    @count: no.of invitations to be fetched.
    """
    if not start:
        start = 'GI'
    toFetchCount = count + 1
    nextPageStart = ''
    prevPageStart = ''
    toFetchEntities = set()

    cols = yield db.get_slice(me.id, "pendingConnections",
                              start=start, count=toFetchCount)
    groupIds = [x.column.name.split(':')[1] for x in cols if len(x.column.name.split(':')) == 2 and x.column.name.split(':')[0] == 'GI']
    pendingConnections = utils.columnsToDict(cols)
    if len(groupIds) == toFetchCount:
        groupIds = groupIds[:count]
        nextPageStart = utils.encodeKey(cols[-1].column.name)
    toFetchEntities.update(groupIds)
    cols = yield db.get_slice(me.id, "pendingConnections", reverse=True,
                              start=start, count=toFetchCount)
    cols = [x for x in cols if len(x.column.name.split(':')) == 2 and x.column.name.split(':')[1] == 'GI']

    if len(cols) > 1:
        prevPageStart = utils.encodeKey(cols[-1].column.name)

    entities = base.EntitySet(toFetchEntities)
    yield entities.fetchData()
    entities.update(me)
    defer.returnValue({"groupIds": groupIds, "entities": entities,
                        "myGroups": [], "prevPageStart": prevPageStart,
                        "nextPageStart": nextPageStart,
                        "pendingConnections": pendingConnections,
                        "groupFollowers": dict([(x, []) for x in groupIds])})
Ejemplo n.º 29
0
    def _renderNotifications(self, request):
        (appchange, script, args, myId) = yield self._getBasicArgs(request)
        landing = not self._ajax
        args["menuId"] = "notifications"

        if script and landing:
            t.render(request, "notifications.mako", **args)

        if script and appchange:
            t.renderScriptBlock(request, "notifications.mako", "layout",
                                landing, "#mainbar", "set", **args)

        start = utils.getRequestArg(request, "start") or ''
        fromFetchMore = ((not landing) and (not appchange) and start)
        data = yield self._getNotifications(request)

        latest = yield db.get_slice(myId, "latest", super_column="notifications")
        latest = utils.columnsToDict(latest)
        latestNotifyIds = [x for x in latest.values()]

        if not start:
            yield db.remove(myId, "latest", super_column="notifications")

        args.update(data)
        args['latestNotifyIds'] = latestNotifyIds
        if script:
            if fromFetchMore:
                t.renderScriptBlock(request, "notifications.mako", "content",
                                    landing, "#next-load-wrapper", "replace",
                                    True, handlers={}, **args)
            else:
                t.renderScriptBlock(request, "notifications.mako", "content",
                                    landing, "#notifications", "set", **args)
            yield utils.render_LatestCounts(request, landing)
        else:
            t.render(request, "notifications.mako", **args)
Ejemplo n.º 30
0
def _ensureTag(tagName, myId, orgId, presetTag=False):
    try:
        tagName = tagName.lower()
        c = yield db.get(orgId, "orgTagsByName", tagName)
        tagId = c.column.value
        c = yield db.get_slice(orgId, "orgTags", super_column=tagId)
        tag = utils.columnsToDict(c)
        if presetTag and not tag.get('isPreset', '') == 'True':
            yield db.insert(orgId, "orgPresetTags", tagId, tagName)

            yield db.insert(orgId, "orgTags", 'True', 'isPreset', tagId)
            tag['isPreset'] = 'True'
    except ttypes.NotFoundException:
        tagId = utils.getUniqueKey()
        tag = {"title": tagName, 'createdBy': myId}
        if presetTag:
            tag['isPreset'] = 'True'
        tagName = tagName.lower()
        yield db.batch_insert(orgId, "orgTags", {tagId: tag})
        yield db.insert(orgId, "orgTagsByName", tagId, tagName)
        if presetTag:
            yield db.insert(orgId, "orgPresetTags", tagId, tagName)

    defer.returnValue((tagId, tag))