Skip to content

Commit

Permalink
Merge pull request #1810 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
fix kv
  • Loading branch information
adnankattekaden authored Dec 13, 2023
2 parents f007f08 + 336a332 commit 50f332b
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 86 deletions.
132 changes: 64 additions & 68 deletions api/common/common_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db.models.functions import Coalesce

from channels.generic.websocket import WebsocketConsumer
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync

from db.learning_circle import LearningCircle
Expand All @@ -16,109 +17,104 @@

from utils.types import IntegrationType, OrganizationType


class GlobalCount(WebsocketConsumer):
class LandingStats:
data = {}
group_name = "landing_stats"

def connect(self):
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept()

self.data = {
'members': self.members_count,
'org_type_counts': self.org_type_counts,
'enablers_mentors_count': self.enablers_mentors_count,
'ig_count': self.interest_groups_count,
'learning_circle_count': self.learning_circles_count
}

self.send(text_data=json.dumps(self.data))

def disconnect(self, code):
self.channel_layer.group_discard(self.group_name, self.channel_name)

@property
def members_count(self):
members_count = User.objects.all().count()
return members_count

@property

def org_type_counts(self):
org_type_counts = Organization.objects.filter(
org_type__in=[OrganizationType.COLLEGE.value, OrganizationType.COMPANY.value,
OrganizationType.COMMUNITY.value]
OrganizationType.COMMUNITY.value]
).values('org_type').annotate(org_count=Coalesce(Count('org_type'), 0))
org_type_counts = list(org_type_counts)

return org_type_counts

@property

def enablers_mentors_count(self):
enablers_mentors_count = UserRoleLink.objects.filter(
role__title__in=["Mentor", "Enabler"]).values(
'role__title').annotate(role_count=Coalesce(Count('role__title'), 0))
enablers_mentors_count = list(enablers_mentors_count)

return enablers_mentors_count

@property

def interest_groups_count(self):
interest_groups_count = InterestGroup.objects.all().count()
return interest_groups_count

@property

def learning_circles_count(self):
learning_circles_count = LearningCircle.objects.all().count()
return learning_circles_count

def get_based_on_sender(self, sender):
if sender == User:
self.data['members'] = self.members_count

def get_data(self, sender):
if sender == None:
self.data = {
'members': self.members_count(),
'org_type_counts': self.org_type_counts(),
'enablers_mentors_count': self.enablers_mentors_count(),
'ig_count': self.interest_groups_count(),
'learning_circle_count': self.learning_circles_count()
}
elif sender == User:
self.data['members'] = self.members_count()

elif sender == Organization:
self.data['org_type_counts'] = self.org_type_counts
self.data['org_type_counts'] = self.org_type_counts()

elif sender == UserRoleLink:
self.data['enablers_mentors_count'] = self.enablers_mentors_count
self.data['enablers_mentors_count'] = self.enablers_mentors_count()

elif sender == InterestGroup:
self.data['ig_count'] = self.interest_groups_count
self.data['ig_count'] = self.interest_groups_count()

elif sender == LearningCircle:
self.data['learning_circle_count'] = self.learning_circles_count

def receive(self, text_data):

@receiver(post_save, sender=User)
@receiver(post_save, sender=LearningCircle)
@receiver(post_save, sender=InterestGroup)
@receiver(post_save, sender=UserRoleLink)
@receiver(post_save, sender=Organization)
@receiver(post_delete, sender=User)
@receiver(post_delete, sender=LearningCircle)
@receiver(post_delete, sender=InterestGroup)
@receiver(post_delete, sender=UserRoleLink)
@receiver(post_delete, sender=Organization)
def db_signals(sender, instance, created=None, *args, **kwargs):
if created:
self.get_based_on_sender(sender)
async_to_sync(self.channel_layer.group_send)(
self.group_name,
{"type": "send_data", "data": self.data}
)

if created == None:
self.get_based_on_sender(sender)
async_to_sync(self.channel_layer.group_send)(
self.group_name,
{"type": "send_data", "data": self.data}
)
self.data['learning_circle_count'] = self.learning_circles_count()


landing_stats = LandingStats()

class GlobalCount(WebsocketConsumer):
data = {}
group_name = "landing_stats"

def connect(self):
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept()

landing_stats.get_data(None)
self.data = landing_stats.data

self.send(text_data=json.dumps(self.data))

def disconnect(self, code):
self.channel_layer.group_discard(self.group_name, self.channel_name)

def send_data(self, event):
self.send(text_data=json.dumps(event['data']))

channel_layer = get_channel_layer()

@receiver(post_save, sender=User)
@receiver(post_save, sender=LearningCircle)
@receiver(post_save, sender=InterestGroup)
@receiver(post_save, sender=UserRoleLink)
@receiver(post_save, sender=Organization)
@receiver(post_delete, sender=User)
@receiver(post_delete, sender=LearningCircle)
@receiver(post_delete, sender=InterestGroup)
@receiver(post_delete, sender=UserRoleLink)
@receiver(post_delete, sender=Organization)
def db_signals(sender, instance, created=None, *args, **kwargs):
if created or created == None:
landing_stats.get_data(sender)
data = landing_stats.data
async_to_sync(channel_layer.group_send)(
"landing_stats",
{"type": "send_data", "data": data}
)
6 changes: 3 additions & 3 deletions api/dashboard/karma_voucher/karma_voucher_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def post(self, request):
if not excel_data:
return CustomResponse(general_message={'Empty csv file.'}).get_failure_response()

