Skip to content

Commit

Permalink
Merge pull request #515 from Krishna-Baldwa/idf2
Browse files Browse the repository at this point in the history
Acad Discussion Forum (Multiple Communities)
  • Loading branch information
Krishna-Baldwa authored Dec 10, 2023
2 parents 2d4fe0c + 0a5e8b2 commit 2a5e824
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 33 deletions.
32 changes: 27 additions & 5 deletions community/serializer_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CommunityPostSerializerMin(serializers.ModelSerializer):
reactions_count = serializers.SerializerMethodField()
user_reaction = serializers.SerializerMethodField()
comments_count = serializers.SerializerMethodField()
posted_by = UserProfileSerializer(read_only=True)
# posted_by = UserProfileSerializer(read_only=True)
posted_by = serializers.SerializerMethodField()
image_url = serializers.SerializerMethodField()
most_liked_comment = serializers.SerializerMethodField()
community = CommunitySerializerMin(read_only=True)
Expand All @@ -58,13 +59,34 @@ class CommunityPostSerializerMin(serializers.ModelSerializer):
has_user_reported = serializers.SerializerMethodField()
posted_by = serializers.SerializerMethodField()

# def get_posted_by(self, obj):
# pb = UserProfile.objects.get(id=obj.posted_by.id)
# if obj.anonymous:
# pb.name = "Anonymous"
# pb.id = "null"
# pb.ldap_id = "null"
# pb.profile_pic = \
# 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM9q9XJKxlskry5gXTz1OXUyem5Ap59lcEGg&usqp=CAU'
# return UserProfileSerializer(pb).data

def get_posted_by(self, obj):
pb = UserProfile.objects.get(id=obj.posted_by.id)
if(obj.anonymous):
pb = UserProfile.objects.get(id=obj.posted_by.id)
if (
obj.anonymous
and "return_for_mod" in self.context
and not self.context["return_for_mod"]
):
pb.name = "Anonymous"
pb.id = "null"
pb.ldap_id = "null"
pb.profile_pic = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM9q9XJKxlskry5gXTz1OXUyem5Ap59lcEGg&usqp=CAU"
pb.ldap_id = "null"
pb.profile_pic = \
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM9q9XJKxlskry5gXTz1OXUyem5Ap59lcEGg&usqp=CAU'
elif (
obj.anonymous
and "return_for_mod" in self.context
and self.context["return_for_mod"]
):
pb.name += " (Anon)"
return UserProfileSerializer(pb).data

def get_most_liked_comment(self, obj):
Expand Down
37 changes: 29 additions & 8 deletions community/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,40 @@ class CommunityPostSerializers(CommunityPostSerializerMin):
comments = serializers.SerializerMethodField()
posted_by = serializers.SerializerMethodField()

def get_posted_by(self, obj):
pb = UserProfile.objects.get(id=obj.posted_by.id)
if(obj.anonymous):
pb.name = "Anonymous"
pb.id = "null"
pb.ldap_id = "null"
pb.profile_pic = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM9q9XJKxlskry5gXTz1OXUyem5Ap59lcEGg&usqp=CAU"
return UserProfileSerializer(pb).data
# def get_posted_by(self, obj):
# pb = UserProfile.objects.get(id=obj.posted_by.id)
# if obj.anonymous:
# pb.name = "Anonymous"
# pb.id = "null"
# pb.ldap_id = "null"
# pb.profile_pic = \
# 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM9q9XJKxlskry5gXTz1OXUyem5Ap59lcEGg&usqp=CAU'
# return UserProfileSerializer(pb).data

def get_comments(self, obj):
comments = obj.comments.filter(deleted=False, status=1)
return CommunityPostSerializerMin(comments, many=True).data

def get_posted_by(self, obj):
pb = UserProfile.objects.get(id=obj.posted_by.id)
if (
obj.anonymous
and "return_for_mod" in self.context
and not self.context["return_for_mod"]
):
pb.name = "Anonymous"
pb.id = "null"
pb.ldap_id = "null"
pb.profile_pic = \
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSM9q9XJKxlskry5gXTz1OXUyem5Ap59lcEGg&usqp=CAU'
elif (
obj.anonymous
and "return_for_mod" in self.context
and self.context["return_for_mod"]
):
pb.name += " (Anon)"
return UserProfileSerializer(pb).data

