Skip to content

Commit

Permalink
Create new command for sftp upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ezkat committed Nov 1, 2023
1 parent 3cdb616 commit 531fb61
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
46 changes: 46 additions & 0 deletions qualitytool/management/commands/save_daily_utilization_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.core.management.base import BaseCommand
from django.utils import timezone
from django.conf import settings
from datetime import datetime
from qualitytool.models import ResourceQualityTool
from qualitytool.manager import qt_manager
import os.path as _
import logging


logger = logging.getLogger()



class Command(BaseCommand):
help = "Saves daily utilization to a CSV file."

def add_arguments(self, parser):
parser.add_argument('--date', action='store')

Check warning on line 19 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L19

Added line #L19 was not covered by tests

def handle(self, *args, **options):
path = options.get('path')
date = options.get('date', None)

Check warning on line 23 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L22-L23

Added lines #L22 - L23 were not covered by tests


if date:
date = timezone.make_aware(datetime.strptime(date, '%Y-%m-%d'))

Check warning on line 27 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L27

Added line #L27 was not covered by tests
else:
date = timezone.now()

Check warning on line 29 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L29

Added line #L29 was not covered by tests


daily_utilizations = [
qt_manager.get_daily_utilization(qualitytool, date)
for qualitytool in ResourceQualityTool.objects.all()
]

if not daily_utilizations:
return

Check warning on line 38 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L38

Added line #L38 was not covered by tests


with open(path, 'w') as csv_file:

Check warning on line 41 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L41

Added line #L41 was not covered by tests
for daily_utilization in daily_utilizations:
csv_file.write('%(row)s\n' % ({
'row': ','.join(str(val) for val in daily_utilization.values())
}))
logging.info(f'Generated new daily utilization csv file with {len(daily_utilizations)} entries.')

Check warning on line 46 in qualitytool/management/commands/save_daily_utilization_csv.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/save_daily_utilization_csv.py#L46

Added line #L46 was not covered by tests
60 changes: 60 additions & 0 deletions qualitytool/management/commands/sftp_daily_utilization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from django.core.management.base import BaseCommand
from django.core.exceptions import ImproperlyConfigured
from django.utils import timezone
from django.conf import settings
from datetime import datetime
from qualitytool.models import ResourceQualityTool
from qualitytool.manager import qt_manager
import os.path as _
import logging
import paramiko


logger = logging.getLogger()

class Command(BaseCommand):
help = "Uploads daily utilization to a sftp server."

def add_arguments(self, parser):
parser.add_argument('path')
parser.add_argument('--date', action='store')

Check warning on line 20 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L19-L20

Added lines #L19 - L20 were not covered by tests

def handle(self, *args, **options):
path = options.get('path')
date = options.get('date', None)

Check warning on line 24 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L23-L24

Added lines #L23 - L24 were not covered by tests

if not settings.QUALITYTOOL_SFTP_HOST:
raise ImproperlyConfigured('Missing env setting: QUALITYTOOL_SFTP_HOST')

Check warning on line 27 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L27

Added line #L27 was not covered by tests

if date:
date = timezone.make_aware(datetime.strptime(date, '%Y-%m-%d'))

Check warning on line 30 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L30

Added line #L30 was not covered by tests
else:
date = timezone.now()

Check warning on line 32 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L32

Added line #L32 was not covered by tests


daily_utilizations = [
qt_manager.get_daily_utilization(qualitytool, date)
for qualitytool in ResourceQualityTool.objects.all()
]

if not daily_utilizations:
return

Check warning on line 41 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L41

Added line #L41 was not covered by tests


transport = paramiko.Transport((settings.QUALITYTOOL_SFTP_HOST, settings.QUALITYTOOL_SFTP_PORT))

Check warning on line 44 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L44

Added line #L44 was not covered by tests

transport.connect(

Check warning on line 46 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L46

Added line #L46 was not covered by tests
username=settings.QUALITYTOOL_SFTP_USERNAME,
password=settings.QUALITYTOOL_SFTP_PASSWORD)

sftp = paramiko.SFTPClient.from_transport(transport)

Check warning on line 50 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L50

Added line #L50 was not covered by tests




with sftp.open(path, 'w') as csv_file:

Check warning on line 55 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L55