temp_headers = ['muid', 'karma', 'hashtag', 'month', 'week']
temp_headers = ['muid', 'karma', 'hashtag', 'month', 'week', 'description', 'event']
first_entry = excel_data[0]
for key in temp_headers:
if key not in first_entry:
Expand Down Expand Up @@ -160,7 +160,7 @@ def post(self, request):
description = voucher['description']
time_or_event = f'{month}/{week}'
event = voucher['event']
if event != '':
if event != '' and event is not None:
time_or_event = f'{event}/{description}'

success_rows.append({
Expand Down Expand Up @@ -348,7 +348,7 @@ class VoucherBaseTemplateAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):
wb = load_workbook('./api/dashboard/karma_voucher/assets/voucher_base_template.xlsx')
wb = load_workbook('./excel-templates/voucher_base_template.xlsx')
ws = wb['Data Definitions']
hashtags = TaskList.objects.all().values_list('hashtag', flat=True)
data = {
Expand Down
2 changes: 1 addition & 1 deletion api/dashboard/organisation/organisation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class OrganisationBaseTemplateAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):
wb = load_workbook('./api/dashboard/organisation/assets/organisation_base_template.xlsx')
wb = load_workbook('./excel-templates/organisation_base_template.xlsx')
ws = wb['Data Definitions']
affiliations = OrgAffiliation.objects.all().values_list('title', flat=True)
districts = District.objects.all().values_list('name', flat=True)
Expand Down
21 changes: 9 additions & 12 deletions api/dashboard/roles/dash_roles_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class RoleBaseTemplateAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):
wb = load_workbook("./api/dashboard/roles/assets/role_base_template.xlsx")
wb = load_workbook("./excel-templates/role_base_template.xlsx")
ws = wb["Data Definitions"]

roles = Role.objects.all().values_list("title", flat=True)
Expand Down Expand Up @@ -353,15 +353,13 @@ def post(self, request):
excel_data = [row for row in excel_data if any(row.values())]
valid_rows = []
error_rows = []

users_to_fetch = set()
roles_to_fetch = set()
user_role_link_to_check = set()

for row in excel_data[1:]:
keys_to_keep = ["muid", "role"]
row_keys = list(row.keys())

# Remove columns other than "muid" and "role"
for key in row_keys:
if key not in keys_to_keep:
Expand Down Expand Up @@ -402,7 +400,6 @@ def post(self, request):

user_id = users_dict.get(user)
role_id = roles_dict.get(role)

if not user_id:
row["muid"] = user
row["role"] = role
Expand All @@ -428,6 +425,7 @@ 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}
user_roles_serializer = dash_roles_serializer.UserRoleBulkAssignSerializer(
data=valid_rows, many=True
)
Expand All @@ -441,17 +439,16 @@ def post(self, request):
"role": user_role_data.get("role_id", ""),
}
)
for role, user_set in users_by_role.items():
DiscordWebhooks.general_updates(
WebHookCategory.BULK_ROLE.value,
WebHookActions.UPDATE.value,
role,
",".join(user_set),
)
else:
error_rows.append(user_roles_serializer.errors)

for role, user_set in users_by_role.items():
DiscordWebhooks.general_updates(
WebHookCategory.BULK_ROLE.value,
WebHookActions.UPDATE.value,
role,
",".join(user_set),
)

return CustomResponse(
response={"Success": success_data, "Failed": error_rows}
).get_success_response()
2 changes: 1 addition & 1 deletion api/dashboard/roles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
path('base-template/', dash_roles_views.RoleBaseTemplateAPI.as_view(), name="roles-base-template"),
path('bulk-assign/', dash_roles_views.UserRoleLinkManagement.as_view(), name="user-roles-assign"), # used to assign a bunch of users a role
path('bulk-assign/<str:role_id>/', dash_roles_views.UserRoleLinkManagement.as_view(), name="user-roles-list"), # used to get the list of users to assign a role
path('bulk-assign-excel/', dash_roles_views.UserRoleBulkAssignAPI.as_view(), name="user-roles-assign-excel"),
path('bulk-assign-excel/', dash_roles_views.UserRoleBulkAssignAPI.as_view(), name="user-roles-assign-excel"), # used to bulk assign required roles to users from excel
path('user-role/', dash_roles_views.UserRole.as_view(), name='create-delete-user-role'),
path('', dash_roles_views.RoleAPI.as_view(), name="roles-list"),
path('', dash_roles_views.RoleAPI.as_view(), name="roles-create"),
Expand Down
2 changes: 1 addition & 1 deletion api/dashboard/task/dash_task_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class TaskBaseTemplateAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):
wb = load_workbook('./api/dashboard/task/assets/task_base_template.xlsx')
wb = load_workbook('./excel-templates/task_base_template.xlsx')
ws = wb['Data Definitions']
levels = Level.objects.all().values_list('name', flat=True)
channels = Channel.objects.all().values_list('name', flat=True)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 50f332b

Please sign in to comment.