diff --git a/CDC_Backend/APIs/admin.py b/CDC_Backend/APIs/admin.py index 6b80e78..66c874d 100644 --- a/CDC_Backend/APIs/admin.py +++ b/CDC_Backend/APIs/admin.py @@ -9,6 +9,7 @@ from import_export.admin import ImportExportMixin, ExportMixin from import_export import resources from .models import * +from .utils import send_email_for_opening class ArrayFieldListFilter(admin.SimpleListFilter): @@ -109,12 +110,22 @@ class PlacementResources(resources.ModelResource): class AdminAdmin(ExportMixin, SimpleHistoryAdmin): resource_class = PlacementResources + def save_model(self, request, obj, form, change): + # Check if email_verified field is being changed from False to True + if change and not obj._state.adding and obj.email_verified and form.initial.get('email_verified', False) != obj.email_verified: + # Run the send_email_for_opening function + send_email_for_opening(obj) + + # Save the model as usual + super().save_model(request, obj, form, change) + + @admin.register(Placement) class Placement(AdminAdmin): - list_display = (COMPANY_NAME, CONTACT_PERSON_NAME, PHONE_NUMBER, 'tier', 'compensation_CTC') + list_display = (COMPANY_NAME, CONTACT_PERSON_NAME, PHONE_NUMBER, 'tier', 'compensation_CTC', 'email_verified', 'updated_at') search_fields = (COMPANY_NAME, CONTACT_PERSON_NAME) - ordering = (COMPANY_NAME, CONTACT_PERSON_NAME, 'tier', 'compensation_CTC') + ordering = ('updated_at', COMPANY_NAME, CONTACT_PERSON_NAME, 'tier', 'compensation_CTC') list_filter = ('tier',) @@ -162,4 +173,29 @@ class PrePlacementOffer(PrePlacementOfferAdmin): def Student(self, obj): return model_admin_url(obj.student) -admin.site.register(Internship) \ No newline at end of file + +class InternshipResources(resources.ModelResource): + class Meta: + model = Internship + exclude = ('id', 'changed_by', 'is_company_details_pdf', 'is_description_pdf', + 'is_compensation_details_pdf', 'is_selection_procedure_details_pdf') + + +class InternAdmin(ExportMixin, SimpleHistoryAdmin): + resource_class = InternshipResources + + def save_model(self, request, obj, form, change): + # Check if email_verified field is being changed from False to True + if change and not obj._state.adding and obj.email_verified and form.initial.get('email_verified', False) != obj.email_verified: + # Run the send_email_for_opening function + send_email_for_opening(obj) + + super().save_model(request, obj, form, change) + + +@admin.register(Internship) +class Placement(InternAdmin): + list_display = (COMPANY_NAME, CONTACT_PERSON_NAME, PHONE_NUMBER, 'stipend', 'email_verified', 'updated_at') + search_fields = (COMPANY_NAME, CONTACT_PERSON_NAME) + ordering = ('updated_at', COMPANY_NAME, CONTACT_PERSON_NAME, 'stipend') + diff --git a/CDC_Backend/APIs/companyViews.py b/CDC_Backend/APIs/companyViews.py index 2b26eca..bfc9ecc 100644 --- a/CDC_Backend/APIs/companyViews.py +++ b/CDC_Backend/APIs/companyViews.py @@ -221,7 +221,8 @@ def addPlacement(request): raise RuntimeError("Error in generating one time verification link for placement") data = { "designation": opening.designation, - "one_time_link": link + "one_time_link": link, + "opening_type": "Job" } sendEmail(opening.email, COMPANY_EMAIl_VERIFICATION_TEMPLATE_SUBJECT, data, @@ -285,20 +286,7 @@ def verifyEmail(request): if send_email_to_company: # Email sending part. - pdfhtml = opening_description_table_html(opening) - name = opening.company_name + '_jnf_response.pdf' - attachment_jnf_respone = { - "name": name, - "html": pdfhtml, - } - data = { - "designation": opening.designation, - "opening_type": opening_type, - "company_name": opening.company_name, - } - sendEmail([opening.email, CDC_MAIl_ADDRESS], - COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT.format(id=opening.id), data, - COMPANY_OPENING_SUBMITTED_TEMPLATE, attachment_jnf_respone) + send_email_for_opening(opening) return Response({'action': "Verify Email", 'message': "Email Verified Successfully"}, status=status.HTTP_200_OK) @@ -493,7 +481,8 @@ def addInternship(request): raise RuntimeError("Error in generating one time verification link for internship") data = { "designation": internship.designation, - "one_time_link": link + "one_time_link": link, + "opening_type": "Internship" } sendEmail(internship.email, COMPANY_EMAIl_VERIFICATION_TEMPLATE_SUBJECT, data, diff --git a/CDC_Backend/APIs/constants.py b/CDC_Backend/APIs/constants.py index 7f97005..b83efe1 100644 --- a/CDC_Backend/APIs/constants.py +++ b/CDC_Backend/APIs/constants.py @@ -1,5 +1,7 @@ import os +DEBUG = os.environ.get('DEBUG') + BRANCH_CHOICES = [ ["CSE", "CSE"], ["EE", "EE"], @@ -160,7 +162,7 @@ SPECIAL_FORMAT_IN_PDF = ['website', 'company_details_pdf_names', 'description_pd 'stipend_description_pdf_names'] COMPANY_OPENING_ERROR_TEMPLATE = "Alert! Error submitting opening for {company_name}." -COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT = "Notification Submitted - {id} - Career Development Cell, IIT Dharwad" +COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT = "Notification Submitted - {id}, {company} - Career Development Cell, IIT Dharwad" STUDENT_APPLICATION_STATUS_TEMPLATE_SUBJECT = 'Application Status - {company_name} - {id}' STUDENT_APPLICATION_SUBMITTED_TEMPLATE_SUBJECT = 'CDC - Application Submitted - {company_name}' STUDENT_APPLICATION_UPDATED_TEMPLATE_SUBJECT = 'CDC - Application Updated - {company_name}' diff --git a/CDC_Backend/APIs/utils.py b/CDC_Backend/APIs/utils.py index 6c93c5b..851c99e 100644 --- a/CDC_Backend/APIs/utils.py +++ b/CDC_Backend/APIs/utils.py @@ -61,7 +61,9 @@ def get_token(): logger.warning("Get Token: " + str(sys.exc_info())) return Response({'action': "Get Token", 'message': str(e)}, status=status.HTTP_400_BAD_REQUEST) + return wrapper_func + return decorator @@ -120,7 +122,8 @@ def isAuthorized(allowed_users=None): user.save() if len(set(user.user_type).intersection(set(allowed_users))) or allowed_users == '*': if "MODIFIED" in headers: - return view_func(request, user.id, user.email, user.user_type, token_id, *args, **kwargs) + return view_func(request, user.id, user.email, user.user_type, token_id, *args, + **kwargs) else: return view_func(request, user.id, user.email, user.user_type, *args, **kwargs) else: @@ -438,3 +441,35 @@ def store_all_files(request): for file in files.getlist(DESCRIPTION_PDF): file_location = STORAGE_DESTINATION_COMPANY_ATTACHMENTS + "temp" + '/' saveFile(file, file_location) + + +def send_email_for_opening(opening): + try: + + # Prepare email data and attachment + pdfhtml = opening_description_table_html(opening) + name = opening.company_name + '_jnf_response.pdf' + attachment_jnf_respone = { + "name": name, + "html": pdfhtml, + } + data = { + "designation": opening.designation, + "opening_type": "INTERNSHIP" if isinstance(opening, Internship) else "PLACEMENT", + "company_name": opening.company_name, + } + + if DEBUG: + emails = [opening.email] + else: + emails = [opening.email, CDC_MAIl_ADDRESS] + # Send the email + sendEmail(emails, + COMPANY_OPENING_SUBMITTED_TEMPLATE_SUBJECT.format(id=opening.designation, company=opening.company_name), data, + COMPANY_OPENING_SUBMITTED_TEMPLATE, attachment_jnf_respone) + + except Exception as e: + # Handle the exception here (e.g., log the error, send an error email, etc.) + print("An error occurred while sending the email:", e) + + diff --git a/CDC_Backend/CDC_Backend/settings.py b/CDC_Backend/CDC_Backend/settings.py index 7df7fc3..355ba6d 100644 --- a/CDC_Backend/CDC_Backend/settings.py +++ b/CDC_Backend/CDC_Backend/settings.py @@ -157,10 +157,14 @@ CORS_ORIGIN_WHITELIST = [ CORS_REPLACE_HTTPS_REFERER = True CSRF_TRUSTED_ORIGINS = [ "https://cdc.iitdh.ac.in", "http://cdc.iitdh.ac.in"] -# EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' -EMAIL_FILE_PATH = './emails' - -EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_BACKEND = '' +if DEBUG: + # file based email backend for development + EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' + EMAIL_FILE_PATH = './test-emails' +else: + # SMTP backend for production + EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_USE_TLS = True EMAIL_PORT = 587 diff --git a/CDC_Backend/templates/company_email_verification.html b/CDC_Backend/templates/company_email_verification.html index 16f39d4..035f21e 100644 --- a/CDC_Backend/templates/company_email_verification.html +++ b/CDC_Backend/templates/company_email_verification.html @@ -44,7 +44,7 @@

- We have received your Job Notification for {{ designation }}. Kindly verify your email by clicking here.