def notify_selected_students(self, actor): if not self.instance.students.exists( ): # Not using 'studying' manager because college may select students in a session after they've graduated. return dsession = self.instance message = "Congratulations! " if dsession.type == 'J': message += "You have been placed at %s. " % ( dsession.dummy_company.name.title()) else: message += "You have grabbed the internship at %s. " % ( dsession.dummy_company.name.title()) students = self.instance.students.all() # Not using 'studying' manager student_usernames = ','.join([ s['profile__username'] for s in students.values('profile__username') ]) for student in students: Notification.objects.create(actor=actor, target=student.profile, message=message) send_mass_mail_task.delay( "Congratulations!", message, [s['profile__pk'] for s in students.values('profile__pk')]) # Log dummyLogger.info('%s - Students selected %s - [DS: %d]' % (actor.username, student_usernames, self.instance.pk))
def notify_all(self, students, actor): if self.cleaned_data.get('mail'): student_pks = self.querysets_to_values( students.values('profile__pk'), 'profile__pk') student_enrolls = self.querysets_to_values( students.values('profile__username'), 'profile__username') subject = self.cleaned_data.get('subject', None) message = self.cleaned_data.get('message', None) send_mass_mail_task.delay(subject, message, student_pks) notificationLogger.info('Session E-Mail sent to %s' % (student_enrolls)) if self.cleaned_data.get('notification'): subject = self.cleaned_data.get('subject', None) message = self.cleaned_data.get('message', None) student_enrolls = self.querysets_to_values( students.values('profile__username'), 'profile__username') notification_data_object = NotificationData.objects.create( subject=subject, message=message) for student in students: student_customeuser_object = student.profile Notification.objects.create( actor=actor, target=student_customeuser_object, notification_data=notification_data_object) notificationLogger.info('Session notification created for %s' % (student_enrolls))
def notify_students_about_new_posting(sender, **kwargs): if not kwargs.get('created'): return session = kwargs.get('instance') association = session.association subject = "New %s Posting - %s" % (dict(Association.PLACEMENT_TYPE)[association.type], association.company.name) message = render_to_string('recruitment/new_posting.txt', {'association': association, 'company': association.company, 'session': session, 'deadline':session.application_deadline + datetime.timedelta(1)}) student_pks = [] customuser_pks = [] years = session.selection_criteria.years.split(',') for stream in association.streams.all(): students = stream.students(manager='studying').filter(current_year__in=years) # Only currently studying students should be notified about new posting student_pks += [s['pk'] for s in students.values('pk')] customuser_pks += [u['profile__pk'] for u in students.values('profile__pk')] # students = Student.studying.filter(pk__in=student_pks) # notification_data = NotificationData.objects.create(message=message, subject=subject) # college = association.college # for student in students: # Notification.objects.create(notification_data=notification_data, actor=college.profile, target=student.profile) send_mass_mail_task.delay(subject, message, customuser_pks)
def notify_disqualified_students(self, actor): students = self.cleaned_data['students'].values('pk') shortlisted_pks = [s['pk'] for s in students] original_students_pks = [s.pk for s in self.students_queryset] disqualified_pks = set(original_students_pks).difference( set(shortlisted_pks)) # disqualified = self.students_queryset.exclude(pk__in=shortlisted_pks) Somehow this isn't working __-__ # Haven't used 'studying' manager because college may remove students from session after they've graduated disqualified = Student.objects.filter(pk__in=disqualified_pks) self.disqualified = disqualified student_disq_usernames = ','.join([ s['profile__username'] for s in disqualified.values('profile__username') ]) message = "Sorry, your involvement in the placement session with %s was till here only!\ \nThanks for showing your interest.\ \nBest of luck for your future endeavours." % ( self.instance.association.company.name) for student in disqualified: Notification.objects.create(actor=actor, target=student.profile, message=message) # send mass email association = self.instance.association subject = '%s session with %s' % ('Internship' if association.type == 'I' else 'Job', association.company.name) disqualified_user_pks = [ u['profile__pk'] for u in disqualified.values('profile__pk') ] send_mass_mail_task.delay(subject, message, disqualified_user_pks) # send_mass_mail_task.delay(subject, message, list(disqualified_pks)) # Log recruitmentLogger.info( '%s - Students disqualified %s - [S: %d]' % (actor.username, student_disq_usernames, self.instance.pk))
def notify_disqualified_students(self, actor): shortlisted_pks = [ s['pk'] for s in self.cleaned_data['students'].values('pk') ] # disqualified = self.students_queryset.exclude(pk__in=shortlisted_pks) Somehow this doesn't work _._ original_students_pks = [s.pk for s in self.students_queryset] disqualified_pks = set(original_students_pks).difference( set(shortlisted_pks)) # Not using 'studying' filter because college may remove students from a session after they've graduated. disqualified = Student.objects.filter(pk__in=disqualified_pks) self.disqualified = disqualified student_disq_usernames = ','.join([ s['profile__username'] for s in disqualified.values('profile__username') ]) message = "Sorry, your involvement in the %s session by %s was till here only!\ \nThanks for showing your interest.\ \nBest of luck for your future." % ("job" if self.instance.type == 'J' else "internship", self.instance.dummy_company.name) for student in disqualified: Notification.objects.create(actor=actor, target=student.profile, message=message) # send mass email subject = '%s session with %s' % ('Internship' if self.instance.type == 'I' else 'Job', self.instance.dummy_company.name) disqualified_user_pks = [ u['profile__pk'] for u in disqualified.values('profile__pk') ] send_mass_mail_task.delay(subject, message, disqualified_user_pks) # send_mass_mail_task.delay(subject, message, list(disqualified_pks)) # LOG dummyLogger.info( '%s - Students disqualified %s - [DS: %d]' % (actor.username, student_disq_usernames, self.instance.pk))
def notify_students_about_new_posting(sender, **kwargs): dsession = kwargs.get('instance') action = kwargs.get('action') reverse = kwargs.get('reverse') # print('Created: ', getattr(dsession, 'created', None)) if action == 'post_add' and not reverse and (hasattr(dsession, 'created') and dsession.created): # streams = Stream.objects.filter(pk__in=kwargs.get('pk_set')) subject = "New %s Posting - %s" % (dict( DummySession.PLACEMENT_TYPE)[dsession.type], dsession.dummy_company.name) message = render_to_string( 'dummy_company/new_posting.txt', { 'dcompany': dsession.dummy_company, 'dsession': dsession, 'deadline': dsession.application_deadline + datetime.timedelta(1) }) student_pks = [] customuser_pks = [] years = dsession.selection_criteria.years.split(',') for stream in dsession.streams.all(): students = stream.students(manager='studying').filter( current_year__in=years ) # Only currently studying students should be notified about new posting student_pks += [s['pk'] for s in students.values('pk')] customuser_pks += [ u['profile__pk'] for u in students.values('profile__pk') ] # students = Student.studying.filter(pk__in=student_pks) # notification_data = NotificationData.objects.create(message=message, subject=subject) IMPORT ME # college = association.college # for student in students: # Notification.objects.create(notification_data=notification_data, actor=college.profile, target=student.profile) send_mass_mail_task.delay(subject, message, customuser_pks)
def solve_issue(request): if request.user.type == 'F': if request.method == 'GET': identifier = request.GET.get('identifier', None) if not identifier: raise Http404 hashids = Hashids("ALonePinkSpiderInYellowWoods") pk_tuple = hashids.decode(identifier) pk_list = list(pk_tuple) possible_pk = pk_list[0] / 1000033 if not possible_pk.is_integer(): raise Http404 issue_object = get_object_or_404(Issue, pk=possible_pk) context = dict() context['student'] = issue_object.actor context['issue_object'] = issue_object context['identifier'] = identifier context['issue_type'] = get_issue_name(issue_object.issue_type) form_object = IssueReplyForm() context['solve_issue_form'] = form_object raw_html = render(request, 'notification/submit_solution.html', context=context) return HttpResponse(raw_html) if request.method == 'POST': identifier = request.POST.get('identifier', None) if_email = js_to_django_boolean(request.POST.get('if_email')) if not identifier: raise Http404 hashids = Hashids("ALonePinkSpiderInYellowWoods") pk_tuple = hashids.decode(identifier) pk_list = list(pk_tuple) possible_pk = pk_list[0] / 1000033 if not possible_pk.is_integer(): raise Http404 issue_object = get_object_or_404(Issue, pk=possible_pk) form_object = IssueReplyForm(request.POST, faculty=request.user.faculty, root_issue=issue_object) if form_object.is_valid(): form_object.save() notification_message = "Your issue with subject : " + issue_object.subject + " has been solved." notification_object = Notification.objects.create( actor=request.user, target=issue_object.actor.profile, message=notification_message) notification_object.save() issue_object.solved_by = request.user.faculty.get_full_name() issue_object.save() if if_email: send_mass_mail_task.delay('Reply for your Issue', issue_object.issue_reply.reply, issue_object.actor.pk) notificationLogger.info( '%s solved an issue; issue pk %d' % (request.user.username, issue_object.pk)) return HttpResponse(status=201) else: return JsonResponse(status=400, data={'errors': form_object._errors}) else: raise PermissionDenied
def create_notification(request): ''' The view is too localized for different users. Sometime in the future, it would be wise to make things generic. Also, being a fledgling in django once, I was reluctant on using ModelForms. BIG MISTAKE. ''' if request.user.type == 'C' or request.user.type == 'F': if request.method == 'POST': if request.user.type == 'F': college_object = request.user.faculty.college faculty_object = request.user.faculty sender_name = faculty_object.firstname #getting all the peer faculties except the initiator. faculties = college_object.faculties.all().exclude( pk=faculty_object.pk) else: college_object = request.user.college sender_name = 'TPO' #getting all the faculties. faculties = college_object.faculties.all() students_selected = request.POST.getlist('student_list[]') if not any(students_selected): return JsonResponse( status=400, data={ 'errors': 'Please select at least 1 Student. Check if Select Year field is not blank.' }) subject = request.POST.get('subject') if '\n' in subject or len(subject) < 1 or len(subject) > 255: return JsonResponse( status=400, data={'errors': 'Please enter the Subject Properly.'}) message = request.POST.get('message') if_sms = js_to_django_boolean(request.POST.get('if_sms')) if_email = js_to_django_boolean(request.POST.get('if_email')) if if_email: if len(subject) < 5: return JsonResponse( status=400, data={ 'errors': 'For E-Mails to be sent, please make sure the subject has at least 5 characters.' }) sms_message = request.POST.get('sms_message') if if_sms: if len(sms_message) < 30 or len(sms_message) > 160: return JsonResponse( status=400, data={ 'errors': 'Please keep the length of SMS Message between 30 and 160 Characters.' }) college_customuser_object = college_object.profile college_students_queryset = college_object.students( manager='studying').all() # Only currently studying students if not field_length_test(subject, 256): return JsonResponse(status=400, data={"error": "Length Exceeded."}) student_objects = college_students_queryset.filter( profile__username__in=students_selected) student_enrolls = querysets_to_values( student_objects.values('profile__username'), 'profile__username') student_phone_numbers = querysets_to_values( student_objects.values('phone_number'), 'phone_number') #following statement is getting customuser-pks. student_pks = querysets_to_values( student_objects.values('profile__pk'), 'profile__pk') if if_email: send_mass_mail_task.delay(subject, message, student_pks) if request.user.type == 'F': notificationLogger.info('%s sent E-Mail to %s' % (faculty_object, student_enrolls)) else: notificationLogger.info('%s sent E-Mail to %s' % (college_object, student_enrolls)) if if_sms: send_mass_sms_task.delay(college_object.pk, sms_message, student_phone_numbers, template_name='') if request.user.type == 'F': notificationLogger.info('%s sent SMS Message to %s' % (faculty_object, student_enrolls)) else: notificationLogger.info('%s sent SMS Message to %s' % (college_object, student_enrolls)) notification_data_object = NotificationData.objects.create( subject=subject, message=message, sms_message=sms_message) if request.user.type == 'F': notificationLogger.info('%s created a notification for %s' % (faculty_object, student_enrolls)) else: notificationLogger.info('%s created a notification for %s' % (college_object, student_enrolls)) for student_object in student_objects: student_customeuser_object = student_object.profile notification_object = Notification.objects.create( actor=college_customuser_object, target=student_customeuser_object, notification_data=notification_data_object) notification_object.save() #sending(cc) it to all the faculties as well. faculty_pks = querysets_to_values(faculties.values('profile__pk'), 'profile__pk') cc_added_subject = add_cc(subject, 255, sender_name) cc_added_sms = add_cc(sms_message, 159, sender_name) cc_notification_data_object = NotificationData.objects.create( subject=cc_added_subject, message=message, sms_message=cc_added_sms) for faculty in faculties: faculty_customuser_object = faculty.profile notification_object = Notification.objects.create( actor=college_customuser_object, target=faculty_customuser_object, notification_data=cc_notification_data_object) notification_object.save() if request.user.type == 'F': faculty_customuser_object = faculty_object.profile notification_object = Notification.objects.create( actor=faculty_customuser_object, target=college_customuser_object, notification_data=cc_notification_data_object) notification_object.save() #add email of college admin to send email if faculty initiated notification. faculty_pks.append(college_object.profile.pk) #sending mail and sms to faculties if if_email: send_mass_mail_task.delay(cc_added_subject, message, faculty_pks) notificationLogger.info('sent CC-Email to faculties') if if_sms: faculty_phone_numbers = querysets_to_values( faculties.values('phone_number'), 'phone_number') #leaving 1st parameter in the below function as is. send_mass_sms_task.delay(college_object.pk, cc_added_sms, faculty_phone_numbers, template_name='') notificationLogger.info('sent CC-SMS Message to faculties') notificationLogger.info( 'Winded up faculty. Done all notifications. Bye.') return HttpResponse(len(students_selected)) else: raise PermissionDenied else: raise PermissionDenied