Beispiel #1
0
def dispatchActivityStartWarnning(m):
    content = json.loads(m.content)
    mediaId = content.get("activityId")
    mediaInfo = mediaSerializer(objectToDict(getMediaById(mediaId)))
    userInfo = userSerializer(
        objectToDict(UserService().getById(uha.TUser(),
                                           content.get("userId"))))
    content = {}

    content["usersInfo"] = [userInfo]
    content["mediaInfo"] = mediaInfo
    m.content = json.dumps(content)
    offset = 0
    total = 0
    query = {"filter": {"activityId": mediaId}}
    while offset >= total:
        ret = PublishService().query(uha.TParticipantInfo(), json.dumps(query))
        offset = ret.offset
        total = ret.total
        m.type = "user.activity.attention.start.warning"
        startTime = time.strftime(
            "%m月%d日%H时%M分",
            time.gmtime(mediaInfo["activityInfo"]["startTime"]))
        for r in ret.result:
            m.dispatchTo = r.userId
            m.alert = AlertTemplate.activity_warning.format(
                userName=userInfo["name"],
                activityName=mediaInfo["title"],
                startTime=startTime)
            cachedDispatchMessage(m)
Beispiel #2
0
def getMembersByName(pageId, name):
        query = {}
        query["filter"] = {}
        query["filter"]["pageId"] = pageId
        query["limit"] = 1000
        query["offset"] = 0
       
        queryString = json.dumps(query)
        
        ms = govpageService().query(uha.TGovPageMember(), queryString)
        membersInfo = []
        
        for i in ms.result:
            d = objectToDict(i)
            u = userSerializer({"id": d["userId"]})
            if "name" not in u:
                continue
            d["name"] = u["name"]
            if d["name"] == name:
                membersInfo.append({
                    "name": d["name"],
                    "userId": d["userId"],
                    "memberId": d["id"]
                })
        if len(membersInfo) != 1:
            print "error", pageId, membersInfo
        return membersInfo
Beispiel #3
0
def testDispatchMessage(id):
    m = NoticeService().getById(uha.TNoticeMessage(), id)
    if m:
        m = dictToObject(objectToDict(m), uha.TDispatchMessage())
        m = dispatchMessage(m)
        logger.info(m)
    else:
        logger.info("not valid id %s" % id)
Beispiel #4
0
def generateUserData(user):
    try:
        d = userSerializer(objectToDict(user))
        data = uha.TRecommenderData()
        data.type = "UHA_TUSER"
        data.docString = json.dumps(d)
        recommenderService().updateData(data)
    except Exception as e:
        logger.error(e)
Beispiel #5
0
def generateMediaData(media):
    try:
        d = mediaSerializer(objectToDict(media))
        data = uha.TRecommenderData()
        if not d["isValid"]:
            deleteMediaCascade(d.get("id"))
        data.type = "UHA_TMEDIA"
        data.docString = json.dumps(d)
        recommenderService().updateData(data)
    except Exception as e:
        logger.error(e)
Beispiel #6
0
def generateGovpageData(govpage):
    try:
        d = govPageSerializer(objectToDict(govpage))
        data = uha.TRecommenderData()
        data.type = "UHA_TGOVPAGE"
        data.docString = json.dumps(d)
        recommenderService().updateData(data)

        deleteGovpageMembersCascade(d.get("id"))
    except Exception as e:
        logger.error(e)
