Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions cms/management/commands/populate_ilw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
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)
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 {user} : {e}")

def get_fosses(payee):
foss_lang = CdFossLanguages.objects.filter(payment=payee)
Expand All @@ -26,30 +29,44 @@ 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
return participants+event_users
except Exception as e:
print(f"event failed: {purpose:>15} \n{e}")
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)
set_user_type_ilw(users,d)
if users:
set_user_type_ilw(users,d)
self.stdout.write('Ending populate_ilw_data command...')
17 changes: 15 additions & 2 deletions cms/management/commands/populate_subscription_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)]
Expand All @@ -24,14 +27,24 @@ 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 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...')
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'))
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'])
Expand Down
6 changes: 4 additions & 2 deletions spoken/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,10 @@ 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)
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)
tr_rec = TutorialResource.objects.select_related().get(tutorial_detail=td_rec, language=Language.objects.get(name=lang))
Expand Down