Skip to content

Commit

Permalink
Merge pull request #1819 from gtech-mulearn:dev
Browse files Browse the repository at this point in the history
[PATCH] Some minor patches
  • Loading branch information
MZaFaRM authored Dec 13, 2023
2 parents 5ed78b2 + 8b04426 commit 2d9bf45
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 137 deletions.
4 changes: 2 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ DATABASE_NAME=bot
DATABASE_HOST=localhost
DATABASE_PORT=3306

REDIS_HOST=host
REDIS_PORT=1234
REDIS_HOST=localhost
REDIS_PORT=6379

DISCORD_WEBHOOK_LINK=

Expand Down
34 changes: 13 additions & 21 deletions api/dashboard/lc/dash_lc_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,37 +683,29 @@ class Meta:


class IgTaskDetailsSerializer(serializers.ModelSerializer):
task = serializers.CharField(source='title')
task_status = serializers.SerializerMethodField()
task_id = serializers.CharField(source='id')
task_title = serializers.CharField(source='title')
task_level = serializers.CharField(source='level.level_order')
task_level_karma = serializers.CharField(source='level.karma')
task_karma = serializers.CharField(source='karma')
task_description = serializers.CharField(source='description')
interest_group = serializers.CharField(source='ig.name')

task_hashtag = serializers.CharField(source='hashtag')
completed_users= serializers.SerializerMethodField()
class Meta:
model = TaskList
fields = [
"task_id",
"task",

"task_title",
"task_karma",
"task_description",
"interest_group",
"task_status",
"task_level",
"task_level_karma",
"task_hashtag",
"completed_users",
]

def get_task_status(self, obj):
user_ig_links = UserIgLink.objects.filter(ig=obj.ig).select_related('user')
for user_ig_link in user_ig_links:
if obj.karma_activity_log_task.filter(
user=user_ig_link.user,
peer_approved=True).exists():
return True
else:
return False
def get_completed_users(self, obj):
karma_activity_log=KarmaActivityLog.objects.filter(task=obj,appraiser_approved=True).select_related('user')
completed_users=[]
for karma in karma_activity_log:
completed_users.append(karma.user.id)
return completed_users


class AddMemberSerializer(serializers.ModelSerializer):
Expand Down
16 changes: 11 additions & 5 deletions api/dashboard/lc/dash_lc_view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uuid
from collections import defaultdict

from decouple import config
from django.core.mail import send_mail
Expand All @@ -11,7 +12,7 @@
from api.notification.notifications_utils import NotificationUtils
from db.learning_circle import LearningCircle, UserCircleLink, CircleMeetingLog
from db.user import User
from db.task import TaskList
from db.task import TaskList,KarmaActivityLog
from utils.permission import JWTUtils
from utils.response import CustomResponse
from utils.utils import send_template_mail, DateTimeUtils
Expand Down Expand Up @@ -679,15 +680,20 @@ def put(self, request, circle_id):

class IgTaskDetailsAPI(APIView):
def get(self, request, circle_id):
task_list = TaskList.objects.filter(ig__learning_circle_ig__id=circle_id)

task_list = TaskList.objects.filter(ig__learning_circle_ig__id=circle_id).order_by('level__level_order')
serializer = IgTaskDetailsSerializer(
task_list,
many=True,
)

serialized_data = serializer.data
grouped_tasks = defaultdict(list)
for task in serialized_data:
task_level = task['task_level']
task.pop('task_level')
grouped_tasks[f'Level {task_level}'].append(task)
grouped_tasks_dict = dict(grouped_tasks)
return CustomResponse(
response=serializer.data
response=grouped_tasks_dict
).get_success_response()


Expand Down
18 changes: 15 additions & 3 deletions api/dashboard/profile/profile_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from db.organization import UserOrganizationLink
from db.organization import UserOrganizationLink, District
from db.task import InterestGroup, KarmaActivityLog, Level, TaskList, Wallet, UserIgLink, UserLvlLink
from db.user import User, UserSettings, Socials
from utils.exception import CustomException
Expand Down Expand Up @@ -274,7 +274,8 @@ def update(self, instance, validated_data):