Beispiel #7
0
def dispatchMessage(dispatchMessage):
    if dispatchMessage.type in ["user.media.comment", "user.media.vote"]:
        content = json.loads(dispatchMessage.content)
        mediaId = content.get("targetId")
        mediaInfo = getMediaById(mediaId)
        userInfo = UserService().getById(uha.TUser(), content.get("userId"))

        content["mediaInfo"] = mediaSerializer(objectToDict(mediaInfo))
        content["userInfo"] = userSerializer(objectToDict(userInfo))
        dispatchMessage.content = json.dumps(content)

        dispatchMessage.dispatchTo = mediaInfo.promulgatorId
        if dispatchMessage.type == "user.media.comment":
            dispatchMessage.alert = "%s 评论了 %s" % (userInfo.name,
                                                   mediaInfo.title)
        elif dispatchMessage.type == "user.media.vote":
            dispatchMessage.alert = "%s 赞了 %s" % (userInfo.name,
                                                  mediaInfo.title)
        dispatchMessage = cachedDispatchMessage(dispatchMessage)
    elif isMatchType(dispatchMessage.type, ["user.activity"]):
        content = json.loads(dispatchMessage.content)
        mediaId = content.get("activityId")
        mediaInfo = getMediaById(mediaId)
        userInfo = UserService().getById(uha.TUser(), content.get("userId"))

        content["mediaInfo"] = mediaSerializer(objectToDict(mediaInfo))
        content["userInfo"] = userSerializer(objectToDict(userInfo))
        dispatchMessage.content = json.dumps(content)

        dispatchMessage.dispatchTo = mediaInfo.promulgatorId
        if dispatchMessage.type == "user.activity.join":
            dispatchMessage.alert = "%s 申请加入了 %s 活动" % (userInfo.name,
                                                        mediaInfo.title)
        elif dispatchMessage.type == "user.activity.exit":
            dispatchMessage.alert = "%s 退出了 %s 活动" % (userInfo.name,
                                                      mediaInfo.title)
        dispatchMessage = cachedDispatchMessage(dispatchMessage)
        dealWithActivityMessage(dispatchMessage)

    elif isMatchType(dispatchMessage.type, [
            "govpage.report.media", "govpage.contribute",
            "govpage.confirm.contribute"
    ]):
        content = json.loads(dispatchMessage.content)
        mediaId = content.get("sourceMediaId")
        pageId = content.get("pageId")
        mediaInfo = getMediaById(mediaId)
        userInfo = UserService().getById(uha.TUser(), mediaInfo.promulgatorId)

        content["mediaInfo"] = mediaSerializer(objectToDict(mediaInfo))
        content["userInfo"] = userSerializer(objectToDict(userInfo))
        dispatchMessage.content = json.dumps(content)

        if dispatchMessage.type.find("govpage.contribute") == 0:
            dispatchMessage.alert = "%s 申请投稿 %s" % (userInfo.name,
                                                    mediaInfo.title)
        elif dispatchMessage.type == "govpage.report.media":
            dispatchMessage.alert = "%s 举报了 %s" % (userInfo.name,
                                                   mediaInfo.title)
        if isMatchType(dispatchMessage.type, ["govpage.confirm.contribute"]):
            dispatchMessage.dispatchTo = dispatchMessage.senderId
            dispatchMessage = cachedDispatchMessage(dispatchMessage)
        roles = ["admin", "creator"]
        ms = getGovpageMembersByRole(pageId, roles)
        logger.info(ms)
        for i in ms.result:
            dispatchMessage.dispatchTo = i.userId
            dispatchMessage = cachedDispatchMessage(dispatchMessage)
            logger.info(dispatchMessage)

    elif dispatchMessage.type in ["govpage.apply.join", "govpage.apply.admin"]:
        content = json.loads(dispatchMessage.content)
        pageId = content.get("pageId")
        pageInfo = GovpageService().getById(uha.TGovPage(), pageId)
        userInfo = UserService().getById(uha.TUser(), dispatchMessage.senderId)

        content["pageInfo"] = govPageSerializer(objectToDict(pageInfo))
        content["userInfo"] = userSerializer(objectToDict(userInfo))
        dispatchMessage.content = json.dumps(content)

        if dispatchMessage.type == "govpage.apply.join":
            dispatchMessage.alert = "%s 申请加入 %s" % (userInfo.name,
                                                    pageInfo.name)
        elif dispatchMessage.type == "govpage.apply.admin":
            dispatchMessage.alert = "%s 申请加入 %s" % (userInfo.name,
                                                    pageInfo.name)
        admins = getGovpageMembersByRole(pageId)
        for i in admins.result:
            dispatchMessage.dispatchTo = i.userId
            dispatchMessage = cachedDispatchMessage(dispatchMessage)
    else:
        dispatchMessage = cachedDispatchMessage(dispatchMessage)
    return dispatchMessage
