From 91b14d20abd44e7bbf439dbe168ec59ba695795d Mon Sep 17 00:00:00 2001 From: ankitamk14 Date: Tue, 21 May 2024 10:38:08 +0530 Subject: [PATCH 1/4] Restriction for tutorial views --- spoken/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spoken/views.py b/spoken/views.py index f2e990e60..0b706c2f5 100644 --- a/spoken/views.py +++ b/spoken/views.py @@ -313,8 +313,8 @@ def is_valid_user(user,foss,lang): def watch_tutorial(request, foss, tutorial, lang): try: foss = unquote_plus(foss) - # is_valid_user_for_tut = is_valid_user(request.user,foss,lang) - is_valid_user_for_tut = True #Temporary making videos available to all + is_valid_user_for_tut = is_valid_user(request.user,foss,lang) + # is_valid_user_for_tut = True #Temporary making videos available to all tutorial = unquote_plus(tutorial) td_rec = TutorialDetail.objects.get(foss__foss=foss, tutorial=tutorial) tr_rec = TutorialResource.objects.select_related().get(tutorial_detail=td_rec, language=Language.objects.get(name=lang)) From e30b975f30def0c22689f4fb235ee85d8bc9d26e Mon Sep 17 00:00:00 2001 From: ankitamk14 Date: Tue, 21 May 2024 12:19:23 +0530 Subject: [PATCH 2/4] modifications in management commands --- cms/management/commands/populate_ilw_data.py | 20 ++++++++++++------- .../commands/populate_subscription_data.py | 9 +++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cms/management/commands/populate_ilw_data.py b/cms/management/commands/populate_ilw_data.py index 30c62dca7..3f8f8c990 100644 --- a/cms/management/commands/populate_ilw_data.py +++ b/cms/management/commands/populate_ilw_data.py @@ -12,11 +12,13 @@ def set_user_type_ilw(user_lst,foss={}): for user in user_lst: try: ut=UserType.objects.get(user_id=user) + if ut.ilw != foss: + ut.ilw = foss + ut.save() except UserType.DoesNotExist: - ut=UserType.objects.create(user_id=user) - if ut.ilw != foss: - ut.ilw = foss - ut.save() + ut=UserType.objects.create(user_id=user, ilw=foss) + except Exception as e: + print(f"Exception in set_user_type_ilw : {e}") def get_fosses(payee): foss_lang = CdFossLanguages.objects.filter(payment=payee) @@ -36,9 +38,12 @@ def get_ilw_users(payee): participants = [x for x in Participant.objects.filter(event=event).values_list('user_id',flat=True)] event_users = [x.id for x in User.objects.filter(Q(email=event.event_coordinator_email) | Q(id=event.entry_user_id))] #Event coordinator & Training Manager #return users who are participants /coordinator / Training Manager of the ILW event - return participants+event_users + unique_users = list(set(participants+event_users)) + return unique_users + except TrainingEvents.DoesNotExist: + print(f"\033[93m TrainingEvent not found: {purpose} \n \033[0m") except Exception as e: - print(f"event failed: {purpose:>15} \n{e}") + print(f"\033[93m get_ilw_users exception: {purpose} \n{e} \033[0m") class Command(BaseCommand): @@ -51,5 +56,6 @@ def handle(self, *args, **options): for payee in payee_list: d = get_fosses(payee) users = get_ilw_users(payee) - set_user_type_ilw(users,d) + if users: + set_user_type_ilw(users,d) self.stdout.write('Ending populate_ilw_data command...') diff --git a/cms/management/commands/populate_subscription_data.py b/cms/management/commands/populate_subscription_data.py index 5f37fb90a..9cea380b4 100644 --- a/cms/management/commands/populate_subscription_data.py +++ b/cms/management/commands/populate_subscription_data.py @@ -2,6 +2,7 @@ from django.db.models import Max from events.models import Organiser, AcademicKey, StudentBatch, StudentMaster, Student, Invigilator from cms.models import UserType +from django.utils import timezone def update_subscription(users,expiry_date): for user in users: @@ -15,6 +16,8 @@ def update_subscription(users,expiry_date): ut = UserType.objects.create(user_id=user,subscription=expiry_date) except Exception as e: print(f"failed for user: {user:>10}\n{e}") + except Exception as e: + print(f"\033[93mException update_subscription for user : {user} : {e} \033[0m") def get_users_from_acad(academic_id): organisers = [x for x in Organiser.objects.filter(academic_id=academic_id).values_list('user',flat=True)] @@ -24,14 +27,16 @@ def get_users_from_acad(academic_id): students = [x for x in Student.objects.filter(id__in=student_ids).values_list('user',flat=True)] # return users who are organisers, invigilators or students of the given academic center users_from_acad = organisers + invigilators + students - return users_from_acad + unique_users_from_acad = list(set(users_from_acad)) + return unique_users_from_acad class Command(BaseCommand): help = 'Populate cms_usertype table from events_academickey. It populates data for organisers, invigilators and students for paid academic centers with check on expiry date' def handle(self, *args, **options): self.stdout.write('Starting subscription management command...') - acad_keys = AcademicKey.objects.values('academic_id').annotate(latest_expiry_date=Max('expiry_date')) + today = timezone.now().date() + acad_keys = AcademicKey.objects.filter(expiry_date__gte=today).values('academic_id').annotate(latest_expiry_date=Max('expiry_date')) for key in acad_keys: expiry_date = key['latest_expiry_date'] users = get_users_from_acad(key['academic_id']) From 681d303036d4434fe7b3ccba20f7b4750101c342 Mon Sep 17 00:00:00 2001 From: ankitamk14 Date: Tue, 21 May 2024 16:38:28 +0530 Subject: [PATCH 3/4] added arguments to management commands --- cms/management/commands/populate_ilw_data.py | 47 ++++++++++++------- .../commands/populate_subscription_data.py | 8 ++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/cms/management/commands/populate_ilw_data.py b/cms/management/commands/populate_ilw_data.py index 3f8f8c990..722f9f947 100644 --- a/cms/management/commands/populate_ilw_data.py +++ b/cms/management/commands/populate_ilw_data.py @@ -9,6 +9,7 @@ from django.conf import settings def set_user_type_ilw(user_lst,foss={}): + print(f"\033[95m user_lst : {len(user_lst)} \033[0m") for user in user_lst: try: ut=UserType.objects.get(user_id=user) @@ -18,7 +19,7 @@ def set_user_type_ilw(user_lst,foss={}): except UserType.DoesNotExist: ut=UserType.objects.create(user_id=user, ilw=foss) except Exception as e: - print(f"Exception in set_user_type_ilw : {e}") + print(f"Exception in set_user_type_ilw {user} : {e}") def get_fosses(payee): foss_lang = CdFossLanguages.objects.filter(payment=payee) @@ -28,31 +29,41 @@ def get_fosses(payee): return d def get_ilw_users(payee): - purpose = payee.purpose - if purpose == getattr(settings, 'EVENT_CD_CONTENT', 'cdcontent'): # users : individual payee - users = [payee.user_id] - return users - else: - try: - event = TrainingEvents.objects.get(id=purpose) - participants = [x for x in Participant.objects.filter(event=event).values_list('user_id',flat=True)] - event_users = [x.id for x in User.objects.filter(Q(email=event.event_coordinator_email) | Q(id=event.entry_user_id))] #Event coordinator & Training Manager - #return users who are participants /coordinator / Training Manager of the ILW event - unique_users = list(set(participants+event_users)) - return unique_users - except TrainingEvents.DoesNotExist: - print(f"\033[93m TrainingEvent not found: {purpose} \n \033[0m") - except Exception as e: - print(f"\033[93m get_ilw_users exception: {purpose} \n{e} \033[0m") + if payee.status == 1: + purpose = payee.purpose + if purpose == getattr(settings, 'EVENT_CD_CONTENT', 'cdcontent'): # users : individual payee + users = [payee.user_id] + return users + else: + try: + event = TrainingEvents.objects.get(id=purpose) + participants = [x for x in Participant.objects.filter(event=event).values_list('user_id',flat=True)] + event_users = [x.id for x in User.objects.filter(Q(email=event.event_coordinator_email) | Q(id=event.entry_user_id))] #Event coordinator & Training Manager + #return users who are participants /coordinator / Training Manager of the ILW event + unique_users = list(set(participants+event_users)) + return unique_users + except TrainingEvents.DoesNotExist: + print(f"\033[93m TrainingEvent not found: {purpose} \n \033[0m") + except Exception as e: + print(f"\033[93m get_ilw_users exception: {purpose} \n{e} \033[0m") + return [] class Command(BaseCommand): help = 'Populate cms_usertype table from donate_payee. It populates data for individual cd content download users,organizers, invigilators and participants for specific paid fosses.' + def add_arguments(self, parser): + parser.add_argument( + '--year', type=int, help='Filter Payee objects by year of date_updated' + ) + def handle(self, *args, **options): # Your code goes here self.stdout.write('Starting populate_ilw_data command...') - payee_list = Payee.objects.all() + year = options.get('year') + payee_list = Payee.objects.filter(status=1) + if year: + payee_list = payee_list.filter(updated__year=year) for payee in payee_list: d = get_fosses(payee) users = get_ilw_users(payee) diff --git a/cms/management/commands/populate_subscription_data.py b/cms/management/commands/populate_subscription_data.py index 9cea380b4..65ba3b06a 100644 --- a/cms/management/commands/populate_subscription_data.py +++ b/cms/management/commands/populate_subscription_data.py @@ -33,10 +33,18 @@ def get_users_from_acad(academic_id): class Command(BaseCommand): help = 'Populate cms_usertype table from events_academickey. It populates data for organisers, invigilators and students for paid academic centers with check on expiry date' + def add_arguments(self, parser): + parser.add_argument( + '--month', type=int, help='Filter Academic Key objects by month of expiry_date' + ) + def handle(self, *args, **options): self.stdout.write('Starting subscription management command...') today = timezone.now().date() acad_keys = AcademicKey.objects.filter(expiry_date__gte=today).values('academic_id').annotate(latest_expiry_date=Max('expiry_date')) + month = options.get('month') + if month: + acad_keys = acad_keys.filter(expiry_date__month=month) for key in acad_keys: expiry_date = key['latest_expiry_date'] users = get_users_from_acad(key['academic_id']) From 4844e73e8f81e7b07335cc222aa52a27ca4cf44e Mon Sep 17 00:00:00 2001 From: ankitamk14 Date: Tue, 21 May 2024 17:00:26 +0530 Subject: [PATCH 4/4] Print statements --- spoken/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spoken/views.py b/spoken/views.py index 0b706c2f5..5dbe96f7e 100644 --- a/spoken/views.py +++ b/spoken/views.py @@ -314,6 +314,8 @@ def watch_tutorial(request, foss, tutorial, lang): try: foss = unquote_plus(foss) is_valid_user_for_tut = is_valid_user(request.user,foss,lang) + print(f"\033[92m is_valid_user_for_tut : {is_valid_user_for_tut} \033[0m") + print(f"\033[92m request.user : {request.user} \033[0m") # is_valid_user_for_tut = True #Temporary making videos available to all tutorial = unquote_plus(tutorial) td_rec = TutorialDetail.objects.get(foss__foss=foss, tutorial=tutorial)