Skip to content

Commit

Permalink
Merge branch 'dev-server' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
shaheenhyderk committed Nov 3, 2023
2 parents 42bbad2 + 158a4ff commit e85203d
Show file tree
Hide file tree
Showing 33 changed files with 1,121 additions and 300 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ DATABASE_NAME=bot
DATABASE_HOST=localhost
DATABASE_PORT=3306

REDIS_HOST=host
REDIS_PORT=1234

DISCORD_WEBHOOK_LINK=

EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ media/
env/
venv/
.vscode
.idea
.idea

dump.rdb
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM python:3.10-slim-buster
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN pip install gunicorn
RUN mkdir /var/log/mulearnbackend
RUN apt-get update -y && apt-get install -y default-libmysqlclient-dev python-dev && apt install build-essential -y
COPY ./requirements.txt .
Expand Down
114 changes: 114 additions & 0 deletions api/common/common_consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import json

from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.db.models import Count
from django.db.models.functions import Coalesce

from db.learning_circle import LearningCircle
from db.learning_circle import UserCircleLink
from db.organization import Organization
from db.task import InterestGroup
from db.user import User, UserRoleLink

from utils.types import IntegrationType, OrganizationType
from utils.baseconsumer import BaseConsumer


class GlobalCount(BaseConsumer):

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

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)

interest_groups_count = InterestGroup.objects.all().count()
learning_circles_count = LearningCircle.objects.all().count()
members_count = User.objects.all().count()

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

self.send(text_data=json.dumps(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)
def user_post_save(sender, instance, created, **kwargs):
data = {}

if created:
if sender == User:
count = User.objects.all().count()
data['members'] = count

elif sender == LearningCircle:
count = LearningCircle.objects.all().count()
data['learning_circle_count'] = count

elif sender == InterestGroup:
count = InterestGroup.objects.all().count()
data['ig_count'] = count

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

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

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

@receiver(post_delete, sender=User)
@receiver(post_delete, sender=LearningCircle)
@receiver(post_delete, sender=InterestGroup)
def user_post_delete(sender, instance, created, **kwargs):
data = {}

if sender == User:
count = User.objects.all().count()
data['members'] = count

elif sender == LearningCircle:
count = LearningCircle.objects.all().count()
data['learning_circle_count'] = count

elif sender == InterestGroup:
count = InterestGroup.objects.all().count()
data['ig_count'] = count

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

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

self.send(text_data=json.dumps(data))
23 changes: 17 additions & 6 deletions api/common/common_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def get(self, request):
request,
search_fields=["first_name", "last_name", "muid", "circle_name", 'circle_ig', "organisation", "dwms_id",
"karma_earned"],
sort_fields={"first_name": "first_name", "muid": "muid", "circle_name": "circle_name",
sort_fields={"first_name": "first_name", "last_name": "last_name", "muid": "muid",
"circle_name": "circle_name",
"circle_ig": "circle_ig", "organisation": "organisation", "dwms_id": "dwms_id",
"karma_earned": "karma_earned"},
)
Expand Down Expand Up @@ -206,7 +207,7 @@ def get(self, request):
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value, created_at__date=date)
.values(org_title=F("org__title"))
.annotate(
learning_circle_count=Count("id"), user_count=Count("usercirclelink")
learning_circle_count=Count("id"), user_count=Count("user_circle_link_circle")
)
.order_by("org_title")
)
Expand All @@ -215,12 +216,24 @@ def get(self, request):
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values(org_title=F("org__title"))
.annotate(
learning_circle_count=Count("id"), user_count=Count("usercirclelink")
learning_circle_count=Count("id"), user_count=Count("user_circle_link_circle")
)
.order_by("org_title")
)

return CustomResponse(response=learning_circles_info).get_success_response()
paginated_queryset = CommonUtils.get_paginated_queryset(
learning_circles_info,
request,
search_fields=["org_title", "learning_circle_count", "user_count"],
sort_fields={"org_title": "org_title", "learning_circle_count": "learning_circle_count",
"user_count": "user_count"},
)

collegewise_info_data = CollegeInfoSerializer(paginated_queryset.get("queryset"), many=True).data

return CustomResponse().paginated_response(
data=collegewise_info_data, pagination=paginated_queryset.get("pagination")
)


class GlobalCountAPI(APIView):
Expand Down Expand Up @@ -261,7 +274,6 @@ def get(self, request):

class GTASANDSHOREAPI(APIView):
def get(self, request):

response = requests.get('https://devfolio.vez.social/rank')
if response.status_code == 200:
# Save JSON response to a local file
Expand All @@ -276,7 +288,6 @@ def get(self, request):

# Create a dictionary to store the grouped data
grouped_colleges = {}

for college, count in data.items():
# Clean the college name by removing spaces and converting to lowercase
cleaned_college = college.replace(" ", "").lower()
Expand Down
6 changes: 6 additions & 0 deletions api/common/routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from . import common_consumer

urlpatterns = [
path("landing-stats/", common_consumer.GlobalCount.as_asgi())
]
3 changes: 2 additions & 1 deletion api/dashboard/channels/channel_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def put(self, request, channel_id):
serializer = ChannelCUDSerializer(
channel,
data=request.data,
context={"user_id": user_id}
context={"user_id": user_id},
partial=True
)

if serializer.is_valid():
Expand Down
Binary file not shown.
30 changes: 30 additions & 0 deletions api/dashboard/karma_voucher/karma_voucher_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
from .karma_voucher_serializer import VoucherLogCSVSerializer, VoucherLogSerializer, VoucherLogCreateSerializer, \
VoucherLogUpdateSerializer

from openpyxl import load_workbook
from tempfile import NamedTemporaryFile
from io import BytesIO
from django.http import FileResponse

class ImportVoucherLogAPI(APIView):
authentication_classes = [CustomizePermission]
Expand Down Expand Up @@ -323,3 +327,29 @@ def get(self, request):
voucher_serializer_data = VoucherLogSerializer(voucher_serializer, many=True).data

return CommonUtils.generate_csv(voucher_serializer_data, 'Voucher Log')

class VoucherBaseTemplateAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):
wb = load_workbook('./api/dashboard/karma_voucher/assets/base_template.xlsx')
ws = wb['Data Definitions']
hashtags = TaskList.objects.all().values_list('hashtag', flat=True)
data = {
'hashtag': hashtags,
'month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'],
'week': ['W1', 'W2', 'W3', 'W4', 'W5']
}
# Write data column-wise
for col_num, (col_name, col_values) in enumerate(data.items(), start=1):
for row, value in enumerate(col_values, start=2):
ws.cell(row=row, column=col_num, value=value)
# Save the file
with NamedTemporaryFile() as tmp:
tmp.close() # with statement opened tmp, close it so wb.save can open it
wb.save(tmp.name)
with open(tmp.name, 'rb') as f:
f.seek(0)
new_file_object = f.read()
return FileResponse(BytesIO(new_file_object), as_attachment=True, filename='base_template.xlsx')
4 changes: 3 additions & 1 deletion api/dashboard/karma_voucher/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
path('create/', karma_voucher_view.VoucherLogAPI.as_view()),
path('update/<str:voucher_id>/', karma_voucher_view.VoucherLogAPI.as_view()),
path('delete/<str:voucher_id>/', karma_voucher_view.VoucherLogAPI.as_view()),
]

path('base-template/', karma_voucher_view.VoucherBaseTemplateAPI.as_view()),
]
Loading

0 comments on commit e85203d

Please sign in to comment.