Skip to content

Commit

Permalink
Merge pull request #2412 from FJNR-inc/develop
Browse files Browse the repository at this point in the history
new Release
  • Loading branch information
RignonNoel authored May 14, 2024
2 parents dca1090 + a6df14d commit 7e1d4cb
Show file tree
Hide file tree
Showing 21 changed files with 494 additions and 57 deletions.
8 changes: 4 additions & 4 deletions .env.docker
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Docker database
DB_HOST=db
DB_USER=root
DB_PASSWORD=my_password
DB_HOST="db"
DB_USER="root"
DB_PASSWORD="my_password"
DB_PORT=5432
DB_NAME=blitz
DB_NAME="blitz"

## GENERAL SETTINGS
DEBUG=True
Expand Down
33 changes: 26 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
FROM lambci/lambda:build-python3.8
FROM python:3.8-slim-buster

LABEL maintainer="[email protected]"
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

COPY requirements.txt /requirements.txt
COPY requirements-dev.txt /requirements-dev.txt
RUN apt-get update \
# dependencies for building Python packages
&& apt-get install -y build-essential \
# psycopg dependencies
&& apt-get install -y libpq-dev \
# Translations dependencies
&& apt-get install -y gettext \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/* \

RUN pip --timeout=1000 --no-cache-dir install -r /requirements.txt
RUN pip --timeout=1000 --no-cache-dir install -r /requirements-dev.txt
WORKDIR /app

# Adds our application code to the image
COPY . .

RUN pip install -r requirements.txt
RUN pip install -r requirements-dev.txt

RUN mkdir -p /opt/project

COPY ./docker/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start

COPY ./docker/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

WORKDIR /opt/project

ENTRYPOINT ["/entrypoint"]
18 changes: 18 additions & 0 deletions blitz_api/migrations/0031_alter_exportmedia_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2024-04-30 16:51

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blitz_api', '0030_auto_20230627_0926'),
]

operations = [
migrations.AlterField(
model_name='exportmedia',
name='type',
field=models.CharField(choices=[('ANONYMOUS CHRONO DATA', 'Anonymous Chrono data'), ('OTHER', 'Other'), ('RETREAT SALES', 'Retreat sales'), ('RETREAT PARTICIPATION', 'Retreat participation'), ('RETREAT OPTIONS', 'Retreat options'), ('COUPON USAGE', 'Coupon usage'), ('SALES AND REFUND', 'Sales and refund')], default='OTHER', max_length=255),
),
]
4 changes: 4 additions & 0 deletions blitz_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ def check_and_notify_renew_membership(self):

def credit_tickets(self, nb_tickets: int):
self.tickets += nb_tickets
if self.tickets < 0:
self.tickets = 0
self.save()

@property
Expand Down Expand Up @@ -754,6 +756,7 @@ class ExportMedia(models.Model):
EXPORT_RETREAT_PARTICIPATION = 'RETREAT PARTICIPATION'
EXPORT_RETREAT_OPTIONS = 'RETREAT OPTIONS'
EXPORT_COUPON_USAGE = 'COUPON USAGE'
EXPORT_SALES_AND_REFUND = 'SALES AND REFUND'

EXPORT_CHOICES = (
(EXPORT_ANONYMOUS_CHRONO_DATA, _('Anonymous Chrono data')),
Expand All @@ -762,6 +765,7 @@ class ExportMedia(models.Model):
(EXPORT_RETREAT_PARTICIPATION, _('Retreat participation')),
(EXPORT_RETREAT_OPTIONS, _('Retreat options')),
(EXPORT_COUPON_USAGE, _('Coupon usage')),
(EXPORT_SALES_AND_REFUND, _('Sales and refund')),
)

file = models.FileField(
Expand Down
10 changes: 10 additions & 0 deletions blitz_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@
default=False,
cast=bool,
),
'STATUS_URLS': {
'ASSIGN_RETREAT_TOMATOES': config(
'STATUS_URL_ASSIGN_RETREAT_TOMATOES',
default='',
),
'TASK_EXECUTION': config(
'STATUS_URL_TASK_EXECUTION',
default='',
),
},
'FRONTEND_INTEGRATION': {
'SSO_URL': config(
'SSO_URL',
Expand Down
7 changes: 5 additions & 2 deletions blitz_api/tests/tests_view_Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,8 @@ def test_credit_ticket_not_int(self):

def test_credit_ticket_negative_int(self):
"""
Ensure admin can't credit negative tickets to a user
Ensure admin can credit negative tickets to a user but it won't
go lower than 0
"""
user = UserFactory()
self.assertEqual(user.tickets, 1)
Expand All @@ -849,8 +850,10 @@ def test_credit_ticket_negative_int(self):
)
self.assertEqual(
response.status_code,
status.HTTP_400_BAD_REQUEST,
status.HTTP_200_OK,
)
user = User.objects.get(pk=user.id)
self.assertEqual(user.tickets, 0)

def test_member_filter(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions blitz_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ def credit_tickets(self, request, pk=None):
"""
user = self.get_object()
nb_tickets = request.data.get('nb_tickets', None)
if not isinstance(nb_tickets, int) or nb_tickets < 1:
if not isinstance(nb_tickets, int):
error = {
'nb_tickets': _("nb_tickets must be a positive integer.")}
'nb_tickets': _("nb_tickets must be an integer.")}
return Response(error, status=status.HTTP_400_BAD_REQUEST)

user.credit_tickets(nb_tickets)
Expand Down
12 changes: 12 additions & 0 deletions cron_manager/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from celery import shared_task
from django.conf import settings
import requests


@shared_task
def trigger_task_executions():
from .cron_function import execute_tasks

execute_tasks()

try:
status_url = settings.LOCAL_SETTINGS['STATUS_URLS']['TASK_EXECUTION']

if status_url:
requests.get(status_url)
except Exception:
# We don't want to block the task because of a status update
# Status system should already report the error if needed
pass
28 changes: 6 additions & 22 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ version: '3.9'
services:
db:
image: postgres:latest
restart: always
environment:
POSTGRES_DB: blitz
POSTGRES_USER: root
Expand All @@ -12,36 +11,21 @@ services:
- db:/var/lib/postgresql/data
ports:
- 5432:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready -U root -d blitz"]
interval: 5s
timeout: 5s
retries: 5
start_period: 60s

api:
restart: always
build:
context: .
env_file: .env.docker
context: ./
dockerfile: Dockerfile
env_file: ./.env.docker
command: /start
volumes:
- .:/opt/project
- /opt/project/src
command: /start
depends_on:
db:
condition: service_healthy
ports:
- 8000:8000

documentation:
restart: always
build: ./
command: "mkdocs serve"
volumes:
- .:/opt/project
ports:
- "8001:8001"
depends_on:
- db

volumes:
db:
36 changes: 36 additions & 0 deletions docker/entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# if any of the commands in your code fails for any reason, the entire script fails
set -o errexit
# fail exit if one of your pipe command fails
set -o pipefail
# exits if any of your variables is not set
set -o nounset

postgres_ready() {
python << END
import sys
import psycopg
try:
psycopg.connect(
dbname="${DB_NAME}",
user="${DB_USER}",
password="${DB_PASSWORD}",
host="${DB_HOST}",
port="${DB_PORT}",
)
except psycopg.OperationalError:
sys.exit(-1)
sys.exit(0)
END
}
until postgres_ready; do
>&2 echo 'Waiting for PostgreSQL to become available...'
sleep 1
done
>&2 echo 'PostgreSQL is available'

exec "$@"
9 changes: 9 additions & 0 deletions log_management/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def export_anonymous_chrono_data_month(self, request, queryset):
'export_anonymous_chrono_data_month'


def export_anonymous_chrono_data_all(self, request, queryset):
export_anonymous_chrono_data.delay(request.user.id)


export_anonymous_chrono_data_all.short_description = \
'export_anonymous_chrono_data_all'


class LogAdmin(admin.ModelAdmin):
list_display = ('source', 'level', 'error_code', 'message', 'created')
search_fields = (
Expand Down Expand Up @@ -52,6 +60,7 @@ class EmailLogAdmin(admin.ModelAdmin):
class ActionLogAdmin(admin.ModelAdmin):
actions = [
export_anonymous_chrono_data_month,
export_anonymous_chrono_data_all,
]
list_display = (
'id',
Expand Down
6 changes: 4 additions & 2 deletions log_management/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def export_anonymous_chrono_data(admin_id, start_date=None, end_date=None):
from log_management.models import ActionLog
from blitz_api.models import ExportMedia

start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S %z')
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S %z')
if start_date : start_date = datetime.datetime.strptime(
start_date, '%Y-%m-%d %H:%M:%S %z')
if end_date : end_date = datetime.datetime.strptime(
end_date, '%Y-%m-%d %H:%M:%S %z')

anonymized_actions = ActionLog.anonymize_data(start_date, end_date)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ python-decouple==3.5
django-storages==1.14.2
dj_database_url==0.5.0
zappa==0.54.0
requests==2.31.0
psycopg-binary==3.1.12
psycopg==3.1.12
django-safedelete==1.3.3
Expand Down
8 changes: 7 additions & 1 deletion retirement/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def update(self, instance, validated_data):
validated_data,
)

# Update retreat seats
# Add a new spot for the waitlist
free_seats = current_retreat.places_remaining
if current_retreat.reserved_seats or free_seats == 1:
current_retreat.add_wait_queue_place(user)
Expand Down Expand Up @@ -542,6 +542,7 @@ def update(self, instance, validated_data):
"retreat."
)]
})
new_retreat.check_and_use_reserved_place(user)
if user_waiting:
user_waiting.delete()

Expand Down Expand Up @@ -1051,3 +1052,8 @@ class Meta:
'view_name': 'retreat:waitqueueplacereserved-detail',
},
}

def to_representation(self, instance):
data = super(WaitQueuePlaceReservedSerializer, self).to_representation(instance)
data['retreat_id'] = instance.wait_queue_place.retreat.id
return data
13 changes: 13 additions & 0 deletions retirement/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db import transaction
from django.db.models import Q
from django.utils import timezone
from django.conf import settings
import requests


@shared_task
Expand Down Expand Up @@ -31,3 +33,14 @@ def assign_retreat_tomatoes():
)
date.tomatoes_assigned = True
date.save()

try:
urls = settings.LOCAL_SETTINGS['STATUS_URLS']
status_url = urls['ASSIGN_RETREAT_TOMATOES']

if status_url:
requests.get(status_url)
except Exception:
# We don't want to block the task because of a status update
# Status system should already report the error if needed
pass
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class WaitQueuePlaceReservedTests(CustomAPITestCase):
'used',
'available',
'create',
'retreat_id',
]

def setUp(self) -> None:
Expand Down
Loading

0 comments on commit 7e1d4cb

Please sign in to comment.