From f400b7d0c9f9349aee5b20b19b3b1eed5b3887eb Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 22 Aug 2022 15:02:07 +0300 Subject: [PATCH] remove --- .deploy/deploy-dev.yaml | 17 +++++++++++++++++ .deploy/deploy-prod.yaml | 6 +++--- battleship/management/__init__.py | 0 battleship/management/commands/__init__.py | 0 battleship/management/commands/remove.py | 15 +++++++++++++++ battleship/views.py | 3 +++ battleship_back/settings.py | 2 +- 7 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 battleship/management/__init__.py create mode 100644 battleship/management/commands/__init__.py create mode 100644 battleship/management/commands/remove.py diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index 4067060..4737321 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -36,6 +36,23 @@ services: parallelism: 1 order: start-first + remove: + image: mathwave/sprint-repo:battleship-back + networks: + - battleship-net + environment: + DB_HOST: "pg.develop.sprinthub.ru" + DB_PASSWORD: $DB_PASSWORD_DEV + DEBUG: "true" + command: ./manage.py remove + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + networks: battleship-net: driver: overlay diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index 4067060..1f72e42 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -24,9 +24,9 @@ services: networks: - battleship-net environment: - DB_HOST: "pg.develop.sprinthub.ru" - DB_PASSWORD: $DB_PASSWORD_DEV - DEBUG: "true" + DB_HOST: "pg.sprinthub.ru" + DB_PASSWORD: $DB_PASSWORD_PROD + DEBUG: "false" command: bash -c "./manage.py migrate && ./manage.py runserver 0.0.0.0:8000" deploy: mode: replicated diff --git a/battleship/management/__init__.py b/battleship/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/battleship/management/commands/__init__.py b/battleship/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/battleship/management/commands/remove.py b/battleship/management/commands/remove.py new file mode 100644 index 0000000..dc56837 --- /dev/null +++ b/battleship/management/commands/remove.py @@ -0,0 +1,15 @@ +from datetime import timedelta +from time import sleep + +from django.core.management import BaseCommand +from django.utils import timezone + +from battleship.models import Game + + +class Command(BaseCommand): + def handle(self, *args, **options): + while True: + print('deleting old games') + Game.objects.filter(last_move_ts__lte=timezone.now() - timedelta(days=1)).delete() + sleep(60 * 60 * 24) diff --git a/battleship/views.py b/battleship/views.py index 06aa928..29630c6 100644 --- a/battleship/views.py +++ b/battleship/views.py @@ -2,6 +2,8 @@ from django.http import JsonResponse from django.shortcuts import render # Create your views here. +from django.utils import timezone + from battleship.models import Game, Player, generate_token @@ -74,6 +76,7 @@ def shoot(request): else: new_symb = '.' player.game.turn = (1 - player.game.turn) + player.game.last_move_ts = timezone.now() player.game.save() if pos == 0: player2.field = new_symb + player2.field[1:] diff --git a/battleship_back/settings.py b/battleship_back/settings.py index aa70a6f..e002240 100644 --- a/battleship_back/settings.py +++ b/battleship_back/settings.py @@ -23,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = 'django-insecure-$x=irhk3ts1ae%b+kpr+g%mk3v$eb++^0eh(eg0)+-hb+ane82' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.getenv("DEBUG", "true") == "true" ALLOWED_HOSTS = ['*']