class Meta:
model = CommunityPost
fields = (
Expand Down
8 changes: 4 additions & 4 deletions community/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_community_get(self):

def test_communitypost_alllist(self):
"""Test if communityposts can be listed."""
url = "/api/communityposts?status=1"
url = "/api/communityposts?status=1&community=" + str(self.test_community_1.id)
response = self.client1.get(url, format="json")
data = response.data["data"]
self.assertEqual(response.status_code, 200)
Expand All @@ -82,14 +82,14 @@ def test_communitypost_alllist(self):
)

def test_communitypost_yourlist(self):
url = "/api/communityposts"
url = "/api/communityposts?community=" + str(self.test_community_1.id)
response = self.client1.get(url, format="json")
data = response.data["data"]
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.data["count"],
CommunityPost.objects.filter(
thread_rank=1, posted_by=self.user1.profile
thread_rank=1, posted_by=self.user1.profile, community=self.test_community_1
).count(),
)
self.assertListEqual(
Expand All @@ -98,7 +98,7 @@ def test_communitypost_yourlist(self):
)

def test_communitypost_pendinglist(self):
url = "/api/communityposts?status=0"
url = "/api/communityposts?status=0&community=" + str(self.test_community_1.id)
response = self.client1.get(url, format="json")
data = response.data["data"]
self.assertEqual(response.status_code, 200)
Expand Down
7 changes: 1 addition & 6 deletions community/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

urlpatterns = [
path(
"communities",
CommunityViewSet.as_view(
{
"get": "list",
}
),
"communities", CommunityViewSet.as_view({"get": "list", "post": "create"})
), # viewing the list of communities
path(
"communities/<pk>",
Expand Down
34 changes: 29 additions & 5 deletions community/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from roles.helpers import forbidden_no_privileges
from helpers.misc import query_from_num
from helpers.misc import query_search
from users.models import UserProfile


class ModeratorViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -70,7 +71,12 @@ def retrieve_full(self, request, pk):
self.queryset, request
)
post = self.get_community_post(pk)
serialized = CommunityPostSerializers(post, context={"request": request}).data
return_for_mod = False
if user_has_privilege(request.user.profile, post.community.body.id, "AppP"):
return_for_mod = True
serialized = CommunityPostSerializers(
post, context={"return_for_mod": return_for_mod}
).data

return Response(serialized)

Expand All @@ -81,29 +87,36 @@ def list(self, request):

# Check for time and date filtered query params
status = request.GET.get("status")
comm_id = request.GET.get("community")
if comm_id is None:
return Response({"message": "comm_id is required"}, status=400)
community = get_object_or_404(Community.objects, id=comm_id)

# If your posts
if status is None:
queryset = CommunityPost.objects.filter(
thread_rank=1, posted_by=request.user.profile
thread_rank=1, community=community, posted_by=request.user.profile
).order_by("-time_of_modification")
else:
# If reported posts
if status == "3":
queryset = CommunityPost.objects.filter(
status=status, deleted=False
status=status, community=community, deleted=False
).order_by("-time_of_modification")
# queryset = CommunityPost.objects.all()

else:
queryset = CommunityPost.objects.filter(
status=status, deleted=False, thread_rank=1
status=status, community=community, deleted=False, thread_rank=1
).order_by("-time_of_modification")
queryset = query_search(request, 3, queryset, ["content"], "posts")
queryset = query_from_num(request, 20, queryset)
return_for_mod = False
if user_has_privilege(request.user.profile, community.body.id, "AppP"):
return_for_mod = True

serializer = CommunityPostSerializerMin(
queryset, many=True, context={"request": request}
queryset, many=True, context={"return_for_mod": return_for_mod}
)
data = serializer.data

Expand All @@ -117,6 +130,7 @@ def create(self, request):
if "community" not in request.data or not request.data["community"]:
return forbidden_no_privileges()

user, created = UserProfile.objects.get_or_create(user=request.user)
return super().create(request)

@login_required_ajax
Expand Down Expand Up @@ -235,3 +249,13 @@ def get_community(self, pk):
return get_object_or_404(self.queryset, id=pk)
except ValueError:
return get_object_or_404(self.queryset, str_id=pk)

@login_required_ajax
def create(self, request):
name = request.data["name"]
user, created = UserProfile.objects.get_or_create(user=request.user)
if not Community.objects.all().filter(name=name).exists():
super().create(request)
return Response({"message": "Community created"})

return Response({"message": "Community already exists"}, status=400)
1 change: 1 addition & 0 deletions helpers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def query_from_num(request, default_num, queryset):

return queryset[from_i: from_i + num]


def query_search( # pylint: disable=too-many-arguments
request, min_length, queryset, fields, collection, order_relevance=False
):
Expand Down
11 changes: 7 additions & 4 deletions other/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ def notify_new_commpost(pk):
notify.send(instance, recipient=users, verb="New post added in " + community.name)

for interest in instance.interests.all():
users = User.objects.filter(
id__in=UserInterest.filter(title=interest.title)
.user.filter(active=True)
.values("user_id")
users = (
User.objects.filter(
id__in=UserInterest.objects.filter(title=interest.title)
)
.filter(is_active=True)
.values("id")
)

notify.send(
instance,
recipient=users,
Expand Down
65 changes: 65 additions & 0 deletions roles/migrations/0020_communityrole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 3.2.16 on 2023-07-07 14:40

from django.db import migrations, models
import django.db.models.deletion
import multiselectfield.db.fields
import uuid


class Migration(migrations.Migration):
dependencies = [
("community", "0028_auto_20221003_2130"),
("roles", "0019_alter_bodyrole_permissions"),
]

operations = [
migrations.CreateModel(
name="CommunityRole",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("time_of_creation", models.DateTimeField(auto_now_add=True)),
("name", models.CharField(max_length=50)),
("inheritable", models.BooleanField(default=False)),
(
"permissions",
multiselectfield.db.fields.MultiSelectField(
choices=[
("AddE", "Add Event"),
("UpdE", "Update Event"),
("DelE", "Delete Event"),
("UpdB", "Update Body"),
("Role", "Modify Roles"),
("VerA", "Verify Achievements"),
("AppP", "Moderate Post"),
("ModC", "Moderate Comment"),
],
max_length=39,
),
),
("priority", models.IntegerField(default=0)),
("official_post", models.BooleanField(default=True)),
("permanent", models.BooleanField(default=False)),
(
"community",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="roles",
to="community.community",
),
),
],
options={
"verbose_name": "Community Role",
"verbose_name_plural": "Community Roles",
"ordering": ("community__name", "priority"),
},
),
]
16 changes: 16 additions & 0 deletions roles/migrations/0021_delete_communityrole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 3.2.16 on 2023-07-22 13:35

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("users", "0043_remove_userprofile_community_roles"),
("roles", "0020_communityrole"),
]

