allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [04/10] allura git commit: [#8253] Adding Reactable artifact class
Date Thu, 08 Nov 2018 21:53:45 GMT
[#8253] Adding Reactable artifact class


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/62df9f20
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/62df9f20
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/62df9f20

Branch: refs/heads/master
Commit: 62df9f2079acc7c802fff662da3f78e86e9fbf3f
Parents: e696260
Author: Shalitha <shalithasuranga@gmail.com>
Authored: Tue Oct 30 22:02:04 2018 +0530
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Thu Nov 8 21:53:25 2018 +0000

----------------------------------------------------------------------
 Allura/allura/model/artifact.py | 64 ++++++++++++++++++++++++++++++++++++
 Allura/allura/model/discuss.py  |  4 +--
 2 files changed, 66 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/62df9f20/Allura/allura/model/artifact.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/artifact.py b/Allura/allura/model/artifact.py
index 7933e7f..21aad0d 100644
--- a/Allura/allura/model/artifact.py
+++ b/Allura/allura/model/artifact.py
@@ -1051,6 +1051,70 @@ class VotableArtifact(MappedClass):
         }
 
 
+class ReactableArtifact(MappedClass):
+
+    """Reaction support for the Artifact. Use as a mixin."""
+
+    class __mongometa__:
+        session = main_orm_session
+        name = 'reaction'
+
+    react_thumbs_up = FieldProperty(int, if_missing=0)
+    react_thumbs_down = FieldProperty(int, if_missing=0)
+    react_laugh = FieldProperty(int, if_missing=0)
+    react_hooray = FieldProperty(int, if_missing=0)
+    react_confused = FieldProperty(int, if_missing=0)
+    react_heart = FieldProperty(int, if_missing=0)
+
+    react_users = FieldProperty({str: None}, if_missing=dict())
+    # dict to store reactions vs usernames
+
+    reaction_list = ['thumbs_up', 'thumbs_down', 'laugh', 'hooray', 'confused', 'heart']
+
+    def post_reaction(self, r, user):
+        current_reaction = self.user_reacted(user)
+        if current_reaction is None:
+            # no prev reactions. simply append
+            if r in self.react_users:
+                self.react_users[r].append(user.username)
+            else:
+                self.react_users[r] = [user.username]
+            self.update_react_count(r, 1)
+        elif current_reaction == r:
+            # prev=current so remove
+            self.react_users[r].remove(user.username)
+            self.update_react_count(r, -1)
+        else:
+            # prev!=currnet so remove prev then append
+            self.react_users[current_reaction].remove(user.username)
+            if r in self.react_users:
+                self.react_users[r].append(user.username)
+            else:
+                self.react_users[r] = [user.username]
+            self.update_react_count(current_reaction, -1)
+            self.update_react_count(r, 1)
+
+    def user_reacted(self, user):
+        for i in self.reaction_list:
+            if i in self.react_users:
+                if user.username in self.react_users[i]:
+                    return i
+        return 
+    
+    def update_react_count(self, r, i):
+        if r == 'thumbs_up':
+            self.react_thumbs_up += i
+        elif r == 'thumbs_down':
+            self.react_thumbs_down += i
+        elif r == 'laugh':
+            self.react_laugh += i
+        elif r == 'hooray':
+            self.react_hooray += i
+        elif r == 'confused':
+            self.react_confused += i
+        elif r == 'heart':
+            self.react_heart += i
+
 class MovedArtifact(Artifact):
 
     class __mongometa__:

http://git-wip-us.apache.org/repos/asf/allura/blob/62df9f20/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index d6dc8d7..c7fcc27 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -36,7 +36,7 @@ from allura.lib import security
 from allura.lib.security import require_access, has_access
 from allura.lib import utils
 from allura.model.notification import Notification, Mailbox
-from .artifact import Artifact, ArtifactReference, VersionedArtifact, Snapshot, Message,
Feed
+from .artifact import Artifact, ArtifactReference, VersionedArtifact, Snapshot, Message,
Feed, ReactableArtifact
 from .attachments import BaseAttachment
 from .auth import User, ProjectRole, AlluraUserProperty
 from .timeline import ActivityObject
@@ -489,7 +489,7 @@ class PostHistory(Snapshot):
         return result
 
 
-class Post(Message, VersionedArtifact, ActivityObject):
+class Post(Message, VersionedArtifact, ActivityObject, ReactableArtifact):
 
     class __mongometa__:
         name = 'post'


Mime
View raw message