class UserProfileEditSerializer(serializers.ModelSerializer):
communities = serializers.ListField(write_only=True)



def to_representation(self, instance):
data = super().to_representation(instance)
communities = instance.user_organization_link_user.filter(
Expand All @@ -283,6 +284,13 @@ def to_representation(self, instance):
data["communities"] = (
[community.org_id for community in communities] if communities else []
)

district = instance.district
if district:
data["district"] = district.name
else:
data["district"] = None

return data

def update(self, instance, validated_data):
Expand All @@ -292,7 +300,6 @@ def update(self, instance, validated_data):
instance.user_organization_link_user.filter(
org__org_type=OrganizationType.COMMUNITY.value
).delete()

user_organization_links = [
UserOrganizationLink(
id=uuid.uuid4(),
Expand All @@ -308,6 +315,8 @@ def update(self, instance, validated_data):
UserOrganizationLink.objects.bulk_create(
user_organization_links)



return super().update(instance, validated_data)

class Meta:
Expand All @@ -320,7 +329,10 @@ class Meta:
"communities",
"gender",
"dob",
"district",

]



class UserIgListSerializer(serializers.ModelSerializer):
Expand Down
48 changes: 27 additions & 21 deletions api/dashboard/roles/dash_roles_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,12 @@ def put(self, request, role_id):
return CustomResponse(response=serialized_users.data).get_success_response()

@role_required([RoleType.ADMIN.value])
def patch(self, request):
def post(self, request, role_id):
"""
Assigns a large bunch of users a certain role
"""
request_data = request.data.copy()
request_data["role"] = role_id
request_data["created_by"] = JWTUtils.fetch_user_id(request)
serialized_users = dash_roles_serializer.RoleAssignmentSerializer(
data=request_data
Expand All @@ -214,31 +215,34 @@ def patch(self, request):
return CustomResponse(response=serialized_users.errors).get_failure_response()

@role_required([RoleType.ADMIN.value])
def delete(self, request):
def patch(self, request, role_id):
"""
Removes a role from a large bunch of users
"""
role = Role.objects.get(pk=request.data.get("role"))
user_role_links = UserRoleLink.objects.filter(
user__pk__in=request.data.get("users"),
role=role,
)
number = user_role_links.count()
try:
role = Role.objects.get(pk=role_id)
user_role_links = UserRoleLink.objects.filter(
user__pk__in=request.data.get("users"),
role=role,
)
number = user_role_links.count()

DiscordWebhooks.general_updates(
WebHookCategory.BULK_ROLE.value,
WebHookActions.DELETE.value,
role.title,
",".join(list(user_role_links.values_list("user_id", flat=True))),
)
DiscordWebhooks.general_updates(
WebHookCategory.BULK_ROLE.value,
WebHookActions.DELETE.value,
role.title,
",".join(list(user_role_links.values_list("user_id", flat=True))),
)

user_role_links.delete()
user_role_links.delete()

return CustomResponse(
general_message=(
f"Successfully removed the '{role.title}' role from {number} users"
)
).get_success_response()
return CustomResponse(
general_message=(
f"Successfully removed the '{role.title}' role from {number} users"
)
).get_success_response()
except Role.DoesNotExist as e:
return CustomResponse(general_message=str(e)).get_failure_response()


class UserRole(APIView):
Expand Down Expand Up @@ -425,7 +429,9 @@ def post(self, request):
row["created_by_id"] = request_user_id
valid_rows.append(row)

users_by_role = {role_title: users for role_title, users in users_by_role.items() if users}
users_by_role = {
role_title: users for role_title, users in users_by_role.items() if users
}
user_roles_serializer = dash_roles_serializer.UserRoleBulkAssignSerializer(
data=valid_rows, many=True
)
Expand Down
21 changes: 11 additions & 10 deletions db/hackathon.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.db import models

from db.organization import Organization, District
from db.organization import District, Organization
from db.user import User
from decouple import config as decouple_config

from .helper import get_system_admin_id

# fmt: off
# noinspection PyPep8
Expand All @@ -26,9 +27,9 @@ class Hackathon(models.Model):
event_start = models.DateTimeField(blank=True, null=True)
event_end = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=20, blank=True, null=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='updated_by', related_name='hackathon_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='created_by', related_name='hackathon_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -42,9 +43,9 @@ class HackathonForm(models.Model):
field_name = models.CharField(max_length=255)
field_type = models.CharField(max_length=50)
is_required = models.BooleanField(default=False)
updated_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='updated_by', related_name='hackathon_form_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_form_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='created_by', related_name='hackathon_form_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_form_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -56,9 +57,9 @@ class HackathonOrganiserLink(models.Model):
id = models.CharField(primary_key=True, max_length=36)
organiser = models.ForeignKey(User, on_delete=models.CASCADE)
hackathon = models.ForeignKey(Hackathon, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='updated_by', related_name='hackathon_organiser_link_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_organiser_link_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='created_by', related_name='hackathon_organiser_link_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_organiser_link_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -70,9 +71,9 @@ class HackathonUserSubmission(models.Model):
id = models.CharField(primary_key=True, max_length=36)
user = models.ForeignKey(User, on_delete=models.CASCADE)
hackathon = models.ForeignKey(Hackathon, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='updated_by', related_name='hackathon_submission_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_submission_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='created_by', related_name='hackathon_submission_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_submission_created_by')
created_at = models.DateTimeField(auto_now_add=True)
data = models.JSONField(max_length=2000, blank=True, null=True)

Expand Down
6 changes: 6 additions & 0 deletions db/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from decouple import config


def get_system_admin_id():
from db.user import User
return User.objects.get(pk=config("SYSTEM_ADMIN_ID"))
5 changes: 3 additions & 2 deletions db/integrations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import uuid

from decouple import config as decouple_config
from django.db import models

from db.user import User

from .helper import get_system_admin_id

# fmt: off
# noinspection PyPep8

Expand All @@ -21,7 +22,7 @@ class Meta:
db_table = "integration"


#

class IntegrationAuthorization(models.Model):
id = models.CharField(max_length=36, primary_key=True, default=uuid.uuid4)
integration = models.ForeignKey(Integration, on_delete=models.CASCADE, null=False, related_name="integration_authorization_integration",)
Expand Down
10 changes: 5 additions & 5 deletions db/learning_circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from db.task import InterestGroup, Organization
from db.user import User
from decouple import config as decouple_config

from .helper import get_system_admin_id

# fmt: off
# noinspection PyPep8
Expand All @@ -22,10 +22,10 @@ class LearningCircle(models.Model):
meet_time = models.CharField(max_length=10, blank=True, null=True)
day = models.CharField(max_length=20, blank=True, null=True)
note = models.CharField(max_length=500, blank=True, null=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column="updated_by",
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column="updated_by",
related_name="learning_circle_updated_by")
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column="created_by",
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column="created_by",
related_name="learning_circle_created_by")
created_at = models.DateTimeField(auto_now=True)

Expand Down Expand Up @@ -59,10 +59,10 @@ class CircleMeetingLog(models.Model):
attendees = models.CharField(max_length=216)
agenda = models.CharField(max_length=2000)
images = models.ImageField(max_length=200, upload_to='lc/meet-report')
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
related_name='circle_meeting_log_created_by')
created_at = models.DateTimeField(auto_now=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
related_name='circle_meeting_log_updated_by')
updated_at = models.DateTimeField(auto_now=True)

Expand Down
5 changes: 3 additions & 2 deletions db/notification.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import uuid

from decouple import config as decouple_config
from django.db import models

from db.user import User

from .helper import get_system_admin_id

# fmt: off
# noinspection PyPep8

Expand All @@ -16,7 +17,7 @@ class Notification(models.Model):
button = models.CharField(max_length=10, blank=True, null=True)
url = models.CharField(max_length=100, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, on_delete=models.SET(decouple_config("SYSTEM_ADMIN_ID")), db_column="created_by", related_name="created_notifications")
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column="created_by", related_name="created_notifications")

class Meta:
managed = False
Expand Down
Loading

0 comments on commit 2d9bf45

Please sign in to comment.