operations = [
migrations.DeleteModel(
name="CommunityRole",
),
]
27 changes: 27 additions & 0 deletions roles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ def __str__(self):
return self.body.name + " " + self.name


"""
Added Community Role Model : To allow to have various communities in a single body and have different roles for each
community.
Ditched For Now
"""
# class CommunityRole(models.Model):
# """A role for a bodywhich can be granted to multiple users."""

# id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
# time_of_creation = models.DateTimeField(auto_now_add=True)
# name = models.CharField(max_length=50)
# community = models.ForeignKey('community.Community', on_delete=models.CASCADE, related_name='roles')
# inheritable = models.BooleanField(default=False)
# permissions = MultiSelectField(choices=PERMISSION_CHOICES)
# priority = models.IntegerField(default=0)
# official_post = models.BooleanField(default=True)
# permanent = models.BooleanField(default=False)

# class Meta:
# verbose_name = "Community Role"
# verbose_name_plural = "Community Roles"
# ordering = ("community__name", "priority")

# def __str__(self):
# return self.community.name + " " + self.name


INSTITUTE_PERMISSION_CHOICES = (
("AddB", "Add Body"),
("DelB", "Delete Body"),
Expand Down
20 changes: 20 additions & 0 deletions users/migrations/0041_userprofile_community_roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.16 on 2023-07-07 14:40

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("roles", "0020_communityrole"),
("users", "0040_remove_userprofile_followed_communities"),
]

operations = [
migrations.AddField(
model_name="userprofile",
name="community_roles",
field=models.ManyToManyField(
blank=True, related_name="users", to="roles.CommunityRole"
),
),
]
Loading

0 comments on commit 2a5e824

Please sign in to comment.