From 8d97183dd370bf396ea8a7c207db874655ae8981 Mon Sep 17 00:00:00 2001 From: uttamthummala Date: Fri, 27 Oct 2023 15:54:17 +0530 Subject: [PATCH 01/12] fixed mailer to send mails in batches --- CDC_Backend/APIs/utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index ff54a8a..8f92cc1 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -214,14 +214,16 @@ def sendEmail(email_to, subject, data, template, attachment_jnf_response=None): else: recipient_list = [str(email_to), ] - msg = EmailMultiAlternatives(subject, text_content, email_from,None,bcc=recipient_list) - msg.attach_alternative(html_content, "text/html") - if attachment_jnf_response: - # logger.info(attachment_jnf_response) - pdf = pdfkit.from_string(attachment_jnf_response['html'], False, - options={"--enable-local-file-access": "", '--dpi': '96'}) - msg.attach(attachment_jnf_response['name'], pdf, 'application/pdf') - msg.send() + #batch 100 ppl to send as bcc + for i in range(0,len(recipient_list),100): + msg = EmailMultiAlternatives(subject, text_content, email_from,None,bcc=recipient_list[i:i+100]) + msg.attach_alternative(html_content, "text/html") + if attachment_jnf_response: + # logger.info(attachment_jnf_response) + pdf = pdfkit.from_string(attachment_jnf_response['html'], False, + options={"--enable-local-file-access": "", '--dpi': '96'}) + msg.attach(attachment_jnf_response['name'], pdf, 'application/pdf') + msg.send() return True except: logger.error("Send Email: " + str(sys.exc_info())) From e05c4761a406841eedf199f729815c23aaf7d42d Mon Sep 17 00:00:00 2001 From: uttamthummala Date: Sun, 29 Oct 2023 16:51:48 +0530 Subject: [PATCH 02/12] changed endpoints to populate notifications --- CDC_Backend/APIs/adminViews.py | 10 +++++++--- CDC_Backend/APIs/utils.py | 11 ++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CDC_Backend/APIs/adminViews.py b/CDC_Backend/APIs/adminViews.py index eb76008..794d580 100644 --- a/CDC_Backend/APIs/adminViews.py +++ b/CDC_Backend/APIs/adminViews.py @@ -124,7 +124,7 @@ def updateDeadline(request, id, email, user_type): opening.deadline_datetime = datetime.datetime.strptime(data[DEADLINE_DATETIME], '%Y-%m-%d %H:%M:%S %z') opening.changed_by = get_object_or_404(User, id=id) opening.save() - send_opening_to_notifications_service(id=opening.id,name=opening.company_name,deadline=data[DEADLINE_DATETIME],role=opening.designation) + send_opening_to_notifications_service(id=opening.id,name=opening.company_name,deadline=data[DEADLINE_DATETIME],role=opening.designation,opening_type=opening_type) return Response({'action': "Update Deadline", 'message': "Deadline Updated"}, status=status.HTTP_200_OK) except Http404: @@ -162,7 +162,7 @@ def updateOfferAccepted(request, id, email, user_type): opening.save() if opening.offer_accepted: deadline=deadline_datetime.strftime('%Y-%m-%d %H:%M:%S %z') - send_opening_to_notifications_service(id=opening.id,name=opening.company_name,deadline=deadline,role=opening.designation) + send_opening_to_notifications_service(id=opening.id,name=opening.company_name,deadline=deadline,role=opening.designation,opening_type=opening_type) send_opening_notifications(opening.id,opening_type) else: raise ValueError("Offer Status already updated") @@ -766,7 +766,11 @@ def get_eligible_students(request): opening_type= data[OPENING_TYPE] else: opening_type= "Placement" - eligible_students=get_eligible_emails(opening_id=opening_id, opening_type=opening_type) + if "send_all" in data: + send_all = "True"==data["send_all"] + else: + send_all = False + eligible_students=get_eligible_emails(opening_id=opening_id, opening_type=opening_type, send_all=send_all) return Response({'action': "Get Eligible Students", 'message': "Eligible Students Fetched", 'eligible_students': eligible_students}, status=status.HTTP_200_OK) diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 8f92cc1..2db341c 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -457,7 +457,7 @@ def send_opening_notifications(opening_id, opening_type=PLACEMENT): logger.warning('Utils - send_opening_notifications: ' + str(sys.exc_info())) return False -def get_eligible_emails(opening_id, opening_type=PLACEMENT): +def get_eligible_emails(opening_id, opening_type=PLACEMENT,send_all=False): try: # print(opening_id, opening_type) if opening_type == PLACEMENT: @@ -473,6 +473,10 @@ def get_eligible_emails(opening_id, opening_type=PLACEMENT): isinstance(opening,Internship) and InternshipApplicationConditions(student, opening)[0]): try: student_user = get_object_or_404(User, id=student.id) + #if send_all True send all students eligible for the opening + if send_all: + emails.append(student_user.email) + continue # check if he applied if opening_type == PLACEMENT: if PlacementApplication.objects.filter(student=student, placement=opening).exists(): @@ -565,12 +569,13 @@ def send_email_for_opening(opening): @background_task.background(schedule=2) -def send_opening_to_notifications_service(id,name,deadline,role): +def send_opening_to_notifications_service(id,name,deadline,role,opening_type=PLACEMENT): data={ "id":id, "company":name, "deadline":deadline, - "role":role + "role":role, + "opening_type":opening_type } encoded=jwt.encode(data,os.environ.get("JWT_SECRET_KEY"),algorithm="HS256") data_={ From 8981627dee4d50a8065c5950e840cdbbfebab499 Mon Sep 17 00:00:00 2001 From: karthik mv Date: Tue, 31 Oct 2023 00:50:19 +0530 Subject: [PATCH 03/12] Update run_prod.sh for prod --- CDC_Backend/run_prod.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CDC_Backend/run_prod.sh b/CDC_Backend/run_prod.sh index 6476aab..044b5e2 100644 --- a/CDC_Backend/run_prod.sh +++ b/CDC_Backend/run_prod.sh @@ -1 +1 @@ -gunicorn --certfile=/home/cdc/Desktop/1f9476e3959ebe60.crt --keyfile=/home/cdc/Desktop/star_iitdh_key.key --bind localhost:8000 CDC_Backend.wsgi --access-logfile access.log --error-logfile error.log --forwarded-allow-ips="cdc.iitdh.ac.in" & +gunicorn --certfile=/home/cdc/Desktop/1f9476e3959ebe60.crt --keyfile=/home/cdc/Desktop/star_iitdh_key.key --bind localhost:8000 CDC_Backend.wsgi --access-logfile access.log --error-logfile error.log --forwarded-allow-ips="cdc.iitdh.ac.in" From cdf1e7bf192df7a683b0c9d7c09af99f2a8bc12b Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Thu, 9 Nov 2023 17:45:29 +0530 Subject: [PATCH 04/12] fixed inf validation and logs every inf and jnf request --- CDC_Backend/APIs/companyViews.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index cd73ce6..9ffb803 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -17,6 +17,8 @@ logger = logging.getLogger('db') RECAPTCHA_VALUE, JOB_LOCATION ]) def addPlacement(request): + logger.info("INF filled by " + str(request.data['email'])) + logger.info(json.dumps(request.data)) try: data = request.data files = request.FILES @@ -353,6 +355,7 @@ def autoFillInf(request): CONTACT_PERSON_NAME, PHONE_NUMBER, EMAIL, RECAPTCHA_VALUE]) def addInternship(request): logger.info("INF filled by " + str(request.data['email'])) + logger.info(json.dumps(request.data)) try: data = request.data files = request.FILES @@ -416,12 +419,12 @@ def addInternship(request): else: internship.is_work_from_home = False - if data[ALLOWED_BATCH] is None or json.loads(data[ALLOWED_BATCH]) == "": + if ALLOWED_BATCH in data[ALLOWED_BATCH] and data[ALLOWED_BATCH] is None or json.loads(data[ALLOWED_BATCH]) == "": raise ValueError('Allowed Branch cannot be empty') - elif set(json.loads(data[ALLOWED_BATCH])).issubset(BATCHES): + elif ALLOWED_BATCH in data[ALLOWED_BATCH] and set(json.loads(data[ALLOWED_BATCH])).issubset(BATCHES): internship.allowed_batch = json.loads(data[ALLOWED_BATCH]) else: - raise ValueError('Allowed Batch must be a subset of ' + str(BATCHES)) + internship.allowed_batch = ['2021'] if data[ALLOWED_BRANCH] is None or json.loads(data[ALLOWED_BRANCH]) == "": raise ValueError('Allowed Branch cannot be empty') From 8fe297c490360bd696e6a9f311dd98375848022d Mon Sep 17 00:00:00 2001 From: uttamthummala <84305043+uttamthummala@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:26:06 +0530 Subject: [PATCH 05/12] fixed typo --- CDC_Backend/APIs/companyViews.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index 9ffb803..83410f6 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -17,7 +17,7 @@ logger = logging.getLogger('db') RECAPTCHA_VALUE, JOB_LOCATION ]) def addPlacement(request): - logger.info("INF filled by " + str(request.data['email'])) + logger.info("JNF filled by " + str(request.data['email'])) logger.info(json.dumps(request.data)) try: data = request.data From 1c3103ed4ab6c6ea96fe7a6eb41a382b620bbf3e Mon Sep 17 00:00:00 2001 From: uttamthummala <84305043+uttamthummala@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:37:36 +0530 Subject: [PATCH 06/12] fixed typo --- CDC_Backend/APIs/companyViews.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index 83410f6..d721b12 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -420,7 +420,7 @@ def addInternship(request): internship.is_work_from_home = False if ALLOWED_BATCH in data[ALLOWED_BATCH] and data[ALLOWED_BATCH] is None or json.loads(data[ALLOWED_BATCH]) == "": - raise ValueError('Allowed Branch cannot be empty') + raise ValueError('Allowed Batches cannot be empty') elif ALLOWED_BATCH in data[ALLOWED_BATCH] and set(json.loads(data[ALLOWED_BATCH])).issubset(BATCHES): internship.allowed_batch = json.loads(data[ALLOWED_BATCH]) else: From 72c6187d0d97687dabb463821fe04311f71158ac Mon Sep 17 00:00:00 2001 From: karthik mv Date: Thu, 9 Nov 2023 19:44:57 +0530 Subject: [PATCH 07/12] Update companyViews.py --- CDC_Backend/APIs/companyViews.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index d721b12..5611b45 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -418,13 +418,14 @@ def addInternship(request): internship.is_work_from_home = True else: internship.is_work_from_home = False - - if ALLOWED_BATCH in data[ALLOWED_BATCH] and data[ALLOWED_BATCH] is None or json.loads(data[ALLOWED_BATCH]) == "": + + if ALLOWED_BATCH in data and (data[ALLOWED_BATCH] is None or json.loads(data[ALLOWED_BATCH]) == ""): raise ValueError('Allowed Batches cannot be empty') - elif ALLOWED_BATCH in data[ALLOWED_BATCH] and set(json.loads(data[ALLOWED_BATCH])).issubset(BATCHES): + elif ALLOWED_BATCH in data and set(json.loads(data[ALLOWED_BATCH])).issubset(BATCHES): internship.allowed_batch = json.loads(data[ALLOWED_BATCH]) else: internship.allowed_batch = ['2021'] + if data[ALLOWED_BRANCH] is None or json.loads(data[ALLOWED_BRANCH]) == "": raise ValueError('Allowed Branch cannot be empty') From 7c3d4f9e4d888be1111640b4a339efbc3a2c5c8b Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Sat, 11 Nov 2023 03:35:29 +0530 Subject: [PATCH 08/12] remove json dumps --- CDC_Backend/APIs/companyViews.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index d721b12..c9f8da9 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -18,7 +18,7 @@ logger = logging.getLogger('db') ]) def addPlacement(request): logger.info("JNF filled by " + str(request.data['email'])) - logger.info(json.dumps(request.data)) + logger.info(request.data) try: data = request.data files = request.FILES @@ -355,7 +355,7 @@ def autoFillInf(request): CONTACT_PERSON_NAME, PHONE_NUMBER, EMAIL, RECAPTCHA_VALUE]) def addInternship(request): logger.info("INF filled by " + str(request.data['email'])) - logger.info(json.dumps(request.data)) + logger.info(request.data) try: data = request.data files = request.FILES From 6e957413970a199adf7d89a3949045aa6499f127 Mon Sep 17 00:00:00 2001 From: uttamthummala Date: Mon, 13 Nov 2023 23:43:12 +0530 Subject: [PATCH 09/12] added auto clean test files and test objects --- CDC_Backend/APIs/companyViews.py | 4 +- CDC_Backend/APIs/cron.py | 63 +++++++++++++++++++++++++++++ CDC_Backend/CDC_Backend/settings.py | 7 +++- requirements.txt | 1 + setup.sh | 2 +- 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 CDC_Backend/APIs/cron.py diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index 350c1ad..c8e769d 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -235,14 +235,14 @@ def addPlacement(request): except ValueError as e: store_all_files(request) - exception_email(data) + #exception_email(data) logger.warning("ValueError in addPlacement: " + str(e)) logger.warning(traceback.format_exc()) return Response({'action': "Add Placement", 'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) except: store_all_files(request) - exception_email(data) + #exception_email(data) logger.warning("Add New Placement: " + str(sys.exc_info())) logger.warning(traceback.format_exc()) return Response({'action': "Add Placement", 'message': "Something went wrong"}, diff --git a/CDC_Backend/APIs/cron.py b/CDC_Backend/APIs/cron.py new file mode 100644 index 0000000..be45f12 --- /dev/null +++ b/CDC_Backend/APIs/cron.py @@ -0,0 +1,63 @@ +from .models import * +from .utils import * +import shutil +import logging +import traceback + +logger=logging.getLogger('db') +def clean_up_tests(): + # Delete all the test internships and placements created + try: + internships= Internship.objects.filter(company_name="test company",email="notifications@cdc-iitdh.tech") + hits_internship=len(internships) + for internship in internships: + #count number of file + files = os.listdir(STORAGE_DESTINATION_COMPANY_ATTACHMENTS+internship.id) + if len(files) == 4: + print("working fine") + else: + print("not working fine") + logger.error("files submitted in inf are not getting stored for test case"+internship.description) + + #remove folder from the server + print("removing folder ",STORAGE_DESTINATION_COMPANY_ATTACHMENTS+internship.id) + shutil.rmtree(STORAGE_DESTINATION_COMPANY_ATTACHMENTS+internship.id) + + + internship.delete() + + placements= Placement.objects.filter(company_name="test company",email="notifications@cdc-iitdh.tech") + hits_placement=len(placements) + for placement in placements: + #count number of file + files = os.listdir(STORAGE_DESTINATION_COMPANY_ATTACHMENTS+placement.id) + if len(files) == 4: + print("working fine") + else: + print("not working fine") + logger.error("files submitted in inf are not getting stored for test case"+placement.description) + + #remove folder from the server + print("removing folder ",STORAGE_DESTINATION_COMPANY_ATTACHMENTS+internship.id) + shutil.rmtree(STORAGE_DESTINATION_COMPANY_ATTACHMENTS+placement.id) + + + placement.delete() + + if hits_internship >= 6: + print("all hits are working fine") + else: + print("some hits are not working fine") + logger.error("some test hits are not working fine for internship") + + if hits_placement >= 6: + print("all hits are working fine") + else: + print("some hits are not working fine") + logger.error("some test hits are not working fine for placement") + + except : + logger.error("error in clean up function") + logger.error(traceback.format_exc()) + + \ No newline at end of file diff --git a/CDC_Backend/CDC_Backend/settings.py b/CDC_Backend/CDC_Backend/settings.py index 355ba6d..174d9c7 100644 --- a/CDC_Backend/CDC_Backend/settings.py +++ b/CDC_Backend/CDC_Backend/settings.py @@ -47,7 +47,8 @@ INSTALLED_APPS = [ 'background_task', 'simple_history', 'import_export', - 'django_extensions' + 'django_extensions', + 'django_crontab', ] MIDDLEWARE = [ @@ -203,3 +204,7 @@ LOGGING = { } # django_heroku.settings(locals()) + +CRONJOBS = [ + ('0 8,20 * * *', 'APIs.cron.clean_up_tests') +] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index efc1a43..cf6cf51 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ Django==3.2.13 django-background-tasks==1.2.5 django-compat==1.0.15 django-cors-headers==3.11.0 +django-crontab==0.7.1 django-db-logger==0.1.12 django-extensions==3.2.1 django-import-export==2.8.0 diff --git a/setup.sh b/setup.sh index 1ba33a4..1bc917c 100755 --- a/setup.sh +++ b/setup.sh @@ -8,7 +8,7 @@ python3 manage.py migrate echo "Migrations complete" python3 manage.py collectstatic --noinput - +python3 manage.py crontab add DIR="./Storage" if [ -d "$DIR" ]; then ### Take action if $DIR exists ### From 3819d3d7ed1a700f63b57682c54d87cfd454e866 Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Sun, 19 Nov 2023 16:14:42 +0530 Subject: [PATCH 10/12] can apply only to greater tiers --- CDC_Backend/APIs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 2db341c..78e261e 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -250,11 +250,11 @@ def PlacementApplicationConditions(student, placement): return True, "Conditions Satisfied" for i in selected_companies: - if int(i.placement.tier) < int(placement.tier): + if int(i.placement.tier) <= int(placement.tier): return False, "Can't apply for this tier" for i in PPO: - if int(i.tier) < int(placement.tier): + if int(i.tier) <= int(placement.tier): return False, "Can't apply for this tier" if student.degree != 'bTech' and not placement.rs_eligible: From c298e313e20a6265ae261ceec307429877b6407c Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Sun, 19 Nov 2023 16:37:41 +0530 Subject: [PATCH 11/12] 2 offer for tier 1 --- CDC_Backend/APIs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 78e261e..3c626ba 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -250,11 +250,11 @@ def PlacementApplicationConditions(student, placement): return True, "Conditions Satisfied" for i in selected_companies: - if int(i.placement.tier) <= int(placement.tier): + if int(i.placement.tier) != 1 and int(i.placement.tier) <= int(placement.tier): return False, "Can't apply for this tier" for i in PPO: - if int(i.tier) <= int(placement.tier): + if int(i.tier) != 1 and int(i.tier) <= int(placement.tier): return False, "Can't apply for this tier" if student.degree != 'bTech' and not placement.rs_eligible: From 3d3584ab8e5fc7ca55553517498ea85b85c42f80 Mon Sep 17 00:00:00 2001 From: karthikmurakonda Date: Wed, 29 Nov 2023 23:05:55 +0530 Subject: [PATCH 12/12] fix tier validation --- CDC_Backend/APIs/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 3c626ba..3d87410 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -252,10 +252,14 @@ def PlacementApplicationConditions(student, placement): for i in selected_companies: if int(i.placement.tier) != 1 and int(i.placement.tier) <= int(placement.tier): return False, "Can't apply for this tier" + elif int(i.placement.tier) == 1 and int(placement.tier) != 1: + return False, "Can't apply for this tier" for i in PPO: if int(i.tier) != 1 and int(i.tier) <= int(placement.tier): return False, "Can't apply for this tier" + elif int(i.tier) == 1 and int(placement.tier) != 1: + return False, "Can't apply for this tier" if student.degree != 'bTech' and not placement.rs_eligible: raise PermissionError("Can't apply for this placement")