From 6e957413970a199adf7d89a3949045aa6499f127 Mon Sep 17 00:00:00 2001 From: uttamthummala Date: Mon, 13 Nov 2023 23:43:12 +0530 Subject: [PATCH] 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 ###