Exemplo n.º 1
0
class Vote(base.ViewletBase):

    voteAdapter = None
    is_manager = None
    can_vote = None

    # Update methods are guaranteed to be called before rendering for
    # Viewlets and Portlets (Because they are IContentProvider objects)
    # and for z3c.forms and formlib forms. But *not* for normal Browser Pages
    def update(self):
        super(Vote, self).update()

        if self.voteAdapter is None:
            self.voteAdapter = IVoting(self.context)

        if self.is_manager is None:
            self.pm = getToolByName(self.context, 'portal_membership')
            self.is_manager = self.pm.checkPermission(
                ViewManagementScreens, self.context)
            self.can_follow = self.pm.checkPermission(
                DoVote, self.context)

    def voteavailableapproved(self):
        "是否当前用户在赞成队列,true:已在队列中"
        userid = self.pm.getAuthenticatedMember().getId()
        return not(self.voteAdapter.voteavailableapproved(userid))

    def voteavailabledisapproved(self):
        "是否当前用户在反对队列,true:已在队列中"
        userid = self.pm.getAuthenticatedMember().getId()
        return not(self.voteAdapter.voteavailabledisapproved(userid))
    def voteNum(self):
        "当前对象赞成总票数"
        return self.voteAdapter.voteNum
Exemplo n.º 2
0
    def test_AnswerLikeEvent_event(self):
        from emc.kb.events import LikeEvent
        from emc.kb.events import UnLikeEvent
        from emc.kb.contents.answer import Ianswer
        from emc.kb.interfaces import IVoting
        portal = self.layer['portal']
        setRoles(portal, TEST_USER_ID, ('Manager',))
        portal.invokeFactory('emc.kb.questionfolder', 'questionfolder')
        portal['questionfolder'].invokeFactory('emc.kb.question', 'question',
                             discription=u"discription",
                             additional=u"additional",
                             isfollowed=True,
                             affiliatedtopics=u"affiliatedtopics",
                             relatedquestion=u"relatedquestion",
                             followernum=u"followernum",
                             pageview=u"pageview",
                             date=u"date"
                             )
        portal['questionfolder']['question'].invokeFactory('emc.kb.answer', 'answer',
                             answerer=u"answerer",
#                             voteNum=u"voteNum",
                             voters=u"voters",
                             content=u"content of the answer",
                             date=u"date of the answer",
                             )
        file=portal['questionfolder']['question']['answer']
            
        event.notify(LikeEvent(file))
        mp = getToolByName(portal,'portal_membership')
        userobject= mp.getAuthenticatedMember()
        username = userobject.getId()
        agreelist = list(userobject.getProperty('mylike'))
        evlute = IVoting(file)
        
        uid = IUUID(file,None)        
        self.assertTrue(uid in agreelist)
        self.assertTrue(evlute.voteavailableapproved(username))
        self.assertEqual(1,evlute.voteNum)

        catalog = getToolByName(portal, 'portal_catalog')       
        

        results = list(catalog({'voteNum': 1}))

        self.assertEqual(1, len(results)) 

        event.notify(UnLikeEvent(file))
        mp = getToolByName(portal,'portal_membership')
        userobject= mp.getAuthenticatedMember()
        username = userobject.getId()
        disagreelist = list(userobject.getProperty('myunlike'))
        evlute = IVoting(file)
        
        uid = IUUID(file,None)
        self.assertTrue(uid in disagreelist)
        self.assertTrue(evlute.voteavailabledisapproved(username))
Exemplo n.º 3
0
def approve(obj, event):
    """approve the answer"""

    mp = getToolByName(obj,'portal_membership')
    userobject= mp.getAuthenticatedMember()
    username = userobject.getId()
    agreelist = list(userobject.getProperty('mylike'))

    uuid = IUUID(obj,None)
    if uuid == None:return    
    if not uuid in agreelist:
        agreelist.append(uuid)
        userobject.setProperties(mylike=agreelist)

    evlute = IVoting(obj)
    if not evlute.voteavailableapproved(username):
        evlute.agree(username)
        obj.voteNum = evlute.voteNum
        obj.totalNum = evlute.voteNum - len(evlute.disapproved)
        obj.reindexObject()