Added line #L55 was not covered by tests
for daily_utilization in daily_utilizations:
csv_file.write('%(row)s\n' % ({
'row': ','.join(str(val) for val in daily_utilization.values())
}))
logging.info(f'Generated new daily utilization csv file with {len(daily_utilizations)} entries.')

Check warning on line 60 in qualitytool/management/commands/sftp_daily_utilization.py

View check run for this annotation

Codecov / codecov/patch

qualitytool/management/commands/sftp_daily_utilization.py#L60

Added line #L60 was not covered by tests
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ more-itertools
ntlm-auth
oauthlib
packaging
paramiko
phonenumbers
Pillow>7.0.0
pluggy
Expand Down
23 changes: 22 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ babel==2.11.0
# via
# -r requirements.in
# delorean
bcrypt==4.0.1
# via paramiko
beautifulsoup4==4.11.2
# via -r requirements.in
cached-property==1.5.2
Expand All @@ -48,6 +50,7 @@ cffi==1.15.1
# via
# -r requirements.in
# cryptography
# pynacl
chardet==5.1.0
# via -r requirements.in
charset-normalizer==3.0.1
Expand All @@ -71,6 +74,7 @@ cryptography==41.0.4
# via
# -r requirements.in
# djangorestframework-simplejwt
# paramiko
# pyjwt
# pyspnego
# requests-ntlm
Expand Down Expand Up @@ -204,6 +208,10 @@ ecdsa==0.18.0
# via
# -r requirements.in
# python-jose
exceptiongroup==1.1.3
# via
# cattrs
# pytest
exchangelib==4.9.0
# via -r requirements.in
factory-boy==3.2.1
Expand Down Expand Up @@ -291,6 +299,8 @@ packaging==23.0
# deprecation
# drf-yasg
# pytest
paramiko==3.3.1
# via -r requirements.in
phonenumbers==8.13.6
# via -r requirements.in
pillow==10.0.1
Expand Down Expand Up @@ -353,6 +363,8 @@ pylint-plugin-utils==0.7
# via
# -r requirements.in
# pylint-django
pynacl==1.5.0
# via paramiko
pyparsing==3.0.9
# via -r requirements.in
pyrsistent==0.19.3
Expand Down Expand Up @@ -444,7 +456,9 @@ ruamel-yaml==0.17.21
# -r requirements.in
# drf-yasg
ruamel-yaml-clib==0.2.7
# via -r requirements.in
# via
# -r requirements.in
# ruamel-yaml
sentry-sdk==1.15.0
# via -r requirements.in
six==1.16.0
Expand Down Expand Up @@ -477,13 +491,20 @@ tendo==0.3.0
# via -r requirements.in
text-unidecode==1.3
# via -r requirements.in
tomli==2.0.1
# via
# coverage
# pylint
# pytest
tomlkit==0.11.6
# via pylint
typed-ast==1.5.4
# via -r requirements.in
typing-extensions==4.5.0
# via
# astroid
# django-modeltranslation
# pylint
# swagger-spec-validator
tzdata==2022.7
# via
Expand Down
9 changes: 9 additions & 0 deletions respa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
QUALITYTOOL_PASSWORD=(str, ''),
QUALITYTOOL_API_BASE=(str, ''),
QUALITYTOOL_ENABLED=(bool, False),
QUALITYTOOL_SFTP_HOST=(str, ''),
QUALITYTOOL_SFTP_PORT=(int, 22),
QUALITYTOOL_SFTP_USERNAME=(str, ''),
QUALITYTOOL_SFTP_PASSWORD=(str, '')
)
environ.Env.read_env()
# used for generating links to images, when no request context is available
Expand Down Expand Up @@ -171,6 +175,11 @@
QUALITYTOOL_API_BASE = env('QUALITYTOOL_API_BASE')
QUALITYTOOL_ENABLED = env('QUALITYTOOL_ENABLED')

QUALITYTOOL_SFTP_HOST = env('QUALITYTOOL_SFTP_HOST')
QUALITYTOOL_SFTP_PORT = env('QUALITYTOOL_SFTP_PORT')
QUALITYTOOL_SFTP_USERNAME = env('QUALITYTOOL_SFTP_USERNAME')
QUALITYTOOL_SFTP_PASSWORD = env('QUALITYTOOL_SFTP_PASSWORD')

# Application definition
INSTALLED_APPS = [
'helusers',
Expand Down

0 comments on commit 531fb61

Please sign in to comment.