Skip to content

Commit

Permalink
Merge pull request #3215 from webkom/pizzareaction
Browse files Browse the repository at this point in the history
Add reactions to meetings
  • Loading branch information
jonasdeluna committed Feb 28, 2023
2 parents 3462a11 + 2955559 commit ba4fd6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lego/apps/meetings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
MeetingInvitationPermissionHandler,
MeetingPermissionHandler,
)
from lego.apps.reactions.models import Reaction
from lego.apps.stats.utils import track
from lego.apps.users.models import User
from lego.utils.models import BasisModel
Expand All @@ -25,6 +26,7 @@ class Meeting(BasisModel):
description = models.TextField(blank=True, default="")
comments = GenericRelation(Comment)
mazemap_poi = models.PositiveIntegerField(null=True)
reactions = GenericRelation(Reaction)

report = ContentField(blank=True, allow_images=True)
report_author = models.ForeignKey(
Expand All @@ -41,6 +43,26 @@ class Meeting(BasisModel):
through_fields=("meeting", "user"),
)

def get_reactions_grouped(self, user):
grouped = {}
for reaction in self.reactions.all():
if reaction.emoji.pk not in grouped:
grouped[reaction.emoji.pk] = {
"emoji": reaction.emoji.pk,
"unicode_string": reaction.emoji.unicode_string,
"count": 0,
"has_reacted": False,
"reaction_id": None,
}

grouped[reaction.emoji.pk]["count"] += 1

if reaction.created_by == user:
grouped[reaction.emoji.pk]["has_reacted"] = True
grouped[reaction.emoji.pk]["reaction_id"] = reaction.id

return sorted(grouped.values(), key=lambda kv: kv["count"], reverse=True)

class Meta:
permission_handler = MeetingPermissionHandler()

Expand Down
6 changes: 6 additions & 0 deletions lego/apps/meetings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class MeetingDetailSerializer(BasisModelSerializer):
created_by = PublicUserField(read_only=True)
comments = CommentSerializer(read_only=True, many=True)
content_target = CharField(read_only=True)
reactions_grouped = serializers.SerializerMethodField()

def get_reactions_grouped(self, obj):
user = self.context["request"].user
return obj.get_reactions_grouped(user)

class Meta:
model = Meeting
Expand All @@ -79,6 +84,7 @@ class Meta:
"comments",
"content_target",
"mazemap_poi",
"reactions_grouped",
)
read_only = True

Expand Down

0 comments on commit ba4fd6d

Please sign in to comment.