Skip to content

Commit

Permalink
Merge pull request #302 from City-of-Turku/develop
Browse files Browse the repository at this point in the history
Release tku-v1.9
  • Loading branch information
ezkat authored Nov 20, 2023
2 parents 1cd15d5 + 4fae05f commit 8ed8678
Show file tree
Hide file tree
Showing 33 changed files with 759 additions and 188 deletions.
79 changes: 79 additions & 0 deletions docs/qualitytool_commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Qualitytool Django management commands


## Qualitytool command: post_daily_utilization

### Overview
This command posts the daily utilization of respa resources to Suomi.fi qualitytool API

### Usage
To use the command, run:

```
python manage.py post_daily_utilization [Optional: --date YYYY-mm-dd]
```

### Options
- `--date YYYY-mm-dd (Optional)`: The date for which the utilization data should be uploaded. Defaults to the current date (`datetime.now().date()`).

### Example
To upload utilization data for January 1, 2023:

```
python manage.py post_daily_utilization --date 2023-01-01
```


## Qualitytool command: save_daily_utilization_csv

### Overview
This command saves the daily utilization of respa resources to a given file path.

### Usage
To use the command, run:

```
python manage.py save_daily_utilization_csv /file/path.csv [Optional: --date YYYY-mm-dd]
```

### Options
- `path`: The destination filepath
- `--date YYYY-mm-dd (Optional)`: The date for which the utilization data should be uploaded. Defaults to the current date (`datetime.now().date()`).

### Example
To upload utilization data for January 1, 2023:

```
python manage.py save_daily_utilization_csv /file/path.csv --date 2023-01-01
```

## Qualitytool command: sftp_daily_utilization

### Overview
This command uploads the daily utilization of respa resources to a sftp server.

### Usage
To use the command, run:

```
python manage.py sftp_daily_utilization /file/path.csv [Optional: --date YYYY-mm-dd]
```

### Environment Variables
Ensure the following environment variables are set before running the command:

- `QUALITYTOOL_SFTP_HOST`: The hostname of the SFTP server.
- `QUALITYTOOL_SFTP_PORT`: The port number for SFTP (e.g., 22).
- `QUALITYTOOL_SFTP_USERNAME`: The username for authenticating with the SFTP server.
- `QUALITYTOOL_SFTP_PASSWORD`: The password for authenticating with the SFTP server.

### Options
- `path`: The destination filepath on the SFTP server where the utilization CSV file will be uploaded.
- `--date YYYY-mm-dd (Optional)`: The date for which the utilization data should be uploaded. Defaults to the current date (`datetime.now().date()`).

### Example
To upload utilization data for January 1, 2023:

```
python manage.py sftp_daily_utilization /file/path.csv --date 2023-01-01
```
7 changes: 5 additions & 2 deletions locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ msgstr ""
msgid "Reservation bulk created"
msgstr ""

msgid "Reservation reminder"
msgstr ""

#: notifications/models.py:102 resources/models/reservation.py:137
#: resources/models/resource.py:686
msgid "Type"
Expand Down Expand Up @@ -1356,7 +1359,7 @@ msgstr ""

#: resources/models/resource.py:187
msgid "E-mail addresses for client correspondence"
msgstr ""
msgstr "Email addresses for receiving notifications of customer reservations"

#: resources/models/resource.py:189
msgid "Authentication"
Expand Down Expand Up @@ -2010,7 +2013,7 @@ msgstr ""

#: respa_admin/templates/respa_admin/resources/form/_booking.html:41
msgid "A notification will be sent when a space is booked."
msgstr ""
msgstr "Notifications are sent regarding customer-made reservations, including their confirmations, changes, and cancellations."

#: respa_admin/templates/respa_admin/resources/form/_equipment.html:8
msgid "Choose from the list below"
Expand Down
11 changes: 9 additions & 2 deletions locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ msgstr "Varauskommentti luotu"
msgid "Catering order comment created"
msgstr "Tarjoilutilauskommentti luotu"

#: notifications/models.py:98
msgid "Reservation bulk created"
msgstr "Varausmassa luotu"

msgid "Reservation reminder"
msgstr "Varausmuistutus"

msgid "Type"
msgstr "Tyyppi"

Expand Down Expand Up @@ -1440,7 +1447,7 @@ msgid "E-mail notifications"
msgstr "Sähköposti-ilmoitukset"

msgid "A notification will be sent when a space is booked."
msgstr "Ilmoitus lähetetään, kun tilaa varataan."
msgstr "Ilmoituksia lähetetään asiakkaiden tekemistä varauksista sekä niiden vahvistuksista, muutoksista ja peruutuksista."

msgid "Extra questions for reservation"
msgstr "Varauksen lisäkysymykset"
Expand Down Expand Up @@ -1701,7 +1708,7 @@ msgid "Reservation created official"
msgstr "Varaus luotu virkailija"

msgid "E-mail addresses for client correspondence"
msgstr "Sähköpostiosoitteet asiakasvarausten muutoksille"
msgstr "Sähköpostiosoitteet ilmoitusten vastaanottamiseen asiakasvarauksista"

msgid "Municipalitys"
msgstr "Kunnat"
Expand Down
12 changes: 11 additions & 1 deletion locale/sv/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ msgstr "Bokningskommentar skapad"
msgid "Catering order comment created"
msgstr "Catering beställning komment skapad"

#: notifications/models.py:98
msgid "Reservation bulk created"
msgstr "Reservationssats skapad"

msgid "Reservation reminder"
msgstr "Påminnelse om reservation"

msgid "Type"
msgstr "Typ"

Expand Down Expand Up @@ -985,6 +992,9 @@ msgstr "Syften"
msgid "Need manual confirmation"
msgstr "Kräver manuell bekräftelse"