Beispiel #8
0
def dealWithActivityMessage(dispatchMessage):
    content = json.loads(dispatchMessage.content)
    mediaId = content.get("activityId")
    mediaInfo = mediaSerializer(objectToDict(getMediaById(mediaId)))
    userInfo = userSerializer(
        objectToDict(UserService().getById(uha.TUser(),
                                           content.get("userId"))))

    content["mediaInfo"] = mediaInfo
    content["userInfo"] = userInfo
    dispatchMessage.content = json.dumps(content)

    dispatchMessage.dispatchTo = mediaInfo["promulgatorId"]

    dispatchMessage = cachedDispatchMessage(dispatchMessage)
    if dispatchMessage.type == "user.activity.join":
        filter = {
            "type": "user.activity.attention.join",
            "status": "new",
            "dispatchTo": mediaInfo["promulgatorId"]
        }
        m = NoticeService().getOneByFilter(uha.TDispatchMessage(),
                                           json.dumps(filter))

        if m and json.loads(m.content).get("activityId"):
            m.alert = AlertTemplate.activity_join.format(
                userName=userInfo["name"], activityName=mediaInfo["title"])
            content = json.loads(m.content)
            content["usersInfo"].append(userInfo)
            m.content = json.dumps(content)
            NoticeService().updateById(m)
        else:
            m = copy.deepcopy(dispatchMessage)
            content = {}
            content["usersInfo"] = [userInfo]
            content["mediaInfo"] = mediaInfo
            m.content = json.dumps(content)
            m.type = "user.activity.attention.join"
            m.alert = AlertTemplate.activity_join.format(
                userName=userInfo["name"], activityName=mediaInfo["title"])
            cachedDispatchMessage(m)
    elif dispatchMessage.type == "user.activity.confirm.join":
        m = copy.deepcopy(dispatchMessage)
        content = {}
        content["usersInfo"] = [userInfo]
        content["mediaInfo"] = mediaInfo
        m.content = json.dumps(content)
        m.dispatchTo = userInfo["id"]
        m.alert = AlertTemplate.activity_confirm.format(
            userName=userInfo["name"], activityName=mediaInfo["title"])
        m.type = "user.activity.attention.confirm.join"
        cachedDispatchMessage(m)
    elif dispatchMessage.type == "user.activity.reject.join":
        m = copy.deepcopy(dispatchMessage)
        content = {}
        content["usersInfo"] = [userInfo]
        content["mediaInfo"] = mediaInfo
        m.content = json.dumps(content)
        m.dispatchTo = userInfo["id"]
        m.alert = AlertTemplate.activity_reject.format(
            userName=userInfo["name"], activityName=mediaInfo["title"])
        m.type = "user.activity.attention.reject.join"
        cachedDispatchMessage(m)
    elif "user.activity.start.warning" == dispatchMessage.type:
        m = copy.deepcopy(dispatchMessage)
        dispatchActivityStartWarnning(m)
    elif "user.activity.exit" == dispatchMessage.type:
        m = copy.deepcopy(dispatchMessage)
        content = {}
        content["usersInfo"] = [userInfo]
        content["mediaInfo"] = mediaInfo
        m.content = json.dumps(content)
        m.dispatchTo = userInfo["id"]
        m.alert = AlertTemplate.activity_exit.format(
            activityName=mediaInfo["title"])
        m.type = "user.activity.attention.exit"
        cachedDispatchMessage(m)