crontab 사용 이유
매일 18:00 에 요청자가 있는지 확인하고, 요청자가 있는 제공자에게 알림을 전송하는 기능을 만들려고 합니다.
Django 의 쿼리셋을 이용하여 오늘 날짜의 데이터들을 가져오는 것은 전에 포스팅했기 때문에
아래 링크를 확인해주시면 됩니다.
https://newbiecs.tistory.com/239?category=724785
Django filter(__day)
Order 테이블 안에 created_at 이라는 Datetimefield를 오늘날짜와 비교하려고 찾아보던 중 filter(필드명__day)라는 것을 알게되었고 활용해보았습니다. order.craeted_at 안에는 아래와 같은 데이터가 들어있습..
newbiecs.tistory.com
from django.db.models import Q
from django.utils import timezone
# import Order
# import Channel
def order_confirm():
	day = int(timezone.now().strftime('%d')) 오늘의 날짜를 가져온다.
    today_order = Order.objects.filter(
    	Q(creted_at__day=day) & Q(status=2)).values('channel').order_by('channel').distinct()
주문자 <-> 요청자는 연결되어 있고, 중복을 제거하기 위해서 .distinct()를 추가해주었습니다.

# cron.py
from django.db.models import Q
from django.utils import timezone
# import Order
# import Channel
# import Celebrity
def order_confirm():
	day = int(timezone.now().strftime('%d')) 오늘의 일수를 가져온다.
    today_order = Order.objects.filter(
    	Q(creted_at__day=day) & Q(status=2)).values('channel').order_by('channel').distinct()
        
    for torder in today_order:
        idx = torder['channel'] # 제공자의 ID를 idx에 넣는다.
        ch = Channel.objects.get(pk=idx) # 제공자의 ID로 제공자(Channel) 테이블에서 정보를 꺼낸다.
        celebrity = Celebrity.objects.get(channel=ch) # 제공자 <-> 제공자개인정보
        celebrity_phonenumber = celebrity.phonenumber # 제공자개인정보에서 알림을 보내기 위한 폰 번호
        n.order_confirmation_alarm(celebrity_phonenumber) # 를 인자값으로 넘겨준다.
이제는 cron.py 안에 있는 order_confirm을 6시 전에 한번 수행하게 하면 될 것 입니다.
('59 17 * * * ', '앱이름.파일명.함수명')
17:59 분으로 해둔 이유는 알림을 보내는 API 가 혹시라도 양이 많으면 시간이 오래걸릴 것을 대비해서 59분으로 설정하였습니다.
python manage.py crontab add 추가
python manage.py crontab delete 삭제
python manage.py crontab show 보기
'Web > Django' 카테고리의 다른 글
| Django - allauth signupform custom (0) | 2020.01.02 | 
|---|---|
| Django allauth User has no field named 'username' 해결방법 (0) | 2019.12.30 | 
| Django filter(__day) (0) | 2019.12.05 | 
| Django CBV : DetailView에서 paginate_by 사용하기 (0) | 2019.11.29 | 
| Django - Custom Manager, QuerySet (0) | 2019.11.20 | 
			
			
				
			
댓글