msgid "E-mail addresses for client correspondence"
msgstr "E-postadresser för att ta emot meddelanden om kundreservationer"

msgid "Authentication"
msgstr "Autentisering"

Expand Down Expand Up @@ -1439,7 +1449,7 @@ msgid "E-mail notifications"
msgstr "E-post notifikationer"

msgid "A notification will be sent when a space is booked."
msgstr "Ett meddelande kommer att skickas då ett utrymme har bokats."
msgstr "Meddelanden skickas angående kundbokningar, inklusive deras bekräftelser, ändringar och avbokningar."

msgid "Extra questions for reservation"
msgstr "Extra frågor för bokning"
Expand Down
18 changes: 18 additions & 0 deletions notifications/migrations/0019_add_type_reservation_reminder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2023-10-04 06:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('notifications', '0018_auto_20220125_0815'),
]

operations = [
migrations.AlterField(
model_name='notificationtemplate',
name='type',
field=models.CharField(choices=[('reservation_requested', 'Reservation requested'), ('reservation_requested_official', 'Reservation requested official'), ('reservation_requested_official_by_official', 'Reservation requested by official'), ('reservation_cancelled', 'Reservation cancelled'), ('reservation_cancelled_official', 'Reservation cancelled official'), ('reservation_cancelled_by_official', 'Reservation cancelled by official'), ('reservation_created', 'Reservation created'), ('reservation_created_official', 'Reservation created official'), ('reservation_created_by_official', 'Reservation created by official'), ('reservation_modified', 'Reservation modified'), ('reservation_modified_official', 'Reservation modified official'), ('reservation_modified_by_official', 'Reservation modified by official'), ('reservation_created_with_access_code', 'Reservation created with access code'), ('reservation_created_with_access_code_official', 'Reservation created with access code official'), ('reservation_created_with_access_code_by_official', 'Reservation created with access code by official'), ('reservation_confirmed', 'Reservation confirmed'), ('reservation_denied', 'Reservation denied'), ('reservation_access_code_created', 'Access code was created for a reservation'), ('reservation_waiting_for_payment', 'Reservation waiting for payment'), ('catering_order_created', 'Catering order created'), ('catering_order_modified', 'Catering order modified'), ('catering_order_deleted', 'Catering order deleted'), ('reservation_comment_created', 'Reservation comment created'), ('catering_order_comment_created', 'Catering order comment created'), ('reservation_bulk_created', 'Reservation bulk created'), ('reservation_reminder', 'Reservation reminder')], db_index=True, max_length=100, verbose_name='Type'),
),
]
4 changes: 3 additions & 1 deletion notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NotificationType:
RESERVATION_CANCELLED_BY_OFFICIAL = 'reservation_cancelled_by_official'

RESERVATION_CREATED = 'reservation_created'
RESERVATION_REMINDER = 'reservation_reminder'
RESERVATION_CREATED_OFFICIAL = 'reservation_created_official'
RESERVATION_CREATED_BY_OFFICIAL = 'reservation_created_by_official'

Expand Down Expand Up @@ -99,7 +100,8 @@ class NotificationTemplate(TranslatableModel):
(NotificationType.RESERVATION_COMMENT_CREATED, _('Reservation comment created')),
(NotificationType.CATERING_ORDER_COMMENT_CREATED, _('Catering order comment created')),

(NotificationType.RESERVATION_BULK_CREATED, _('Reservation bulk created'))
(NotificationType.RESERVATION_BULK_CREATED, _('Reservation bulk created')),
(NotificationType.RESERVATION_REMINDER, _('Reservation reminder')),
)

type = models.CharField(
Expand Down
9 changes: 5 additions & 4 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,12 @@ def get_order_lines(self):
def get_price(self) -> Decimal:
total_sum = sum(order_line.get_price() for order_line in self.get_order_lines())
# The final total is rounded, NO ROUNDING BEFORE THIS.
return total_sum.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
return Decimal(total_sum).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)

def set_state(
self, new_state: str, log_message: str = None,
save: bool = True, update_reservation_state: bool = True
save: bool = True, update_reservation_state: bool = True,
**kwargs
) -> None:
assert new_state in (Order.WAITING, Order.CONFIRMED, Order.REJECTED, Order.EXPIRED, Order.CANCELLED)

Expand Down Expand Up @@ -843,9 +844,9 @@ def set_state(

if update_reservation_state:
if new_state == Order.CONFIRMED:
self.reservation.set_state(Reservation.CONFIRMED, None)
self.reservation.set_state(Reservation.CONFIRMED, kwargs.get('user', self.reservation.user))
elif new_state in (Order.REJECTED, Order.EXPIRED, Order.CANCELLED):
self.reservation.set_state(Reservation.CANCELLED, None)
self.reservation.set_state(Reservation.CANCELLED, kwargs.get('user', self.reservation.user))

if save:
self.save()
Expand Down
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')

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


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


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

if not daily_utilizations:
return


with open(path, 'w') as csv_file:
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.')
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')

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

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

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


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

if not daily_utilizations:
return


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

transport.connect(
username=settings.QUALITYTOOL_SFTP_USERNAME,
password=settings.QUALITYTOOL_SFTP_PASSWORD)

sftp = paramiko.SFTPClient.from_transport(transport)




with sftp.open(path, 'w') as csv_file:
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.')
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ daemonize
database-sanitizer>=0.4.0
defusedxml
Delorean
Django==3.2.20
Django==3.2.23
django-admin-json-editor==0.2.3
django-allauth
django-anymail
Expand Down Expand Up @@ -73,6 +73,7 @@ more-itertools
ntlm-auth
oauthlib
packaging
paramiko
phonenumbers
Pillow>7.0.0
pluggy
Expand Down
Loading

0 comments on commit 8ed8678

Please sign in to comment.