diff --git a/Dockerfile b/Dockerfile index d325e73..6eee288 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,8 @@ ADD requirements /requirements RUN mkdir -p logs/ && \ apt update && \ - apt install -y libpq5 procps && \ - pip install -r /requirements/live.txt + apt install -y libpq5 procps cron && \ + pip install -r /requirements/live.txt && \ + echo "30 */8 * * * python manage.py snap_up_health_check" >> /var/spool/cron/crontabs/root -CMD nohup python manage.py continue_snap_up & gunicorn --bind=0.0.0.0:8000 --timeout=300 --keep-alive=60 src.wsgi:application +CMD nohup python manage.py continue_snap_up & cron && gunicorn --bind=0.0.0.0:8000 --timeout=300 --keep-alive=60 src.wsgi:application diff --git a/README.md b/README.md index 8620a4a..a343ad2 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Snap Up THSR (Work in progress) +# Snap Up THSR __!!此專案為純研究用途,請勿用於不當交易行為!!__ -這是一份自動化高鐵購票系統的後端程式碼 +這是一份自動化高鐵購票系統,使用 Django 後台進行使用者管理與訂票提單系統,協助完成訂票的最後一哩路。 使用技術: - Django @@ -10,4 +10,4 @@ __!!此專案為純研究用途,請勿用於不當交易行為!!__ 本系統已實踐會員系統與基礎購票功能,未來將會陸續新增以下功能: 1. 多種類的自動購票策略 -2. 疏運期間售票時間調整 +2. 疏運期間售票提醒 diff --git a/src/booking/admin.py b/src/booking/admin.py index ac7febf..ab2ea26 100644 --- a/src/booking/admin.py +++ b/src/booking/admin.py @@ -1,8 +1,7 @@ from django.contrib import admin from basis.admin import ForeignKeyLinksMixin -from user.admin import UserAdminMixin, UserFilter -from user.models import User +from user.admin import UserAdminMixin # Register your models here. from .models import THSRTicket, BookingRequest diff --git a/src/booking/management/commands/snap_up_health_check.py b/src/booking/management/commands/snap_up_health_check.py new file mode 100644 index 0000000..6d675bf --- /dev/null +++ b/src/booking/management/commands/snap_up_health_check.py @@ -0,0 +1,36 @@ +from django.conf import settings +from django.core.management.base import BaseCommand +from django.core.mail import send_mail +from basis.logger import log + + +def count_lines(filename: str) -> int: + with open(filename, 'r') as file: + lines = file.readlines() + return len(lines) + + +def clear_log(filename: str): + with open(filename, 'w') as file: + file.write('') + + +LOG_PATH = '/src/logs/django.log' + + +class Command(BaseCommand): + help = 'Check the health of the application' + + def handle(self, *args, **options): + lines = count_lines(LOG_PATH) + if lines: + clear_log(LOG_PATH) + log.info('Snap up health check: OK') + return + + send_mail( + subject='Snap Up THSR 工作排程警告', + message='Snap Up THSR 的工作排程已久無運作,記得去檢查一下!', + from_email=settings.EMAIL_HOST_USER, + recipient_list=['arcade0425@gmail.com'], + ) diff --git a/src/user/admin.py b/src/user/admin.py index 074f7a6..0b93b97 100644 --- a/src/user/admin.py +++ b/src/user/admin.py @@ -72,21 +72,6 @@ def save_model(self, request, obj, form, change): super().save_model(request, obj, form, change) -class UserFilter(admin.SimpleListFilter): - title = ('使用者帳號') - parameter_name = 'user' - - def lookups(self, request, model_admin): - if request.user.is_superuser: - return [(user.id, user.email) for user in User.objects.all()] - return [(request.user.id, request.user.email)] - - def queryset(self, request, queryset): - if self.value(): - return queryset.filter(user__id=self.value()) - return queryset - - class CustomUserAdmin(UserAdmin): # Update the list of fields to display in the admin interface. list_display = ('email', 'personal_id', 'phone', 'buy_discount_ticket', 'use_tgo_account')